-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnemies.js
77 lines (68 loc) · 1.58 KB
/
Enemies.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
function Enemies()
{
this.list = [];
}
//create new enemy
//x -> x position of enemy, will default to just off screen
//y -> center y position for new enemy
//generator -> instance of generator class
Enemies.prototype.spawn = function(x, y, generator)
{
var e = generator.generateCharacter(100, true);
e.x = x;
e.y = y;
this.list.push(e);
}
//update all enemies
//deltaX -> world scroll amount for this step
//totalMS -> total program running time in milliseconds
//generator -> instance of generator class
Enemies.prototype.update = function(totalMS, generator)
{
var e;
for(var i = this.list.length-1; i >= 0; --i)
{
e = this.list[i];
if(e.x < -e.w)
{
this.list.splice(i, 1);
continue;
}
e.update(totalMS + e.seed, generator);
}
}
//draw all enemies
//context -> pointer to draw context
Enemies.prototype.render = function(context)
{
for(i in this.list)
{
this.list[i].render(context);
}
}
Enemies.prototype.checkProjectile = function(x, y)
{
var e;
for(var i = this.list.length-1; i >= 0; --i)
{
e = this.list[i];
if(e.x - e.w * 0.5 < x
&& e.x + e.w * 0.5 > x
&& e.y - e.h * 0.5 < y
&& e.y + e.h * 0.5 > y)
{
this.list.splice(i, 1);
return true;
}
}
return false;
}
//translate all enemies by given value
Enemies.prototype.translate = function(x, y)
{
for(i in this.list)
{
this.list[i].x += x;
this.list[i].y += y;
}
}