|
| 1 | +package dev.zwazel.internal.debug; |
| 2 | + |
| 3 | +import dev.zwazel.internal.PublicGameWorld; |
| 4 | +import dev.zwazel.internal.game.map.MapDefinition; |
| 5 | +import dev.zwazel.internal.game.transform.Vec3; |
| 6 | +import dev.zwazel.internal.game.utils.Node; |
| 7 | +import lombok.Data; |
| 8 | +import lombok.EqualsAndHashCode; |
| 9 | + |
| 10 | +import javax.swing.*; |
| 11 | +import java.awt.*; |
| 12 | +import java.awt.event.KeyAdapter; |
| 13 | +import java.awt.event.KeyEvent; |
| 14 | +import java.util.LinkedList; |
| 15 | + |
| 16 | +@EqualsAndHashCode(callSuper = true) |
| 17 | +@Data |
| 18 | +public class MapVisualiser extends JPanel { |
| 19 | + private final PublicGameWorld world; |
| 20 | + private final int CELL_SIZE = 50; |
| 21 | + private DrawingMode drawingMode = DrawingMode.HEIGHT; |
| 22 | + private LinkedList<Node> path = new LinkedList<>(); |
| 23 | + |
| 24 | + public void showMap() { |
| 25 | + int width = ((int) world.getGameConfig().mapDefinition().width() + 1) * CELL_SIZE; |
| 26 | + int height = ((int) world.getGameConfig().mapDefinition().depth() + 1) * CELL_SIZE; |
| 27 | + |
| 28 | + JFrame frame = new JFrame("Map Visualiser"); |
| 29 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 30 | + frame.setSize(width, height); |
| 31 | + frame.getContentPane().add(this); |
| 32 | + frame.setLocationRelativeTo(null); |
| 33 | + frame.setVisible(true); |
| 34 | + |
| 35 | + // Add key listener to switch drawing modes |
| 36 | + frame.addKeyListener(new KeyAdapter() { |
| 37 | + @Override |
| 38 | + public void keyPressed(KeyEvent e) { |
| 39 | + if (e.getKeyCode() == KeyEvent.VK_SPACE) { |
| 40 | + toggleDrawingMode(); |
| 41 | + } |
| 42 | + } |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + // Drawing the map |
| 47 | + @Override |
| 48 | + public void paintComponent(Graphics g) { |
| 49 | + super.paintComponent(g); |
| 50 | + Graphics2D g2d = (Graphics2D) g; |
| 51 | + MapDefinition mapDefinition = world.getGameConfig().mapDefinition(); |
| 52 | + |
| 53 | + switch (drawingMode) { |
| 54 | + case HEIGHT -> drawHeightMap(g2d, mapDefinition); |
| 55 | + case PATH -> drawPath(g2d, mapDefinition); |
| 56 | + } |
| 57 | + |
| 58 | + // Draw cell borders |
| 59 | + drawCellBorders(g2d, mapDefinition); |
| 60 | + } |
| 61 | + |
| 62 | + private void drawHeightMap(Graphics2D g2d, MapDefinition mapDefinition) { |
| 63 | + // Determine min and max heights to normalize cell colors |
| 64 | + float min = Float.MAX_VALUE; |
| 65 | + float max = Float.MIN_VALUE; |
| 66 | + for (int x = 0; x < mapDefinition.width(); x++) { |
| 67 | + for (int y = 0; y < mapDefinition.depth(); y++) { |
| 68 | + float tileHeight = mapDefinition.tiles()[y][x]; |
| 69 | + if (tileHeight < min) { |
| 70 | + min = tileHeight; |
| 71 | + } |
| 72 | + if (tileHeight > max) { |
| 73 | + max = tileHeight; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // First Pass: Draw each cell with a color gradient based on its height. |
| 79 | + for (int x = 0; x < mapDefinition.width(); x++) { |
| 80 | + for (int y = 0; y < mapDefinition.depth(); y++) { |
| 81 | + float tileHeight = mapDefinition.tiles()[y][x]; |
| 82 | + float normalizedHeight = (tileHeight - min) / (max - min); |
| 83 | + // Draw cell |
| 84 | + Color cellColor = new Color(normalizedHeight, 1 - normalizedHeight, 0); |
| 85 | + g2d.setColor(cellColor); |
| 86 | + g2d.fillRect( |
| 87 | + x * CELL_SIZE, |
| 88 | + y * CELL_SIZE, |
| 89 | + CELL_SIZE, |
| 90 | + CELL_SIZE |
| 91 | + ); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // Second Pass: Draw tank |
| 96 | + drawTank(g2d, world); |
| 97 | + |
| 98 | + // Third Pass: Draw heights |
| 99 | + for (int x = 0; x < mapDefinition.width(); x++) { |
| 100 | + for (int y = 0; y < mapDefinition.depth(); y++) { |
| 101 | + float tileHeight = mapDefinition.tiles()[y][x]; |
| 102 | + g2d.setColor(Color.BLACK); |
| 103 | + g2d.drawString( |
| 104 | + String.format("%.2f", tileHeight), |
| 105 | + x * CELL_SIZE + 5, |
| 106 | + y * CELL_SIZE + 15 |
| 107 | + ); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private void drawPath(Graphics2D g2d, MapDefinition mapDefinition) { |
| 113 | + // First pass: Draw Tank |
| 114 | + drawTank(g2d, world); |
| 115 | + |
| 116 | + // Second pass: Draw start and end cells |
| 117 | + if (!path.isEmpty()) { |
| 118 | + Node startNode = path.getFirst(); |
| 119 | + Node endNode = path.getLast(); |
| 120 | + |
| 121 | + // Draw start cell in green |
| 122 | + g2d.setColor(Color.GREEN); |
| 123 | + g2d.fillRect( |
| 124 | + startNode.getX() * CELL_SIZE, |
| 125 | + startNode.getY() * CELL_SIZE, |
| 126 | + CELL_SIZE, |
| 127 | + CELL_SIZE |
| 128 | + ); |
| 129 | + |
| 130 | + // Draw end cell in red |
| 131 | + g2d.setColor(Color.RED); |
| 132 | + g2d.fillRect( |
| 133 | + endNode.getX() * CELL_SIZE, |
| 134 | + endNode.getY() * CELL_SIZE, |
| 135 | + CELL_SIZE, |
| 136 | + CELL_SIZE |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + // Third pass: Draw Path |
| 141 | + g2d.setColor(Color.RED); |
| 142 | + for (int i = 0; i < path.size() - 1; i++) { |
| 143 | + Node node1 = path.get(i); |
| 144 | + Node node2 = path.get(i + 1); |
| 145 | + g2d.drawLine( |
| 146 | + node1.getX() * CELL_SIZE + CELL_SIZE / 2, |
| 147 | + node1.getY() * CELL_SIZE + CELL_SIZE / 2, |
| 148 | + node2.getX() * CELL_SIZE + CELL_SIZE / 2, |
| 149 | + node2.getY() * CELL_SIZE + CELL_SIZE / 2 |
| 150 | + ); |
| 151 | + } |
| 152 | + |
| 153 | + // Fourth pass: Draw costs |
| 154 | + for (Node node : path) { |
| 155 | + g2d.setColor(Color.BLACK); |
| 156 | + g2d.drawString( |
| 157 | + String.format("%.2f", node.getCost()), |
| 158 | + node.getX() * CELL_SIZE + 5, |
| 159 | + node.getY() * CELL_SIZE + 15 |
| 160 | + ); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + private void drawCellBorders(Graphics2D g2d, MapDefinition mapDefinition) { |
| 165 | + for (int x = 0; x < mapDefinition.width(); x++) { |
| 166 | + for (int y = 0; y < mapDefinition.depth(); y++) { |
| 167 | + // Draw cell border |
| 168 | + g2d.setColor(Color.BLACK); |
| 169 | + g2d.drawRect( |
| 170 | + x * CELL_SIZE, |
| 171 | + y * CELL_SIZE, |
| 172 | + CELL_SIZE, |
| 173 | + CELL_SIZE |
| 174 | + ); |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + private void drawTank(Graphics2D g2d, PublicGameWorld world) { |
| 180 | + // Draw location of my tank (drawing the cell blue, and the actual position as a point) |
| 181 | + Vec3 myPosition = world.getMyState().transformBody().getTranslation(); |
| 182 | + Vec3 closestTile = world.getGameConfig().mapDefinition().getClosestTileFromWorld(myPosition); |
| 183 | + |
| 184 | + g2d.setColor(Color.BLUE); |
| 185 | + g2d.fillRect( |
| 186 | + (int) closestTile.getX() * CELL_SIZE, |
| 187 | + (int) closestTile.getZ() * CELL_SIZE, |
| 188 | + CELL_SIZE, |
| 189 | + CELL_SIZE |
| 190 | + ); |
| 191 | + |
| 192 | + // Turn the position from float to int, so it can be drawn. from units to pixels |
| 193 | + int x = (int) (myPosition.getX() * CELL_SIZE); |
| 194 | + int y = (int) (myPosition.getZ() * CELL_SIZE); |
| 195 | + |
| 196 | + g2d.setColor(Color.ORANGE); |
| 197 | + g2d.fillOval( |
| 198 | + x, |
| 199 | + y, |
| 200 | + 15, |
| 201 | + 15 |
| 202 | + ); |
| 203 | + } |
| 204 | + |
| 205 | + private void toggleDrawingMode() { |
| 206 | + DrawingMode[] modes = DrawingMode.values(); |
| 207 | + int currentIndex = drawingMode.ordinal(); |
| 208 | + int nextIndex = (currentIndex + 1) % modes.length; |
| 209 | + drawingMode = modes[nextIndex]; |
| 210 | + repaint(); |
| 211 | + } |
| 212 | + |
| 213 | + // Enum for switching drawing modes |
| 214 | + public enum DrawingMode { |
| 215 | + HEIGHT, |
| 216 | + PATH |
| 217 | + } |
| 218 | +} |
0 commit comments