-
Notifications
You must be signed in to change notification settings - Fork 0
/
bomb.js
99 lines (87 loc) · 2.65 KB
/
bomb.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
// Bomb.js
// Contains BOTH Bomb + Explosions
function Bomb(x, y, game)
{
this.width = 14;
this.height = 23;
this.x = x;
this.y = y;
// Seperate X, Y to draw it centered on the tile.
this.draw_x = x - this.width / 2 + 16;
this.draw_y = y + this.height / 2 - 16;
this.lifetime = 3;
this.currentFrame = 0;
this.animationOffsets = [0, 16, 32],
this.timePassed = 0;
this.power = 2;
this.done = false;
this.image = game.bombImage;
this.game = game;
this.cxt = game.cxt;
}
Bomb.prototype.update = function(dt)
{
this.timePassed += dt;
if(this.lifetime <= 0)
{
this.done = true;
this.game.explode(this.x, this.y, this.power);
}
if(this.timePassed >= 500) // miliseconds, this number * lifetime = total time to blow up
{
this.timePassed = 0;
this.lifetime--;
this.currentFrame++;
}
this.cxt.drawImage(this.image,
this.animationOffsets[this.currentFrame], 0, this.width, this.height,
this.draw_x, this.draw_y, this.width, this.height);
var tilePos = this.game.map.getTileIndex(this.x, this.y);
this.cxt.strokeStyle = "red";
this.cxt.strokeRect(tilePos[1] * 32, tilePos[0]* 32, 32, 32);
}
function Explosion(x, y, game)
{
this.x = x;
this.y = y;
this.currentFrame = 0;
this.animationOffsets = [0, 34, 63, 98],
this.width = 26,
this.height = 27,
this.animateTimer = 0;
this.power = 2; //could be used for damage ?
this.done = false;
this.game = game;
this.cxt = game.cxt;
this.map = this.game.map;
this.image = game.explodeImage;
}
Explosion.prototype.update = function(dt)
{
this.animateTimer += dt;
if(this.currentFrame==0)
{
var explosion_tile_index = this.map.getTileIndex(this.x, this.y);
var player_position = this.game.player.getPosition();
var player_tile_index = this.map.getTileIndex(player_position.x, player_position.y);
console.log(explosion_tile_index, player_tile_index);
if(explosion_tile_index[0]==player_tile_index[0] && explosion_tile_index[1]==player_tile_index[1])
{
this.game.player.ticks, this.game.player.currentFrame = 0;
this.game.player.alive = false;
}
}
if(this.animateTimer>=200)
{
if(this.currentFrame ==this.animationOffsets.length)
this.done = true;
else
{
this.animateTimer = 0;
this.currentFrame++;
}
}
this.cxt.drawImage(this.image,
this.animationOffsets[this.currentFrame], 0, this.width, this.height,
this.x, this.y, this.width, this.height);
}