-
Notifications
You must be signed in to change notification settings - Fork 0
/
bomberdan.js
159 lines (145 loc) · 4 KB
/
bomberdan.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
var cxt;
var bombImage = new Image(); //making this global T_T
var explodeImage = new Image(); //and this too :(
var objects = [];
var tick = 0;
// shim layer with setTimeout fallback
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
})();
function BomberDan()
{
this.canvas = document.getElementById("canvas");
this.cxt = canvas.getContext("2d");
this.cxt.strokeStyle = "yellow";
this.bombImage = new Image();
this.explodeImage = new Image();
this.bombImage.src = "img/bomb.png";
this.explodeImage.src = "img/explosions.png";
this.objects = [];
this.keyboard = {};
this.map = new Map(this);
this.player = new Player(this, "img/Bomberman.gif");
this.lastUpdated = Date.now();
requestAnimFrame(this.draw.bind(this));
}
// Power will be used later, right now its hardcoded to 2
BomberDan.prototype.explode = function(x, y, power)
{
var mapIndex = this.map.getTileIndex(x, y);
var tx = mapIndex[1];
var ty = mapIndex[0];
this.map.tiles[ty][tx] = 0;
this.objects.push(new Explosion(tx*32, ty*32, this));
// Check to the left and "blow tiles up"
if(tx-1 >= 0)
{
this.map.tiles[ty][tx-1] = 0;
this.objects.push(new Explosion((tx-1)*32, ty*32, this));
if(tx-2 >=0)
{
this.map.tiles[ty][tx-2] = 0;
this.objects.push(new Explosion((tx-2)*32, ty*32, this));
}
}
// now to the right...
if(tx+1 <= this.map.tiles[0].length)
{
this.map.tiles[ty][tx+1] = 0;
this.objects.push(new Explosion((tx+1)*32, ty*32, this));
if(tx+2 <= this.map.tiles[0].length)
{
this.map.tiles[ty][tx+2] = 0;
this.objects.push(new Explosion((tx+2)*32, ty*32, this));
}
}
// above
if(ty-1 >= 0)
{
this.map.tiles[ty-1][tx] = 0;
this.objects.push(new Explosion(tx*32, (ty-1)*32, this));
if(ty-2 >=0)
{
this.map.tiles[ty-2][tx] = 0;
this.objects.push(new Explosion(tx*32, (ty-2)*32, this));
}
}
// below
if(ty+1 <= this.map.tiles.length-1)
{
this.map.tiles[ty+1][tx] = 0;
this.objects.push(new Explosion(tx*32, (ty+1)*32, this));
if(ty+2 <= this.map.tiles.length-1)
{
this.map.tiles [ty+2][tx] = 0;
this.objects.push(new Explosion(tx*32, (ty+2)*32, this));
}
}
}
BomberDan.prototype.update = function()
{
var dt = 0.001 * (Date.now() - this.lastUpdated);
this.map.update(dt);
this.player.update(dt);
for(var i = this.objects.length - 1; i >= 0; i--)
{
this.objects[i].update(dt);
if(this.objects[i].done)
{
this.objects.splice(i, 1);
}
}
this.lastUpdated = Date.now();
}
BomberDan.prototype.draw = function()
{
var dt = 0.001 * (Date.now() - this.lastUpdated);
this.cxt.clearRect(0,0,this.canvas.width,this.canvas.height);
this.map.update(dt);
this.player.update(dt);
for(var i = this.objects.length - 1; i >= 0; i--)
{
this.objects[i].update(dt);
if(this.objects[i].done)
{
this.objects.splice(i, 1);
}
}
this.lastUpdated = Date.now();
window.requestAnimFrame(this.draw.bind(this));
}
window.onload = function(){
var game = new BomberDan();
document.addEventListener("keydown", function(e) {
game.keyboard[e.keyCode] = true;
});
// attachEvent(document, "keydown", function(e) {
// game.keyboard[e.keyCode] = true;
// });
document.addEventListener("keyup", function(e) {
game.keyboard[e.keyCode] = false;
});
// attachEvent(document, "keyup", function(e) {
// game.keyboard[e.keyCode] = false;
// });
document.body.onmouseup = function(e)
{
console.log(game.map.getTileIndex(e.x, e.y));
console.log(game.objects);
};
}
//Input functions taken from http://hudson.joshy.org:9001/job/canvas-book/ws/out/chapter05.html
function attachEvent(node,name,func) {
if(node.addEventListener) {
node.addEventListener(name,func,false);
} else if(node.attachEvent) {
node.attachEvent(name,func);
}
};