-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enemy.pde
63 lines (56 loc) · 1.17 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
class Enemy extends GameObject
{
float x,y;
float w = 50;
float h = 50;
float initSpeed = 2;
float speed = initSpeed;
GameObject p;
float awareDistance = 500;
int countFrame = 30;
int hp = 100;
Enemy(float x, float y, float w, float h, int hp, GameObject p)
{
setX(x);
setY(y);
this.w = w;
this.h = h;
this.p = p;
}
void hit(int damage){
hp -= damage;
}
@Override
void move(){
float dist = getX() - p.getX();
if (dist <= awareDistance && dist > 0){
changeSpeed();
setX(getX() - speed);
} else if(dist >= -awareDistance && dist < 0) {
changeSpeed();
setX(getX() + speed);
}
}
void changeSpeed(){
if(countFrame >= 60){
countFrame = 0;
speed = (float)Math.random() * initSpeed;
}else{
countFrame++;
}
}
public float getAwareDistance(){
return awareDistance;
}
public void setAwareDistance(float awareDistance){
this.awareDistance = awareDistance;
}
@Override
void display(){
noFill();
noStroke();
//fill(255);
rect(getX(), getY(), 55, 55);
enemywalking.display(getX()-enemywalking.getWidth()+40, getY());
}
}