5b5929a73cb46963c3b51d5a10075663392fcb9f
[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 if (logic.clara.alive) {
288 switch (this.talkcount) {
289 case 0:
290 logic.gameinterface.talk(new Dialogue( [ { actor: logic.player, text: "Good morning, Carlos! What's up?" },
291 { actor: this, text: "Hey, John! Not too much, I'm just trying to sell some shit to this new client." },
292 { actor: logic.player, text: "And so? Is it hard to convince them?" },
293 { actor: this, text: "Of course, they don't want to buy. It's shit." },
294 { actor: this, text: "I mean, who would want to buy shit?" },
295 { actor: logic.player, text: "Yeah. Relatable... But why are you trying to sell shit? I mean, why not one of our cutting edge technologies?" },
296 { actor: this, text: "They couldn't afford it." },
297 { actor: logic.player, text: "Then why are you trying to sell them... anything at all? They'd better off with a cheaper company." },
298 { actor: this, text: "Company policies. Plus the management instructed me to sell to them, no matter what." },
299 { actor: logic.player, text: "That sucks... Well... good luck!" },
300 { actor: this, text: "Thanks..." } ] ));
301 logic.closeDoor("cutedoor");
302 logic.openDoor("carlosdoor");
303 break;
304 case 1:
305 logic.gameinterface.talk(new Dialogue( [ { actor: this, text: "*Sigh.* I'll probably not work here for long, if I can't sell this shit to the customer." },
306 { actor: this, text: "These layoffs are terrifying. They let go of anyone for even the slightest mistake." },
307 { actor: logic.player, text: "But it's not a mistake when our client doesn't buy something they don't need." },
308 { actor: this, text: "Tell this to my boss..." } ] ));
309 break;
310 case 2:
311 logic.gameinterface.talk(new Dialogue( [ { actor: this, text: "You know, generally, I think it sucks being a child. One thing I miss though, if you're a child, you're allowed to be silly." },
312 { actor: this, text: "..." },
313 { actor: this, text: "For example, when my Dad told me that semen is the seed of animals, I theoretized, if I'd bury some pig semen in the garden, a pig would grow out of it..." },
314 { actor: this, text: "I was totally fascinated by the thought, because I really wanted to have a pet pig, but Dad wouldn't let me to have one." },
315 { actor: this, text: "So I thought I could grow my own pig in the garden from pig seeds!" },
316 { actor: logic.player, text: "Interesting. Where are you going with this?" },
317 { actor: this, text: "I don't know... sometimes I feel like I'd rather sell pig semen to our clients, heh!" },
318 { actor: this, text: "Uhm..." },
319 { actor: this, text: "Totally don't tell this to Saiki!" } ] ));
320 this.pigsemen = true;
321 break;
322 case 3:
323 logic.gameinterface.talk(new Dialogue( [ { actor: this, text: "Weren't you heading to the IT department to the right?" },
324 { actor: this, text: "You know... the green department which looks way better than blue!" },
325 { actor: this, text: "..." },
326 { actor: this, text: "Damn, why do I have to work in blue? I just feel... so blue about it." } ] ));
327 break;
328 case 4:
329 logic.gameinterface.talk(new Dialogue( [ { actor: this, text: "Weren't you heading to the conveniently green department to the right? I guess Saiki is waiting for you." },
330 { actor: this, text: "You're so lucky that you can work with her every day!" } ] ));
331 break;
332 default:
333 logic.gameinterface.talk(new Dialogue( [ { actor: this, text: "Guess you'll stay here then. It's cool, you're great company anyway." } ] ));
334 }
335 }
336 else {
337 switch (this.talkcount) {
338 case 0:
339 logic.gameinterface.talk(new Dialogue( [ { actor: logic.player, text: "Good morning, Carlos!" },
340 { actor: this, text: "Whoa, Clara! You have changed so much! I barely recognize you. What's up?" },
341 { actor: logic.player, text: "I need to talk to Saiki at the IT department, but... my card doesn't allow me to enter there." },
342 { actor: this, text: "Well... I can certainly help you with that..." },
343 { actor: this, text: "..." } ] ));
344 break;
345 case 1:
346 logic.gameinterface.talk(new Dialogue( [ { actor: logic.player, text: "Carlos. You promised to help me." },
347 { actor: this, text: "Uhm... It's just... Please give me 5 mins to finish this e-mail." },
348 { actor: logic.player, text: "I don't have much time, Carlos." },
349 { actor: this, text: "See, it's just 5 mins, please be patient. I have a chocolate bar in the fridge. I give it to you. Feel free to have it meanwhile." },
350 { actor: logic.player, text: "..." } ] ));
351 break;
352 case 2:
353 logic.gameinterface.talk(new Dialogue( [ { actor: logic.player, text: "Time is up, Carlos!" },
354 { actor: this, text: "Please give me just another min." },
355 { actor: logic.player, text: "Fine. Please just give me your card and I'll help myself!" },
356 { actor: this, text: "But... you know that company policies forbid that, Clara! Please just be a little more patient!" },
357 { actor: logic.player, text: "You will give me your card now or you will regret!" },
358 { actor: this, text: "...?" },
359 { actor: this, text: "You're not gonna treat me like this. Forget it!" },
360 { actor: logic.player, text: "You'll not work here tomorrow." } ] ));
361 break;
362 default:
363 logic.gameinterface.talk(new Dialogue( [ { actor: this, text: "..." } ] ));
364 }
365 }
366 super.actionTalk();
367 }
368
369 actionLeave() {
370 if (logic.clara.alive) {
371 logic.gameinterface.dropNotice(this.shortname + ": Bye, John! Pass on my greetings to Saiki!");
372 }
373 }
374
375 actionTake() {
376 logic.closeDoor("cutedoor");
377 logic.openDoor("carlosdoor");
378 return super.actionTake();
379 }
380
381 endTalk() {
382 return (logic.clara.alive) || (this.talkcount < 3);
383 }
384 }
385
386
387 class GameInterface extends Phaser.Group {
388 constructor(game, parent) {
389 super(game, parent, 'GUI', false, false, 0);
390 this.back_notice = new Phaser.Graphics(game, 0, game.height - 50);
391 this.back_notice.beginFill(0x000000);
392 this.back_notice.drawRect(0, 0, game.width, 30);
393 this.back_notice.endFill();
394 this.add(this.back_notice);
395 this.text_notice = new Phaser.Text(game, 0, this.back_notice.y, null, { align: 'left', fill: 'white', font: 'Ubuntu Mono', fontSize: 18, fontWeight: 'bold' });
396 this.text_notice.anchor.setTo(-0.5, -0.2);
397 this.add(this.text_notice);
398 this.clearNotice();
399
400 this.back_menu = new Phaser.Graphics(game, 150, (game.height / 2) + 40);
401 this.back_menu.beginFill(0x000000);
402 this.back_menu.drawRect(0, 0, 155, 105);
403 this.back_menu.endFill();
404 this.back_menu.lineStyle(3, Phaser.Color.YELLOW, 1);
405 this.back_menu.moveTo(10, 35);
406 this.back_menu.lineTo(this.back_menu.width - 10, 35);
407 this.add(this.back_menu);
408 var style = { align: 'left', fill: 'yellow', font: 'Ubuntu Mono', fontSize: 22, fontWeight: 'bold' };
409 this.text_menutitle = new Phaser.Text(game, this.back_menu.x, this.back_menu.y, null, style);
410 this.text_menutitle.anchor.setTo(-0.2, -0.2);
411 style = { align: 'left', fill: 'white', font: 'Ubuntu Mono', fontSize: 18, fontWeight: 'bold' };
412 this.text_menuitem_Talk = new Phaser.Text(game, this.back_menu.x + 35, this.back_menu.y + 40, "Talk", style);
413 this.text_menuitem_Leave = new Phaser.Text(game, this.back_menu.x + 35, this.text_menuitem_Talk.y + GUI_MENUITEM_DISTANCE, "Leave", style);
414 this.text_menuitem_Take = new Phaser.Text(game, this.back_menu.x + 35, this.text_menuitem_Leave.y + GUI_MENUITEM_DISTANCE, "Take ID", style);
415 style.fill = 'yellow';
416 this.text_menucursor = new Phaser.Text(game, this.back_menu.x + 15, this.text_menuitem_Talk.y, "→", style);
417 this.add(this.text_menutitle);
418 this.add(this.text_menuitem_Talk);
419 this.add(this.text_menuitem_Leave);
420 this.add(this.text_menuitem_Take);
421 this.add(this.text_menucursor);
422 this.leaveMenu();
423 this.last_menustep = 0;
424
425 this.back_talk = new Phaser.Graphics(game, 100, game.height - 120);
426 this.back_talk.beginFill(0x000000);
427 this.back_talk.drawRect(0, 0, game.width - 200, 105);
428 this.back_talk.endFill();
429 this.back_talk.lineStyle(3, Phaser.Color.YELLOW, 1);
430 this.back_talk.moveTo(10, 35);
431 this.back_talk.lineTo(this.back_talk.width - 10, 35);
432 this.add(this.back_talk);
433 style = { align: 'left', fill: 'yellow', font: 'Ubuntu Mono', fontSize: 22, fontWeight: 'bold' };
434 this.text_talktitle = new Phaser.Text(game, this.back_talk.x, this.back_talk.y, null, style);
435 this.text_talktitle.anchor.setTo(-0.2, -0.2);
436 this.add(this.text_talktitle);
437 style = { align: 'left', fill: 'white', font: 'Ubuntu Mono', fontSize: 18, fontWeight: 'bold' };
438 this.text_talk = new Phaser.Text(game, this.back_talk.x + 35, this.back_talk.y + 40, null, style);
439 this.text_talk.wordWrap = true;
440 this.text_talk.wordWrapWidth = this.back_talk.width - 70;
441 this.text_talk.lineSpacing = -5;
442 this.add(this.text_talk);
443 this.leaveTalk();
444 this.last_talk = 0;
445 }
446
447 dropNotice(text) {
448 this.text_notice.text = text;
449 this.back_notice.visible = true;
450 this.text_notice.visible = true;
451 }
452
453 clearNotice() {
454 this.back_notice.visible = false;
455 this.text_notice.visible = false;
456 }
457
458 npcMenu(npc) {
459 if (!this.inMenu) {
460 this.inMenu = true;
461 this.clearNotice();
462 this.text_menutitle.text = npc.shortname;
463 this.back_menu.visible = true;
464 this.text_menutitle.visible = true;
465 this.text_menuitem_Talk.visible = true;
466 this.text_menuitem_Leave.visible = true;
467 this.text_menuitem_Take.visible = true;
468 this.currentmenuitem = MENUITEM_TALK;
469 this.actualizeCursorPosition();
470 this.text_menucursor.visible = true;
471 this.last_menustep = game.time.now;
472 }
473 }
474
475 leaveMenu() {
476 this.back_menu.visible = false;
477 this.text_menutitle.visible = false;
478 this.text_menuitem_Talk.visible = false;
479 this.text_menuitem_Leave.visible = false;
480 this.text_menuitem_Take.visible = false;
481 this.text_menucursor.visible = false;
482 this.inMenu = false;
483 }
484
485 talk(dialogue) {
486 this.inTalk = true;
487 this.dialogue = dialogue;
488 this.advanceTalk();
489 this.back_talk.visible = true;
490 this.text_talktitle.visible = true;
491 this.text_talk.visible = true;
492 }
493
494 leaveTalk() {
495 this.inTalk = false;
496 this.dialogue = null;
497 this.back_talk.visible = false;
498 this.text_talktitle.visible = false;
499 this.text_talk.visible = false;
500 }
501
502 advanceTalk() {
503 console.log(this.dialogue);
504 console.log(this.dialogue.actual());
505 var actualdialogue = this.dialogue.actual();
506 if (actualdialogue) {
507 this.text_talktitle.text = actualdialogue.actor.shortname;
508 this.text_talk.text = actualdialogue.text;
509 this.dialogue.advance();
510 }
511 else {
512 this.leaveTalk();
513 logic.endTalk();
514 }
515 this.last_talk = game.time.now;
516 }
517
518 actualizeCursorPosition() {
519 this.text_menucursor.y = this.text_menuitem_Talk.y + (this.currentmenuitem * GUI_MENUITEM_DISTANCE);
520 }
521
522 update() {
523 if ((this.inMenu) && (hasTimePassed(this.last_menustep, WAIT_MENUSTEP))) {
524 if (cursors.up.isDown)
525 {
526 this.currentmenuitem--;
527 if (this.currentmenuitem < MENUITEM_TALK) this.currentmenuitem = MENUITEM_TAKE;
528 this.actualizeCursorPosition();
529 }
530 else if (cursors.down.isDown)
531 {
532 this.currentmenuitem++;
533 if (this.currentmenuitem > MENUITEM_TAKE) this.currentmenuitem = MENUITEM_TALK;
534 this.actualizeCursorPosition();
535 }
536 if (game.input.keyboard.isDown(Phaser.Keyboard.ENTER)) {
537 logic.callMenu(this.currentmenuitem);
538 }
539 this.last_menustep = game.time.now;
540 }
541 if ((this.inTalk) && (game.input.keyboard.isDown(Phaser.Keyboard.ENTER)) && (hasTimePassed(this.last_talk, WAIT_TALK))) {
542 this.advanceTalk();
543 }
544 }
545 }
546
547 class GameLogic {
548
549 constructor() {
550 this.player = this.clara = this.carlos = this.saiki = this.peter = this.bianca = null;
551 this.gameinterface = new GameInterface(game, game.stage);
552 //this.doors = game.add.group(game.world, "doors");
553 this.last_menuselect = 0;
554 }
555
556 createObject(object) {
557 switch (object.type) {
558 case 'spawnpoint': this.createCharacter(object); break;
559 case 'door': this.createDoor(object); break;
560 case '': console.error("Object type is empty:", object); break;
561 default: console.error("Unknown object type:", object);
562 }
563 }
564
565 createCharacter(object) {
566 var newChar;
567 switch (object.name) {
568 case 'john':
569 newChar = new Player(object.x, object.y, 'player');
570 this.player = newChar;
571 this.player.enablePhysics();
572 game.camera.follow(this.player);
573 break;
574 case 'clara':
575 newChar = new NPC_Clara(object.x, object.y, 'clara', "Clara", "Clara Tnavelerri", 200);
576 this.clara = newChar;
577 break;
578 case 'carlos':
579 newChar = new NPC_Carlos(object.x, object.y, 'carlos', "Carlos", "Carlos Elbacalper", 150);
580 this.carlos = newChar;
581 break;
582 default:
583 console.error("Unknown character:", object);
584 }
585 newChar.name = object.name;
586 game.add.existing(newChar);
587 console.log(newChar);
588 }
589
590 createDoor(object) {
591 /* Calculate movement vector and correct position (32 = half tile width/height). */
592 var vector;
593 switch (object.rotation) {
594 case undefined:
595 case 0: vector = new Phaser.Point( -1, 0); object.x += 32; object.y -= 32; break;
596 case 90: vector = new Phaser.Point( 0, -1); object.x += 32; object.y += 32; break;
597 case 180: vector = new Phaser.Point( 1, 0); object.x -= 32; object.y += 32; break;
598 case 270: vector = new Phaser.Point( 0, 1); object.x -= 32; object.y -= 32; break;
599 default: console.error("Invalid rotation:", object.rotation);
600 }
601 this.doors.add(new Door(object.x, object.y, object.name, object.rotation, vector, object.properties.longpanel));
602 }
603
604 callMenu(menuitem) {
605 console.log("Menu callback received:", menuitem);
606 this.last_menuselect = game.time.now;
607 this.gameinterface.leaveMenu();
608 switch (menuitem) {
609 case MENUITEM_TALK:
610 this.player.interactablenpc.actionTalk();
611 break;
612 case MENUITEM_LEAVE:
613 this.player.interactablenpc.actionLeave();
614 this.player.unfreeze();
615 break;
616 case MENUITEM_TAKE:
617 if (this.player.interactablenpc.actionTake())
618 this.player.unfreeze();
619 break;
620 }
621 }
622
623 endTalk() {
624 if (this.player.interactablenpc) {
625 if (this.player.interactablenpc.endTalk()) {
626 this.gameinterface.npcMenu(this.player.interactablenpc);
627 }
628 else {
629 this.player.unfreeze();
630 this.last_menuselect = game.time.now;
631 }
632 }
633 }
634
635 openDoor(doorname) {
636 this.doors.children.forEach(function(o) { if (o.name == doorname) o.open(); });
637 }
638
639 closeDoor(doorname) {
640 this.doors.children.forEach(function(o) { if (o.name == doorname) o.close(); });
641 }
642
643 update() {
644 if ((game.input.keyboard.isDown(Phaser.Keyboard.ENTER)) && (hasTimePassed(this.last_menuselect, WAIT_MENUSTEP))) {
645 if ((this.player.interactablenpc) && (this.player.interactablenpc.interactable) && (!this.gameinterface.inMenu) && (!this.gameinterface.inTalk)) {
646 console.log("Starting interaction with", this.player.interactablenpc.fullname);
647 this.player.freeze();
648 this.gameinterface.npcMenu(this.player.interactablenpc);
649 }
650 }
651 }
652
653 }
654
655 class GamePlay extends Phaser.State {
656
657 constructor() {
658
659 super();
660 logic = new GameLogic();
661 game.world.updateOnlyExistingChildren = true;
662 //game.state.add('GameOver', GameOver, false);
663
664 }
665
666 preload() {
667
668 game.load.image('player', 'john.png');
669 game.load.image('clara', 'clara.png');
670 game.load.image('carlos', 'carlos.png');
671 game.load.image('saiki', 'saiki.png');
672 game.load.image('tileset', 'tileset.png');
673 game.load.image('objects', 'objects.png');
674 game.load.tilemap('gamemap', 'tilemap.json', null, Phaser.Tilemap.TILED_JSON);
675
676 }
677
678 create() {
679
680 game.world.setBounds(0, 0, 800, 600);
681 game.stage.backgroundColor = '#000000';
682 game.physics.startSystem(Phaser.Physics.ARCADE);
683 console.log("Debug enabled:", !game.debug.isDisabled);
684
685 var map = game.add.tilemap('gamemap');
686 map.addTilesetImage('tileset', 'tileset');
687 map.addTilesetImage('objects', 'objects');
688 var layer_floor = map.createLayer('floor');
689 layer_floor.resizeWorld();
690 this.layer_walls = map.createLayer('walls');
691 map.setCollisionBetween(1, 100, true, this.layer_walls);
692 this.layer_furniture = map.createLayer('furniture');
693 map.setCollisionBetween(1, 100, true, this.layer_furniture);
694
695 // FIXME: Don't create a group here like this, it's too ugly! Now I have it here due to the display order.
696 logic.doors = game.add.group(game.world, "doors");
697
698 map.objects['objects'].forEach(function(o) { logic.createObject(o); });
699 console.log(map.objects);
700
701 cursors = game.input.keyboard.createCursorKeys();
702 console.log(logic);
703
704 }
705
706 update() {
707
708 game.physics.arcade.collide(logic.player, this.layer_walls);
709 game.physics.arcade.collide(logic.player, this.layer_furniture);
710 game.physics.arcade.collide(logic.player, logic.doors);
711 logic.update();
712
713 }
714 }