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