Last active
November 15, 2015 15:59
-
-
Save kvld/7aba6ed939d17710908f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.awt.*; | |
| import model.*; | |
| import static java.lang.StrictMath.*; | |
| import java.util.*; | |
| import java.text.DecimalFormat; | |
| import java.util.Scanner; | |
| import java.io.PrintWriter; | |
| import java.io.File; | |
| import java.io.BufferedReader; | |
| import java.io.FileReader; | |
| 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 LinkedList<Point2I> carPos = new LinkedList<Point2I>(); | |
| private HashMap<Integer, Point2D> nextPoint = new HashMap<Integer, Point2D>(); | |
| private final Integer PLAYER_ID = 1; | |
| private final String TAG = "LocalTestRendererListener"; | |
| private Color N_WAYPOINT_COLOR = new Color(209, 209, 209, 80); | |
| private void printDebug(Object a) { | |
| System.out.println("[" + TAG + "] " + a); | |
| } | |
| private int getTileCode(TileType tile) { | |
| switch (tile) { | |
| case EMPTY: | |
| return 0; | |
| case VERTICAL: | |
| return 1; | |
| case HORIZONTAL: | |
| return 2; | |
| case LEFT_TOP_CORNER: | |
| return 3; | |
| case RIGHT_TOP_CORNER: | |
| return 4; | |
| case LEFT_BOTTOM_CORNER: | |
| return 5; | |
| case RIGHT_BOTTOM_CORNER: | |
| return 6; | |
| case LEFT_HEADED_T: | |
| return 7; | |
| case RIGHT_HEADED_T: | |
| return 8; | |
| case TOP_HEADED_T: | |
| return 9; | |
| case BOTTOM_HEADED_T: | |
| return 10; | |
| case CROSSROADS: | |
| return 11; | |
| } | |
| return -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); | |
| for (Car car : world.getCars()) { | |
| if (car.getPlayerId() == PLAYER_ID) { | |
| if (carPos.size() < 400) { | |
| carPos.add(new Point2I(car.getX(), car.getY())); | |
| } else { | |
| carPos.removeFirst(); | |
| carPos.add(new Point2I(car.getX(), car.getY())); | |
| } | |
| } | |
| } | |
| for (Car car : world.getCars()) { | |
| if (car.getPlayerId() == PLAYER_ID) { | |
| graphics.setColor(N_WAYPOINT_COLOR); | |
| fillRect(car.getNextWaypointX() * game.getTrackTileSize(), | |
| car.getNextWaypointY() * game.getTrackTileSize(), | |
| game.getTrackTileSize(), | |
| game.getTrackTileSize()); | |
| } | |
| } | |
| 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 (Point2I entry : carPos) { | |
| graphics.setColor(Color.RED); | |
| fillCircle(entry.getX(), entry.getY(), world.getCars()[0].getHeight() / 20.0D); | |
| } | |
| int[] map01X = new int[]{0, 0, 0, 2, 3, 6, 7, 7, 7, 4, 3, 3, 3, 3, 1, 0}; | |
| int[] map01Y = new int[]{6, 5, 4, 4, 4, 4, 4, 1, 0, 0, 0, 4, 6, 7, 7, 7}; | |
| graphics.setColor(Color.BLUE); | |
| int i = 0, j = 0; | |
| for (TileType[] f: world.getTilesXY()) { | |
| j = 0; | |
| for (TileType tile: f) { | |
| if (!(world.getMapName().equals("default") || world.getMapName().equals("map01") || world.getMapName().equals("map02") || world.getMapName().equals("map03"))) { | |
| break; | |
| } | |
| graphics.setColor(Color.BLUE); | |
| graphics.setFont(new Font("Verdana", Font.PLAIN, 10)); | |
| drawString(getTileCode(tile) + "", i * 25 + 5, canvasHeight - (world.getTilesXY().length - j - 1) * 25 - 12); | |
| j++; | |
| } | |
| i++; | |
| } | |
| graphics.setColor(Color.BLACK); | |
| graphics.setFont(new Font("Verdana", Font.PLAIN, 10)); | |
| for (Car car : world.getCars()) { | |
| if (car.getPlayerId() == PLAYER_ID) { | |
| drawString("engine: " + String.format("%.4f", car.getEnginePower()), canvasWidth - 100, canvasHeight - 90); | |
| drawString("wheel: " + String.format("%.4f", car.getWheelTurn()), canvasWidth - 100, canvasHeight - 70); | |
| drawString("speed: " + String.format("%.4f", hypot(car.getSpeedX(), car.getSpeedY())), canvasWidth - 100, canvasHeight - 50); | |
| } | |
| } | |
| try { | |
| BufferedReader br = new BufferedReader(new FileReader("way.txt")); | |
| int t = 0; | |
| double xx = 0, yy = 0; | |
| String sCurrentLine = ""; | |
| while ((sCurrentLine = br.readLine()) != null) { | |
| try { | |
| String[] parts = sCurrentLine.split(" "); | |
| t = Integer.parseInt(parts[0]); | |
| xx = Double.parseDouble(parts[1]); | |
| yy = Double.parseDouble(parts[2]); | |
| nextPoint.put(t, new Point2D(xx, yy)); | |
| } catch (Exception e) { | |
| break; | |
| } | |
| } | |
| Point2D d = nextPoint.get(world.getTick()); | |
| if (d != null) { | |
| graphics.setColor(Color.BLUE); | |
| fillCircle(d.x, d.y, 20); | |
| } | |
| } catch (Exception e) { | |
| } | |
| } | |
| 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 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 void drawString(String str, int x, int y) { | |
| graphics.drawString(str, x, y); | |
| } | |
| 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; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment