-
Notifications
You must be signed in to change notification settings - Fork 0
/
man.pde
145 lines (135 loc) · 4.42 KB
/
man.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
class Man {
PVector loc;
int moveRequested; // 1-N, 2-E, 3-S, 4-W
int lastMove;
PVector direction;
float speed;
boolean moving;
boolean inTunnel;
boolean inBox; // only may be true for a ghost
boolean cornering;
int corneringTimer;
boolean isGhost;
void updateMove() {
if (moving) {
// check if change of direction is required
if (boolean(moveRequested)) {
if (moveRequested == 1 || moveRequested == 3) {
// NORTH or SOUTH - x val must be near integer
if ( abs(loc.x - int(loc.x)) < 2*speed && validMove(round(loc.x), round(loc.y), moveRequested).valid || inBox) {
loc.x = round(loc.x);
if (direction.x != 0 && isGhost) {
cornering = true;
corneringTimer = millis();
}
if (moveRequested == 1) {
// NORTH
direction.x = 0; direction.y = -1;
} else {
// SOUTH
direction.x = 0; direction.y = 1;
}
lastMove = moveRequested;
moveRequested = 0;
}
} else if (moveRequested == 2 || moveRequested == 4) {
// EAST or WEST - y val must be near integer
if ( abs(loc.y - int(loc.y)) < 2*speed && (validMove(round(loc.x), round(loc.y), moveRequested).valid || inBox)) {
loc.y = round(loc.y);
if (direction.y != 0 && isGhost) {
cornering = true;
corneringTimer = millis();
}
if (moveRequested == 2) {
// EAST
direction.x = 1; direction.y = 0;
} else {
// WEST
direction.x = -1; direction.y = 0;
}
lastMove = moveRequested;
moveRequested = 0;
}
}
}
// account for ghost cornering
float curSpeed=0;
if (isGhost) {
if (cornering) {
curSpeed = speed*.6;
} else {
curSpeed = speed;
}
if (cornering && millis() - corneringTimer > 1000.0*8.0/60.0) {
cornering = false;
}
} else {
curSpeed = speed;
}
// calculate next position
PVector nextLoc = new PVector(0,0);
nextLoc.x = loc.x + curSpeed*direction.x;
nextLoc.y = loc.y + curSpeed*direction.y;
// check if we're passing a tile midpoint in this timestep
if (abs(floor(nextLoc.x)-floor(loc.x)) > 0.5 || abs(floor(nextLoc.y)-floor(loc.y)) > 0.5) {
int nextMove;
if (nextLoc.y < loc.y) {
nextMove = 1; // NORTH
} else if (nextLoc.y > loc.y) {
nextMove = 3; // SOUTH
} else if (nextLoc.x > loc.x) {
nextMove = 2; // EAST
} else {
nextMove = 4; // WEST
}
MoveResult moveResult = validMove(round(nextLoc.x),round(nextLoc.y),nextMove);
boolean valid = moveResult.valid;
inTunnel = moveResult.tunnel;
if (valid || inBox) {
// continue moving
loc = nextLoc;
} else {
// stop
loc.x = round(loc.x);
loc.y = round(loc.y);
direction.set(0.0,0.0,0.0);
}
} else {
// NOT passing midpoint - continue moving
loc.x = loc.x + curSpeed*direction.x;
loc.y = loc.y + curSpeed*direction.y;
}
// check if its in the tunnel
if (inTunnel) {
if (loc.x > tunnel2.x+1) {
loc.x = tunnel1.x-1;
} else if (loc.x < tunnel1.x-1) {
loc.x = tunnel2.x+1;
}
}
// if (PVector.dist(loc,tunnel1) < speed*2 && lastMove == 4) {
// inTunnel = true;
// // left tunnel
// direction.set(-1.0,0.0,0.0);
// loc = PVector.add(loc, PVector.mult(direction,speed));
// if (loc.x <= -1.0 + speed*2) {
// // move to next tunnel
// loc.x = tunnel2.x;
// }
// }
// if (PVector.dist(loc,tunnel2) < speed*2 && lastMove == 2) {
// inTunnel = true;
// // right tunnel
// direction.set(1.0,0.0,0.0);
// loc = PVector.add(loc, PVector.mult(direction,speed));
// println("loc " + loc.x);
// println(tunnel2.x + 1 - speed*2);
// if (loc.x >= tunnel2.x+.5) {
// // move to next tunnel
// println("warped");
// loc.x = tunnel1.x;
// }
// }
} // end if moving
} // end void updateMove()
}