public GridPanel(Client client, Snapshot snapshot) { setDoubleBuffered(true); setOpaque(false); setLayout(null); this.client = client; this.controlPanel = client.getControlPanel(); squareSize = INITIAL_SQUARE_SIZE; left = 0 - STARTING_GRID_SIZE / 2; right = 0 + STARTING_GRID_SIZE / 2; top = 0 - STARTING_GRID_SIZE / 2; bottom = 0 + STARTING_GRID_SIZE / 2; if (snapshot != null) { NodeList nl = snapshot.getTileElements(); for (int i = 0; i < nl.getLength(); i++) { Element el = (Element) nl.item(i); Position pos = XmlUtils.extractPosition(el); if (pos.x <= left) left = pos.x - 1; if (pos.x >= right) right = pos.x + 1; if (pos.y <= top) top = pos.y - 1; if (pos.y >= bottom) bottom = pos.y + 1; } } registerMouseListeners(); controlPanel.registerSwingComponents(this); }
@Override public void saveToSnapshot(Document doc, Element node) { for (Road tunnel : tunnels) { if (tunnel.getTile().getPosition() != null && tunnel.getTunnelEnd() != Road.OPEN_TUNNEL) { Element el = doc.createElement("tunnel"); node.appendChild(el); XmlUtils.injectPosition(el, tunnel.getTile().getPosition()); el.setAttribute("location", tunnel.getLocation().toString()); el.setAttribute("player", "" + (tunnel.getTunnelEnd() % 100)); el.setAttribute("b", tunnel.getTunnelEnd() > 100 ? "yes" : "no"); } } }
@Override public void loadFromSnapshot(Document doc, Element node) { NodeList nl = node.getElementsByTagName("tunnel"); for (int i = 0; i < nl.getLength(); i++) { Element el = (Element) nl.item(i); Position pos = XmlUtils.extractPosition(el); Location loc = Location.valueOf(el.getAttribute("location")); Road road = (Road) getBoard().get(pos).getFeature(loc); if (!road.isTunnelEnd()) { logger.error("Tunnel end does not exist."); continue; } Player player = game.getPlayer(Integer.parseInt(el.getAttribute("player"))); boolean isB = "yes".equals(el.getAttribute("b")); road.setTunnelEnd(getTunnelId(player, isB)); game.post(new TunnelPiecePlacedEvent(player, pos, loc, isB)); } }