-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenemy.js
More file actions
60 lines (50 loc) · 1.63 KB
/
enemy.js
File metadata and controls
60 lines (50 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
class Enemy {
constructor(x, y) {
this.x = x;
this.y = y;
this.width = 30;
this.height = 40;
this.speed = 3;
this.health = 50;
this.damage = 10;
this.direction = 0;
this.attackRange = 50;
this.attackCooldown = 0;
}
update(player) {
// Calculate direction to player
this.direction = Utils.angle(this.x, this.y, player.x, player.y);
// Move towards player
const distance = Utils.distance(this.x, this.y, player.x, player.y);
if (distance > this.attackRange) {
this.x += Math.cos(this.direction) * this.speed;
this.y += Math.sin(this.direction) * this.speed;
}
// Attack player if in range
if (distance <= this.attackRange && this.attackCooldown <= 0) {
player.takeDamage(this.damage);
this.attackCooldown = 60; // 1 second at 60 FPS
}
if (this.attackCooldown > 0) {
this.attackCooldown--;
}
}
draw(ctx) {
ctx.save();
ctx.translate(this.x, this.y);
ctx.rotate(this.direction);
// Body
ctx.fillStyle = '#e74c3c';
ctx.fillRect(-this.width/2, -this.height/2, this.width, this.height);
// Head
ctx.fillStyle = '#c0392b';
ctx.beginPath();
ctx.arc(0, -this.height/2 - 10, 10, 0, Math.PI * 2);
ctx.fill();
ctx.restore();
}
takeDamage(amount) {
this.health -= amount;
return this.health <= 0;
}
}