d960d62ad4066ca11756ea3b8a3bf251b63b65da
[shapeshift.git] / shapeshift.js
1 window.onload = function() {
2
3 var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });
4 var ground;
5 var platforms;
6 var guards;
7 var guard1, guard2;
8 var birdcage;
9 var helptext;
10 var cursors;
11 var lastkeytime = 0;
12 var keyboard_handler;
13 var interaction_handler;
14 var actual_dialogue = null;
15
16 var WAIT_KEY = 500;
17
18 var SHAPE_GIRL = 0;
19 var SHAPE_GOAT = 1;
20 var SHAPE_BIRD = 2;
21
22 var dialogue_guard1_girl =
23 [ { actor: 'player', text: "Hello! May I enter this town?" },
24 { actor: 'guard', text: "Sorry, I don't know you.\nI can't let you in." } ];
25 var dialogue_guard1_girl_retry =
26 [ { actor: 'player', text: "But I'm a goddess!" },
27 { actor: 'guard', text: "I'm a feminist.\nI don't put women on pedestals." } ];
28 var dialogue_guard1_goat =
29 [ { actor: 'guard', text: "Hmm, it must be Coralie's lost goat.\nI'd better let it in." } ];
30 var dialogue_guard2_girl =
31 [ { actor: 'player', text: "Hi there! I'm a goddess and can transform into a goat!" },
32 { actor: 'guard', text: "Hey, that's really cool!\nI'm Coralie." },
33 { actor: 'player', text: "Nice bird you have there.\nCan I have it?" },
34 { actor: 'guard', text: "Yeah, it's yours. Just take it." },
35 { actor: 'guard', text: "Don't mind that it took me days to catch it." },
36 { actor: 'player', text: "Observing this bird, I will be able to shapeshift into a bird too!" } ];
37 var dialogue_guard2_goat =
38 [ { actor: 'guard', text: "This is not my goat. Strange to see random\ngoats wandering here while mine is still missing." } ];
39 var dialogue_guard3_girl =
40 [ { actor: 'player', text: "Hi! I'd like to leave the town." },
41 { 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." },
42 { actor: 'player', text: "I just came here before the goat went missing. The other guard can support this." },
43 { actor: 'player', text: "And the goat may have just wandered away.\nYou know they tend to escape, they require lots of space." },
44 { actor: 'guard', text: "Maybe, but we can't be sure. Sorry." } ];
45 var dialogue_guard3_girl_retry =
46 [ { actor: 'player', text: "But I'm a girl! I wouldn't steal anything!" },
47 { actor: 'guard', text: "I'm a feminist.\nI don't give advantages to people based on their genders." } ];
48 var dialogue_guard3_goat =
49 [ { actor: 'guard', text: "Another goat is trying to escape...\nGood that I'm on guard!" } ];
50 var dialogue_guard3_bird =
51 [ { actor: 'guard', text: "Hi birdie! Don't fly at me, please!" } ];
52 var dialogue_guard4_girl =
53 [ { actor: 'guard', text: "You are at home!\nYou don't have to pretend anymore." } ];
54 var dialogue_guard4_goat =
55 [ { actor: 'guard', text: "Welcome home!\nGlad you shapeshifted back to your true form!" },
56 { actor: 'guard', text: "Yes, your true form is goat.\nDidn't you suspect it all along?" } ];
57 var dialogue_guard4_goat_retry =
58 [ { actor: 'guard', text: "(By the way, this is the end of the game.\nYou really can't do anything more.\nThanks for playing!)" } ];
59
60
61 window.addEventListener("keydown", function(e) {
62 // Prevent default browser action for arrows and spacebar
63 if([32, 37, 38, 39, 40].indexOf(e.keyCode) > -1) {
64 e.preventDefault();
65 }
66 }, false);
67
68 function sign(n) {
69 if (n >= 0) { return 1 } else { return -1 };
70 }
71
72 function preload () {
73
74 game.load.image('ground', 'ground.svg');
75 game.load.image('player_girl', 'csaj.svg');
76 game.load.image('player_goat', 'kecskecsaj.svg');
77 game.load.image('player_bird', 'bird.svg');
78 game.load.image('guard', 'guard.svg');
79 game.load.image('birdcage', 'birdcage.svg');
80
81 }
82
83 function create () {
84
85 game.world.setBounds(0, 0, 2800, 600);
86 game.stage.backgroundColor = 0xffffff;
87 game.physics.startSystem(Phaser.Physics.ARCADE);
88
89 platforms = game.add.group();
90 platforms.classType = Phaser.TileSprite;
91 platforms.enableBody = true;
92
93
94 ground = game.add.tileSprite(0, game.world.height - 80, game.world.width, 200, 'ground');
95 platforms.add(ground);
96 ground.body.immovable = true;
97
98
99 player = game.add.sprite(64, game.world.height - 200, 'player_girl');
100 player.anchor.setTo(.4,.5);
101 player.scale.x = 0.5;
102 player.scale.y = player.scale.x;
103 game.physics.arcade.enable(player);
104 player.body.bounce.y = 0.2;
105 player.body.bounce.x = 0.2;
106 player.body.gravity.y = 300;
107 player.body.collideWorldBounds = true;
108 player.pushed = 0;
109 player.shape = SHAPE_GIRL;
110 player.learnedBird = false;
111 game.camera.follow(player);
112
113 guards = game.add.group();
114 guard1 = createGuard(600, game.world.height - 200, 'guard',
115 { girl: dialogue_guard1_girl, goat: dialogue_guard1_goat });
116 guard2 = createGuard(1000, game.world.height - 200, 'player_girl',
117 { girl: dialogue_guard2_girl, goat: dialogue_guard2_goat });
118 guard2.scale.x = -guard2.scale.x;
119 guard3 = createGuard(1400, game.world.height - 200, 'guard',
120 { girl: dialogue_guard3_girl, goat: dialogue_guard3_goat, bird: dialogue_guard3_bird });
121 guard4 = createGuard(2400, game.world.height - 200, 'player_goat',
122 { girl: dialogue_guard4_girl, goat: dialogue_guard4_goat, bird: dialogue_guard4_girl });
123 guard4.scale.x = -guard4.scale.x;
124
125 birdcage = game.add.sprite(1100, game.world.height - 300, 'birdcage');
126 birdcage.scale.x = 0.5;
127 birdcage.scale.y = birdcage.scale.x;
128
129 cursors = game.input.keyboard.createCursorKeys();
130
131 keyboard_handler = keyPress_default;
132 interaction_handler = interaction;
133
134 helptext = game.add.text(80, 30, "Press ARROWS to move.", { align: 'left', fill: '#000000', fontSize: 14 });
135 helptext.font = "Ubuntu Mono";
136
137 }
138
139 function update () {
140
141 game.physics.arcade.collide(player, platforms);
142 game.physics.arcade.collide(guards, platforms);
143 game.physics.arcade.collide(player, guards, interaction_handler);
144
145 if (player.pushed)
146 {
147 if ((game.time.now - player.pushed) > 1000)
148 player.pushed = 0;
149 }
150 else
151 {
152 player.body.velocity.x = 0;
153 }
154
155 if (keyboard_handler) keyboard_handler();
156
157 }
158
159 function keyPress_default() {
160
161 if (cursors.left.isDown)
162 {
163 player.body.velocity.x = -150;
164 player.scale.x = - Math.abs(player.scale.x);
165 }
166 else if (cursors.right.isDown)
167 {
168 player.body.velocity.x = 150;
169 player.scale.x = Math.abs(player.scale.x);
170 }
171 else if (game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR) && ((game.time.now - lastkeytime) > WAIT_KEY))
172 {
173 if (player.shape == SHAPE_GIRL)
174 {
175 player.shape = SHAPE_GOAT;
176 player.loadTexture('player_goat');
177 }
178 else if ((player.shape == SHAPE_GOAT) && (player.learnedBird))
179 {
180 player.shape = SHAPE_BIRD;
181 player.loadTexture('player_bird');
182 }
183 else
184 {
185 player.shape = SHAPE_GIRL;
186 player.loadTexture('player_girl');
187 }
188
189 player.body.setSize(player.texture.width, player.texture.height);
190 if ((ground.y - player.y) < player.body.halfHeight)
191 player.y = ground.y - player.body.halfHeight;
192
193 lastkeytime = game.time.now;
194 }
195
196 if ((cursors.up.isDown) && (player.shape == SHAPE_BIRD))
197 {
198 player.body.velocity.y = -200;
199 }
200
201 }
202
203 function keyPress_indialogue() {
204
205 if (game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR) && ((game.time.now - lastkeytime) > WAIT_KEY))
206 {
207 var dialogue = actual_dialogue.dialogue;
208 if (dialogue[actual_dialogue.state + 1])
209 {
210 var nextState = ++actual_dialogue.state;
211 var actor = null;
212 if (dialogue[nextState].actor == 'player') actor = actual_dialogue.player;
213 else if (dialogue[nextState].actor == 'guard') actor = actual_dialogue.guard;
214
215 actual_dialogue.lastText.kill();
216 actual_dialogue.lastText = putText(actor, dialogue[nextState].text);
217 lastkeytime = game.time.now;
218 }
219 else
220 {
221 leaveDialogue();
222 lastkeytime = game.time.now;
223 }
224 }
225
226 }
227
228 function createGuard(x, y, sprite, dialogues) {
229
230 var guard = game.add.sprite(x, y, sprite);
231 guard.anchor.setTo(.5,.5);
232 guard.scale.x = 0.5;
233 guard.scale.y = guard.scale.x;
234 game.physics.arcade.enable(guard);
235 guard.body.bounce.y = 0.2;
236 guard.body.bounce.x = 0.2;
237 guard.body.gravity.y = 300;
238 guard.body.collideWorldBounds = true;
239 guard.dialogues = dialogues;
240 guards.add(guard);
241 return guard;
242
243 }
244
245 function putText(entity, text) {
246 var textObject = game.add.text(entity.x, entity.y - (entity.height / 2) - 30, text, { align: 'center', fontSize: 14 });
247 textObject.font = 'Ubuntu Mono';
248 textObject.anchor.setTo(.5,.5);
249 return textObject;
250 }
251
252 function interaction(player, guard) {
253
254 console.log('Interaction with ' + guard.key);
255 player.body.velocity.x = -100;
256 player.pushed = game.time.now;
257 guard.body.velocity.x = 0;
258
259 if (player.shape == SHAPE_GIRL)
260 {
261 if ((guard == guard1) && (guard.dialogues.girl == dialogue_guard1_girl))
262 helptext.text = helptext.text + "\nPress SPACE to progress dialogue.";
263 enterDialogue(player, guard, guard.dialogues.girl);
264 }
265 else if (player.shape == SHAPE_GOAT)
266 {
267 enterDialogue(player, guard, guard.dialogues.goat);
268 }
269 else if (player.shape == SHAPE_BIRD)
270 {
271 enterDialogue(player, guard, guard.dialogues.bird);
272 }
273
274 }
275
276 function enterDialogue(player, guard, dialogue) {
277
278 var actor = null;
279 if (dialogue[0].actor == 'player') actor = player;
280 else if (dialogue[0].actor == 'guard') actor = guard;
281
282 var lastText = putText(actor, dialogue[0].text);
283 actual_dialogue = { player: player, guard: guard, dialogue: dialogue, state: 0 , lastText: lastText };
284 keyboard_handler = keyPress_indialogue;
285 interaction_handler = null;
286
287 }
288
289 function leaveDialogue() {
290
291 actual_dialogue.lastText.kill();
292 if (actual_dialogue.dialogue == dialogue_guard1_girl)
293 {
294 helptext.text = helptext.text + "\nPress SPACE to shapeshift.";
295 guard1.dialogues.girl = dialogue_guard1_girl_retry;
296 }
297 else if (actual_dialogue.dialogue == dialogue_guard1_goat)
298 {
299 guard1.kill();
300 }
301 else if (actual_dialogue.dialogue == dialogue_guard2_girl)
302 {
303 guard2.kill();
304 birdcage.kill();
305 player.learnedBird = true;
306 }
307 else if (actual_dialogue.dialogue == dialogue_guard3_girl)
308 {
309 guard3.dialogues.girl = dialogue_guard3_girl_retry;
310 }
311 else if (actual_dialogue.dialogue == dialogue_guard4_goat)
312 {
313 guard4.dialogues.goat = dialogue_guard4_goat_retry;
314 guard4.dialogues.girl = dialogue_guard4_goat_retry;
315 guard4.dialogues.bird = dialogue_guard4_goat_retry;
316 }
317
318 actual_dialogue = null;
319 keyboard_handler = keyPress_default;
320 interaction_handler = interaction;
321
322 }
323
324 };