-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocalTestRendererListener.java
286 lines (224 loc) · 9.64 KB
/
LocalTestRendererListener.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import java.awt.*;
import java.lang.String;
import static java.lang.StrictMath.*;
import model.*;
import static java.lang.StrictMath.*;
public final class LocalTestRendererListener {
private Graphics graphics;
private World world;
private Game game;
private int canvasWidth;
private int canvasHeight;
private double left;
private double top;
private double width;
private double height;
private static final int FONT_SIZE_BIG = 42;
private static final int FONT_SIZE_SMALL = 18;
private static int currentWPId = -1;
private static long myId = -1;
public void beforeDrawScene(Graphics graphics, World world, Game game, int canvasWidth, int canvasHeight,
double left, double top, double width, double height) {
updateFields(graphics, world, game, canvasWidth, canvasHeight, left, top, width, height);
double trackTileSize = game.getTrackTileSize();
Point2I nextWP = new Point2I();
int nextWPId = 1;
double nOffset = 60.0D;
if (myId == -1) {
for (Player player : world.getPlayers()) {
if (player.getName().equals("MyStrategy")) {
myId = player.getId();
}
}
}
for (int[] waypoint : world.getWaypoints()) {
double x = waypoint[0] * trackTileSize + 100.0D;
double y = waypoint[1] * trackTileSize + 100.0D;
graphics.setColor(new Color(252, 255, 127));
fillRect(x, y, trackTileSize - 200.0D, trackTileSize - 200.0D);
}
for (Car car : world.getCars()) {
if (car.getPlayerId() == myId) {
nextWP.setX(car.getNextWaypointX());
nextWP.setY(car.getNextWaypointY());
double x = nextWP.getX() * trackTileSize + 100.0D;
double y = nextWP.getY() * trackTileSize + 100.0D;
graphics.setColor(new Color(75, 255, 63));
fillRect(x, y, trackTileSize - 200.0D, trackTileSize - 200.0D);
}
}
for (int[] waypoint : world.getWaypoints()) {
if (world.getMapName().equals("map01") && currentWPId > 6 && nextWP.getX() == 3 && nextWP.getY() == 4) {
currentWPId = 11;
break;
}
if (nextWP.getX() == waypoint[0] && nextWP.getY() == waypoint[1]) {
currentWPId = nextWPId - 1;
break;
}
nextWPId = nextWPId + 1;
}
nextWPId = 1;
graphics.setColor(Color.BLACK);
for (int[] waypoint : world.getWaypoints()) {
double x = waypoint[0] * trackTileSize + 320.0D;
double y = waypoint[1] * trackTileSize + 490.0D;
if (nextWPId >= 10) {
x = x - nOffset;
}
if (world.getMapName().equals("map01")) {
if (nextWPId == 5 && (currentWPId >= 0 && currentWPId < 6)) {
drawString(nextWPId + "", FONT_SIZE_BIG, x, y);
drawString("12", FONT_SIZE_SMALL, x + 220.0D, y + 200.0D);
} else if (nextWPId == 12 && currentWPId >= 6) {
drawString(nextWPId + "", FONT_SIZE_BIG, x, y);
drawString("5", FONT_SIZE_SMALL, x - 170.0D, y + 200.0D);
} else if (nextWPId != 5 && nextWPId != 12) {
drawString(nextWPId + "", FONT_SIZE_BIG, x, y);
}
} else {
drawString(nextWPId + "", FONT_SIZE_BIG, x, y);
}
nextWPId = nextWPId + 1;
}
graphics.setColor(Color.BLACK);
}
public void afterDrawScene(Graphics graphics, World world, Game game, int canvasWidth, int canvasHeight,
double left, double top, double width, double height) {
updateFields(graphics, world, game, canvasWidth, canvasHeight, left, top, width, height);
for (Car car : world.getCars()) {
if (car.getPlayerId() == myId) {
graphics.setColor(new Color(255, 255, 255));
double speedModule = hypot(car.getSpeedX(), car.getSpeedY());
fillRect(car.getX() - 50.0D, car.getY() - 50.0D, 200.0D, 100.0D);
graphics.setColor(Color.BLACK);
drawString(String.format("%2.0f", speedModule)+ "", FONT_SIZE_SMALL, car.getX() - 40.0D, car.getY() + 50.0D);
}
}
graphics.setColor(Color.BLACK);
}
private void updateFields(Graphics graphics, World world, Game game, int canvasWidth, int canvasHeight,
double left, double top, double width, double height) {
this.graphics = graphics;
this.world = world;
this.game = game;
this.canvasWidth = canvasWidth;
this.canvasHeight = canvasHeight;
this.left = left;
this.top = top;
this.width = width;
this.height = height;
}
private void drawString(String text, int fontSize, double x1, double y1) {
Point2I stringBegin = toCanvasPosition(x1, y1);
graphics.setFont(new Font("Serif", Font.PLAIN, fontSize));
graphics.drawString(text, stringBegin.getX(), stringBegin.getY());
}
private void drawLine(double x1, double y1, double x2, double y2) {
Point2I lineBegin = toCanvasPosition(x1, y1);
Point2I lineEnd = toCanvasPosition(x2, y2);
graphics.drawLine(lineBegin.getX(), lineBegin.getY(), lineEnd.getX(), lineEnd.getY());
}
private void fillCircle(double centerX, double centerY, double radius) {
Point2I topLeft = toCanvasPosition(centerX - radius, centerY - radius);
Point2I size = toCanvasOffset(2.0D * radius, 2.0D * radius);
graphics.fillOval(topLeft.getX(), topLeft.getY(), size.getX(), size.getY());
}
private void drawCircle(double centerX, double centerY, double radius) {
Point2I topLeft = toCanvasPosition(centerX - radius, centerY - radius);
Point2I size = toCanvasOffset(2.0D * radius, 2.0D * radius);
graphics.drawOval(topLeft.getX(), topLeft.getY(), size.getX(), size.getY());
}
private void fillArc(double centerX, double centerY, double radius, int startAngle, int arcAngle) {
Point2I topLeft = toCanvasPosition(centerX - radius, centerY - radius);
Point2I size = toCanvasOffset(2.0D * radius, 2.0D * radius);
graphics.fillArc(topLeft.getX(), topLeft.getY(), size.getX(), size.getY(), startAngle, arcAngle);
}
private void drawArc(double centerX, double centerY, double radius, int startAngle, int arcAngle) {
Point2I topLeft = toCanvasPosition(centerX - radius, centerY - radius);
Point2I size = toCanvasOffset(2.0D * radius, 2.0D * radius);
graphics.drawArc(topLeft.getX(), topLeft.getY(), size.getX(), size.getY(), startAngle, arcAngle);
}
private void fillRect(double left, double top, double width, double height) {
Point2I topLeft = toCanvasPosition(left, top);
Point2I size = toCanvasOffset(width, height);
graphics.fillRect(topLeft.getX(), topLeft.getY(), size.getX(), size.getY());
}
private void drawRect(double left, double top, double width, double height) {
Point2I topLeft = toCanvasPosition(left, top);
Point2I size = toCanvasOffset(width, height);
graphics.drawRect(topLeft.getX(), topLeft.getY(), size.getX(), size.getY());
}
private void drawPolygon(Point2D... points) {
int pointCount = points.length;
for (int pointIndex = 1; pointIndex < pointCount; ++pointIndex) {
Point2D pointA = points[pointIndex];
Point2D pointB = points[pointIndex - 1];
drawLine(pointA.getX(), pointA.getY(), pointB.getX(), pointB.getY());
}
Point2D pointA = points[0];
Point2D pointB = points[pointCount - 1];
drawLine(pointA.getX(), pointA.getY(), pointB.getX(), pointB.getY());
}
private Point2I toCanvasOffset(double x, double y) {
return new Point2I(x * canvasWidth / width, y * canvasHeight / height);
}
private Point2I toCanvasPosition(double x, double y) {
return new Point2I((x - left) * canvasWidth / width, (y - top) * canvasHeight / height);
}
private static final class Point2I {
private int x;
private int y;
private Point2I(double x, double y) {
this.x = toInt(round(x));
this.y = toInt(round(y));
}
private Point2I(int x, int y) {
this.x = x;
this.y = y;
}
private Point2I() {
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
private static int toInt(double value) {
@SuppressWarnings("NumericCastThatLosesPrecision") int intValue = (int) value;
if (abs((double) intValue - value) < 1.0D) {
return intValue;
}
throw new IllegalArgumentException("Can't convert double " + value + " to int.");
}
}
private static final class Point2D {
private double x;
private double y;
private Point2D(double x, double y) {
this.x = x;
this.y = y;
}
private Point2D() {
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
}
}