-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSmoothMover.java
More file actions
241 lines (216 loc) · 5.59 KB
/
Copy pathSmoothMover.java
File metadata and controls
241 lines (216 loc) · 5.59 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// (World, Actor, GreenfootImage, and Greenfoot)
import greenfoot.Actor;
/**
* A variation of an actor that maintains a precise location (using doubles for
* the co-ordinates instead of ints). This allows small precise movements (e.g.
* movements of 1 pixel or less) that do not lose precision.
*
* source: Mover/SmoothMover classes of Greenfoot scenarios
*
* @author Poul Henriksen
* @author Michael Kolling
* @author Neil Brown
*
* @version 3.0
*/
public abstract class SmoothMover extends Actor {
private double exactX = 0.0;
private double exactY = 0.0;
private Vector movement = new Vector();
public SmoothMover() {
super();
}
public SmoothMover(Vector speed) {
this.movement = speed;
}
/**
* move forward with the given speed
*/
public void move() {
move(1);
}
/**
* Move forward by the specified distance. (Overrides the method in Actor).
*/
@Override
public void move(int steps) {
double nextX = exactX + movement.getX() * steps;
double nextY = exactY + movement.getY() * steps;
if(nextX >= getWorld().getWidth() || nextX < 0) {
nextX = exactX;
}
if(nextY >= getWorld().getHeight() || nextY < 0) {
nextY = exactY;
}
setLocation(nextX, nextY);
}
public void setLocation(double x, double y) {
this.exactX = x;
this.exactY = y;
super.setLocation((int) x, (int) y);
}
public void setLocation(int x, int y) {
setLocation((double) x, (double) y);
}
/**
* Ändert die aktuelle Bewegung durch Hinzufügen eines neuen Vektors zu der
* bestehenden Bewegung.
*/
public void addForce(Vector force) {
movement.add(force);
}
/**
* Erhöht die Geschwindigkeit dieses SmoothMovers um den gegebenen Faktor.
* (Faktoren kleiner als 1 verlangsamen die Geschwindigkeit.) Die Richtung
* bleibt unverändert.
*/
public void accelerate(double factor) {
movement.scale(factor);
if(movement.getLength() < 0.15) {
movement.setNeutral();
}
}
/**
* Liefert die Geschwindigkeit dieses Akteurs zurück.
*/
public double getSpeed() {
return movement.getLength();
}
/**
* Setzt die Geschwindigkeit auf einen bestimmten Wert (s)
*
* @param s - Vector-Länge/Geschwindigkeit
* @author (your name)
*/
public void setSpeed(double s) {
movement.setLength(s);
}
/**
* Liefert die aktuelle Bewegung dieses Objekts (als Vektor) zurück.
*/
public Vector getMovement() {
return movement;
}
/**
* Gibt <code>true</code> zurück, wenn sich der Actor am Weltrand befindet.
*/
public boolean atWorldEdge() {
boolean atEdge = false;
if(getX() <= 0 || getX() >= getWorld().getWidth() - 1) {
atEdge = true;
} else if(getY() <= 0 || getY() >= getWorld().getHeight() - 1) {
atEdge = true;
}
return (atEdge);
}
/**
* Prüft, ob wir auf den Rand des Universums gestoßen sind. Wenn ja, wird
* abgeprallt.
*/
public void bounceAtEdge() {
if(getX() <= 0 || getX() >= getWorld().getWidth() - 1) {
setLocation((double) getX(), (double) getY());
getMovement().revertHorizontal();
} else if(getY() <= 0 || getY() >= getWorld().getHeight() - 1) {
setLocation((double) getX(), (double) getY());
getMovement().revertVertical();
}
}
/**
* Wenn der Weltrand erreicht ist soll sich der Actor nicht weiterbewegen.
*/
public void stopAtWorldEdge() {
if(atWorldEdge()) {
this.setSpeed(0.0);
if(getX() <= 0) {
setLocation(1.0, getExactY());
} else if(getX() >= getWorld().getWidth() - 1) {
setLocation(getWorld().getWidth() - 2, getExactY());
} else if(getY() <= 0) {
setLocation(getExactX(), 1.0);
} else if(getY() >= getWorld().getHeight() - 1) {
setLocation(getExactX(), getWorld().getHeight() - 2);
}
}
}
/**
* Stoppt die Bewegung, die ein Actor gerade ausführt.
*
* @author (Simon Häber)
*/
public void stop() {
this.movement.setNeutral();
}
/**
* Setzt die Richtung des Vectors auf eine eine Bestimmte Grad-Zahl Damit keine
* Fehler einstehen, schrängt die Funktion den Bereich auf [0..359] ein
*
* @param angle - Grad-Zahl in Grad
* @author (Simon Häber)
*/
public void setDirection(int angle) {
if(angle > 359) {
angle = angle - 360;
} else if(angle < 0) {
angle = angle + 360;
}
this.movement.setDirection(angle);
}
/**
* Berechnet Entfernung zwischen a und b; beides Double
*
* @author (Simon Häber)
*/
public static double calcDistance(double a, double b) {
return (b - a);
}
/**
* Berechnet Entfernung zwischen a und b; beides Integer
*
* @author (Simon Häber)
*/
public static int calcDistance(int a, int b) {
return (b - a);
}
/**
* Untermethode für makePetsBounce() und makeKidStop() - Invertiert die Bewegung
* an der jeweiligen Achse
*/
public void bounceVertical() {
setLocation((double) getX(), (double) getY());
getMovement().revertVertical();
}
/**
* Untermethode für makePetsBounce() und makeKidStop() - Invertiert die Bewegung
* an der jeweiligen Achse
*/
public void bounceHorizontal() {
setLocation((double) getX(), (double) getY());
getMovement().revertHorizontal();
}
/**
* Return the exact x-coordinate (as a double).
*/
public double getExactX() {
return exactX;
}
/**
* Return the exact y-coordinate (as a double).
*/
public double getExactY() {
return exactY;
}
@Override
public GameWorld getWorld() {
return (GameWorld) super.getWorld();
}
public boolean hasReachedDestination(int destinationX, int destinationY, double toleranceRange) {
double dx = calcDistance(getExactX(), destinationX);
double dy = calcDistance(getExactY(), destinationY);
double remainingDistance = Math.hypot(dx, dy);
if(remainingDistance <= toleranceRange) {
return true;
}
return false;
}
}