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