4f8ed78274bff09d54a83de4a56e301965ff00cc
[wgj58.git] / wgj58.js
1 var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: function() { this.state.add('GamePlay', GamePlay, true); } });
2 var logic;
3 var cursors;
4
5 const PLAYER_SPEED = 200;
6 const WAIT_MENUSTEP = 100;
7 const WAIT_TALK = 250;
8
9 const MENUITEM_TALK = 0;
10 const MENUITEM_LEAVE = 1;
11 const MENUITEM_TAKE = 2;
12
13 const GUI_MENUITEM_DISTANCE = 20;
14
15 window.addEventListener("keydown", function(e) {
16 // Prevent default browser action for arrows and spacebar
17 if([32, 37, 38, 39, 40].indexOf(e.keyCode) > -1) {
18 e.preventDefault();
19 }
20 }, false);
21
22 function hasTimePassed(time, delay) {
23 return (game.time.now - time) > delay;
24 }
25
26 class Dialogue {
27 // dialogue is array of {actor, text} records.
28 constructor(dialogue) {
29 this.dialogue = dialogue;
30 this.state = 0;
31 }
32
33 actual() {
34 return this.dialogue[this.state];
35 }
36
37 advance() {
38 this.state++;
39 }
40 }
41
42 class Door extends Phaser.TileSprite {
43 constructor(x, y, name, rotation, vector, longpanel) {
44 super(game, x, y, 64, 64, 'objects');
45 this.name = name;
46 this.longpanel = longpanel;
47 if (!longpanel) {
48 this.anchor = new Phaser.Point(0.5, 0.5);
49 this.tilePosition = new Phaser.Point(-64, -64);
50 this.openvector = Phaser.Point.multiply(vector, new Phaser.Point(56, 56));
51 }
52 else {
53 this.width *= 2;
54 this.anchor = new Phaser.Point(0.75, 0.5);
55 this.tilePosition = new Phaser.Point(0, -64);
56 this.openvector = Phaser.Point.multiply(vector, new Phaser.Point(116, 116));
57 }
58 if (rotation === undefined) rotation = 0;
59 this.rotation = rotation * (Math.PI / 180);
60 this.closetween = game.add.tween(this).to({ x: this.position.x, y: this.position.y }, 1000, Phaser.Easing.Sinusoidal.InOut, false, 0, 0, false);
61 this.openposition = Phaser.Point.add(this.position, this.openvector);
62 this.opentween = game.add.tween(this).to({ x: this.openposition.x, y: this.openposition.y }, 1000, Phaser.Easing.Sinusoidal.InOut, false, 0, 0, false);
63 this.isOpen = false;
64 game.physics.arcade.enable(this);
65 this.body.immovable = true;
66 this.setBody(rotation);
67 }
68
69 setBody(rotation) {
70 switch(rotation) {
71 case 0:
72 case 180:
73 this.body.setSize(this.width, 10, this.longpanel * (rotation / 180) * 64, 27);
74 break;
75 case 90:
76 case 270:
77 this.body.setSize(10, this.width, 27 + (this.longpanel * 64), this.longpanel * ((rotation - 270) / 180) * 64);
78 break;
79 default:
80 console.log("Unable to set body due to unknown rotation:", rotation);
81 }
82 }
83
84 open() {
85 if (!this.isOpen) {
86 this.opentween.start();
87 this.isOpen = true;
88 }
89 }
90
91 close() {
92 if (this.isOpen) {
93 this.closetween.start();
94 this.isOpen = false;
95 }
96 }
97
98 update() {
99 game.debug.body(this);
100 }
101 }
102
103 class Player extends Phaser.Sprite {
104 constructor(x, y) {
105 super(game, x, y, 'player');
106 this.y -= this.height;
107 this.shortname = "John";
108 this.fullname = "John Evals";
109 this.offerInteraction(null);
110 this.unfreeze();
111 }
112
113 enablePhysics() {
114 game.physics.arcade.enable(this);
115 this.body.bounce.y = 0.2;
116 this.body.bounce.x = 0.2;
117 this.body.collideWorldBounds = true;
118 }
119
120 control() {
121 if ((this.alive) && (!this.freezed))
122 {
123 if (cursors.left.isDown)
124 {
125 this.body.velocity.x = -PLAYER_SPEED;
126 }
127 else if (cursors.right.isDown)
128 {
129 this.body.velocity.x = PLAYER_SPEED;
130 }
131 if (cursors.up.isDown)
132 {
133 this.body.velocity.y = -PLAYER_SPEED;
134 }
135 else if (cursors.down.isDown)
136 {
137 this.body.velocity.y = PLAYER_SPEED;
138 }
139 }
140 }
141
142 update() {
143 this.body.velocity.x = this.body.velocity.y = 0;
144 this.control();
145 }
146
147 freeze() {
148 this.freezed = true;
149 }
150
151 unfreeze() {
152 this.freezed = false;
153 }
154
155 offerInteraction(npc) {
156 this.interactablenpc = npc;
157 }
158
159 takeMe(npc) {
160 this.loadTexture(npc.key);
161 npc.kill();
162 }
163 }
164
165 class GameNPC extends Phaser.Sprite {
166 constructor(x, y, key, shortname, fullname, interaction_distance) {
167 super(game, x, y, key);
168 this.y -= this.height;
169 this.shortname = shortname;
170 this.fullname = fullname;
171 this.interaction_distance = interaction_distance;
172 this.interactable = false;
173 this.talkcount = 0;
174 }
175
176 kill() {
177 super.kill();
178 this.exists = false;
179 }
180
181 update() {
182 super.update();
183 if ((!this.interactable) && (game.physics.arcade.distanceBetween(this, logic.player) < this.interaction_distance)) {
184 logic.gameinterface.dropNotice("(ENTER) Interact with " + this.shortname + "!");
185 logic.player.offerInteraction(this);
186 this.interactable = true;
187 }
188 else if ((this.interactable) && (game.physics.arcade.distanceBetween(this, logic.player) > this.interaction_distance)) {
189 logic.gameinterface.clearNotice();
190 logic.player.offerInteraction(null);
191 this.interactable = false;
192 }
193 }
194
195 actionTalk() {
196 this.talkcount++;
197 }
198
199 actionLeave() {
200 }
201
202 actionTake() {
203 logic.player.offerInteraction(null);
204 logic.player.takeMe(this);
205 return true;
206 }
207
208 endTalk() {
209 return true;
210 }
211 }
212
213 class NPC_Clara extends GameNPC {
214 actionTalk() {
215 switch (this.talkcount) {
216 case 0:
217 logic.gameinterface.talk(new Dialogue( [ { actor: this, text: "What a morning..." },
218 { actor: logic.player, text: "Hi Clara! What happened?" },
219 { actor: this, text: "No one cares to tell. I just know that I have to log on people manually, as the access control system doesn't work properly." },
220 { actor: logic.player, text: "Hmm... I'll look into it. Maybe it's just that everyone is fired." },
221 { actor: this, text: "Haha! Wouldn't joke about this, though, due to the recent layoffs." },
222 { actor: logic.player, text: "Well, maybe if we dare to joke about it, it won't happen to us..." },
223 { actor: this, text: "Wish it worked like that... Is it some superstition like the belief that having an umbrella with you prevents rain?" },
224 { actor: logic.player, text: "Nah, that actually works; it's not a superstition, but Murphy's Law!" },
225 { actor: this, text: "If you say so..." } ] ));
226 logic.openDoor("cutedoor");
227 break;
228 case 1:
229 logic.gameinterface.talk(new Dialogue( [ { actor: this, text: "John, have you ever thought about losing your employee card?" },
230 { actor: logic.player, text: "Yeah, you'd just get a new one." },
231 { actor: this, text: "You don't understand me, John!" },
232 { actor: this, text: "..." },
233 { actor: this, text: "I mean... Losing it for real..." },
234 { actor: this, text: "Doesn't it feel like it's the culmination of your being?" },
235 { actor: this, text: "If I had no card, would I still exist?" },
236 { actor: logic.player, text: "..." },
237 { actor: logic.player, text: "You sound very philosophical today." } ] ));
238 break;
239 case 2:
240 case 3:
241 case 4:
242 logic.gameinterface.talk(new Dialogue( [ { actor: this, text: "Would you like to hear a random fun fact?" },
243 { actor: logic.player, text: "Of course!" },
244 { actor: this, text: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s," },
245 { actor: this, text: "when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap" },
246 { actor: this, text: "into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem" },
247 { actor: this, text: "Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." },
248 { actor: logic.player, text: "That's very interesting, I didn't know that!" },
249 { actor: logic.player, text: "Thanks for sharing, Clara!" } ] ));
250 break;
251 case 5:
252 logic.gameinterface.talk(new Dialogue( [ { actor: this, text: "Would you like to hear a random fun fact?" },
253 { actor: logic.player, text: "Of course!..." },
254 { actor: logic.player, text: "But first..." },
255 { actor: this, text: "What is it?" },
256 { actor: logic.player, text: "..." },
257 { actor: logic.player, text: "Never mind, go on with what you wanted to say." },
258 { actor: this, text: "Lorem Ipsum is simply dummy text of the printing and typesetting industry..." },
259 { actor: logic.player, text: "I knew that already..." },
260 { actor: logic.player, text: "But..." },
261 { actor: logic.player, text: "Would you go out on a date with me?" },
262 { actor: this, text: "..." },
263 { actor: this, text: "No." } ] ));
264 break;
265 default:
266 logic.gameinterface.talk(new Dialogue( [ { actor: this, text: "..." } ] ));
267 }
268 super.actionTalk();
269 }
270
271 actionLeave() {
272 logic.gameinterface.dropNotice(this.shortname + ": Have a great day, John!");
273 }
274
275 actionTake() {
276 logic.openDoor("cutedoor");
277 return super.actionTake();
278 }
279
280 endTalk() {
281 return (this.talkcount < 6);
282 }
283 }
284
285 class NPC_Carlos extends GameNPC {
286 actionTalk() {
287 super.actionTalk();
288 logic.endTalk();
289 }
290
291 actionTake() {
292 logic.openDoor("carlosdoor");
293 return super.actionTake();
294 }
295 }
296
297
298 class GameInterface extends Phaser.Group {
299 constructor(game, parent) {
300 super(game, parent, 'GUI', false, false, 0);
301 this.back_notice = new Phaser.Graphics(game, 0, game.height - 50);
302 this.back_notice.beginFill(0x000000);
303 this.back_notice.drawRect(0, 0, game.width, 30);
304 this.back_notice.endFill();
305 this.add(this.back_notice);
306 this.text_notice = new Phaser.Text(game, 0, this.back_notice.y, null, { align: 'left', fill: 'white', font: 'Ubuntu Mono', fontSize: 18, fontWeight: 'bold' });
307 this.text_notice.anchor.setTo(-0.5, -0.2);
308 this.add(this.text_notice);
309 this.clearNotice();
310
311 this.back_menu = new Phaser.Graphics(game, 150, (game.height / 2) + 40);
312 this.back_menu.beginFill(0x000000);
313 this.back_menu.drawRect(0, 0, 155, 105);
314 this.back_menu.endFill();
315 this.back_menu.lineStyle(3, Phaser.Color.YELLOW, 1);
316 this.back_menu.moveTo(10, 35);
317 this.back_menu.lineTo(this.back_menu.width - 10, 35);
318 this.add(this.back_menu);
319 var style = { align: 'left', fill: 'yellow', font: 'Ubuntu Mono', fontSize: 22, fontWeight: 'bold' };
320 this.text_menutitle = new Phaser.Text(game, this.back_menu.x, this.back_menu.y, null, style);
321 this.text_menutitle.anchor.setTo(-0.2, -0.2);
322 style = { align: 'left', fill: 'white', font: 'Ubuntu Mono', fontSize: 18, fontWeight: 'bold' };
323 this.text_menuitem_Talk = new Phaser.Text(game, this.back_menu.x + 35, this.back_menu.y + 40, "Talk", style);
324 this.text_menuitem_Leave = new Phaser.Text(game, this.back_menu.x + 35, this.text_menuitem_Talk.y + GUI_MENUITEM_DISTANCE, "Leave", style);
325 this.text_menuitem_Take = new Phaser.Text(game, this.back_menu.x + 35, this.text_menuitem_Leave.y + GUI_MENUITEM_DISTANCE, "Take ID", style);
326 style.fill = 'yellow';
327 this.text_menucursor = new Phaser.Text(game, this.back_menu.x + 15, this.text_menuitem_Talk.y, "→", style);
328 this.add(this.text_menutitle);
329 this.add(this.text_menuitem_Talk);
330 this.add(this.text_menuitem_Leave);
331 this.add(this.text_menuitem_Take);
332 this.add(this.text_menucursor);
333 this.leaveMenu();
334 this.last_menustep = 0;
335
336 this.back_talk = new Phaser.Graphics(game, 100, game.height - 120);
337 this.back_talk.beginFill(0x000000);
338 this.back_talk.drawRect(0, 0, game.width - 200, 105);
339 this.back_talk.endFill();
340 this.back_talk.lineStyle(3, Phaser.Color.YELLOW, 1);
341 this.back_talk.moveTo(10, 35);
342 this.back_talk.lineTo(this.back_talk.width - 10, 35);
343 this.add(this.back_talk);
344 style = { align: 'left', fill: 'yellow', font: 'Ubuntu Mono', fontSize: 22, fontWeight: 'bold' };
345 this.text_talktitle = new Phaser.Text(game, this.back_talk.x, this.back_talk.y, null, style);
346 this.text_talktitle.anchor.setTo(-0.2, -0.2);
347 this.add(this.text_talktitle);
348 style = { align: 'left', fill: 'white', font: 'Ubuntu Mono', fontSize: 18, fontWeight: 'bold' };
349 this.text_talk = new Phaser.Text(game, this.back_talk.x + 35, this.back_talk.y + 40, null, style);
350 this.text_talk.wordWrap = true;
351 this.text_talk.wordWrapWidth = this.back_talk.width - 70;
352 this.text_talk.lineSpacing = -5;
353 this.add(this.text_talk);
354 this.leaveTalk();
355 this.last_talk = 0;
356 }
357
358 dropNotice(text) {
359 this.text_notice.text = text;
360 this.back_notice.visible = true;
361 this.text_notice.visible = true;
362 }
363
364 clearNotice() {
365 this.back_notice.visible = false;
366 this.text_notice.visible = false;
367 }
368
369 npcMenu(npc) {
370 if (!this.inMenu) {
371 this.inMenu = true;
372 this.clearNotice();
373 this.text_menutitle.text = npc.shortname;
374 this.back_menu.visible = true;
375 this.text_menutitle.visible = true;
376 this.text_menuitem_Talk.visible = true;
377 this.text_menuitem_Leave.visible = true;
378 this.text_menuitem_Take.visible = true;
379 this.currentmenuitem = MENUITEM_TALK;
380 this.actualizeCursorPosition();
381 this.text_menucursor.visible = true;
382 this.last_menustep = game.time.now;
383 }
384 }
385
386 leaveMenu() {
387 this.back_menu.visible = false;
388 this.text_menutitle.visible = false;
389 this.text_menuitem_Talk.visible = false;
390 this.text_menuitem_Leave.visible = false;
391 this.text_menuitem_Take.visible = false;
392 this.text_menucursor.visible = false;
393 this.inMenu = false;
394 }
395
396 talk(dialogue) {
397 this.inTalk = true;
398 this.dialogue = dialogue;
399 this.advanceTalk();
400 this.back_talk.visible = true;
401 this.text_talktitle.visible = true;
402 this.text_talk.visible = true;
403 }
404
405 leaveTalk() {
406 this.inTalk = false;
407 this.dialogue = null;
408 this.back_talk.visible = false;
409 this.text_talktitle.visible = false;
410 this.text_talk.visible = false;
411 }
412
413 advanceTalk() {
414 console.log(this.dialogue);
415 console.log(this.dialogue.actual());
416 var actualdialogue = this.dialogue.actual();
417 if (actualdialogue) {
418 this.text_talktitle.text = actualdialogue.actor.shortname;
419 this.text_talk.text = actualdialogue.text;
420 this.dialogue.advance();
421 }
422 else {
423 this.leaveTalk();
424 logic.endTalk();
425 }
426 this.last_talk = game.time.now;
427 }
428
429 actualizeCursorPosition() {
430 this.text_menucursor.y = this.text_menuitem_Talk.y + (this.currentmenuitem * GUI_MENUITEM_DISTANCE);
431 }
432
433 update() {
434 if ((this.inMenu) && (hasTimePassed(this.last_menustep, WAIT_MENUSTEP))) {
435 if (cursors.up.isDown)
436 {
437 this.currentmenuitem--;
438 if (this.currentmenuitem < MENUITEM_TALK) this.currentmenuitem = MENUITEM_TAKE;
439 this.actualizeCursorPosition();
440 }
441 else if (cursors.down.isDown)
442 {
443 this.currentmenuitem++;
444 if (this.currentmenuitem > MENUITEM_TAKE) this.currentmenuitem = MENUITEM_TALK;
445 this.actualizeCursorPosition();
446 }
447 if (game.input.keyboard.isDown(Phaser.Keyboard.ENTER)) {
448 logic.callMenu(this.currentmenuitem);
449 }
450 this.last_menustep = game.time.now;
451 }
452 if ((this.inTalk) && (game.input.keyboard.isDown(Phaser.Keyboard.ENTER)) && (hasTimePassed(this.last_talk, WAIT_TALK))) {
453 this.advanceTalk();
454 }
455 }
456 }
457
458 class GameLogic {
459
460 constructor() {
461 this.player = this.clara = this.carlos = this.saiki = this.peter = this.bianca = null;
462 this.gameinterface = new GameInterface(game, game.stage);
463 //this.doors = game.add.group(game.world, "doors");
464 this.last_menuselect = 0;
465 }
466
467 createObject(object) {
468 switch (object.type) {
469 case 'spawnpoint': this.createCharacter(object); break;
470 case 'door': this.createDoor(object); break;
471 case '': console.error("Object type is empty:", object); break;
472 default: console.error("Unknown object type:", object);
473 }
474 }
475
476 createCharacter(object) {
477 var newChar;
478 switch (object.name) {
479 case 'john':
480 newChar = new Player(object.x, object.y, 'player');
481 this.player = newChar;
482 this.player.enablePhysics();
483 game.camera.follow(this.player);
484 break;
485 case 'clara':
486 newChar = new NPC_Clara(object.x, object.y, 'clara', "Clara", "Clara Tnavelerri", 200);
487 this.clara = newChar;
488 break;
489 case 'carlos':
490 newChar = new NPC_Carlos(object.x, object.y, 'carlos', "Carlos", "Carlos Elbacalper", 150);
491 this.carlos = newChar;
492 break;
493 default:
494 console.error("Unknown character:", object);
495 }
496 newChar.name = object.name;
497 game.add.existing(newChar);
498 console.log(newChar);
499 }
500
501 createDoor(object) {
502 /* Calculate movement vector and correct position (32 = half tile width/height). */
503 var vector;
504 switch (object.rotation) {
505 case undefined:
506 case 0: vector = new Phaser.Point( -1, 0); object.x += 32; object.y -= 32; break;
507 case 90: vector = new Phaser.Point( 0, -1); object.x += 32; object.y += 32; break;
508 case 180: vector = new Phaser.Point( 1, 0); object.x -= 32; object.y += 32; break;
509 case 270: vector = new Phaser.Point( 0, 1); object.x -= 32; object.y -= 32; break;
510 default: console.error("Invalid rotation:", object.rotation);
511 }
512 this.doors.add(new Door(object.x, object.y, object.name, object.rotation, vector, object.properties.longpanel));
513 }
514
515 callMenu(menuitem) {
516 console.log("Menu callback received:", menuitem);
517 this.last_menuselect = game.time.now;
518 this.gameinterface.leaveMenu();
519 switch (menuitem) {
520 case MENUITEM_TALK:
521 this.player.interactablenpc.actionTalk();
522 break;
523 case MENUITEM_LEAVE:
524 this.player.interactablenpc.actionLeave();
525 this.player.unfreeze();
526 break;
527 case MENUITEM_TAKE:
528 if (this.player.interactablenpc.actionTake())
529 this.player.unfreeze();
530 break;
531 }
532 }
533
534 endTalk() {
535 if (this.player.interactablenpc) {
536 if (this.player.interactablenpc.endTalk()) {
537 this.gameinterface.npcMenu(this.player.interactablenpc);
538 }
539 else {
540 this.player.unfreeze();
541 this.last_menuselect = game.time.now;
542 }
543 }
544 }
545
546 openDoor(doorname) {
547 this.doors.children.forEach(function(o) { if (o.name == doorname) o.open(); });
548 }
549
550 closeDoor(doorname) {
551 this.doors.children.forEach(function(o) { if (o.name == doorname) o.close(); });
552 }
553
554 update() {
555 if ((game.input.keyboard.isDown(Phaser.Keyboard.ENTER)) && (hasTimePassed(this.last_menuselect, WAIT_MENUSTEP))) {
556 if ((this.player.interactablenpc) && (this.player.interactablenpc.interactable) && (!this.gameinterface.inMenu) && (!this.gameinterface.inTalk)) {
557 console.log("Starting interaction with", this.player.interactablenpc.fullname);
558 this.player.freeze();
559 this.gameinterface.npcMenu(this.player.interactablenpc);
560 }
561 }
562 }
563
564 }
565
566 class GamePlay extends Phaser.State {
567
568 constructor() {
569
570 super();
571 logic = new GameLogic();
572 game.world.updateOnlyExistingChildren = true;
573 //game.state.add('GameOver', GameOver, false);
574
575 }
576
577 preload() {
578
579 game.load.image('player', 'john.png');
580 game.load.image('clara', 'clara.png');
581 game.load.image('carlos', 'carlos.png');
582 game.load.image('saiki', 'saiki.png');
583 game.load.image('tileset', 'tileset.png');
584 game.load.image('objects', 'objects.png');
585 game.load.tilemap('gamemap', 'tilemap.json', null, Phaser.Tilemap.TILED_JSON);
586
587 }
588
589 create() {
590
591 game.world.setBounds(0, 0, 800, 600);
592 game.stage.backgroundColor = '#000000';
593 game.physics.startSystem(Phaser.Physics.ARCADE);
594 console.log("Debug enabled:", !game.debug.isDisabled);
595
596 var map = game.add.tilemap('gamemap');
597 map.addTilesetImage('tileset', 'tileset');
598 map.addTilesetImage('objects', 'objects');
599 var layer_floor = map.createLayer('floor');
600 layer_floor.resizeWorld();
601 this.layer_walls = map.createLayer('walls');
602 map.setCollisionBetween(1, 100, true, this.layer_walls);
603 this.layer_furniture = map.createLayer('furniture');
604 map.setCollisionBetween(1, 100, true, this.layer_furniture);
605
606 // FIXME: Don't create a group here like this, it's too ugly! Now I have it here due to the display order.
607 logic.doors = game.add.group(game.world, "doors");
608
609 map.objects['objects'].forEach(function(o) { logic.createObject(o); });
610 console.log(map.objects);
611
612 cursors = game.input.keyboard.createCursorKeys();
613 console.log(logic);
614
615 }
616
617 update() {
618
619 game.physics.arcade.collide(logic.player, this.layer_walls);
620 game.physics.arcade.collide(logic.player, this.layer_furniture);
621 game.physics.arcade.collide(logic.player, logic.doors);
622 logic.update();
623
624 }
625 }