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