1 window
.onload = function() {
3 var game
= new Phaser
.Game(800, 600, Phaser
.AUTO
, '', { preload
: preload
, create
: create
, update
: update
});
12 var interaction_handler
;
13 var actual_dialogue
= null;
21 var dialogue_guard1_girl
=
22 [ { actor
: 'player', text
: "Hello! May I enter this town?" },
23 { actor
: 'guard', text
: "Sorry, I don't know you.\nI can't let you in." } ];
24 var dialogue_guard1_girl_retry
=
25 [ { actor
: 'player', text
: "But I'm a goddess!" },
26 { actor
: 'guard', text
: "I'm a feminist.\nI don't put women on pedestals." } ];
27 var dialogue_guard1_goat
=
28 [ { actor
: 'guard', text
: "Hmm, it must be Coralie's lost goat.\nI'd better let it in." } ];
29 var dialogue_guard2_girl
=
30 [ { actor
: 'player', text
: "Hi there! I'm a goddess and can transform into a goat!" },
31 { actor
: 'guard', text
: "Hey, that's really cool!\nI'm Coralie." },
32 { actor
: 'player', text
: "Nice bird you have there.\nCan I have it?" },
33 { actor
: 'guard', text
: "Yeah, it's yours. Just take it." },
34 { actor
: 'guard', text
: "Don't mind that it took me days to catch it." },
35 { actor
: 'player', text
: "Observing this bird, I will be able to shapeshift into a bird too!" } ];
36 var dialogue_guard2_goat
=
37 [ { actor
: 'guard', text
: "This is not my goat. Strange to see random\ngoats wandering here while mine is still missing." } ];
38 var dialogue_guard3_girl
=
39 [ { actor
: 'player', text
: "Hi! I'd like to leave the town." },
40 { actor
: 'guard', text
: "Sorry, we have a report of a missing goat.\nIt may have been stolen. We can't let anyone out\nuntil we find the thief." },
41 { actor
: 'player', text
: "I just came here before the goat went missing. The other guard can support this." },
42 { actor
: 'player', text
: "And the goat may have just wandered away.\nYou know they tend to escape, they require lots of space." },
43 { actor
: 'guard', text
: "Maybe, but we can't be sure. Sorry." } ];
44 var dialogue_guard3_girl_retry
=
45 [ { actor
: 'player', text
: "But I'm a girl! I wouldn't steal anything!" },
46 { actor
: 'guard', text
: "I'm a feminist.\nI don't give advantages to people based on their genders." } ];
47 var dialogue_guard3_goat
=
48 [ { actor
: 'guard', text
: "Another goat is trying to escape...\nGood that I'm on guard!" } ];
49 var dialogue_guard3_bird
=
50 [ { actor
: 'guard', text
: "Hi birdie! Don't fly at me, please!" } ];
51 var dialogue_guard4_girl
=
52 [ { actor
: 'guard', text
: "You are at home!\nYou don't have to pretend anymore." } ];
53 var dialogue_guard4_goat
=
54 [ { actor
: 'guard', text
: "Welcome home!\nGlad you shapeshifted back to your true form!" },
55 { actor
: 'guard', text
: "Yes, your true form is goat.\nDidn't you suspect it all along?" } ];
56 var dialogue_guard4_goat_retry
=
57 [ { actor
: 'guard', text
: "(By the way, this is the end of the game.\nYou really can't do anything more.\nThanks for playing!)" } ];
60 window
.addEventListener("keydown", function(e
) {
61 // Prevent default browser action for arrows and spacebar
62 if([32, 37, 38, 39, 40].indexOf(e
.keyCode
) > -1) {
68 if (n
>= 0) { return 1 } else { return -1 };
73 game
.load
.image('ground', 'ground.svg');
74 game
.load
.image('player_girl', 'csaj.svg');
75 game
.load
.image('player_goat', 'kecskecsaj.svg');
76 game
.load
.image('player_bird', 'bird.svg');
77 game
.load
.image('guard', 'guard.svg');
78 game
.load
.image('birdcage', 'birdcage.svg');
84 game
.world
.setBounds(0, 0, 2800, 600);
85 game
.stage
.backgroundColor
= 0xffffff;
86 game
.physics
.startSystem(Phaser
.Physics
.ARCADE
);
88 platforms
= game
.add
.group();
89 platforms
.classType
= Phaser
.TileSprite
;
90 platforms
.enableBody
= true;
93 var ground
= game
.add
.tileSprite(0, game
.world
.height
- 80, game
.world
.width
, 200, 'ground');
94 platforms
.add(ground
);
95 ground
.body
.immovable
= true;
98 player
= game
.add
.sprite(64, game
.world
.height
- 200, 'player_girl');
99 player
.anchor
.setTo(.4,.5);
100 player
.scale
.x
= 0.5;
101 player
.scale
.y
= player
.scale
.x
;
102 game
.physics
.arcade
.enable(player
);
103 player
.body
.bounce
.y
= 0.2;
104 player
.body
.bounce
.x
= 0.2;
105 player
.body
.gravity
.y
= 300;
106 player
.body
.collideWorldBounds
= true;
108 player
.shape
= SHAPE_GIRL
;
109 player
.learnedBird
= false;
110 game
.camera
.follow(player
);
112 guards
= game
.add
.group();
113 guard1
= createGuard(600, game
.world
.height
- 200, 'guard',
114 { girl
: dialogue_guard1_girl
, goat
: dialogue_guard1_goat
});
115 guard2
= createGuard(1000, game
.world
.height
- 200, 'player_girl',
116 { girl
: dialogue_guard2_girl
, goat
: dialogue_guard2_goat
});
117 guard2
.scale
.x
= -guard2
.scale
.x
;
118 guard3
= createGuard(1400, game
.world
.height
- 200, 'guard',
119 { girl
: dialogue_guard3_girl
, goat
: dialogue_guard3_goat
, bird
: dialogue_guard3_bird
});
120 guard4
= createGuard(2400, game
.world
.height
- 200, 'player_goat',
121 { girl
: dialogue_guard4_girl
, goat
: dialogue_guard4_goat
, bird
: dialogue_guard4_girl
});
122 guard4
.scale
.x
= -guard4
.scale
.x
;
124 birdcage
= game
.add
.sprite(1100, game
.world
.height
- 300, 'birdcage');
125 birdcage
.scale
.x
= 0.5;
126 birdcage
.scale
.y
= birdcage
.scale
.x
;
128 cursors
= game
.input
.keyboard
.createCursorKeys();
130 keyboard_handler
= keyPress_default
;
131 interaction_handler
= interaction
;
133 helptext
= game
.add
.text(80, 30, "Press ARROWS to move.", { align
: 'left', fill
: '#000000', fontSize
: 14 });
134 helptext
.font
= "Ubuntu Mono";
140 game
.physics
.arcade
.collide(player
, platforms
);
141 game
.physics
.arcade
.collide(guards
, platforms
);
142 game
.physics
.arcade
.collide(player
, guards
, interaction_handler
);
146 if ((game
.time
.now
- player
.pushed
) > 1000)
151 player
.body
.velocity
.x
= 0;
154 if (keyboard_handler
) keyboard_handler();
158 function keyPress_default() {
160 if (cursors
.left
.isDown
)
162 player
.body
.velocity
.x
= -150;
163 player
.scale
.x
= - Math
.abs(player
.scale
.x
);
165 else if (cursors
.right
.isDown
)
167 player
.body
.velocity
.x
= 150;
168 player
.scale
.x
= Math
.abs(player
.scale
.x
);
170 else if (game
.input
.keyboard
.isDown(Phaser
.Keyboard
.SPACEBAR
) && ((game
.time
.now
- lastkeytime
) > WAIT_KEY
))
172 if (player
.shape
== SHAPE_GIRL
)
174 player
.shape
= SHAPE_GOAT
;
175 player
.loadTexture('player_goat');
177 else if ((player
.shape
== SHAPE_GOAT
) && (player
.learnedBird
))
179 player
.shape
= SHAPE_BIRD
;
180 player
.loadTexture('player_bird');
184 player
.shape
= SHAPE_GIRL
;
185 player
.loadTexture('player_girl');
187 lastkeytime
= game
.time
.now
;
190 if ((cursors
.up
.isDown
) && (player
.shape
== SHAPE_BIRD
))
192 player
.body
.velocity
.y
= -200;
197 function keyPress_indialogue() {
199 if (game
.input
.keyboard
.isDown(Phaser
.Keyboard
.SPACEBAR
) && ((game
.time
.now
- lastkeytime
) > WAIT_KEY
))
201 var dialogue
= actual_dialogue
.dialogue
;
202 if (dialogue
[actual_dialogue
.state
+ 1])
204 var nextState
= ++actual_dialogue
.state
;
206 if (dialogue
[nextState
].actor
== 'player') actor
= actual_dialogue
.player
;
207 else if (dialogue
[nextState
].actor
== 'guard') actor
= actual_dialogue
.guard
;
209 actual_dialogue
.lastText
.kill();
210 actual_dialogue
.lastText
= putText(actor
, dialogue
[nextState
].text
);
211 lastkeytime
= game
.time
.now
;
216 lastkeytime
= game
.time
.now
;
222 function createGuard(x
, y
, sprite
, dialogues
) {
224 var guard
= game
.add
.sprite(x
, y
, sprite
);
225 guard
.anchor
.setTo(.5,.5);
227 guard
.scale
.y
= guard
.scale
.x
;
228 game
.physics
.arcade
.enable(guard
);
229 guard
.body
.bounce
.y
= 0.2;
230 guard
.body
.bounce
.x
= 0.2;
231 guard
.body
.gravity
.y
= 300;
232 guard
.body
.collideWorldBounds
= true;
233 guard
.dialogues
= dialogues
;
239 function putText(entity
, text
) {
240 var textObject
= game
.add
.text(entity
.x
, entity
.y
- (entity
.height
/ 2) - 30, text
, { align
: 'center', fontSize
: 14 });
241 textObject
.font
= 'Ubuntu Mono';
242 textObject
.anchor
.setTo(.5,.5);
246 function interaction(player
, guard
) {
248 console
.log('Interaction with ' + guard
.key
);
249 player
.body
.velocity
.x
= -100;
250 player
.pushed
= game
.time
.now
;
251 guard
.body
.velocity
.x
= 0;
253 if (player
.shape
== SHAPE_GIRL
)
255 if ((guard
== guard1
) && (guard
.dialogues
.girl
== dialogue_guard1_girl
))
256 helptext
.text
= helptext
.text
+ "\nPress SPACE to progress dialogue.";
257 enterDialogue(player
, guard
, guard
.dialogues
.girl
);
259 else if (player
.shape
== SHAPE_GOAT
)
261 enterDialogue(player
, guard
, guard
.dialogues
.goat
);
263 else if (player
.shape
== SHAPE_BIRD
)
265 enterDialogue(player
, guard
, guard
.dialogues
.bird
);
270 function enterDialogue(player
, guard
, dialogue
) {
273 if (dialogue
[0].actor
== 'player') actor
= player
;
274 else if (dialogue
[0].actor
== 'guard') actor
= guard
;
276 var lastText
= putText(actor
, dialogue
[0].text
);
277 actual_dialogue
= { player
: player
, guard
: guard
, dialogue
: dialogue
, state
: 0 , lastText
: lastText
};
278 keyboard_handler
= keyPress_indialogue
;
279 interaction_handler
= null;
283 function leaveDialogue() {
285 actual_dialogue
.lastText
.kill();
286 if (actual_dialogue
.dialogue
== dialogue_guard1_girl
)
288 helptext
.text
= helptext
.text
+ "\nPress SPACE to shapeshift.";
289 guard1
.dialogues
.girl
= dialogue_guard1_girl_retry
;
291 else if (actual_dialogue
.dialogue
== dialogue_guard1_goat
)
295 else if (actual_dialogue
.dialogue
== dialogue_guard2_girl
)
299 player
.learnedBird
= true;
301 else if (actual_dialogue
.dialogue
== dialogue_guard3_girl
)
303 guard3
.dialogues
.girl
= dialogue_guard3_girl_retry
;
305 else if (actual_dialogue
.dialogue
== dialogue_guard4_goat
)
307 guard4
.dialogues
.goat
= dialogue_guard4_goat_retry
;
308 guard4
.dialogues
.girl
= dialogue_guard4_goat_retry
;
309 guard4
.dialogues
.bird
= dialogue_guard4_goat_retry
;
312 actual_dialogue
= null;
313 keyboard_handler
= keyPress_default
;
314 interaction_handler
= interaction
;