Repository navigation
Expand file tree
/
Copy pathgame.js
More file actions
308 lines (260 loc) · 10.8 KB
/
Copy pathgame.js
File metadata and controls
308 lines (260 loc) · 10.8 KB
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
function Game(width,height) {
var self = this;
this.renderer = PIXI.autoDetectRenderer(width,height);
this.lastFrame=new Date();
this.activeContext = null;
self.gameLoop = gameLoop;
function gameLoop() {
if(self.ready()) {
var thisFrame=new Date();
var delta=thisFrame.getTime()-self.lastFrame.getTime();
self.lastFrame=thisFrame;
self.activeContext.update(delta);
//alert("about to render scene.");
self.renderer.render(self.activeContext.stage);
//alert("Scene rendered!");
}
}
self.ready = ready;
function ready() {
return self.activeContext.loaded;
}
}
function GameContext(map) {
var self = this;
this.map = map;
this.stage = new PIXI.Stage(0x77FF77,true);
this.map.zombies = new entityList(this);
this.map.turrets = new entityList(this);
this.buildMenu = new BuildMenu(this);
this.buildTime = 10000;
this.buildTimer = new TimerText;
this.map_DO = loadTileMap(this.map);
this.loaded = false;
this.mouseTool = null;
this.animators = new entityList(this);
this.resources = null;
this.scoreboard = null;
this.phase = "none";
self.setMouseTool = setMouseTool;
self.removeMouseTool = removeMouseTool;
self.startBuildPhase = startBuildPhase;
self.getByGridCoords = getByGridCoords;
this.mouseTurret = new Turret(this);
self.load = load;
function load() {
self.stage.addChild(self.map_DO);
self.stage.addChild(self.buildMenu.menu_DO);
self.buildMenu.menu_DO.position = new PIXI.Point(640,0);
self.buildMenu.addEntry("turrets",new MenuEntry(new PIXI.Sprite(PIXI.Texture.fromImage("buildmenu/turret_blue.png")),
"Turrets",self.setMouseTool,self.mouseTurret));
self.buildMenu.addEntry("acid_spray",new MenuEntry(new PIXI.Sprite(PIXI.Texture.fromImage("buildmenu/acid_blue.png")),
"Acid Spray",null));
self.buildMenu.addEntry("walls",new MenuEntry(new PIXI.Sprite(PIXI.Texture.fromImage("buildmenu/wallend_blue.png")),
"Walls", null));
self.buildTimer.cntr.position = new PIXI.Point(self.map.width*self.map.tilewidth/2,
self.map.height*self.map.tileheight*0.1);
self.scoreboard = new Scoreboard(self,"");
self.map_DO.addChild(self.buildTimer.cntr);
self.loaded = true;
}
self.clearEntityListAndSprites = clearEntityListAndSprites;
function startBuildPhase(resources) {
self.clearEntityListAndSprites(self.map.turrets);
self.clearEntityListAndSprites(self.map.zombies);
self.phase = "build";
self.resources = resources;
self.map_DO.setInteractive(true);
self.buildMenu.activate();
self.buildMenu.updateText("turrets","Turrets x "+self.resources.nTurrets);
self.buildMenu.updateText("acid_spray","Acid Spray x "+self.resources.nAcidSpray);
self.buildMenu.updateText("walls","Walls x "+self.resources.nWallTiles);
self.map_DO.addChild(self.buildTimer.cntr);
self.animators.add(new ValueAnimator(function(value) {
self.buildTimer.updateText(value);
},self.buildTime,0,self.buildTime));
setTimeout(self.startAttackPhase,self.buildTime);
}
self.startAttackPhase = startAttackPhase;
function startAttackPhase() {
self.removeMouseTool();
self.buildMenu.deactivate();
self.map_DO.removeChild(self.buildTimer.cntr);
for(var i = 0; i < 10; ++i) {
var zomb = new zombie(self,Math.random()*608,0);
self.map.zombies.add(zomb);
self.map_DO.addChild(zomb.sprite);
}
self.phase = "attack";
}
self.startVictoryPhase = startVictoryPhase;
function startVictoryPhase() {
self.phase = "victory";
self.scoreboard.updateText("Victory!\n\nTurrets used: "+(10-self.resources.nTurrets)+"\nAcid used: 0\nWall tiles used: 0");
self.scoreboard.open();
}
self.startDefeatedPhase = startDefeatedPhase;
function startDefeatedPhase() {
self.phase = "defeat";
self.scoreboard.updateText("You have been overrun!");
self.scoreboard.open();
}
self.update = update;
function update(delta) {
if(self.phase == "attack") if(self.map.zombies.entities.length == 0) self.startVictoryPhase();
if(self.mouseTool !== null) {
self.mouseTool.sprite.position = self.stage.getMousePosition();
}
for(var a in self.animators.entities) {
if(self.animators.entities[a].finished) self.animators.remove(self.animators.entities[a]);
}
self.map.zombies.update(delta);
self.map.turrets.update(delta);
self.animators.update(delta);
}
this.map_DO.mousedown = function(mouseData) {
if(self.phase == "build") {
var grid_x = Math.floor(self.stage.getMousePosition().x/self.map.tilewidth);
var grid_y = Math.floor(self.stage.getMousePosition().y/self.map.tileheight);
if (self.mouseTool !== null && self.canPlaceonGrid(grid_x,grid_y)) {
if (self.mouseTool instanceof Turret && self.resources.nTurrets > 0) {
self.addToMapGrid(self.mouseTool,grid_x,grid_y);
--self.resources.nTurrets;
self.buildMenu.updateText("turrets","Turrets x "+self.resources.nTurrets);
}
}
}
}
function setMouseTool(entityWithSprite) {
if(self.mouseTool === entityWithSprite) {
self.removeMouseTool();
return;
}
if(self.mouseTool !== null) self.map_DO.removeChild(self.mouseTool.sprite);
self.mouseTool = entityWithSprite;
self.stage.addChild(self.mouseTool.sprite);
}
function removeMouseTool() {
if(self.mouseTool !== null) {
self.stage.removeChild(self.mouseTool.sprite);
self.mouseTool = null;
}
}
self.canPlaceonGrid = canPlaceonGrid;
function canPlaceonGrid(grid_x,grid_y) {
return !detectCollision(getBlockedPlacementArray(self.map),grid_x,grid_y) &&
!getByGridCoords(grid_x,grid_y) &&
!getByGridCoords(grid_x+1,grid_y) &&
!getByGridCoords(grid_x+1,grid_y+1) &&
!getByGridCoords(grid_x,grid_y+1) &&
!getByGridCoords(grid_x-1,grid_y+1) &&
!getByGridCoords(grid_x-1,grid_y) &&
!getByGridCoords(grid_x-1,grid_y-1) &&
!getByGridCoords(grid_x,grid_y-1) &&
!getByGridCoords(grid_x+1,grid_y-1);
}
self.addToMapGrid = addToMapGrid;
function addToMapGrid(gridobject,grid_x,grid_y) {
var placedobject;
if(gridobject instanceof Turret) {
placedobject = new Turret(self);
placedobject.grid_x = grid_x; placedobject.grid_y = grid_y;
placedobject.sprite.position = new PIXI.Point((grid_x+0.5)*self.map.tilewidth,
(grid_y+0.5)*self.map.tileheight);
self.map.turrets.add(placedobject);
self.map_DO.addChild(placedobject.sprite);
}
}
function getByGridCoords(grid_x,grid_y) {
for(var e in self.map.turrets.entities) {
if (self.map.turrets.entities[e].grid_x == grid_x && self.map.turrets.entities[e].grid_y == grid_y)
return self.map.turrets.entities[e];
}
return null;
}
function clearEntityListAndSprites(entitylist) {
var length = entitylist.entities.length;
for(var e = 0; e < length; ++e) {
self.map_DO.removeChild(entitylist.entities[0].sprite);
entitylist.entities.splice(0,1);
}
}
}
function entityList(context,pEntities) {
var self = this;
this.context = context;
if (pEntities !== undefined) this.entities = pEntities;
else this.entities = new Array;
self.add = add;
function add(ent) {
self.entities.push(ent);
ent.context = self.context;
}
self.remove = remove;
function remove(ent) {
for (var e in self.entities) {
if (ent === self.entities[e]) {
self.entities.splice(e,1);
return;
}
}
}
self.update = update;
function update(delta) {
for (var i in self.entities) {
ent = self.entities[i];
if(ent.update !== undefined) {
ent.update(delta);
}
}
}
}
function distance(x0,y0,x1,y1) {
return Math.sqrt(Math.pow(x1-x0,2)+
Math.pow(y1-y0,2));
}
function ValueAnimator(setValueCallback,initialValue,targetValue,timeToTake) {
var self = this;
this.initialValue = initialValue;
this.value = initialValue;
this.targetValue = targetValue;
this.startTime = new Date();
this.setValueCallback = setValueCallback;
this.timeToTake = timeToTake;
this.finished = false;
this.increasing = self.targetValue - self.initialValue > 0;
self.update = update;
function update(delta) {
if(!self.finished) {
self.value += delta/self.timeToTake*(self.targetValue-self.initialValue);
self.setValueCallback(self.value);
}
if((self.increasing && self.value > self.targetValue) ||
(!self.increasing && self.value < self.targetValue)) {
self.finished = true;
self.setValueCallback(self.targetValue);
}
}
}
function TimerText() {
var self = this;
this.bgspr = new PIXI.Sprite(PIXI.Texture.fromImage("counterbg.png"));
this.bgspr.anchor = new PIXI.Point(0.5,0.4);
this.displayText = new PIXI.Text("Start",{ font: "16pt Arial" });
this.cntr = new PIXI.DisplayObjectContainer;
this.cntr.addChild(this.bgspr);
this.cntr.addChild(this.displayText);
self.updateText = updateText;
function updateText(time) {
this.cntr.removeChild(this.displayText);
var text = Math.floor(time/1000)+"."+(Math.floor(time/100) % 10);
self.displayText = new PIXI.Text(text,{ font: "bold 20pt Arial", fill: "white" });
self.displayText.anchor = new PIXI.Point(0.5,(self.displayText.height-32)/2/self.displayText.height+0.1);
self.cntr.addChild(this.displayText);
}
}
function Resources(nTurrets,nAcidSpray,nWallTiles) {
this.nTurrets = nTurrets;
this.nAcidSpray = nAcidSpray;
this.nWallTiles = nWallTiles;
}