-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspawnableenemy.js
More file actions
90 lines (82 loc) · 3.11 KB
/
spawnableenemy.js
File metadata and controls
90 lines (82 loc) · 3.11 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const SpawnableEnemyState = {
Alive: 0,
Dying: 1,
Dead: 2
}
class SpawnableEnemy{
constructor(x, y, movePixelsPerMs, aliveSprites, deadSprites, collisionRect, animationSpeed, animationFrameChangedCallback, escapedCallback){
this.x = x;
this.y = y;
this.movePixelsPerMs = movePixelsPerMs;
this.previousUpdateTime = -1;
this.timeSpawned = -1;
this.timeOfDeath = -1;
this.state = SpawnableEnemyState.Alive;
this.aliveSprites = aliveSprites;
this.deadSprites = deadSprites;
this.animationFrame = 0;
this.collisionRect = collisionRect;
this.animationSpeed = animationSpeed;
this.animationFrameChangedCallback = animationFrameChangedCallback;
this.escapedCallback = escapedCallback;
}
die(){
this.state = SpawnableEnemyState.Dying;
this.timeOfDeath = -1;
this.animationFrame = 0;
score.add(200);
}
checkCollisions(arrowManager){
if (this.state == SpawnableEnemyState.Alive && arrowManager.collidesWith(this.x + this.collisionRect.left, this.x + this.collisionRect.right, this.y + this.collisionRect.top, this.y + this.collisionRect.bottom)){
let arrow = arrowManager.getCollidingArrow(this.x + this.collisionRect.left, this.x + this.collisionRect.right, this.y + this.collisionRect.top, this.y + this.collisionRect.bottom);
return arrow;
}
return null;
}
checkCollisionWithArcher(archer){
return archer.collidesWith(this.x + this.collisionRect.left, this.x + this.collisionRect.right, this.y + this.collisionRect.top, this.y + this.collisionRect.bottom);
}
update(time){
if (this.state == SpawnableEnemyState.Alive){
if (this.previousUpdateTime == -1){
this.previousUpdateTime = time;
}
if (this.timeSpawned == -1){
this.timeSpawned = time;
}
var timeSincePrevUpdateMs = time - this.previousUpdateTime;
this.previousUpdateTime = time;
this.x -= timeSincePrevUpdateMs * this.movePixelsPerMs.dx;
this.y -= timeSincePrevUpdateMs * this.movePixelsPerMs.dy;
if (this.x < -this.collisionRect.right) {
this.state = SpawnableEnemyState.Dead;
if (this.escapedCallback){
this.escapedCallback(this);
}
}
var prevAnimFrame = this.animationFrame;
this.animationFrame = Math.floor((time - this.timeSpawned)/this.animationSpeed) % this.aliveSprites.length;
if (this.animationFrame != prevAnimFrame && this.animationFrameChangedCallback){
this.animationFrameChangedCallback(this.animationFrame, this);
}
}
if (this.state == SpawnableEnemyState.Dying){
if (this.timeOfDeath == -1){
this.timeOfDeath = time;
}
var timeSinceDeath = time - this.timeOfDeath;
if (timeSinceDeath > 500){
this.state = SpawnableEnemyState.Dead;
}
this.animationFrame = Math.floor(timeSinceDeath/this.animationSpeed) % this.deadSprites.length;
}
}
draw(ctx){
if (this.animationFrame != 0 && this.state == SpawnableEnemyState.Dying){
console.log(this.animationFrame);
}
if (this.state == SpawnableEnemyState.Alive || this.state == SpawnableEnemyState.Dying){
ctx.drawImage(this.state == SpawnableEnemyState.Alive ? this.aliveSprites[this.animationFrame] : this.deadSprites[this.animationFrame], this.x, this.y);
}
}
}