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