-
Notifications
You must be signed in to change notification settings - Fork 707
/
game.js
222 lines (182 loc) · 5.86 KB
/
game.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
function Game(debugMode) {
var dimensions = {
width: 50,
height: 25
};
this.dimensions = dimensions;
_currentCode = '';
_globalInventory = [];
_commands = (commands = localStorage.getItem('helpCommands')) ? commands.split(';') : [];
this.levelFileNames = [
'01_cellBlockA.jsx',
'02_theLongWayOut.jsx',
'03_validationEngaged.jsx',
'04_multiplicity.jsx',
'05_minesweeper.jsx',
'06_drones101.jsx',
'07_colors.jsx',
'08_intoTheWoods.jsx',
'09_fordingTheRiver.jsx',
'10_ambush.jsx',
'11_robot.jsx',
'12_robotNav.jsx',
'13_robotMaze.jsx',
//'14_crispsContest.jsx',
'15_exceptionalCrossing.jsx',
'16_pointers.jsx',
'17_superDrEvalBros.jsx',
//'18_domManipulation.jsx',
//'19_bossFight.jsx',
//'20_ULTIMA_BOSS_FIGHT.jsx',
'99_credits.jsx'
];
this.currentLevel = 1;
this.levelReached = localStorage.getItem('levelReached') || 1;
this.displayedChapters = [];
this.addToGlobalInventory = function (item) { _globalInventory.push(item); }
this.checkGlobalInventory = function (item) { return _globalInventory.indexOf(item) > -1; }
this.getHelpCommands = function () { return _commands; };
this.init = function () {
// Initialize sound
this.sound = new Sound();
// Initialize map display
this.display = ROT.Display.create(this, {
width: dimensions.width,
height: dimensions.height,
fontSize: 20
});
this.display.setupEventHandlers();
display = this.display;
$('#screen').append(this.display.getContainer());
$('#drawingCanvas').click(function () {
display.focus();
});
// Initialize output display
this.output = ROT.Display.create(this, {
width: dimensions.width * 1.33,
height: 2,
fontSize: 15
});
$('#output').append(this.output.getContainer());
// Initialize map and editor
this.editor = new CodeEditor("editor", 600, 500);
this.map = new Map(this.display, this);
// Enable controls
this.enableShortcutKeys();
this.enableButtons();
// Enable debug features
if (debugMode) {
this.levelReached = 999; // make all levels accessible
_commands = Object.keys(this.reference); // display all help
this.sound.toggleSound(); // mute sound by default in debug mode
}
this.intro();
};
this.intro = function () {
this.display.focus();
this.display.playIntro(this.map);
};
this.start = function () {
this.getLevel(1);
};
this.moveToNextLevel = function () {
var game = this;
game.currentLevel++;
game.sound.playSound('complete');
//we disable moving so the player can't move during the fadeout
game.map.getPlayer().canMove = false;
game.getLevel(game.currentLevel);
};
this.jumpToNthLevel = function (levelNum) {
var game = this;
game.currentLevel = levelNum;
// Give the player all necessary objects
if (levelNum > 1) {
game.addToGlobalInventory('computer');
$('#editorPane').fadeIn();
game.editor.refresh();
}
if (levelNum > 7) {
game.addToGlobalInventory('phone');
$('#phoneButton').show();
}
game.getLevel(levelNum);
game.display.focus();
}
// makes an ajax request to get the level text file and
// then loads it into the game
this.getLevel = function (levelNumber) {
var game = this;
game.currentLevel = levelNumber;
game.levelReached = Math.max(levelNumber, game.levelReached);
if (!debugMode) {
localStorage.setItem('levelReached', game.levelReached);
}
var fileName = game.levelFileNames[levelNumber - 1];
$.get('levels/' + fileName, function (lvlCode) {
// load level code in editor
game.editor.loadCode(lvlCode);
// start the level and fade in
game.evalLevelCode(null, null, true);
game.display.focus();
// store the commands introduced in this level (for api reference)
_commands = _commands.concat(game.editor.getProperties().commandsIntroduced).unique();
localStorage.setItem('helpCommands', _commands.join(';'));
});
}
// restart level with currently loaded code
this.restartLevel = function () {
this.editor.setCode(_currentCode);
this.evalLevelCode();
}
this.evalLevelCode = function (allCode, playerCode, isNewLevel) {
var game = this;
// by default, get code from the editor
var loadedFromEditor = false;
if (!allCode) {
allCode = this.editor.getCode();
playerCode = this.editor.getPlayerCode();
loadedFromEditor = true;
}
// save current display state (for scrolling up later)
this.display.saveGrid(this.map);
// validate the code
// if it passes validation, returns the startLevel function if it pass
// if it fails validation, returns false
var validatedStartLevel = this.validate(allCode, playerCode, !loadedFromEditor);
if (validatedStartLevel) { // code is valid
// reset the map
this.map.reset();
this.map.setProperties(this.editor.getProperties()['mapProperties']);
// save editor state
_currentCode = allCode;
if (loadedFromEditor) {
this.editor.saveGoodState();
}
// clear drawing canvas
$('#drawingCanvas')[0].width = $('#drawingCanvas')[0].width;
// start the level
var map = this.map; var display = this.display; var output = this.output;
validatedStartLevel(map);
// draw the map
game.display.fadeIn(this.map, isNewLevel ? 100 : 10, function () {});
$('#static').hide();
// start bg music for this level
if (this.editor.getProperties()['music']) {
this.sound.playTrackByName(this.currentLevel, this.editor.getProperties()['music']);
} else {
this.sound.playTrackByNum(this.currentLevel);
}
// finally, allow player movement
this.map.getPlayer().canMove = true;
game.display.focus();
} else { // code is invalid
// play error sound
this.sound.playSound('static');
// disable player movement
this.map.getPlayer().canMove = false;
}
}
// Constructor
this.init();
};