/
main.js
329 lines (312 loc) · 9.11 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Level name and day
var levels = [
{
level:'level1',
day:'Sunnudagr',
texts: [
'By Thor, what a headache!',
'I must have drunk too much mead last night,',
'but I cannot remember how I got here!',
'...',
'...wait, it\'s all coming back now...',
'(Retrace your trail of destruction to the front gate)'
]
},
{
level:'level2',
day:'Manadagr',
texts: [
'Manadagr: Birger, son of Hlodvir, is marrying a glorious wench!',
'We had too much to drink.'
]
},
{
level:'level3',
day:'Tysdagr',
texts: [
'Tysdagr: that troll Thorgeir won\'t let us pillage today!',
'So instead we had a drink. Maybe too much.'
]
},
{
level:'level4',
day:'Ooinsdagr',
texts: [
'Ooinsdagr: Njal quarreled with his wench again.',
'We cheered him up by getting drunk.'
]
},
{
level:'level5',
day:'Thorsdagr',
texts: [
'Thorsdagr: Ragnar is getting married to that sweet lass Skuld!',
'We celebrated by having drinks.'
]
},
{
level:'level6',
day:'Frjadagr',
texts: [
'Frjadagr: nothing to do but listen to Sigurd spin tales.',
'Eventually we decided to drink instead.'
]
},
{
level:'level7',
day:'Laugardagr',
texts: [
'Laugardagr: I keep breaking things so Ulf got me new stuff.',
'What a fine Viking! I had to thank him by buying drinks.'
]
}
];
var GameState = function(game){};
GameState.prototype.preload = function() {
};
GameState.prototype.create = function() {
this.game.stage.backgroundColor = 0x664a33;
this.sounds = {
step: this.game.add.audio("step"),
bump: this.game.add.audio("bump"),
breakSound: this.game.add.audio("break"),
pickup: this.game.add.audio("pickup"),
cat: this.game.add.audio("cat"),
glass: this.game.add.audio("glass"),
vomit: this.game.add.audio("vomit"),
smash: this.game.add.audio("smash"),
fanfare: this.game.add.audio("fanfare"),
tada: this.game.add.audio("tada")
};
this.groups = {
bg: this.game.add.group(),
sprites: this.game.add.group(),
dialogs: this.game.add.group()
};
this.levelIndex = 0;
this.map = new Map(this.game, this.groups.bg, levels[this.levelIndex].level);
this.player = new Player(this.game,
this.map.getBed(),
['hrrng', 'hic', 'groan']);
this.groups.sprites.add(this.player);
this.dialog = new Dialog(this.game,
SCREEN_WIDTH / 2, SCREEN_HEIGHT - 64,
levels[this.levelIndex].texts);
this.groups.dialogs.add(this.dialog);
this.instantReplay = this.game.add.text(48, 48, 'Instant Replay', {
font: "32px VT323", fill: "#ff6666", align: "left"
});
this.instantReplayTween = this.game.add.tween(this.instantReplay)
.to({alpha: 0.5}, 300, Phaser.Easing.Sinusoidal.InOut)
.to({alpha: 1}, 300, Phaser.Easing.Sinusoidal.InOut)
.loop();
this.instantReplayTween.start();
this.instantReplayTween.pause();
this.instantReplay.alpha = 0;
this.hintText = this.game.add.text(SCREEN_WIDTH - 190, 48, 'Press R to reset', {
font: "24px VT323", fill: "#66ff66", align: "right"
});
this.hintTextTween = this.game.add.tween(this.hintText)
.to({alpha: 0.5}, 300, Phaser.Easing.Sinusoidal.InOut)
.to({alpha: 1}, 300, Phaser.Easing.Sinusoidal.InOut)
.loop();
this.hintTextTween.start();
this.hintTextTween.pause();
this.hintText.alpha = 0;
this.hintDisplayCounter = 0;
this.shoveCounter = 0;
var registerKey = function(thegame, keycode, dir) {
var key = thegame.game.input.keyboard.addKey(keycode);
key.onDown.add(function(k) { thegame.move(dir); }, thegame);
};
registerKey(this, Phaser.Keyboard.UP, 'up');
registerKey(this, Phaser.Keyboard.DOWN, 'down');
registerKey(this, Phaser.Keyboard.LEFT, 'left');
registerKey(this, Phaser.Keyboard.RIGHT, 'right');
this.game.input.keyboard.addKey(Phaser.Keyboard.R).onDown.add(function(k) {
this.reset();
}, this);
this.moves = [];
this.movesIndex = -1;
this.allowSpecialMovement = false;
this.win = false;
};
GameState.prototype.stopReplay = function() {
this.instantReplayTween.pause();
this.instantReplay.alpha = 0;
// Show next level
if (this.levelIndex < levels.length) {
this.map.stop();
this.map = new Map(this.game, this.groups.bg, levels[this.levelIndex].level);
this.dialog.setTexts(levels[this.levelIndex].texts);
this.dialog.alpha = 1;
} else {
if (this.levelIndex >= levels.length) {
// Show end game screen
console.log('Winner');
this.win = true;
this.makeWinScreen();
return;
}
}
this.moves = [];
this.movesIndex = -1;
this.player.move(this.map.getBed());
this.player.strip();
};
GameState.prototype.update = function() {
// If we're showing instant replay
if (this.instantReplay.alpha > 0) {
// Check for end
if (this.movesIndex < 0) {
this.stopReplay();
return;
}
// Replay the moves in reverse order
if (this.instantReplayCounter > 15) {
this.allowSpecialMovement = true;
this.move(this.moves[this.movesIndex]);
this.allowSpecialMovement = false;
this.movesIndex--;
this.instantReplayCounter = 0;
}
this.instantReplayCounter++;
}
// Show hint text
if (this.hintText.alpha > 0) {
this.hintDisplayCounter++;
if (this.hintDisplayCounter > 300) {
this.hintTextTween.pause();
this.hintText.alpha = 0;
this.hintDisplayCounter = 0;
this.shoveCounter = 0;
}
}
};
GameState.prototype.move = function(dir) {
if (this.win) {
return;
}
// If there are dialog boxes alive, move them instead
if (this.dialog.alpha > 0) {
if (!this.dialog.next()) {
// Check if we haven't showed the instant replay yet
if (this.moves.length > 0) {
this.instantReplayTween.resume();
this.instantReplayCounter = 0;
this.dialog.alpha = 0;
return;
}
if (this.levelIndex >= levels.length) {
// Show end game screen
console.log('Winner');
this.win = true;
this.makeWinScreen();
return;
}
this.dialog.alpha = 0;
this.map.switchTiles();
}
return;
}
// Find new grid position to move to
var grid = {x:this.player.grid.x, y:this.player.grid.y};
addDir(grid, dir);
// Normal game movement
if (this.movesIndex < 0) {
// Check for out of bounds
if (grid.x < 0 || grid.y < 0 ||
grid.x >= SCREEN_WIDTH / TILE_SIZE || grid.y >= SCREEN_HEIGHT / TILE_SIZE) {
this.sounds.fanfare.play();
this.dialog.setTexts([getScoreText(this.map, levels[this.levelIndex].day)]);
this.dialog.alpha = 1;
this.levelIndex++;
this.movesIndex = this.moves.length - 1;
return;
}
// Check for wall collision
if (this.map.isWall(grid)) {
this.sounds.bump.play('', 0, 0.7);
this.moves.push(dir);
this.shoveCounter++;
if (this.shoveCounter > 5) {
this.hintTextTween.resume();
//console.log('show hint');
}
//console.log('shove ' + dir);
} else {
this.player.move(grid);
this.sounds.step.play('', 0, 0.7);
this.moves.push(dirReverse(dir));
//console.log('move ' + dir);
}
} else {
if (this.allowSpecialMovement) {
// Reverse game movement
if (this.map.isRealWall(grid)) {
this.sounds.bump.play('', 0, 0.7);
} else {
this.player.move(grid);
this.sounds.step.play('', 0, 0.7);
}
} else {
// Trying to move during instant replay
// Cancel replay and move on
this.stopReplay();
return;
}
}
// Destroy items
var indices = [null];
if (this.movesIndex < 0) {
indices = this.map.destroyAt(grid, dir);
} else {
// Restore items
indices = this.map.restoreAt(grid, dir);
}
if (indices[0] >= 0) {
if (indices[0] == 187) {
this.sounds.cat.play();
} else if ((indices[0] >= 178 && indices[0] <= 183) || indices[0] == 210) {
this.sounds.glass.play();
} else if (indices[0] >= 189 && indices[0] <= 194) {
this.sounds.smash.play();
}else {
this.sounds.breakSound.play();
}
} else if (indices[1] >= 0) {
if (indices[1] == 135) {
this.sounds.vomit.play();
} else {
this.sounds.pickup.play();
if (this.movesIndex < 0) {
this.player.clothe();
} else {
this.player.strip();
}
}
}
};
GameState.prototype.reset = function(k) {
this.moves = [];
this.map.reset();
this.movesIndex = -1;
this.player.move(this.map.getBed());
this.player.strip();
};
GameState.prototype.makeWinScreen = function() {
this.sounds.tada.play();
var win = this.game.add.sprite(64, 64, 'win');
win.width *= 2;
win.height *= 2;
var viking = this.game.add.sprite(96, 96, 'viking');
viking.width *= 2;
viking.height *= 2;
viking.animations.add('bob', [0, 1], 4, true);
viking.animations.play('bob');
var platino = this.game.add.sprite(SCREEN_WIDTH - 96 - 32, 96, 'platino');
platino.width *= 2;
platino.height *= 2;
platino.animations.add('bob', [0, 1], 4, true);
platino.animations.play('bob');
};