-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenemyChar.js
More file actions
73 lines (60 loc) · 1.63 KB
/
Copy pathenemyChar.js
File metadata and controls
73 lines (60 loc) · 1.63 KB
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
/**
* Created by Phillip McIntyre on 1/28/2015.
*/
function addEnemy (a, b, da, db, id){
var enemy = {
imgSrc: E1_PATH_CHAR,
width: E1_CHAR_WIDTH,
height: E1_CHAR_HEIGHT,
x: a,
y: b,
dx: da,
dy: db,
id: id,
alive: true,
passed: false
};
enemySprites.push(enemy);
}
function enemyInBounds(r) {
if (r.x >= 0 && r.x + E1_CHAR_WIDTH <= STAGE_WIDTH)
return true;
else
return false;
}
function updateEnemies () {
for(var i = 0; i < enemySprites.length; i++) {
var b = enemySprites[i];
if (enemyInBounds(b) && b.alive) {
b.alive = true;
if (!enemyInBounds(b) && (b.x + b.dx > STAGE_WIDTH || b.x - b.dx < 0))
b.dx = 2 * -b.dx;
b.y += b.dy;
b.x += b.dx;
b.dx = 3 * Math.sin(b.id * Math.PI/64);
b.id++;
if (b.y > STAGE_HEIGHT) {
b.passed = true;
b.alive = false;
numEnemiesPass++;
}
}
else {
b.alive = false;
}
}
}
function drawEnemies(ctx) {
for(var i = 0; i < enemySprites.length; i++){
var b = enemySprites[i];
if(b.alive && !b.passed){
var image = new Image();
image.src = b.imgSrc;
if(b.x + b.dx > STAGE_WIDTH)
b.x = 0;
else if (b.x - b.dx < 0)
b.x = STAGE_WIDTH - E1_CHAR_WIDTH;
ctx.drawImage(image, 0, 0, E1_CHAR_WIDTH, E1_CHAR_HEIGHT, b.x + b.dx, b.y + b.dy, E1_CHAR_WIDTH, E1_CHAR_HEIGHT);
}
}
}