-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
447 lines (407 loc) · 14.2 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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
var windowSize = { x: 1152, y: 480 };
var mouse;
var bg;
var units = [];
var buildings = [];
var cursors;
var credits;
var buttons = {};
var creditsEnemy = 5000;
var enemyState = {
trucks: 0,
marineTankCounter: 0,
army: 0
};
var gameState = 'play';
var splash;
var scrollbar;
// Layout: statusbar, game window, build bars
var statusHeight = 50;
var gameWindowHeight = 280;
var groundY = statusHeight + gameWindowHeight;
// Groups
// Create them ourselves because we need to control the Z order
var groups;
var music;
var tooltip;
function preload () {
game.load.image('bgimage', 'images/bg.jpg');
game.load.image('victory', 'images/victory.jpg');
game.load.image('defeat', 'images/defeat.jpg');
game.load.image('brushed', 'images/brushed.jpg');
game.load.image('scroll_back', 'images/scroll_back.jpg');
game.load.image('scroll', 'images/scroll.png');
game.load.image('marine_button', 'images/marine_button.jpg');
game.load.image('jeep_button', 'images/jeep_button.jpg');
game.load.image('tank_button', 'images/tank_button.jpg');
game.load.image('truck_button', 'images/truck_button.jpg');
game.load.audio('bgaudio', 'audio/bg.mp3');
game.load.image('frame', 'images/frame.png');
game.load.image('selection', 'images/selection.png');
game.load.spritesheet('cursors', 'images/cursors.png', 40, 40);
game.load.image('shade', 'images/shade.png');
game.load.image('health', 'images/health.png');
game.load.image('mana', 'images/mana.png');
game.load.image('health_back', 'images/health_back.png');
game.load.image('cap_left', 'images/cap_left.png');
game.load.image('cap_right', 'images/cap_right.png');
game.load.spritesheet('digits', 'images/digits.png', 32, 32);
game.load.image('ore', 'images/ore.png');
game.load.image('sabot', 'images/sabot.png');
game.load.image('bullet', 'images/bullet.png');
game.load.image('tank', 'images/tank.png');
game.load.image('tank_enemy', 'images/tank_enemy.png');
game.load.image('marine', 'images/marine.png');
game.load.image('marine_enemy', 'images/marine_enemy.png');
game.load.image('jeep', 'images/jeep.png');
game.load.image('jeep_enemy', 'images/jeep_enemy.png');
game.load.image('truck', 'images/truck.png');
game.load.image('truck_enemy', 'images/truck_enemy.png');
game.load.image('war_fac', 'images/war_fac.png');
game.load.image('war_fac_enemy', 'images/war_fac_enemy.png');
game.load.image('barracks', 'images/barracks.png');
game.load.image('barracks_enemy', 'images/barracks_enemy.png');
game.load.image('refinery', 'images/refinery.png');
game.load.image('refinery_enemy', 'images/refinery_enemy.png');
game.load.audio('collect', 'audio/collect.mp3');
game.load.audio('explode', 'audio/explode.mp3');
game.load.audio('agony', 'audio/agony.mp3');
game.load.audio('tank_fire', 'audio/tank_fire.mp3');
game.load.audio('browning', 'audio/browning.mp3');
game.load.audio('m16', 'audio/m16.mp3');
game.load.audio('ready', 'audio/ready.mp3');
game.load.audio('affirmative', 'audio/affirmative.mp3');
game.load.audio('training', 'audio/training.mp3');
game.load.audio('training_complete', 'audio/training_complete.mp3');
game.load.audio('building', 'audio/building.mp3');
game.load.audio('unit_ready', 'audio/unit_ready.mp3');
cursors = game.input.keyboard.createCursorKeys();
}
function create () {
game.stage.backgroundColor = '0x666699';
var i;
bg = game.add.sprite(0, statusHeight, 'bgimage');
music = game.add.audio('bgaudio');
music.play('', 0, 0.3, true);
groups = {
buildings: game.add.group(),
ore: game.add.group(),
units: game.add.group()
};
game.world.setBounds(0, 0, 10000, windowSize.y);
// Credits
var topbg = game.add.sprite(0, 0, 'brushed');
topbg.fixedToCamera = true;
topbg.cameraOffset.y = -100;
credits = new Credits(game, 9, 9);
credits.addCredits(5000);
// Add a bunch of tanks
/*for (var i = 1000; i < 1500; i += 150) {
units.push(NewTank(i, 'player'));
}*/
// Add some enemy units
for (i = game.world.bounds.width - 500;
i < game.world.bounds.width - 400;
i += 100) {
units.push(NewTankEnemy(i, 'cpu'));
units.push(NewMarineEnemy(i, 'cpu'));
}
// Add buildings
var barracks = NewBarracks(250, 'player', NewMarine);
buildings.push(barracks);
var factory = NewFactory(750, 'player', [NewJeep, NewTank]);
buildings.push(factory);
var refinery = NewRefinery(1250, 'player', NewTruck);
buildings.push(refinery);
buildings.push(NewBarracks(game.world.bounds.width - 250, 'cpu', NewMarineEnemy));
buildings.push(NewFactory(game.world.bounds.width - 750, 'cpu', [NewJeep, NewTankEnemy]));
buildings.push(NewRefinery(game.world.bounds.width - 1250, 'cpu', NewTruck));
// Add ore patches
for (i = 2000; i < game.world.bounds.width - 2000; i += Math.random(2000) + 500) {
var patchSize = Math.random(800) + 200;
for (var j = 0; j < patchSize; j += oreWidth) {
ore = groups.ore.create(i + j, groundY, 'ore');
ore.anchor.x = 0.5;
ore.anchor.y = 1;
ore.health = Math.random(500) + 300;
oreWidth = ore.width;
}
}
// Bottom
var bottombg = game.add.sprite(0, 0, 'brushed');
bottombg.fixedToCamera = true;
bottombg.cameraOffset.y = groundY;
scrollbar = new Scrollbar(game, 0, groundY);
var style = { font: "18px Arial", fill: "#dddd33" };
var x = 10;
var y = groundY + scrollbar.sprite.height + 10;
buttons.marine = new BuildButton(game, x, y, 'marine_button', barracks, credits, 0);
x += buttons.marine.button.width + 10;
buttons.jeep = new BuildButton(game, x, y, 'jeep_button', factory, credits, 0);
x += buttons.jeep.button.width + 10;
buttons.tank = new BuildButton(game, x, y, 'tank_button', factory, credits, 1);
x += buttons.tank.button.width + 10;
buttons.truck = new BuildButton(game, x, y, 'truck_button', refinery, credits, 0);
tooltip = game.add.text(0, 0, "", style);
// Need to set these after tooltip is created
buttons.marine.setTooltip(tooltip, 'Marine');
buttons.jeep.setTooltip(tooltip, 'Jeep');
buttons.tank.setTooltip(tooltip, 'Tank');
buttons.truck.setTooltip(tooltip, 'Harvester truck');
mouse = new Mouse(game, statusHeight, gameWindowHeight);
splash = null;
}
// Building factory functions
function NewBarracks(x, team, unitFunc) {
var b = new Building(game, groups.buildings,
'barracks' + (team === 'player' ? '' : '_enemy'),
'explode',
'training', 'training_complete',
200, {x:x, y:groundY}, team);
b.addUnit(unitFunc, 150, 100);
return b;
}
function NewFactory(x, team, unitFuncs) {
var b = new Building(game, groups.buildings,
'war_fac' + (team === 'player' ? '' : '_enemy'),
'explode',
'building', 'unit_ready',
300, {x:x, y:groundY}, team);
var costs = [450, 600];
var buildtimes = [320, 500];
for (var i = 0; i < unitFuncs.length; i++) {
b.addUnit(unitFuncs[i], costs[i], buildtimes[i]);
}
return b;
}
function NewRefinery(x, team, unitFunc) {
var b = new Building(game, groups.buildings,
'refinery' + (team === 'player' ? '' : '_enemy'),
'explode',
'building', 'unit_ready',
300, {x:x, y:groundY}, team);
b.addUnit(unitFunc, 1500, 1000);
b.isRefinery = true;
return b;
}
// Unit factory functions
function NewTank(x, team) {
return new Unit(game, groups.units,
'tank',
'sabot', 1, 110, -48,
'explode',
3.0,
20.0, 'tank_fire', 1.0, 400,
100,
{x:x, y:groundY},
team);
}
function NewTankEnemy(x, team) {
return new Unit(game, groups.units,
'tank_enemy',
'sabot', 1, 110, -48,
'explode',
2.5,
20.0, 'tank_fire', 1.0, 400,
120,
{x:x, y:groundY},
team);
}
function NewMarine(x, team) {
return new Unit(game, groups.units,
'marine',
'bullet', 5, 30, -40,
'agony',
2.4,
10.0, 'm16', 1.9, 300,
50,
{x:x, y:groundY},
team);
}
function NewMarineEnemy(x, team) {
return new Unit(game, groups.units,
'marine_enemy',
'bullet', 5, 30, -40,
'agony',
2.0,
10.0, 'm16', 1.9, 300,
60,
{x:x, y:groundY},
team);
}
function NewJeep(x, team) {
return new Unit(game, groups.units,
'jeep' + (team === 'player' ? '' : '_enemy'),
'bullet', 4, 80, -48,
'explode',
4.0,
12.0, 'browning', 2.1, 350,
80,
{x:x, y:groundY},
team);
}
function NewTruck(x, team) {
return new Unit(game, groups.units,
'truck' + (team === 'player' ? '' : '_enemy'),
'bullet', 5, 20, -40,
'explode',
1.5,
0, 'm16', 2.0, 300,
200,
{x:x, y:groundY},
team);
}
var cameraSpeed = 40;
function update() {
if (gameState === 'play') {
var i;
// Select unit on click
mouse.update(groups.units);
for (i = 0; i < buildings.length; i++) {
var building = buildings[i];
if (building.team !== 'player') {
// Enemy build AI: one truck per 10 units, 3x marines, 2x jeep, 1x tank
for (var j = 0; j < building.units.length; j++) {
var tryBuild = false;
var canBuild = building.canBuild(creditsEnemy, j);
if (enemyState.trucks < enemyState.army / 10 + 1) {
tryBuild = building.isRefinery;
if (tryBuild && canBuild) {
enemyState.trucks++;
}
} else if (enemyState.marineTankCounter <= 3) {
tryBuild = building.name === 'barracks_enemy';
if (tryBuild && canBuild) {
enemyState.marineTankCounter++;
enemyState.army++;
}
} else if (enemyState.marineTankCounter <= 5) {
tryBuild = building.name === 'war_fac_enemy' && j === 0;
if (tryBuild && canBuild) {
enemyState.marineTankCounter++;
enemyState.army++;
}
} else {
tryBuild = building.name === 'war_fac_enemy' && j === 1;
if (tryBuild && canBuild) {
enemyState.marineTankCounter = 0;
enemyState.army++;
}
}
if (tryBuild && building.canBuild(creditsEnemy, j)) {
building.build(j);
creditsEnemy -= building.units[j].cost;
}
}
}
}
for (i = 0; i < units.length; i++) {
var unit = units[i];
unit.update(units, buildings, groups.ore);
// Check for refining
if (unit.oreDeposited > 0) {
if (unit.team === 'player') {
credits.addCredits(unit.oreDeposited);
} else {
// The CPU cheats!
creditsEnemy += unit.oreDeposited * 1.4;
}
unit.oreDeposited = 0;
}
// Check for dead units
if (unit.sprite.health <= 0) {
if (unit.team !== 'player') {
if (unit.name == 'truck_enemy') {
enemyState.trucks--;
} else {
enemyState.army--;
}
}
unit.kill(groups.units);
units.splice(i, 1);
i--;
}
}
for (i = 0; i < buildings.length; i++) {
buildings[i].update(units);
// Check for dead buildings
if (buildings[i].sprite.health <= 0) {
// play death effects
buildings[i].kill(groups.buildings);
buildings.splice(i, 1);
i--;
// Check victory conditions
var playerAlive = false;
var enemyAlive = false;
for (var j = 0; j < buildings.length; j++) {
if (!buildings[j].sprite.alive) {
continue;
}
if (buildings[j].team === 'player') {
playerAlive = true;
} else {
enemyAlive = true;
}
}
if (!playerAlive) {
if (splash === null) {
splash = game.add.sprite(game.camera.x + game.width / 2,
game.camera.y + game.height / 2,
'defeat');
splash.x -= splash.width / 2;
splash.y -= splash.height / 2;
gameState = 'end';
}
} else if (!enemyAlive) {
if (splash === null) {
splash = game.add.sprite(game.camera.x + game.width / 2,
game.camera.y + game.height / 2,
'victory');
splash.x -= splash.width / 2;
splash.y -= splash.height / 2;
gameState = 'end';
}
}
}
}
credits.update();
scrollbar.update();
buttons.marine.update();
buttons.jeep.update();
buttons.tank.update();
buttons.truck.update();
// Move camera
if (cursors.right.isDown) {
game.camera.x += cameraSpeed;
} else if (cursors.left.isDown) {
game.camera.x -= cameraSpeed;
}
// Parallax
bg.x = game.camera.x * 0.7;
} else if (gameState === 'end') {
}
}
var Boot = function (game) {
};
Boot.prototype = {
create: function () {
this.game.stage.backgroundColor = 0x00FFF6;
this.input.maxPointers = 1;
this.game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
this.game.physics.startSystem(Phaser.Physics.ARCADE);
this.state.start('game');
}
};
var GameState = function(game){};
GameState.prototype.preload = preload;
GameState.prototype.create = create;
GameState.prototype.update = update;
var game;
window.onload = function() { setTimeout(function () {
game = new Phaser.Game(
windowSize.x, windowSize.y, Phaser.AUTO, 'gameContainer', null, false, false
);
game.state.add('boot', Boot);
game.state.add('game', GameState);
game.state.start('boot');
}, 1000); };