-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enemy.java
96 lines (85 loc) · 2.44 KB
/
Enemy.java
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
91
92
93
94
95
96
import java.awt.Color;
public class Enemy extends Entity {
private int damage = 0;
private double direction = Direction.getRandomDirection();
private int speed;
private int health;
public Enemy() {
this(getRandomValidStartingCoordinate());
}
public Enemy(EntityCoordinate position) {
this(position, 1);
}
public Enemy(EntityCoordinate position, int speed) {
this(position, speed, 1);
}
public Enemy(EntityCoordinate position, int speed, int health) {
super(position, 15, Color.RED);
this.speed = speed;
this.health = health;
}
public void damage() {
damage(1);
}
public void damage(int damage) {
health -= damage;
if (health <= 0)
kill();
}
public void tick() {
double moveDistance = ((double) speed) * 0.01;
if (approachingJunction()) {
snapToCoordinate();
if (getY() > 4)
direction = Direction.SOUTH;
else if (getY() < 4)
direction = Direction.NORTH;
else if (getX() < 5)
direction = Direction.EAST;
else if (getX() > 5)
direction = Direction.WEST;
else
direction = Direction.SOUTH;
setVelocityVector(new VelocityVector(moveDistance, direction));
}
move();
}
public int getHealth() {
return health;
}
public boolean approachingJunction() {
double error = 0.05;
if (direction == Direction.NORTH && Math.abs(getY() - Math.ceil(getY())) < error)
return true;
if (direction == Direction.SOUTH && Math.abs(getY() - Math.floor(getY())) < error)
return true;
if (direction == Direction.EAST && Math.abs(getX() - Math.ceil(getX())) < error)
return true;
if (direction == Direction.WEST && Math.abs(getX() - Math.floor(getX())) < error)
return true;
return false;
}
public static EntityCoordinate getRandomValidStartingCoordinate() {
EntityCoordinate coord;
double side = Direction.getRandomDirection();
if (side == Direction.NORTH) {
coord = new EntityCoordinate(Math.floor(Math.random() * Map.COLUMNS), 0);
} else if (side == Direction.SOUTH) {
coord = new EntityCoordinate(Math.floor(Math.random() * Map.COLUMNS), Map.ROWS);
} else if (side == Direction.WEST) {
coord = new EntityCoordinate(0, Math.floor(Math.random() * Map.ROWS));
} else {
coord = new EntityCoordinate(Map.COLUMNS, Math.floor(Math.random() * Map.ROWS));
}
return coord;
}
@Override
public int interactWith(Entity entity) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getSize() {
return super.getSize() + 3 * (health - 1);
}
}