-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enemy.pde
75 lines (63 loc) · 1.58 KB
/
Enemy.pde
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
/*
- damage attribute is visually shown by how many spikes an enemy spikes around it # spikes = 360 / damage and then dispaly them
- health could be border- DONE
- speed is size - DONE
- color is overall power - DONE
- spikes!!!
*/
class Enemy {
float x;
float y;
int size;
int speed;
boolean isFlagged;
int damage;
int power;
int health;
PShape shape;
int numberSpikes;
void init() {
shape = createShape();
shape.beginShape();
numberSpikes = damage + 2;
for (int i = 0; i < numberSpikes * 2; i++) {
shape.vertex(0, 0);
}
shape.endShape(CLOSE);
}
void update() {
if (isFlagged) {
return;
}
PVector v = getVelocity();
x += v.x;
y += v.y;
display();
if (isCollidingWithPlayer()) {
isFlagged = true;
player.takeDamage(damage);
}
}
PVector getVelocity() {
PVector velocity = (new PVector(player.x - x, player.y - y)).normalize();
velocity.x *= speed;
velocity.y *= speed;
return velocity;
}
boolean isCollidingWithPlayer() {
return dist(x, y, player.x, player.y) < (size + player.size) / 2;
}
void display() {
float angleBetweenSpikes = PI / numberSpikes;
for (int i = 0; i < numberSpikes * 2; i++) {
float angle = i * angleBetweenSpikes;
int scalar = (i % 2 == 0)? size: size / 2;
float shapeX = x + scalar * cos(angle);
float shapeY = y + scalar * sin(angle);
shape.setVertex(i, shapeX, shapeY);
}
shape.setFill(color(map(power, 1, 10, 0, 255), 0, 0));
shape.setStrokeWeight(health);
shape(shape);
}
}