-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bullets.js
97 lines (82 loc) · 2.18 KB
/
Bullets.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
/**
* Created by JetBrains PhpStorm.
* User: attrib
* Date: 19.02.12
* Time: 11:43
*/
var bullets = {
items: [],
counter: 0,
map: false,
radius: 1.5,
speed: 1.5,
tick: function() {
if (this.items.length > 0) {
$(this.items).each(function(i, bullet) {
bullet.fly();
});
}
},
draw: function(context) {
if (this.items.length > 0) {
$(this.items).each(function(i, bullet) {
bullet.draw(context);
});
}
},
createBullet: function(x, y, tank) {
x += Math.cos(tank.getCanonRotation())*tanks.width;
y += Math.sin(tank.getCanonRotation())*tanks.width;
this.items.push(new Bullet(this.counter, x, y, tank));
this.counter++;
},
removeBullet: function(id) {
var index = -1;
$(this.items).each(function(i, bullet) {
if (bullet.getId() == id) {
index = i;
}
});
if (index >= 0) {
this.items.splice(index, 1);
}
},
setMap: function(oMap) {
this.map = oMap;
this.radius = oMap.getBlockWidth()/10;
this.speed = oMap.getBlockWidth()/10;
}
};
function Bullet(id, ox, oy, tank) {
var x = ox;
var y = oy;
this.draw = function(context) {
context.save();
context.setTransform(-1, 0, 0, 1, x, y);
context.beginPath();
context.arc(0, 0, bullets.radius, 0, 2*Math.PI, false);
context.fill();
context.restore();
};
this.fly = function() {
x += Math.cos(tank.getCanonRotation())*bullets.speed;
y += Math.sin(tank.getCanonRotation())*bullets.speed;
if (bullets.map.hitCheck(x, y, this)) {
bullets.removeBullet(id);
}
else {
var coords = bullets.map.jumpBorder(x, y);
x = coords[0];
y = coords[1];
var testTank = tanks.hitTank(x, y);
if (testTank != false) {
bullets.removeBullet(id);
testTank.respawn();
}
}
redraw();
};
this.getId = function() {
return id;
}
}