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
, vector
, longpanel
) {
44 super(game
, x
, y
, 64, 64, 'objects');
45 this.tilePosition
= new Phaser
.Point(-64, -64);
46 console
.log("New door:", this);
50 class Player
extends Phaser
.Sprite
{
52 super(game
, x
, y
, 'player');
53 this.y
-= this.height
;
54 this.shortname
= "John";
55 this.fullname
= "John Evals";
56 this.offerInteraction(null);
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;
68 if ((this.alive
) && (!this.freezed
))
70 if (cursors
.left
.isDown
)
72 this.body
.velocity
.x
= -PLAYER_SPEED
;
74 else if (cursors
.right
.isDown
)
76 this.body
.velocity
.x
= PLAYER_SPEED
;
78 if (cursors
.up
.isDown
)
80 this.body
.velocity
.y
= -PLAYER_SPEED
;
82 else if (cursors
.down
.isDown
)
84 this.body
.velocity
.y
= PLAYER_SPEED
;
90 this.body
.velocity
.x
= this.body
.velocity
.y
= 0;
102 offerInteraction(npc
) {
103 this.interactablenpc
= npc
;
107 this.loadTexture(npc
.key
);
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;
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;
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;
150 logic
.player
.offerInteraction(null);
151 logic
.player
.takeMe(this);
160 class NPC_Clara
extends GameNPC
{
162 switch (this.talkcount
) {
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..." } ] ));
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." } ] ));
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!" } ] ));
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." } ] ));
212 logic
.gameinterface
.talk(new Dialogue( [ { actor
: this, text
: "..." } ] ));
218 logic
.gameinterface
.dropNotice(this.shortname
+ ": Have a great day, John!");
222 return (this.talkcount
< 6);
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
);
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
);
263 this.last_menustep
= 0;
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
);
288 this.text_notice
.text
= text
;
289 this.back_notice
.visible
= true;
290 this.text_notice
.visible
= true;
294 this.back_notice
.visible
= false;
295 this.text_notice
.visible
= false;
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
;
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;
327 this.dialogue
= dialogue
;
329 this.back_talk
.visible
= true;
330 this.text_talktitle
.visible
= true;
331 this.text_talk
.visible
= true;
336 this.dialogue
= null;
337 this.back_talk
.visible
= false;
338 this.text_talktitle
.visible
= false;
339 this.text_talk
.visible
= false;
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();
355 this.last_talk
= game
.time
.now
;
358 actualizeCursorPosition() {
359 this.text_menucursor
.y
= this.text_menuitem_Talk
.y
+ (this.currentmenuitem
* GUI_MENUITEM_DISTANCE
);
363 if ((this.inMenu
) && (hasTimePassed(this.last_menustep
, WAIT_MENUSTEP
))) {
364 if (cursors
.up
.isDown
)
366 this.currentmenuitem
--;
367 if (this.currentmenuitem
< MENUITEM_TALK
) this.currentmenuitem
= MENUITEM_TAKE
;
368 this.actualizeCursorPosition();
370 else if (cursors
.down
.isDown
)
372 this.currentmenuitem
++;
373 if (this.currentmenuitem
> MENUITEM_TAKE
) this.currentmenuitem
= MENUITEM_TALK
;
374 this.actualizeCursorPosition();
376 if (game
.input
.keyboard
.isDown(Phaser
.Keyboard
.ENTER
)) {
377 logic
.callMenu(this.currentmenuitem
);
379 this.last_menustep
= game
.time
.now
;
381 if ((this.inTalk
) && (game
.input
.keyboard
.isDown(Phaser
.Keyboard
.ENTER
)) && (hasTimePassed(this.last_talk
, WAIT_TALK
))) {
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;
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
);
405 createCharacter(object
) {
407 switch (object
.name
) {
409 newChar
= new Player(object
.x
, object
.y
, 'player');
410 this.player
= newChar
;
411 this.player
.enablePhysics();
412 game
.camera
.follow(this.player
);
415 newChar
= new NPC_Clara(object
.x
, object
.y
, 'clara', "Clara", "Clara Tnavelerri", 200);
416 this.clara
= newChar
;
419 console
.error("Unknown character:", object
);
421 newChar
.name
= object
.name
;
422 game
.add
.existing(newChar
);
423 console
.log(newChar
);
428 switch (object
.rotation
) {
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
);
436 this.doors
.add(new Door(object
.x
, object
.y
, object
.name
, vector
, object
.properties
.longpanel
));
440 console
.log("Menu callback received:", menuitem
);
441 this.last_menuselect
= game
.time
.now
;
442 this.gameinterface
.leaveMenu();
445 this.player
.interactablenpc
.actionTalk();
448 this.player
.interactablenpc
.actionLeave();
449 this.player
.unfreeze();
452 if (this.player
.interactablenpc
.actionTake())
453 this.player
.unfreeze();
459 if (this.player
.interactablenpc
) {
460 if (this.player
.interactablenpc
.endTalk()) {
461 this.gameinterface
.npcMenu(this.player
.interactablenpc
);
464 this.player
.unfreeze();
465 this.last_menuselect
= game
.time
.now
;
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
);
482 class GamePlay
extends Phaser
.State
{
487 logic
= new GameLogic();
488 game
.world
.updateOnlyExistingChildren
= true;
489 //game.state.add('GameOver', GameOver, false);
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
);
506 game
.world
.setBounds(0, 0, 800, 600);
507 game
.stage
.backgroundColor
= '#000000';
508 game
.physics
.startSystem(Phaser
.Physics
.ARCADE
);
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
);
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");
523 map
.objects
['objects'].forEach(function(o
) { logic
.createObject(o
); });
524 console
.log(map
.objects
);
526 cursors
= game
.input
.keyboard
.createCursorKeys();
533 game
.physics
.arcade
.collide(logic
.player
, this.layer_walls
);
534 game
.physics
.arcade
.collide(logic
.player
, this.layer_furniture
);