forked from sorenbs/bananabomber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
403 lines (359 loc) · 14.7 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
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
window.onload = function () {
//start crafty
Crafty.init(400, 336);
//Crafty.canvas();
//turn the sprite map into usable components
Crafty.sprite(16, "sprite.png", {
grass1: [0, 0],
grass2: [1, 0],
grass3: [2, 0],
grass4: [3, 0],
flower: [0, 1],
bush1: [0, 2],
bush2: [1, 2],
player: [0, 3],
enemy: [0, 3],
banana: [4, 0],
empty: [4, 0]
});
/*
function deepCopy(obj) {
var newObj = (obj instanceof Array) ? [] : {};
for (i in obj) {
if (i == 'componentStore') continue;
if(!obj.hasOwnProperty(i)) continue;
if (obj[i] && typeof obj[i] == "object") {
newObj[i] = deepCopy(obj[i]);
} else newObj[i] = obj[i]
}
return newObj;
};
var a = {
e: function() {
return "z";
}
}
var b = deepCopy(a);
console.log(a.e());
console.log("e" in b);
*/
//method to generate the map
function generateWorld() {
//loop through all tiles
for (var i = 0; i < 25; i++) {
for (var j = 0; j < 21; j++) {
//place grass on all tiles
grassType = Crafty.randRange(1, 4);
Crafty.e("2D, DOM, grass" + grassType)
.attr({ x: i * 16, y: j * 16, z:1 });
//grid of bushes
if((i % 2 === 0) && (j % 2 === 0)) {
Crafty.e("2D, DOM, solid, bush1")
.attr({x: i * 16, y: j * 16, z: 2000})
}
//create a fence of bushes
if(i === 0 || i === 24 || j === 0 || j === 20)
Crafty.e("2D, DOM, solid, bush" + Crafty.randRange(1, 2))
.attr({ x: i * 16, y: j * 16, z: 2 });
//generate some nice flowers within the boundaries of the outer bushes
if (i > 0 && i < 24 && j > 0 && j < 20
&& Crafty.randRange(0, 50) > 30
&& !(i === 1 && j >= 16)
&& !(i === 23 && j <= 4)) {
var f = Crafty.e("2D, DOM, flower, Collision, solid, SpriteAnimation, explodable")
.attr({ x: i * 16, y: j * 16, z: 1000 })
.animate("wind", 0, 1, 3)
.animate('wind', 80, -1)
.bind('explode', function() {
this.destroy();
})
.collision();
if(f.hit('solid')) {
f.destroy();
}
}
}
}
}
//the loading screen that will display while our assets load
Crafty.scene("loading", function () {
//load takes an array of assets and a callback when complete
Crafty.load(["sprite.png"], function () {
Crafty.scene("main"); //when everything is loaded, run the main scene
});
//black background with some loading text
Crafty.background("#000");
Crafty.e("2D, DOM, Text").attr({ w: 100, h: 20, x: 150, y: 120 })
.text("Loading")
.css({ "text-align": "center" });
});
//automatically play the loading scene
Crafty.scene("loading");
Crafty.c('BombDropper', {
_dropped: 0,
maxBombs: 2,
_key: Crafty.keys.SPACE,
init: function() {
var dropper = this;
this.requires('Grid')
//Create the bomb
.bind('KeyDown', function(e) {
if (e.key !== this._key) {
return;
}
if(this._dropped < this.maxBombs) {
Crafty.e('BananaBomb')
.attr({z:100})
.col(this.col())
.row(this.row())
.BananaBomb()
.bind('explode', function() {
dropper._dropped--;
});
this._dropped++;
}
});
},
bombDropper: function(key) {
this._key = key;
return this;
}
});
Crafty.c('BananaBomb', {
init: function() {
this.requires("2D, DOM, SpriteAnimation, Grid, banana, explodable")
.animate('explode', 4, 0, 5)
.animate('explode', 50, -1)
.delay(function() {
this.trigger("explode");
}, 4000)
.bind('explode', function() {
this.destroy();
//Create fire from the explosion
for(var i = this.col() - 2; i < this.col()+3; i++)
Crafty.e("BananaFire").attr({ z:9000 }).col(i).row(this.row())
for(var i = this.row() - 2; i < this.row()+3; i++)
Crafty.e("BananaFire").attr({ z:9000 }).col(this.col()).row(i)
});
},
BananaBomb: function() {
//Create shadow fire to help the AI
for(var i = this.col() - 2; i < this.col()+3; i++)
Crafty.e("ShadowBananaFire").attr({ z:9000 }).col(i).row(this.row())
for(var i = this.row() - 2; i < this.row()+3; i++)
Crafty.e("ShadowBananaFire").attr({ z:9000 }).col(this.col()).row(i)
return this;
}
});
Crafty.c('BananaFire', {
init: function() {
this.requires("2D, DOM, SpriteAnimation, banana, Grid, Collision, fire")
.animate('fire', 4, 0, 5)
.animate('fire', 10, -1)
.collision()
.onHit('explodable', function(o) {
for(var i = 0; i < o.length; i++) {
o[i].obj.trigger("explode");
}
})
.delay(function() {
this.destroy();
}, 2000);
}
});
// Helps the AI avoid unsafe tiles. Created when a bomb is dropped and removed after fire is gone
Crafty.c('ShadowBananaFire', {
init: function() {
this.requires("2D, Grid, empty, Collision, ShadowFire")
.collision()
.delay(function() {
this.destroy();
}, 6100);
}
});
Crafty.c('Grid', {
_cellSize: 16,
Grid: function(cellSize) {
if(cellSize) this._cellSize = cellSize;
return this;
},
col: function(col) {
if(arguments.length === 1) {
this.x = this._cellSize * col;
return this;
} else {
return Math.round(this.x / this._cellSize);
}
},
row: function(row) {
if(arguments.length === 1) {
this.y = this._cellSize * row;
return this;
} else {
return Math.round(this.y / this._cellSize);
}
},
snap: function(){
this.x = Math.round(this.x/this._cellSize) * this._cellSize;
this.y = Math.round(this.y/this._cellSize) * this._cellSize;
}
});
Crafty.c('AIControls', {
_move: 'down',
_directions: {0: 'left', 1:'right', 2: 'up', 3: 'down'},
_speed: 3,
_inShadow: false,
AIControls: function (speed) {
if (speed) this._speed = speed;
//functions to determine if there is a free path in some direction
var AIScope = this;
var pathTester = Crafty.e('2D, empty, Collision').attr({z:30000}).collision();
var PathTest = {
left: function() { pathTester.attr({x: AIScope.x-AIScope._speed, y: AIScope.y});
return !(pathTester.hit('solid') || (!AIScope._inShadow && pathTester.hit('ShadowBananaFire')));},
right: function() { pathTester.attr({x: AIScope.x+AIScope._speed, y: AIScope.y});
return !(pathTester.hit('solid') || (!AIScope._inShadow && pathTester.hit('ShadowBananaFire')));},
up: function() { pathTester.attr({x: AIScope.x, y: AIScope.y-AIScope._speed});
return !(pathTester.hit('solid') || (!AIScope._inShadow && pathTester.hit('ShadowBananaFire')));},
down: function() { pathTester.attr({x: AIScope.x, y: AIScope.y+AIScope._speed});
return !(pathTester.hit('solid') || (!AIScope._inShadow && pathTester.hit('ShadowBananaFire')));},
none: function() { return false; }
};
function bombWillHit() {
pathTester.attr({x: AIScope.x-1, y: AIScope.y});
if(pathTester.hit('flower')) { return true; }
pathTester.attr({x: AIScope.x+1, y: AIScope.y});
if(pathTester.hit('flower')) { return true; }
pathTester.attr({x: AIScope.x, y: AIScope.y-1});
if(pathTester.hit('flower')) { return true; }
pathTester.attr({x: AIScope.x, y: AIScope.y+1});
if(pathTester.hit('flower')) { return true; }
return false;
}
this.bind('enterframe', function() {
var nextDirection = '';
if(PathTest[this._move]())
{
nextDirection = this._move;
//when we are at a crossroad interesting things can happen
if(this.x % 16 < this._speed && this.y % 16 < this._speed) {
//change direction
if(Crafty.randRange(0, 2) === 0) {
if(nextDirection === 'down' || nextDirection === 'up') {
if(PathTest.left()) { nextDirection = 'left' }
else if(PathTest.right()) { nextDirection = 'right' }
}else{
if(PathTest.up()) { nextDirection = 'up' }
else if(PathTest.down()) { nextDirection = 'down' }
}
}
if(bombWillHit() &&
!this._inShadow) {
this.trigger('Dropped');
}
}
}else{
this.snap();
nextDirection = this._directions[Crafty.randRange(0,3)];
if(nextDirection === this._move) {
nextDirection = "none"; //we need to think
}
}
this._move = nextDirection;
if(PathTest[this._move]()) {
if (this._move == "right") this.x += this._speed;
else if (this._move == "left") this.x -= this._speed;
else if (this._move == "up") this.y -= this._speed;
else if (this._move == "down") this.y += this._speed;
}
})
.onHit("ShadowBananaFire", function () {
this._inShadow = true;
}, function() {
this._inShadow = false;
});
return this;
}
});
Crafty.c('Ape', {
Ape: function() {
//setup animations
this.requires("SpriteAnimation, Collision")
.animate("walk_left", 6, 3, 8)
.animate("walk_right", 9, 3, 11)
.animate("walk_up", 3, 3, 5)
.animate("walk_down", 0, 3, 2)
//change direction when a direction change event is received
.bind("NewDirection",
function (direction) {
if (direction.x < 0) {
if (!this.isPlaying("walk_left"))
this.stop().animate("walk_left", 10, -1);
}
if (direction.x > 0) {
if (!this.isPlaying("walk_right"))
this.stop().animate("walk_right", 10, -1);
}
if (direction.y < 0) {
if (!this.isPlaying("walk_up"))
this.stop().animate("walk_up", 10, -1);
}
if (direction.y > 0) {
if (!this.isPlaying("walk_down"))
this.stop().animate("walk_down", 10, -1);
}
if(!direction.x && !direction.y) {
this.stop();
}
})
// A rudimentary way to prevent the user from passing solid areas
.bind('Moved', function(from) {
if(this.hit('solid')){
this.attr({x: from.x, y:from.y});
}
}).onHit("fire", function() {
this.destroy();
});
return this;
}
});
Crafty.c("RightControls", {
init: function() {
this.requires('Multiway');
},
rightControls: function(speed) {
this.multiway(speed, {UP_ARROW: -90, DOWN_ARROW: 90, RIGHT_ARROW: 0, LEFT_ARROW: 180})
return this;
}
});
Crafty.c("LeftControls", {
init: function() {
this.requires('Multiway');
},
leftControls: function(speed) {
this.multiway(speed, {W: -90, S: 90, D: 0, A: 180})
return this;
}
});
Crafty.scene("main", function () {
generateWorld();
//create our player entity with some premade components
var player1 = Crafty.e("2D, DOM, Ape, player, LeftControls, BombDropper")
.attr({ x: 16, y: 304, z: 1 })
.leftControls(1)
.Ape();
//create our player entity with some premade components
var player2 = Crafty.e("2D, DOM, Ape, player, RightControls, SpriteAnimation, Collision, BombDropper, Grid")
.attr({ x: 368, y: 16, z: 1 })
.rightControls(1)
.bombDropper(Crafty.keys.ENTER)
.Ape();
/*
//create our enemy entity with some premade components
var enemy = Crafty.e("2D, DOM, Ape, enemy, AIControls, SpriteAnimation, Collision, BombDropper, Grid")
.attr({ x: 368, y: 16, z: 2 })
.AIControls(1)
.Ape();
*/
});
};