1 var game
= new Phaser
.Game(800, 600, Phaser
.AUTO
, '', { preload: function() { this.state
.add('GamePlay', GamePlay
, true); } });
5 const PLAYER_SPEED
= 200;
6 const WAIT_MENUSTEP
= 100;
9 const MENUITEM_TALK
= 0;
10 const MENUITEM_LEAVE
= 1;
11 const MENUITEM_TAKE
= 2;
13 const GUI_MENUITEM_DISTANCE
= 20;
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) {
22 function hasTimePassed(time
, delay
) {
23 return (game
.time
.now
- time
) > delay
;
27 // dialogue is array of {actor, text} records.
28 constructor(dialogue
) {
29 this.dialogue
= dialogue
;
34 return this.dialogue
[this.state
];
42 class Door
extends Phaser
.TileSprite
{
43 constructor(x
, y
, name
, rotation
, vector
, longpanel
) {
44 super(game
, x
, y
, 64, 64, 'objects');
46 this.longpanel
= 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));
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));
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);
64 game
.physics
.arcade
.enable(this);
65 this.body
.immovable
= true;
66 this.setBody(rotation
);
73 this.body
.setSize(this.width
, 10, this.longpanel
* (rotation
/ 180) * 64, 27);
77 this.body
.setSize(10, this.width
, 27 + (this.longpanel
* 64), this.longpanel
* ((rotation
- 270) / 180) * 64);
80 console
.log("Unable to set body due to unknown rotation:", rotation
);
86 this.opentween
.start();
93 this.closetween
.start();
99 game
.debug
.body(this);
103 class Player
extends Phaser
.Sprite
{
105 super(game
, x
, y
, 'player');
106 this.y
-= this.height
;
107 this.shortname
= "John";
108 this.fullname
= "John Evals";
109 this.offerInteraction(null);
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;
121 if ((this.alive
) && (!this.freezed
))
123 if (cursors
.left
.isDown
)
125 this.body
.velocity
.x
= -PLAYER_SPEED
;
127 else if (cursors
.right
.isDown
)
129 this.body
.velocity
.x
= PLAYER_SPEED
;
131 if (cursors
.up
.isDown
)
133 this.body
.velocity
.y
= -PLAYER_SPEED
;
135 else if (cursors
.down
.isDown
)
137 this.body
.velocity
.y
= PLAYER_SPEED
;
143 this.body
.velocity
.x
= this.body
.velocity
.y
= 0;
152 this.freezed
= false;
155 offerInteraction(npc
) {
156 this.interactablenpc
= npc
;
160 this.loadTexture(npc
.key
);
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;
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;
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;
203 logic
.player
.offerInteraction(null);
204 logic
.player
.takeMe(this);
213 class NPC_Clara
extends GameNPC
{
215 switch (this.talkcount
) {
217 logic
.gameinterface
.talk(new Dialogue( [ { actor
: this, text
: "What a morning..." },
218 { actor
: logic
.player
, text
: "Hi Clara! What happened?" },
219 { actor
: this, text
: "No one cares to tell. I just know that I have to log on people manually, as the access control system doesn't work properly." },
220 { actor
: logic
.player
, text
: "Hmm... I'll look into it. Maybe it's just that everyone is fired." },
221 { actor
: this, text
: "Haha! Wouldn't joke about this, though, due to the recent layoffs." },
222 { actor
: logic
.player
, text
: "Well, maybe if we dare to joke about it, it won't happen to us..." },
223 { actor
: this, text
: "Wish it worked like that... Is it some superstition like the belief that having an umbrella with you prevents rain?" },
224 { actor
: logic
.player
, text
: "Nah, that actually works; it's not a superstition, but Murphy's Law!" },
225 { actor
: this, text
: "If you say so..." } ] ));
226 logic
.openDoor("cutedoor");
229 logic
.gameinterface
.talk(new Dialogue( [ { actor
: this, text
: "John, have you ever thought about losing your employee card?" },
230 { actor
: logic
.player
, text
: "Yeah, you'd just get a new one." },
231 { actor
: this, text
: "You don't understand me, John!" },
232 { actor
: this, text
: "..." },
233 { actor
: this, text
: "I mean... Losing it for real..." },
234 { actor
: this, text
: "Doesn't it feel like it's the culmination of your being?" },
235 { actor
: this, text
: "If I had no card, would I still exist?" },
236 { actor
: logic
.player
, text
: "..." },
237 { actor
: logic
.player
, text
: "You sound very philosophical today." } ] ));
238 logic
.closeDoor("cutedoor");
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!" } ] ));
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." } ] ));
267 logic
.gameinterface
.talk(new Dialogue( [ { actor
: this, text
: "..." } ] ));
273 logic
.gameinterface
.dropNotice(this.shortname
+ ": Have a great day, John!");
277 return (this.talkcount
< 6);
282 class GameInterface
extends Phaser
.Group
{
283 constructor(game
, parent
) {
284 super(game
, parent
, 'GUI', false, false, 0);
285 this.back_notice
= new Phaser
.Graphics(game
, 0, game
.height
- 50);
286 this.back_notice
.beginFill(0x000000);
287 this.back_notice
.drawRect(0, 0, game
.width
, 30);
288 this.back_notice
.endFill();
289 this.add(this.back_notice
);
290 this.text_notice
= new Phaser
.Text(game
, 0, this.back_notice
.y
, null, { align
: 'left', fill
: 'white', font
: 'Ubuntu Mono', fontSize
: 18, fontWeight
: 'bold' });
291 this.text_notice
.anchor
.setTo(-0.5, -0.2);
292 this.add(this.text_notice
);
295 this.back_menu
= new Phaser
.Graphics(game
, 150, (game
.height
/ 2) + 40);
296 this.back_menu
.beginFill(0x000000);
297 this.back_menu
.drawRect(0, 0, 155, 105);
298 this.back_menu
.endFill();
299 this.back_menu
.lineStyle(3, Phaser
.Color
.YELLOW
, 1);
300 this.back_menu
.moveTo(10, 35);
301 this.back_menu
.lineTo(this.back_menu
.width
- 10, 35);
302 this.add(this.back_menu
);
303 var style
= { align
: 'left', fill
: 'yellow', font
: 'Ubuntu Mono', fontSize
: 22, fontWeight
: 'bold' };
304 this.text_menutitle
= new Phaser
.Text(game
, this.back_menu
.x
, this.back_menu
.y
, null, style
);
305 this.text_menutitle
.anchor
.setTo(-0.2, -0.2);
306 style
= { align
: 'left', fill
: 'white', font
: 'Ubuntu Mono', fontSize
: 18, fontWeight
: 'bold' };
307 this.text_menuitem_Talk
= new Phaser
.Text(game
, this.back_menu
.x
+ 35, this.back_menu
.y
+ 40, "Talk", style
);
308 this.text_menuitem_Leave
= new Phaser
.Text(game
, this.back_menu
.x
+ 35, this.text_menuitem_Talk
.y
+ GUI_MENUITEM_DISTANCE
, "Leave", style
);
309 this.text_menuitem_Take
= new Phaser
.Text(game
, this.back_menu
.x
+ 35, this.text_menuitem_Leave
.y
+ GUI_MENUITEM_DISTANCE
, "Take ID", style
);
310 style
.fill
= 'yellow';
311 this.text_menucursor
= new Phaser
.Text(game
, this.back_menu
.x
+ 15, this.text_menuitem_Talk
.y
, "→", style
);
312 this.add(this.text_menutitle
);
313 this.add(this.text_menuitem_Talk
);
314 this.add(this.text_menuitem_Leave
);
315 this.add(this.text_menuitem_Take
);
316 this.add(this.text_menucursor
);
318 this.last_menustep
= 0;
320 this.back_talk
= new Phaser
.Graphics(game
, 100, game
.height
- 120);
321 this.back_talk
.beginFill(0x000000);
322 this.back_talk
.drawRect(0, 0, game
.width
- 200, 105);
323 this.back_talk
.endFill();
324 this.back_talk
.lineStyle(3, Phaser
.Color
.YELLOW
, 1);
325 this.back_talk
.moveTo(10, 35);
326 this.back_talk
.lineTo(this.back_talk
.width
- 10, 35);
327 this.add(this.back_talk
);
328 style
= { align
: 'left', fill
: 'yellow', font
: 'Ubuntu Mono', fontSize
: 22, fontWeight
: 'bold' };
329 this.text_talktitle
= new Phaser
.Text(game
, this.back_talk
.x
, this.back_talk
.y
, null, style
);
330 this.text_talktitle
.anchor
.setTo(-0.2, -0.2);
331 this.add(this.text_talktitle
);
332 style
= { align
: 'left', fill
: 'white', font
: 'Ubuntu Mono', fontSize
: 18, fontWeight
: 'bold' };
333 this.text_talk
= new Phaser
.Text(game
, this.back_talk
.x
+ 35, this.back_talk
.y
+ 40, null, style
);
334 this.text_talk
.wordWrap
= true;
335 this.text_talk
.wordWrapWidth
= this.back_talk
.width
- 70;
336 this.text_talk
.lineSpacing
= -5;
337 this.add(this.text_talk
);
343 this.text_notice
.text
= text
;
344 this.back_notice
.visible
= true;
345 this.text_notice
.visible
= true;
349 this.back_notice
.visible
= false;
350 this.text_notice
.visible
= false;
357 this.text_menutitle
.text
= npc
.shortname
;
358 this.back_menu
.visible
= true;
359 this.text_menutitle
.visible
= true;
360 this.text_menuitem_Talk
.visible
= true;
361 this.text_menuitem_Leave
.visible
= true;
362 this.text_menuitem_Take
.visible
= true;
363 this.currentmenuitem
= MENUITEM_TALK
;
364 this.actualizeCursorPosition();
365 this.text_menucursor
.visible
= true;
366 this.last_menustep
= game
.time
.now
;
371 this.back_menu
.visible
= false;
372 this.text_menutitle
.visible
= false;
373 this.text_menuitem_Talk
.visible
= false;
374 this.text_menuitem_Leave
.visible
= false;
375 this.text_menuitem_Take
.visible
= false;
376 this.text_menucursor
.visible
= false;
382 this.dialogue
= dialogue
;
384 this.back_talk
.visible
= true;
385 this.text_talktitle
.visible
= true;
386 this.text_talk
.visible
= true;
391 this.dialogue
= null;
392 this.back_talk
.visible
= false;
393 this.text_talktitle
.visible
= false;
394 this.text_talk
.visible
= false;
398 console
.log(this.dialogue
);
399 console
.log(this.dialogue
.actual());
400 var actualdialogue
= this.dialogue
.actual();
401 if (actualdialogue
) {
402 this.text_talktitle
.text
= actualdialogue
.actor
.shortname
;
403 this.text_talk
.text
= actualdialogue
.text
;
404 this.dialogue
.advance();
410 this.last_talk
= game
.time
.now
;
413 actualizeCursorPosition() {
414 this.text_menucursor
.y
= this.text_menuitem_Talk
.y
+ (this.currentmenuitem
* GUI_MENUITEM_DISTANCE
);
418 if ((this.inMenu
) && (hasTimePassed(this.last_menustep
, WAIT_MENUSTEP
))) {
419 if (cursors
.up
.isDown
)
421 this.currentmenuitem
--;
422 if (this.currentmenuitem
< MENUITEM_TALK
) this.currentmenuitem
= MENUITEM_TAKE
;
423 this.actualizeCursorPosition();
425 else if (cursors
.down
.isDown
)
427 this.currentmenuitem
++;
428 if (this.currentmenuitem
> MENUITEM_TAKE
) this.currentmenuitem
= MENUITEM_TALK
;
429 this.actualizeCursorPosition();
431 if (game
.input
.keyboard
.isDown(Phaser
.Keyboard
.ENTER
)) {
432 logic
.callMenu(this.currentmenuitem
);
434 this.last_menustep
= game
.time
.now
;
436 if ((this.inTalk
) && (game
.input
.keyboard
.isDown(Phaser
.Keyboard
.ENTER
)) && (hasTimePassed(this.last_talk
, WAIT_TALK
))) {
445 this.player
= this.clara
= this.carlos
= this.saiki
= this.peter
= this.bianca
= null;
446 this.gameinterface
= new GameInterface(game
, game
.stage
);
447 //this.doors = game.add.group(game.world, "doors");
448 this.last_menuselect
= 0;
451 createObject(object
) {
452 switch (object
.type
) {
453 case 'spawnpoint': this.createCharacter(object
); break;
454 case 'door': this.createDoor(object
); break;
455 case '': console
.error("Object type is empty:", object
); break;
456 default: console
.error("Unknown object type:", object
);
460 createCharacter(object
) {
462 switch (object
.name
) {
464 newChar
= new Player(object
.x
, object
.y
, 'player');
465 this.player
= newChar
;
466 this.player
.enablePhysics();
467 game
.camera
.follow(this.player
);
470 newChar
= new NPC_Clara(object
.x
, object
.y
, 'clara', "Clara", "Clara Tnavelerri", 200);
471 this.clara
= newChar
;
474 console
.error("Unknown character:", object
);
476 newChar
.name
= object
.name
;
477 game
.add
.existing(newChar
);
478 console
.log(newChar
);
482 /* Calculate movement vector and correct position (32 = half tile width/height). */
484 switch (object.rotation) {
486 case 0: vector = new Phaser.Point( -1, 0); object.x += 32; object.y -= 32; break;
487 case 90: vector = new Phaser.Point( 0, -1); object.x += 32; object.y += 32; break;
488 case 180: vector = new Phaser.Point( 1, 0); object.x -= 32; object.y += 32; break;
489 case 270: vector = new Phaser.Point( 0, 1); object.x -= 32; object.y -= 32; break;
490 default: console.error("Invalid rotation:", object.rotation);
492 this.doors.add(new Door(object.x, object.y, object.name, object.rotation, vector, object.properties.longpanel));
496 console.log("Menu callback received:", menuitem);
497 this.last_menuselect = game.time.now;
498 this.gameinterface.leaveMenu();
501 this.player.interactablenpc.actionTalk();
504 this.player.interactablenpc.actionLeave();
505 this.player.unfreeze();
508 if (this.player.interactablenpc.actionTake())
509 this.player.unfreeze();
515 if (this.player.interactablenpc) {
516 if (this.player.interactablenpc.endTalk()) {
517 this.gameinterface.npcMenu(this.player.interactablenpc);
520 this.player.unfreeze();
521 this.last_menuselect = game.time.now;
527 this.doors.children.forEach(function(o) { if (o.name == doorname) o.open(); });
530 closeDoor(doorname) {
531 this.doors.children.forEach(function(o) { if (o.name == doorname) o.close(); });
535 if ((game.input.keyboard.isDown(Phaser.Keyboard.ENTER)) && (hasTimePassed(this.last_menuselect, WAIT_MENUSTEP))) {
536 if ((this.player.interactablenpc) && (this.player.interactablenpc.interactable) && (!this.gameinterface.inMenu) && (!this.gameinterface.inTalk)) {
537 console.log("Starting interaction with", this.player.interactablenpc.fullname);
538 this.player.freeze();
539 this.gameinterface.npcMenu(this.player.interactablenpc);
546 class GamePlay extends Phaser.State {
551 logic = new GameLogic();
552 game.world.updateOnlyExistingChildren = true;
553 //game.state.add('GameOver', GameOver, false);
559 game.load.image('player', 'john.png');
560 game.load.image('clara', 'clara.png');
561 game.load.image('saiki', 'saiki.png');
562 game.load.image('tileset', 'tileset.png');
563 game.load.image('objects', 'objects.png');
564 game.load.tilemap('gamemap', 'tilemap.json', null, Phaser.Tilemap.TILED_JSON);
570 game.world.setBounds(0, 0, 800, 600);
571 game.stage.backgroundColor = '#000000';
572 game.physics.startSystem(Phaser.Physics.ARCADE);
573 console.log("Debug enabled:", !game.debug.isDisabled);
575 var map = game.add.tilemap('gamemap');
576 map.addTilesetImage('tileset', 'tileset');
577 map.addTilesetImage('objects', 'objects');
578 var layer_floor = map.createLayer('floor');
579 layer_floor.resizeWorld();
580 this.layer_walls = map.createLayer('walls');
581 map.setCollisionBetween(1, 100, true, this.layer_walls);
582 this.layer_furniture = map.createLayer('furniture');
583 map.setCollisionBetween(1, 100, true, this.layer_furniture);
585 // FIXME: Don't create a group here like this, it's too ugly! Now I have it here due to the display order.
586 logic.doors = game.add.group(game.world, "doors");
588 map.objects['objects'].forEach(function(o) { logic.createObject(o); });
589 console.log(map.objects);
591 cursors = game.input.keyboard.createCursorKeys();
598 game.physics.arcade.collide(logic.player, this.layer_walls);
599 game.physics.arcade.collide(logic.player, this.layer_furniture);
600 game.physics.arcade.collide(logic.player, logic.doors);