ed05a37577947edb0c4405782e0e28c27e2f9077
[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 platforms;
5 var guards;
6 var guard1, guard2;
7 var birdcage;
8 var helptext;
9 var cursors;
10 var lastkeytime = 0;
11 var keyboard_handler;
12 var interaction_handler;
13 var actual_dialogue = null;
14
15 var WAIT_KEY = 500;
16
17 var SHAPE_GIRL = 0;
18 var SHAPE_GOAT = 1;
19 var SHAPE_BIRD = 2;
20
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!)" } ];
58
59
60 function sign(n) {
61 if (n >= 0) { return 1 } else { return -1 };
62 }
63
64 function preload () {
65
66 game.load.image('ground', 'ground.svg');
67 game.load.image('player_girl', 'csaj.svg');
68 game.load.image('player_goat', 'kecskecsaj.svg');
69 game.load.image('player_bird', 'bird.svg');
70 game.load.image('guard', 'guard.svg');
71 game.load.image('birdcage', 'birdcage.svg');
72
73 }
74
75 function create () {
76
77 game.world.setBounds(0, 0, 2800, 600);
78 game.stage.backgroundColor = 0xffffff;
79 game.physics.startSystem(Phaser.Physics.ARCADE);
80
81 platforms = game.add.group();
82 platforms.classType = Phaser.TileSprite;
83 platforms.enableBody = true;
84
85
86 var ground = game.add.tileSprite(0, game.world.height - 80, game.world.width, 200, 'ground');
87 platforms.add(ground);
88 ground.body.immovable = true;
89
90
91 player = game.add.sprite(64, game.world.height - 200, 'player_girl');
92 player.anchor.setTo(.4,.5);
93 player.scale.x = 0.5;
94 player.scale.y = player.scale.x;
95 game.physics.arcade.enable(player);
96 player.body.bounce.y = 0.2;
97 player.body.bounce.x = 0.2;
98 player.body.gravity.y = 300;
99 player.body.collideWorldBounds = true;
100 player.pushed = 0;
101 player.shape = SHAPE_GIRL;
102 player.learnedBird = false;
103 game.camera.follow(player);
104
105 guards = game.add.group();
106 guard1 = createGuard(600, game.world.height - 200, 'guard',
107 { girl: dialogue_guard1_girl, goat: dialogue_guard1_goat });
108 guard2 = createGuard(1000, game.world.height - 200, 'player_girl',
109 { girl: dialogue_guard2_girl, goat: dialogue_guard2_goat });
110 guard2.scale.x = -guard2.scale.x;
111 guard3 = createGuard(1400, game.world.height - 200, 'guard',
112 { girl: dialogue_guard3_girl, goat: dialogue_guard3_goat, bird: dialogue_guard3_bird });
113 guard4 = createGuard(2400, game.world.height - 200, 'player_goat',
114 { girl: dialogue_guard4_girl, goat: dialogue_guard4_goat, bird: dialogue_guard4_girl });
115 guard4.scale.x = -guard4.scale.x;
116
117 birdcage = game.add.sprite(1100, game.world.height - 300, 'birdcage');
118 birdcage.scale.x = 0.5;
119 birdcage.scale.y = birdcage.scale.x;
120
121 cursors = game.input.keyboard.createCursorKeys();
122
123 keyboard_handler = keyPress_default;
124 interaction_handler = interaction;
125
126 helptext = game.add.text(80, 30, "Press ARROWS to move.", { align: 'left', fill: '#000000', fontSize: 14 });
127 helptext.font = "Ubuntu Mono";
128
129 }
130
131 function update () {
132
133 game.physics.arcade.collide(player, platforms);
134 game.physics.arcade.collide(guards, platforms);
135 game.physics.arcade.collide(player, guards, interaction_handler);
136
137 if (player.pushed)
138 {
139 if ((game.time.now - player.pushed) > 1000)
140 player.pushed = 0;
141 }
142 else
143 {
144 player.body.velocity.x = 0;
145 }
146
147 if (keyboard_handler) keyboard_handler();
148
149 }
150
151 function keyPress_default() {
152
153 if (cursors.left.isDown)
154 {
155 player.body.velocity.x = -150;
156 player.scale.x = - Math.abs(player.scale.x);
157 }
158 else if (cursors.right.isDown)
159 {
160 player.body.velocity.x = 150;
161 player.scale.x = Math.abs(player.scale.x);
162 }
163 else if (game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR) && ((game.time.now - lastkeytime) > WAIT_KEY))
164 {
165 if (player.shape == SHAPE_GIRL)
166 {
167 player.shape = SHAPE_GOAT;
168 player.loadTexture('player_goat');
169 }
170 else if ((player.shape == SHAPE_GOAT) && (player.learnedBird))
171 {
172 player.shape = SHAPE_BIRD;
173 player.loadTexture('player_bird');
174 }
175 else
176 {
177 player.shape = SHAPE_GIRL;
178 player.loadTexture('player_girl');
179 }
180 lastkeytime = game.time.now;
181 }
182
183 if ((cursors.up.isDown) && (player.shape == SHAPE_BIRD))
184 {
185 player.body.velocity.y = -200;
186 }
187
188 }
189
190 function keyPress_indialogue() {
191
192 if (game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR) && ((game.time.now - lastkeytime) > WAIT_KEY))
193 {
194 var dialogue = actual_dialogue.dialogue;
195 if (dialogue[actual_dialogue.state + 1])
196 {
197 var nextState = ++actual_dialogue.state;
198 var actor = null;
199 if (dialogue[nextState].actor == 'player') actor = actual_dialogue.player;
200 else if (dialogue[nextState].actor == 'guard') actor = actual_dialogue.guard;
201
202 actual_dialogue.lastText.kill();
203 actual_dialogue.lastText = putText(actor, dialogue[nextState].text);
204 lastkeytime = game.time.now;
205 }
206 else
207 {
208 leaveDialogue();
209 lastkeytime = game.time.now;
210 }
211 }
212
213 }
214
215 function createGuard(x, y, sprite, dialogues) {
216
217 var guard = game.add.sprite(x, y, sprite);
218 guard.anchor.setTo(.5,.5);
219 guard.scale.x = 0.5;
220 guard.scale.y = guard.scale.x;
221 game.physics.arcade.enable(guard);
222 guard.body.bounce.y = 0.2;
223 guard.body.bounce.x = 0.2;
224 guard.body.gravity.y = 300;
225 guard.body.collideWorldBounds = true;
226 guard.dialogues = dialogues;
227 guards.add(guard);
228 return guard;
229
230 }
231
232 function putText(entity, text, color = '#000000') {
233 var textObject = game.add.text(entity.x, entity.y - (entity.height / 2) - 30, text, { align: 'center', fill: color, fontSize: 14 });
234 textObject.font = 'Ubuntu Mono';
235 textObject.anchor.setTo(.5,.5);
236 return textObject;
237 }
238
239 function interaction(player, guard) {
240
241 console.log('Interaction with ' + guard.key);
242 player.body.velocity.x = -100;
243 player.pushed = game.time.now;
244 guard.body.velocity.x = 0;
245
246 if (player.shape == SHAPE_GIRL)
247 {
248 if ((guard == guard1) && (guard.dialogues.girl == dialogue_guard1_girl))
249 helptext.text = helptext.text + "\nPress SPACE to progress dialogue.";
250 enterDialogue(player, guard, guard.dialogues.girl);
251 }
252 else if (player.shape == SHAPE_GOAT)
253 {
254 enterDialogue(player, guard, guard.dialogues.goat);
255 }
256 else if (player.shape == SHAPE_BIRD)
257 {
258 enterDialogue(player, guard, guard.dialogues.bird);
259 }
260
261 }
262
263 function enterDialogue(player, guard, dialogue) {
264
265 var actor = null;
266 if (dialogue[0].actor == 'player') actor = player;
267 else if (dialogue[0].actor == 'guard') actor = guard;
268
269 var lastText = putText(actor, dialogue[0].text);
270 actual_dialogue = { player: player, guard: guard, dialogue: dialogue, state: 0 , lastText: lastText };
271 keyboard_handler = keyPress_indialogue;
272 interaction_handler = null;
273
274 }
275
276 function leaveDialogue() {
277
278 actual_dialogue.lastText.kill();
279 if (actual_dialogue.dialogue == dialogue_guard1_girl)
280 {
281 helptext.text = helptext.text + "\nPress SPACE to shapeshift.";
282 guard1.dialogues.girl = dialogue_guard1_girl_retry;
283 }
284 else if (actual_dialogue.dialogue == dialogue_guard1_goat)
285 {
286 guard1.kill();
287 }
288 else if (actual_dialogue.dialogue == dialogue_guard2_girl)
289 {
290 guard2.kill();
291 birdcage.kill();
292 player.learnedBird = true;
293 }
294 else if (actual_dialogue.dialogue == dialogue_guard3_girl)
295 {
296 guard3.dialogues.girl = dialogue_guard3_girl_retry;
297 }
298 else if (actual_dialogue.dialogue == dialogue_guard4_goat)
299 {
300 guard4.dialogues.goat = dialogue_guard4_goat_retry;
301 guard4.dialogues.girl = dialogue_guard4_goat_retry;
302 guard4.dialogues.bird = dialogue_guard4_goat_retry;
303 }
304
305 actual_dialogue = null;
306 keyboard_handler = keyPress_default;
307 interaction_handler = interaction;
308
309 }
310
311 };