This repository was archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnitMovement.pde
113 lines (95 loc) · 2.73 KB
/
UnitMovement.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
class UnitMovement implements Movement {
public static final int UP = 0;
public static final int RIGHT = 1;
public static final int DOWN = 2;
public static final int LEFT = 3;
private Unit unit;
protected int status = Movement.STILL;
protected float spd;
protected PVector dest;
protected int direction = DOWN;
private boolean destReached = false;
public UnitMovement(Unit unit) {
this.unit = unit;
this.spd = unit.getStats().spd;
this.dest = unit.getPosition();
}
public UnitMovement(Creep creep) {
this((Unit) creep);
}
public UnitMovement(Guard guard) {
this((Unit) guard);
}
public void update() {
if (destReached()) return;
float finalSpd;
if (unit.isFrostbitten()) {
finalSpd = spd * UnitHealth.FROSTBITE_SPD;
} else {
finalSpd = spd;
}
float dx = dest.x - unit.getX();
float dy = dest.y - unit.getY();
float dist = sqrt(dx * dx + dy * dy);
if (dist > finalSpd) {
float ratio = finalSpd / dist;
float xMove = ratio * dx;
float yMove = ratio * dy;
unit.setX(xMove + unit.getX());
unit.setY(yMove + unit.getY());
} else {
unit.setPosition(dest);
unit.getAnimator()
.setIdle();
destReached = true;
}
// IDEA: Berserk shaky movement mode (works for non distance checking movement though :/)
// if unit.getX() > dest.x is removed the unit moves in a shaky freaky way.
}
public void moveTo(PVector nextDest) {
if (!dest.equals(nextDest)) {
destReached = false;
dest = nextDest;
_setDirection();
}
}
private void _setDirection() {
int nextDirection;
int nextAnimation;
float dy = dest.y - unit.getY();
float dx = dest.x - unit.getX();
float angle = degrees(
atan2(dy, dx));
if (angle <= 45 && angle >= -45) {
nextDirection = RIGHT;
nextAnimation = UnitAnimator.HORIZONTAL_WALK_IDX;
} else if (angle < -45 && angle > -135) {
nextDirection = UP;
nextAnimation = UnitAnimator.UPWARDS_WALK_IDX;
} else if (angle < -135 || angle > 135) {
nextDirection = LEFT;
nextAnimation = UnitAnimator.HORIZONTAL_WALK_IDX;
} else {
// Between 135 & 45 is implied.
nextDirection = DOWN;
nextAnimation = UnitAnimator.DOWNWARDS_WALK_IDX;
}
if (directionChanged(nextDirection)) {
unit.getAnimator()
.setActiveAnimation(nextAnimation);
direction = nextDirection;
}
}
public int getDirection() {
return direction;
}
public boolean directionChanged(int newDirection) {
return direction != newDirection;
}
public boolean destReached() {
return destReached;
}
public void setSpd(float spd) {
this.spd = spd;
}
}