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 Player
extends Phaser
.Sprite
{
44 super(game
, x
, y
, 'player');
45 this.y
-= this.height
;
46 this.shortname
= "John";
47 this.fullname
= "John Evals";
48 this.offerInteraction(null);
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;
60 if ((this.alive
) && (!this.freezed
))
62 if (cursors
.left
.isDown
)
64 this.body
.velocity
.x
= -PLAYER_SPEED
;
66 else if (cursors
.right
.isDown
)
68 this.body
.velocity
.x
= PLAYER_SPEED
;
70 if (cursors
.up
.isDown
)
72 this.body
.velocity
.y
= -PLAYER_SPEED
;
74 else if (cursors
.down
.isDown
)
76 this.body
.velocity
.y
= PLAYER_SPEED
;
82 this.body
.velocity
.x
= this.body
.velocity
.y
= 0;
94 offerInteraction(npc
) {
95 this.interactablenpc
= npc
;
99 this.loadTexture(npc
.key
);
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;
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;
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;
142 logic
.player
.offerInteraction(null);
143 logic
.player
.takeMe(this);
152 class NPC_Clara
extends GameNPC
{
154 switch (this.talkcount
) {
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..." } ] ));
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." } ] ));
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!" } ] ));
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." } ] ));
204 logic
.gameinterface
.talk(new Dialogue( [ { actor
: this, text
: "..." } ] ));
210 logic
.gameinterface
.dropNotice(this.shortname
+ ": Have a great day, John!");
214 return (this.talkcount
< 6);
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
);
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
);
255 this.last_menustep
= 0;
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
);
280 this.text_notice
.text
= text
;
281 this.back_notice
.visible
= true;
282 this.text_notice
.visible
= true;
286 this.back_notice
.visible
= false;
287 this.text_notice
.visible
= false;
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
;
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;
319 this.dialogue
= dialogue
;
321 this.back_talk
.visible
= true;
322 this.text_talktitle
.visible
= true;
323 this.text_talk
.visible
= true;
328 this.dialogue
= null;
329 this.back_talk
.visible
= false;
330 this.text_talktitle
.visible
= false;
331 this.text_talk
.visible
= false;
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();
347 this.last_talk
= game
.time
.now
;
350 actualizeCursorPosition() {
351 this.text_menucursor
.y
= this.text_menuitem_Talk
.y
+ (this.currentmenuitem
* GUI_MENUITEM_DISTANCE
);
355 if ((this.inMenu
) && (hasTimePassed(this.last_menustep
, WAIT_MENUSTEP
))) {
356 if (cursors
.up
.isDown
)
358 this.currentmenuitem
--;
359 if (this.currentmenuitem
< MENUITEM_TALK
) this.currentmenuitem
= MENUITEM_TAKE
;
360 this.actualizeCursorPosition();
362 else if (cursors
.down
.isDown
)
364 this.currentmenuitem
++;
365 if (this.currentmenuitem
> MENUITEM_TAKE
) this.currentmenuitem
= MENUITEM_TALK
;
366 this.actualizeCursorPosition();
368 if (game
.input
.keyboard
.isDown(Phaser
.Keyboard
.ENTER
)) {
369 logic
.callMenu(this.currentmenuitem
);
371 this.last_menustep
= game
.time
.now
;
373 if ((this.inTalk
) && (game
.input
.keyboard
.isDown(Phaser
.Keyboard
.ENTER
)) && (hasTimePassed(this.last_talk
, WAIT_TALK
))) {
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;
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
);
395 createCharacter(object
) {
397 switch (object
.name
) {
399 newChar
= new Player(object
.x
, object
.y
, 'player');
400 this.player
= newChar
;
401 this.player
.enablePhysics();
402 game
.camera
.follow(this.player
);
405 newChar
= new NPC_Clara(object
.x
, object
.y
, 'clara', "Clara", "Clara Tnavelerri", 200);
406 this.clara
= newChar
;
409 console
.error("Unknown character:", object
);
411 newChar
.name
= object
.name
;
412 game
.add
.existing(newChar
);
413 console
.log(newChar
);
417 console
.log("Menu callback received:", menuitem
);
418 this.last_menuselect
= game
.time
.now
;
419 this.gameinterface
.leaveMenu();
422 this.player
.interactablenpc
.actionTalk();
425 this.player
.interactablenpc
.actionLeave();
426 this.player
.unfreeze();
429 if (this.player
.interactablenpc
.actionTake())
430 this.player
.unfreeze();
436 if (this.player
.interactablenpc
) {
437 if (this.player
.interactablenpc
.endTalk()) {
438 this.gameinterface
.npcMenu(this.player
.interactablenpc
);
441 this.player
.unfreeze();
442 this.last_menuselect
= game
.time
.now
;
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
);
459 class GamePlay
extends Phaser
.State
{
464 logic
= new GameLogic();
465 game
.world
.updateOnlyExistingChildren
= true;
466 //game.state.add('GameOver', GameOver, false);
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
);
483 game
.world
.setBounds(0, 0, 800, 600);
484 game
.stage
.backgroundColor
= '#000000';
485 game
.physics
.startSystem(Phaser
.Physics
.ARCADE
);
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
);
499 cursors
= game
.input
.keyboard
.createCursorKeys();
506 game
.physics
.arcade
.collide(logic
.player
, this.layer_walls
);
507 game
.physics
.arcade
.collide(logic
.player
, this.layer_furniture
);