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