Offscreen Teleportation
[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 this.disguise = npc;
163 }
164 }
165
166 class GameNPC extends Phaser.Sprite {
167 constructor(x, y, key, shortname, fullname, interaction_distance) {
168 super(game, x, y, key);
169 this.y -= this.height;
170 this.shortname = shortname;
171 this.fullname = fullname;
172 this.interaction_distance = interaction_distance;
173 this.interactable = false;
174 this.talkcount = 0;
175 }
176
177 kill() {
178 super.kill();
179 this.exists = false;
180 }
181
182 update() {
183 super.update();
184 if ((!this.interactable) && (game.physics.arcade.distanceBetween(this, logic.player) < this.interaction_distance)) {
185 logic.gameinterface.dropNotice("(ENTER) Interact with " + this.shortname + "!");
186 logic.player.offerInteraction(this);
187 this.interactable = true;
188 }
189 else if ((this.interactable) && (game.physics.arcade.distanceBetween(this, logic.player) > this.interaction_distance)) {
190 logic.gameinterface.clearNotice();
191 logic.player.offerInteraction(null);
192 this.interactable = false;
193 }
194 if ((this.teleport_instructions) && (game.physics.arcade.distanceBetween(this, logic.player) > (game.width + 200))) {
195 this.position = this.teleport_instructions.newPosition;
196 this.teleport_instructions.callback(this.teleport_instructions.callbackValue);
197 this.teleport_instructions = null;
198 }
199 }
200
201 actionTalk() {
202 this.talkcount++;
203 }
204
205 actionLeave() {
206 }
207
208 actionTake() {
209 logic.player.offerInteraction(null);
210 logic.player.takeMe(this);
211 return true;
212 }
213
214 endTalk() {
215 return true;
216 }
217
218 offscreenTeleportation(newPosition, callback, callbackValue) {
219 this.teleport_instructions = { newPosition: newPosition, callback: callback, callbackValue: callbackValue };
220 console.log("Offscreen teleport request registered:", this.teleport_instructions);
221 }
222 }
223
224 class NPC_Clara extends GameNPC {
225 actionTalk() {
226 switch (this.talkcount) {
227 case 0:
228 logic.gameinterface.talk(new Dialogue( [ { actor: this, text: "What a morning..." },
229 { actor: logic.player, text: "Hi Clara! What happened?" },
230 { 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." },
231 { actor: logic.player, text: "Hmm... I'll look into it. Maybe it's just that everyone is fired." },
232 { actor: this, text: "Haha! Wouldn't joke about this, though, due to the recent layoffs." },
233 { actor: logic.player, text: "Well, maybe if we dare to joke about it, it won't happen to us..." },
234 { actor: this, text: "Wish it worked like that... Is it some superstition like the belief that having an umbrella with you prevents rain?" },
235 { actor: logic.player, text: "Nah, that actually works; it's not a superstition, but Murphy's Law!" },
236 { actor: this, text: "If you say so..." },
237 { actor: this, text: "Anyway, I logged you in and opened the door for you." } ] ));
238 logic.openDoor("cutedoor");
239 break;
240 case 1:
241 logic.gameinterface.talk(new Dialogue( [ { actor: this, text: "John, have you ever thought about losing your employee card?" },
242 { actor: logic.player, text: "Yeah, you'd just get a new one." },
243 { actor: this, text: "You don't understand me, John!" },
244 { actor: this, text: "..." },
245 { actor: this, text: "I mean... Losing it for real..." },
246 { actor: this, text: "Doesn't it feel like it's the culmination of your being?" },
247 { actor: this, text: "If I had no card, would I still exist?" },
248 { actor: logic.player, text: "..." },
249 { actor: logic.player, text: "You sound very philosophical today." } ] ));
250 break;
251 case 2:
252 case 3:
253 case 4:
254 logic.gameinterface.talk(new Dialogue( [ { actor: this, text: "Would you like to hear a random fun fact?" },
255 { actor: logic.player, text: "Of course!" },
256 { 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," },
257 { 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" },
258 { actor: this, text: "into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem" },
259 { actor: this, text: "Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." },
260 { actor: logic.player, text: "That's very interesting, I didn't know that!" },
261 { actor: logic.player, text: "Thanks for sharing, Clara!" } ] ));
262 break;
263 case 5:
264 logic.gameinterface.talk(new Dialogue( [ { actor: this, text: "Would you like to hear a random fun fact?" },
265 { actor: logic.player, text: "Of course!..." },
266 { actor: logic.player, text: "But first..." },
267 { actor: this, text: "What is it?" },
268 { actor: logic.player, text: "..." },
269 { actor: logic.player, text: "Never mind, go on with what you wanted to say." },
270 { actor: this, text: "Lorem Ipsum is simply dummy text of the printing and typesetting industry..." },
271 { actor: logic.player, text: "I knew that already..." },
272 { actor: logic.player, text: "But..." },
273 { actor: logic.player, text: "Would you go out on a date with me?" },
274 { actor: this, text: "..." },
275 { actor: this, text: "No." } ] ));
276 break;
277 default:
278 logic.gameinterface.talk(new Dialogue( [ { actor: this, text: "..." } ] ));
279 }
280 super.actionTalk();
281 }
282
283 actionLeave() {
284 logic.gameinterface.dropNotice(this.shortname + ": Have a great day, John!");
285 }
286
287 actionTake() {
288 logic.openDoor("cutedoor");
289 return super.actionTake();
290 }
291
292 endTalk() {
293 return (this.talkcount < 6);
294 }
295 }
296
297 class NPC_Carlos extends GameNPC {
298 actionTalk() {
299 if (logic.clara.alive) {
300 switch (this.talkcount) {
301 case 0:
302 logic.gameinterface.talk(new Dialogue( [ { actor: logic.player, text: "Good morning, Carlos! What's up?" },
303 { actor: this, text: "Hey, John! Not too much, I'm just trying to sell some shit to this new client." },
304 { actor: logic.player, text: "And so? Is it hard to convince them?" },
305 { actor: this, text: "Of course, they don't want to buy. It's shit." },
306 { actor: this, text: "I mean, who would want to buy shit?" },
307 { actor: logic.player, text: "Yeah. Relatable... But why are you trying to sell shit? I mean, why not one of our cutting edge technologies?" },
308 { actor: this, text: "They couldn't afford it." },
309 { actor: logic.player, text: "Then why are you trying to sell them... anything at all? They'd better off with a cheaper company." },
310 { actor: this, text: "Company policies. Plus the management instructed me to sell to them, no matter what." },
311 { actor: logic.player, text: "That sucks... Well... good luck!" },
312 { actor: this, text: "Thanks..." } ] ));
313 logic.closeDoor("cutedoor");
314 logic.openDoor("carlosdoor");
315 break;
316 case 1:
317 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." },
318 { actor: this, text: "These layoffs are terrifying. They let go of anyone for even the slightest mistake." },
319 { actor: logic.player, text: "But it's not a mistake when our client doesn't buy something they don't need." },
320 { actor: this, text: "Tell this to my boss..." } ] ));
321 break;
322 case 2:
323 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." },
324 { actor: this, text: "..." },
325 { 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..." },
326 { 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 have one." },
327 { actor: this, text: "So I thought I could grow my own pig in the garden from pig seeds!" },
328 { actor: logic.player, text: "Interesting. Where are you going with this?" },
329 { actor: this, text: "I don't know... sometimes I feel like I'd rather sell pig semen to our clients, heh!" },
330 { actor: this, text: "Uhm..." },
331 { actor: this, text: "Totally don't tell this to Saiki!" } ] ));
332 this.pigsemen = true;
333 break;
334 case 3:
335 logic.gameinterface.talk(new Dialogue( [ { actor: this, text: "Weren't you heading to the IT department to the right?" },
336 { actor: this, text: "You know... the green department which looks way better than blue!" },
337 { actor: this, text: "..." },
338 { actor: this, text: "Damn, why do I have to work in blue? I just feel... so blue about it." } ] ));
339 break;
340 case 4:
341 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." },
342 { actor: this, text: "You're so lucky that you can work with her every day!" } ] ));
343 break;
344 default:
345 logic.gameinterface.talk(new Dialogue( [ { actor: this, text: "Guess you'll stay here then. It's cool, you're great company anyway." } ] ));
346 }
347 }
348 else {
349 switch (this.talkcount) {
350 case 0:
351 logic.gameinterface.talk(new Dialogue( [ { actor: logic.player, text: "Good morning, Carlos!" },
352 { actor: this, text: "Whoa, Clara! You have changed so much! I barely recognize you. What's up?" },
353 { actor: logic.player, text: "I need to talk to Saiki at the IT department, but... my card doesn't allow me to enter there." },
354 { actor: this, text: "Well... I can certainly help you with that..." },
355 { actor: this, text: "..." } ] ));
356 break;
357 case 1:
358 logic.gameinterface.talk(new Dialogue( [ { actor: logic.player, text: "Carlos. You promised to help me." },
359 { actor: this, text: "Uhm... It's just... Please give me 5 mins to finish this e-mail." },
360 { actor: logic.player, text: "I don't have much time, Carlos." },
361 { 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." },
362 { actor: logic.player, text: "..." } ] ));
363 break;
364 case 2:
365 logic.gameinterface.talk(new Dialogue( [ { actor: logic.player, text: "Time is up, Carlos!" },
366 { actor: this, text: "Please give me just another min." },
367 { actor: logic.player, text: "Fine. Please just give me your card and I'll help myself!" },
368 { actor: this, text: "But... you know that company policies forbid that, Clara! Please just be a little more patient!" },
369 { actor: logic.player, text: "You will give me your card now or you will regret!" },
370 { actor: this, text: "...?" },
371 { actor: this, text: "You're not gonna treat me like this. Forget it!" },
372 { actor: logic.player, text: "You'll not work here tomorrow." } ] ));
373 break;
374 default:
375 logic.gameinterface.talk(new Dialogue( [ { actor: this, text: "..." } ] ));
376 }
377 }
378 super.actionTalk();
379 }
380
381 actionLeave() {
382 if (logic.clara.alive) {
383 logic.gameinterface.dropNotice(this.shortname + ": Bye, John! Pass on my greetings to Saiki!");
384 }
385 }
386
387 actionTake() {
388 logic.closeDoor("cutedoor");
389 logic.openDoor("carlosdoor");
390 return super.actionTake();
391 }
392
393 endTalk() {
394 return (logic.clara.alive) || (this.talkcount < 3);
395 }
396 }
397
398 class NPC_Saiki extends GameNPC {
399 actionTalk() {
400 if (logic.carlos.alive) {
401 switch (this.talkcount) {
402 case 0:
403 logic.gameinterface.talk(new Dialogue( [ { actor: logic.player, text: "Hi Saiki!" },
404 { actor: this, text: "Hi John! Glad you arrived, here's some work for you!" },
405 { actor: logic.player, text: "Awesome! What is it?" },
406 { actor: this, text: "Haha, joking! Actually, our queue is suspiciously empty for today." },
407 { actor: logic.player, text: "Yeah... That's really suspicious!" },
408 { actor: logic.player, text: "Maybe I can get home today in time?" },
409 { actor: this, text: "You mean, you wouldn't spend the mandatory free extra working hours?" },
410 { actor: this, text: "You steal from the company! The company needs your unpaid hours of work! Otherwise, how would we finance the trash can I ordered for the department?" },
411 { actor: this, text: "*Whispers with wink.* I hope you get my sarcasm." },
412 { actor: logic.player, text: "Haha! I always get it, Saiki!" } ] ));
413 logic.closeDoor("carlosdoor");
414 logic.openDoor("saikidoor");
415 break;
416 case 1:
417 logic.gameinterface.talk(new Dialogue( [ { actor: this, text: "Now that I think about it, there's still one thing. You could look into why the access control system is acting so funny today." },
418 { actor: this, text: "Clara has to enter people manually." },
419 { actor: logic.player, text: "Yeah, I'll definitely check that." } ] ));
420 break;
421 case 2:
422 logic.gameinterface.talk(new Dialogue( [ { actor: logic.player, text: "So... what's up with the trash can?" },
423 { actor: this, text: "I'm always up for such deep conversation" },
424 { actor: this, text: "about trash cans." },
425 { actor: this, text: "While we only have one trash can, other departments seem to be abundant of them. And ours is always filled. So I requested an additional trash can." },
426 { actor: this, text: "Now the ticket is in PENDING status. The latest work note is from two months ago, telling there is no budget at the moment." },
427 { actor: logic.player, text: "Hmm... Maybe it's too much to ask. Can we take one from another department?" },
428 { actor: this, text: "No, everyone holds on to their trash cans." },
429 { actor: logic.player, text: "You certainly did your research." },
430 { actor: this, text: "Of course. It's an important subject." } ] ));
431 break;
432 case 3:
433 logic.gameinterface.talk(new Dialogue( [ { actor: this, text: "My parents named me after a Japanese rock singer." },
434 { actor: logic.player, text: "Cool! Which band?" },
435 { actor: this, text: "I don't remember the band name. But the particular singer has a blue rose in her hair most of the time." } ] ));
436 break;
437 default:
438 if ((logic.carlos.pigsemen) && (this.talkcount == 4)) {
439 logic.gameinterface.talk(new Dialogue( [ { actor: logic.player, text: "Did you know that Carlos thought he can grow a pig by burying pig semen in the ground? What do you think about this?" },
440 { actor: this, text: "Hmm..." },
441 { actor: this, text: "I guess it didn't work." },
442 { actor: this, text: "So do you want to know what I think about this?" },
443 { actor: this, text: "..." },
444 { actor: this, text: "IT'S BRILLIANT!!!" },
445 { actor: this, text: "You know, it really sparkled my artistic vision! Now I must draw a pig with its legs rooted into the ground!" },
446 { actor: logic.player, text: "That's gross. Poor pig." },
447 { actor: this, text: "Indeed. But this is the point. He's stuck in one place, cannot move. Like you in life. So you feel sympathy for him." },
448 { actor: logic.player, text: "You have a point." },
449 { actor: this, text: "I think I'll catch Carlos and talk to him. I always liked that guy but I didn't know he's such imaginative." } ] ));
450 this.offscreenTeleportation(new Phaser.Point(logic.carlos.x, logic.carlos.y + 64), this.tpDone, this);
451 }
452 else {
453 if (Phaser.Math.random() < 0.5) {
454 logic.gameinterface.talk(new Dialogue( [ { actor: this, text: "I've found an orchestrated rendition of „Potential for Anything”. You'll have to check it out, it's breathtaking!" } ] ));
455 }
456 else {
457 logic.gameinterface.talk(new Dialogue( [ { actor: this, text: "I don't like that our department is green. The blue one, where Carlos works, looks much better." } ] ));
458 }
459 }
460 }
461 }
462 else {
463 switch (this.talkcount) {
464 case 0:
465 logic.gameinterface.talk(new Dialogue( [ { actor: logic.player, text: "I love you, Saiki!" },
466 { actor: this, text: "Hi Carlos..." },
467 { actor: logic.player, text: "I'm infatuated by the mere thought of you." },
468 { actor: this, text: "Well... it sounds pretty much creepy." },
469 { actor: logic.player, text: "Will you go out on a date with me?" },
470 { actor: this, text: "..." },
471 { actor: this, text: "Sorry to hurt your feelings, but... even though I like you... Your sudden, unprecedented confession makes me scared." },
472 { actor: this, text: "Especially with that facial expression." },
473 { actor: this, text: "I mean... you're not even joking." },
474 { actor: this, text: "So sorry... I won't date you." } ] ));
475 break;
476 case 1:
477 logic.gameinterface.talk(new Dialogue( [ { actor: logic.player, text: "I thought I could impress you." },
478 { actor: this, text: "I'm sorry." } ] ));
479 break;
480 case 2:
481 logic.gameinterface.talk(new Dialogue( [ { actor: logic.player, text: "You will give me your card." },
482 { actor: this, text: "Geez, what for? Go back to your department and work!" },
483 { actor: logic.player, text: "I need it." },
484 { actor: this, text: "..." } ] ));
485 break;
486 default:
487 logic.gameinterface.talk(new Dialogue( [ { actor: this, text: "Don't you have anything better to do, Carlos?" } ] ));
488 }
489 }
490 super.actionTalk();
491 }
492
493 actionLeave() {
494 if (logic.carlos.alive) {
495 logic.gameinterface.dropNotice(this.shortname + ": It was nice talking to you!");
496 }
497 }
498
499 actionTake() {
500 logic.closeDoor("carlosdoor");
501 logic.openDoor("saikidoor");
502 return super.actionTake();
503 }
504
505 endTalk() {
506 return (logic.carlos.alive);
507 }
508
509 tpDone(value) {
510 console.log("Woot-woot! Teleport complete:", value);
511 }
512 }
513
514
515 class GameInterface extends Phaser.Group {
516 constructor(game, parent) {
517 super(game, parent, 'GUI', false, false, 0);
518 this.back_notice = new Phaser.Graphics(game, 0, game.height - 50);
519 this.back_notice.beginFill(0x000000);
520 this.back_notice.drawRect(0, 0, game.width, 30);
521 this.back_notice.endFill();
522 this.add(this.back_notice);
523 this.text_notice = new Phaser.Text(game, 0, this.back_notice.y, null, { align: 'left', fill: 'white', font: 'Ubuntu Mono', fontSize: 18, fontWeight: 'bold' });
524 this.text_notice.anchor.setTo(-0.5, -0.2);
525 this.add(this.text_notice);
526 this.clearNotice();
527
528 this.back_menu = new Phaser.Graphics(game, 150, (game.height / 2) + 40);
529 this.back_menu.beginFill(0x000000);
530 this.back_menu.drawRect(0, 0, 155, 105);
531 this.back_menu.endFill();
532 this.back_menu.lineStyle(3, Phaser.Color.YELLOW, 1);
533 this.back_menu.moveTo(10, 35);
534 this.back_menu.lineTo(this.back_menu.width - 10, 35);
535 this.add(this.back_menu);
536 var style = { align: 'left', fill: 'yellow', font: 'Ubuntu Mono', fontSize: 22, fontWeight: 'bold' };
537 this.text_menutitle = new Phaser.Text(game, this.back_menu.x, this.back_menu.y, null, style);
538 this.text_menutitle.anchor.setTo(-0.2, -0.2);
539 style = { align: 'left', fill: 'white', font: 'Ubuntu Mono', fontSize: 18, fontWeight: 'bold' };
540 this.text_menuitem_Talk = new Phaser.Text(game, this.back_menu.x + 35, this.back_menu.y + 40, "Talk", style);
541 this.text_menuitem_Leave = new Phaser.Text(game, this.back_menu.x + 35, this.text_menuitem_Talk.y + GUI_MENUITEM_DISTANCE, "Leave", style);
542 this.text_menuitem_Take = new Phaser.Text(game, this.back_menu.x + 35, this.text_menuitem_Leave.y + GUI_MENUITEM_DISTANCE, "Take ID", style);
543 style.fill = 'yellow';
544 this.text_menucursor = new Phaser.Text(game, this.back_menu.x + 15, this.text_menuitem_Talk.y, "→", style);
545 this.add(this.text_menutitle);
546 this.add(this.text_menuitem_Talk);
547 this.add(this.text_menuitem_Leave);
548 this.add(this.text_menuitem_Take);
549 this.add(this.text_menucursor);
550 this.leaveMenu();
551 this.last_menustep = 0;
552
553 this.back_talk = new Phaser.Graphics(game, 100, game.height - 120);
554 this.back_talk.beginFill(0x000000);
555 this.back_talk.drawRect(0, 0, game.width - 200, 105);
556 this.back_talk.endFill();
557 this.back_talk.lineStyle(3, Phaser.Color.YELLOW, 1);
558 this.back_talk.moveTo(10, 35);
559 this.back_talk.lineTo(this.back_talk.width - 10, 35);
560 this.add(this.back_talk);
561 style = { align: 'left', fill: 'yellow', font: 'Ubuntu Mono', fontSize: 22, fontWeight: 'bold' };
562 this.text_talktitle = new Phaser.Text(game, this.back_talk.x + 15, this.back_talk.y + 6, null, style);
563 //this.text_talktitle.anchor.setTo(-0.2, -0.2);
564 this.add(this.text_talktitle);
565 this.strikethrough_talktitle = new Phaser.Graphics(game, this.back_talk.x, this.back_talk.y);
566 this.strikethrough_talktitle.lineStyle(3, Phaser.Color.RED, 1);
567 this.strikethrough_talktitle.moveTo(12, 20);
568 this.strikethrough_talktitle.lineTo(67, 20);
569 this.add(this.strikethrough_talktitle);
570 style = { align: 'left', fill: 'white', font: 'Ubuntu Mono', fontSize: 18, fontWeight: 'bold' };
571 this.text_talk = new Phaser.Text(game, this.back_talk.x + 35, this.back_talk.y + 40, null, style);
572 this.text_talk.wordWrap = true;
573 this.text_talk.wordWrapWidth = this.back_talk.width - 70;
574 this.text_talk.lineSpacing = -5;
575 this.add(this.text_talk);
576 this.leaveTalk();
577 this.last_talk = 0;
578 }
579
580 dropNotice(text) {
581 this.text_notice.text = text;
582 this.back_notice.visible = true;
583 this.text_notice.visible = true;
584 }
585
586 clearNotice() {
587 this.back_notice.visible = false;
588 this.text_notice.visible = false;
589 }
590
591 npcMenu(npc) {
592 if (!this.inMenu) {
593 this.inMenu = true;
594 this.clearNotice();
595 this.text_menutitle.text = npc.shortname;
596 this.back_menu.visible = true;
597 this.text_menutitle.visible = true;
598 this.text_menuitem_Talk.visible = true;
599 this.text_menuitem_Leave.visible = true;
600 this.text_menuitem_Take.visible = true;
601 this.currentmenuitem = MENUITEM_TALK;
602 this.actualizeCursorPosition();
603 this.text_menucursor.visible = true;
604 this.last_menustep = game.time.now;
605 }
606 }
607
608 leaveMenu() {
609 this.back_menu.visible = false;
610 this.text_menutitle.visible = false;
611 this.text_menuitem_Talk.visible = false;
612 this.text_menuitem_Leave.visible = false;
613 this.text_menuitem_Take.visible = false;
614 this.text_menucursor.visible = false;
615 this.inMenu = false;
616 }
617
618 talk(dialogue) {
619 this.inTalk = true;
620 this.dialogue = dialogue;
621 this.advanceTalk();
622 this.back_talk.visible = true;
623 this.text_talktitle.visible = true;
624 this.text_talk.visible = true;
625 }
626
627 leaveTalk() {
628 this.inTalk = false;
629 this.dialogue = null;
630 this.back_talk.visible = false;
631 this.text_talktitle.visible = false;
632 this.strikethrough_talktitle.visible = false;
633 this.text_talk.visible = false;
634 }
635
636 advanceTalk() {
637 console.log(this.dialogue);
638 console.log(this.dialogue.actual());
639 var actualdialogue = this.dialogue.actual();
640 if (actualdialogue) {
641 if (actualdialogue.actor.disguise) {
642 this.text_talktitle.text = actualdialogue.actor.shortname + " " + actualdialogue.actor.disguise.shortname;
643 this.strikethrough_talktitle.visible = true;
644 }
645 else {
646 this.text_talktitle.text = actualdialogue.actor.shortname;
647 this.strikethrough_talktitle.visible = false;
648 }
649 this.text_talk.text = actualdialogue.text;
650 this.dialogue.advance();
651 }
652 else {
653 this.leaveTalk();
654 logic.endTalk();
655 }
656 this.last_talk = game.time.now;
657 }
658
659 actualizeCursorPosition() {
660 this.text_menucursor.y = this.text_menuitem_Talk.y + (this.currentmenuitem * GUI_MENUITEM_DISTANCE);
661 }
662
663 update() {
664 if ((this.inMenu) && (hasTimePassed(this.last_menustep, WAIT_MENUSTEP))) {
665 if (cursors.up.isDown)
666 {
667 this.currentmenuitem--;
668 if (this.currentmenuitem < MENUITEM_TALK) this.currentmenuitem = MENUITEM_TAKE;
669 this.actualizeCursorPosition();
670 }
671 else if (cursors.down.isDown)
672 {
673 this.currentmenuitem++;
674 if (this.currentmenuitem > MENUITEM_TAKE) this.currentmenuitem = MENUITEM_TALK;
675 this.actualizeCursorPosition();
676 }
677 if (game.input.keyboard.isDown(Phaser.Keyboard.ENTER)) {
678 logic.callMenu(this.currentmenuitem);
679 }
680 this.last_menustep = game.time.now;
681 }
682 if ((this.inTalk) && (game.input.keyboard.isDown(Phaser.Keyboard.ENTER)) && (hasTimePassed(this.last_talk, WAIT_TALK))) {
683 this.advanceTalk();
684 }
685 }
686 }
687
688 class GameLogic {
689
690 constructor() {
691 this.player = this.clara = this.carlos = this.saiki = this.peter = this.bianca = null;
692 this.gameinterface = new GameInterface(game, game.stage);
693 //this.doors = game.add.group(game.world, "doors");
694 this.last_menuselect = 0;
695 }
696
697 createObject(object) {
698 switch (object.type) {
699 case 'spawnpoint': this.createCharacter(object); break;
700 case 'door': this.createDoor(object); break;
701 case '': console.error("Object type is empty:", object); break;
702 default: console.error("Unknown object type:", object);
703 }
704 }
705
706 createCharacter(object) {
707 var newChar;
708 switch (object.name) {
709 case 'john':
710 newChar = new Player(object.x, object.y, 'player');
711 this.player = newChar;
712 this.player.enablePhysics();
713 game.camera.follow(this.player);
714 break;
715 case 'clara':
716 newChar = new NPC_Clara(object.x, object.y, 'clara', "Clara", "Clara Tnavelerri", 200);
717 this.clara = newChar;
718 break;
719 case 'carlos':
720 newChar = new NPC_Carlos(object.x, object.y, 'carlos', "Carlos", "Carlos Elbacalper", 150);
721 this.carlos = newChar;
722 break;
723 case 'saiki':
724 newChar = new NPC_Saiki(object.x, object.y, 'saiki', "Saiki", "Saiki Ytpme", 150);
725 this.saiki = newChar;
726 break;
727 default:
728 console.error("Unknown character:", object);
729 }
730 newChar.name = object.name;
731 game.add.existing(newChar);
732 console.log(newChar);
733 }
734
735 createDoor(object) {
736 /* Calculate movement vector and correct position (32 = half tile width/height). */
737 var vector;
738 switch (object.rotation) {
739 case undefined:
740 case 0: vector = new Phaser.Point( -1, 0); object.x += 32; object.y -= 32; break;
741 case 90: vector = new Phaser.Point( 0, -1); object.x += 32; object.y += 32; break;
742 case 180: vector = new Phaser.Point( 1, 0); object.x -= 32; object.y += 32; break;
743 case 270: vector = new Phaser.Point( 0, 1); object.x -= 32; object.y -= 32; break;
744 default: console.error("Invalid rotation:", object.rotation);
745 }
746 this.doors.add(new Door(object.x, object.y, object.name, object.rotation, vector, object.properties.longpanel));
747 }
748
749 callMenu(menuitem) {
750 console.log("Menu callback received:", menuitem);
751 this.last_menuselect = game.time.now;
752 this.gameinterface.leaveMenu();
753 switch (menuitem) {
754 case MENUITEM_TALK:
755 this.player.interactablenpc.actionTalk();
756 break;
757 case MENUITEM_LEAVE:
758 this.player.interactablenpc.actionLeave();
759 this.player.unfreeze();
760 break;
761 case MENUITEM_TAKE:
762 if (this.player.interactablenpc.actionTake())
763 this.player.unfreeze();
764 break;
765 }
766 }
767
768 endTalk() {
769 if (this.player.interactablenpc) {
770 if (this.player.interactablenpc.endTalk()) {
771 this.gameinterface.npcMenu(this.player.interactablenpc);
772 }
773 else {
774 this.player.unfreeze();
775 this.last_menuselect = game.time.now;
776 }
777 }
778 }
779
780 openDoor(doorname) {
781 this.doors.children.forEach(function(o) { if (o.name == doorname) o.open(); });
782 }
783
784 closeDoor(doorname) {
785 this.doors.children.forEach(function(o) { if (o.name == doorname) o.close(); });
786 }
787
788 update() {
789 if ((game.input.keyboard.isDown(Phaser.Keyboard.ENTER)) && (hasTimePassed(this.last_menuselect, WAIT_MENUSTEP))) {
790 if ((this.player.interactablenpc) && (this.player.interactablenpc.interactable) && (!this.gameinterface.inMenu) && (!this.gameinterface.inTalk)) {
791 console.log("Starting interaction with", this.player.interactablenpc.fullname);
792 this.player.freeze();
793 this.gameinterface.npcMenu(this.player.interactablenpc);
794 }
795 }
796 }
797
798 }
799
800 class GamePlay extends Phaser.State {
801
802 constructor() {
803
804 super();
805 logic = new GameLogic();
806 game.world.updateOnlyExistingChildren = true;
807 //game.state.add('GameOver', GameOver, false);
808
809 }
810
811 preload() {
812
813 game.load.image('player', 'john.png');
814 game.load.image('clara', 'clara.png');
815 game.load.image('carlos', 'carlos.png');
816 game.load.image('saiki', 'saiki.png');
817 game.load.image('tileset', 'tileset.png');
818 game.load.image('objects', 'objects.png');
819 game.load.tilemap('gamemap', 'tilemap.json', null, Phaser.Tilemap.TILED_JSON);
820
821 }
822
823 create() {
824
825 game.world.setBounds(0, 0, 800, 600);
826 game.stage.backgroundColor = '#000000';
827 game.physics.startSystem(Phaser.Physics.ARCADE);
828 console.log("Debug enabled:", !game.debug.isDisabled);
829
830 var map = game.add.tilemap('gamemap');
831 map.addTilesetImage('tileset', 'tileset');
832 map.addTilesetImage('objects', 'objects');
833 var layer_floor = map.createLayer('floor');
834 layer_floor.resizeWorld();
835 this.layer_walls = map.createLayer('walls');
836 map.setCollisionBetween(1, 100, true, this.layer_walls);
837 this.layer_furniture = map.createLayer('furniture');
838 map.setCollisionBetween(1, 100, true, this.layer_furniture);
839
840 // FIXME: Don't create a group here like this, it's too ugly! Now I have it here due to the display order.
841 logic.doors = game.add.group(game.world, "doors");
842
843 map.objects['objects'].forEach(function(o) { logic.createObject(o); });
844 console.log(map.objects);
845
846 cursors = game.input.keyboard.createCursorKeys();
847 console.log(logic);
848
849 }
850
851 update() {
852
853 game.physics.arcade.collide(logic.player, this.layer_walls);
854 game.physics.arcade.collide(logic.player, this.layer_furniture);
855 game.physics.arcade.collide(logic.player, logic.doors);
856 logic.update();
857
858 }
859 }