-
Notifications
You must be signed in to change notification settings - Fork 11
/
game.js
360 lines (268 loc) · 9.12 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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
function createGame(gameCavas, onGameLoaded)
{
var game = new Object();
game.gameCanvas = document.createElement('canvas');
game.gameCanvas.width = 1600;
game.gameCanvas.height = 200;
game.gameCanvas.className="PixelatedRendering";
game.gameContext = game.gameCanvas.getContext("2d");
game.displayCanvas = gameCavas;
game.displayContext = game.displayCanvas.getContext("2d");
//- Disable Interpolation for scaling
if (typeof game.displayContext.imageSmoothingEnabled !== "undefined")
{
game.displayContext.imageSmoothingEnabled = false;
}
else
{
game.displayContext.mozImageSmoothingEnabled = false;
game.displayContext.webkitImageSmoothingEnabled = false;
game.displayContext.msImageSmoothingEnabled = false;
}
game.palette = new ColorPalette(game);
game.palette.setColor(0, 0x000000, true); // <- balck
game.palette.setColor(1, 0x4040e0, true); // blue: Lemmings Body
game.palette.setColor(2, 0x00b000, true); // green: Lemmings haar
game.palette.setColor(3, 0xf3d3d3, true); // white: Lemmings skin / Letters
game.palette.setColor(4, 0xb2b200, true); // yellow
game.palette.setColor(5, 0xf32020, true); // dark red
game.palette.setColor(6, 0x828282, true); // gray
game.palette.setColor(7, 0xe08020, false); // this color is set by the level
game.SKILL = {
CLIMBER : {value: 1, name: "Climber"},
FLOATER : {value: 2, name: "Floater"},
BOMBER : {value: 3, name: "Bomber"},
BLOCKER : {value: 4, name: "Blocker"},
BUILDER : {value: 5, name: "Builder"},
BASHER : {value: 6, name: "Basher"},
MINER : {value: 7, name: "Miner"},
DIGGER : {value: 8, name: "Digger"}
};
//- available amount of skills
game.skills = new Uint8Array(8);
game.VERSION = {
LEMMINGS : {value: 1, name : "Lemmings original"},
OHNO : {value: 2, name : "Oh No! More Lemmings"},
HOLIDAY93: {value: 3, name : "Holiday 93"},
HOLIDAY94: {value: 4, name : "Holiday 94"},
XMAS91 : {value: 5, name : "X-mas 91 demo"},
XMAS92 : {value: 6, name : "X-mas 92 demo"}
};
game.fullpage = false;
game.version = game.VERSION.LEMMINGS;
game.isGameOver = false;
game.TICKS_PER_SECOND = 14;
//- current relese rate
game.releaseRate = 50;
game.releaseRateMin = 50;
game.nextReleaseTick = 10;
game.releaseCount = 3;
game.releasedCount = 0;
game.savedCount = 0;
game.diedCount = 0;
game.intervallId = 0;
game.decReleaseRate = function()
{
game.releaseRate--;
if (game.releaseRate < game.releaseRateMin) game.releaseRate = game.releaseRateMin;
}
game.incReleaseRate = function()
{
game.releaseRate++;
if (game.releaseRate > 99) game.releaseRate = 99;
}
game.isBreak = false; //- is the game in pause mode?
game.switchBreak = function()
{
game.isBreak = !game.isBreak;
}
//- List of all Lemmings
game.lemmings = Array();
//- View point
game.viewX = 0;
game.viewY = 0;
game.viewScale = 2.5;
game.guiPosY = game.gameCanvas.height - 30;
game.guiScale = 2;
//- Game Time
game.tick = 0;
game.codeGenerator = new CodeGenerator(game);
//-- check
// var c = game.codeGenerator.createCode(107,100);
// c == "JMDMGINMIV"
//--------
game.levelIndexProvider = new LevelIndexProvider(game);
//game.levelIndexProvider.load();
game.levelIndex = 0;
//- URL Parameter: load Level Number...
var hash = window.location.hash.substring(1);
if (hash.length == 10)
{
var levelInfo = game.codeGenerator.reverseCode(hash);
game.levelIndex = levelInfo.levelIndex;
game.version = levelInfo.lemmingsVersion;
}
else
{
var levelPos = Number.parseInt(hash);
if (!isNaN(levelPos))
{
game.levelIndex = levelPos;
}
}
game.eventHandler = new EventHandler(game);
game.triggerHandler = new TriggerHandler(game);
game.fileLoader = new FileLoader();
//- loading main.dat
game.mainFileProvider = new MainFileProvider(game);
game.mainFileProvider.load(function()
{
game.lemmingDrawer = new LemmingDrawer(game);
game.lemmingDrawer.load();
//- creating GUI : requires main.dat
game.gameGui = new GameGui(game);
game.gameGui.load();
//- loading level01.dat
game.levelHandler = new LevelHandler(game);
game.levelHandler.load(onLevelLoaded, game.levelIndex);
});
function onLevelLoaded()
{
game.viewX = game.levelHandler.screenPositionX;
for (var i = 0; i < 10; i++)
{
game.skills[i] = game.levelHandler.skills[i];
}
game.releaseRate = game.levelHandler.releaseRate;
game.releaseRateMin = game.levelHandler.releaseRate;
game.releaseCount = game.levelHandler.releaseCount;
//- load GROUNDxO.DAT and VGAGRx.DAT
game.levelDrawer = new LevelDrawer(game);
game.levelDrawer.load(onLevelDrawerLoaded);
}
function onLevelDrawerLoaded()
{
//- generate terrain layer
game.levelDrawer.createTerrain();
game.levelDrawer.createObjectTrigger();
//- callback
if (typeof onGameLoaded !== "undefined") onGameLoaded();
}
//////////////////////////
///- Public Functions -///
game.startGame = function()
{
//- start game loop
game.intervallId = window.setInterval(function() {game.gameLoop(); }, 1000 / game.TICKS_PER_SECOND);
}
game.gameLoop = function()
{
if (game.isGameOver)
{
//--- Game is over ----
if (typeof game.onGameOver !== "undefined")
{
window.clearInterval(game.intervallId);
game.onGameOver(game.codeGenerator.createCode(game.levelIndex + 1, 100));
}
}
if (!game.isBreak)
{
//---- Game is running... precess the next step/tick
game.tick ++;
if (game.releaseCount > 0)
{
if (game.tick >= game.nextReleaseTick)
{
game.releaseCount--;
game.releasedCount++;
//- get the pos of the entry point
var pos = game.levelHandler.getEntryPos();
//- add one lemming
game.lemmings.push(new Lemming(game, pos.x, pos.y));
//- next Lemming need to be released in ticks
game.nextReleaseTick = game.tick + (Math.floor((99 - game.releaseRate) / 2) + 4);
}
}
var tickNo = game.tick;
var c = game.lemmings.length;
for (var i = 0; i < c; i++)
{
game.lemmings[i].tick(tickNo);
}
}
game.doRendering();
}
game.doRendering = function()
{
if (game.fullpage)
{
//- do we need to rescale the Canvas to the screen?
var needWith = window.innerWidth - 5;
var needHeight = window.innerHeight - 5;
if ((game.displayCanvas.width != needWith) || (game.displayCanvas.height != needHeight))
{
game.displayCanvas.width = needWith;
game.displayCanvas.height = needHeight;
game.displayContext.width = game.displayCanvas.width;
game.displayContext.height = game.displayCanvas.height;
//- rescaling the Canvas removes the "imageSmoothingEnabled" property... reset it!
if (typeof game.displayContext.imageSmoothingEnabled !== "undefined")
{
game.displayContext.imageSmoothingEnabled = false;
}
else
{
game.displayContext.mozImageSmoothingEnabled = false;
game.displayContext.webkitImageSmoothingEnabled = false;
game.displayContext.msImageSmoothingEnabled = false;
}
}
}
//- game rendering...
var gameW = game.gameCanvas.width;
var gameH = game.gameCanvas.height;
var outW = game.displayCanvas.width;
var outH = game.displayCanvas.height;
game.guiScale = outW / game.gameGui.width;
var guiOutHeight = game.gameGui.height * game.guiScale;
var outGameH = outH - guiOutHeight;
game.guiPosY = outGameH;
//-----------
//- Draw Game
//- clear the output with black
game.displayContext.fillStyle = "rgba(0, 0, 0, 255)";
game.displayContext.fillRect(0, 0, outW, outGameH);
//- 1. Layer: deep copy of terrain Image
var img = game.gameContext.createImageData(game.gameTerrain.width, game.gameTerrain.height);
img.data.set(game.gameTerrain.data);
//- 2. add Object Layer
var img = game.levelDrawer.getObjects(img);
game.gameContext.putImageData(img, 0, 0);
//- 3. add Lemmings Layer
var img = game.lemmingDrawer.getImage(img);
game.gameContext.putImageData(img, 0, 0);
//- Display Layers
var dW = gameW - game.viewX; //- display width
if ((dW * game.viewScale) > outW)
{
dW = outW / game.viewScale;
//game.viewScale = outW / dW;
}
var dH = gameH - game.viewY; //- display height
if ((dH * game.viewScale) > outGameH)
{
dH = outGameH / game.viewScale;
//game.viewScale = outH / dH;
}
//- drawImage(image,sx,sy,sw,sh,dx,dy,dw,dh)
game.displayContext.drawImage(game.gameCanvas, game.viewX, game.viewY, dW, dH, 0, 0, dW * game.viewScale , dH * game.viewScale);
//-----------
//- Draw GUI
//- create GameGui Image
var guiImg = game.gameGui.getGui();
//- drawImage(image,sx,sy,sw,sh,dx,dy,dw,dh)
game.displayContext.drawImage(guiImg, 0, 0, guiImg.width, guiImg.height, 0, outH - guiOutHeight, outW, guiOutHeight);
}
return game;
}