/** * Updates the game state to the next tick. First applies the messages past in as arguments, then * updates the nodes, then the lanes, then checks for a winning player. * * @param messages the messages to apply for this game state tick */ public void update(Message[] messages) { this.validateLock(); for (Message m : messages) m.apply(this); this.validateLock(); for (Node n : map.getNodes()) n.update(); this.validateLock(); for (Lane l : map.getLanes()) l.update(); this.validateLock(); double energyToAdd = ENERGY_INCREMENT_RATE * this.getLastLoopTime(); for (Player p : players.values()) { p.setPlayerEnergy(p.getPlayerEnergy() + energyToAdd); if (p.getPlayerEnergy() > MAX_PLAYER_ENERGY) p.setPlayerEnergy(MAX_PLAYER_ENERGY); } timerTick++; lastLoopTime = this.getTime() - timeAtEndOfLastLoop; timeAtEndOfLastLoop = this.getTime(); this.validateLock(); // check for win Node n1 = map.getNodes()[0]; for (Node n : map.getNodes()) if (n1.getOwner() == null || !n1.getOwner().equals(n.getOwner())) return; winningPlayer = n1.getOwner(); }
/** @return all the units in the game state */ public List<Unit> getUnits() { List<Unit> units = new ArrayList<Unit>(); Lane[] lanes = map.getLanes(); for (Lane l : lanes) { Wave[] waves = l.getWaves(); for (Wave w : waves) { Unit[] us = w.getUnits(); for (Unit u : us) { units.add(u); } } } Collections.sort( units, new Comparator<Unit>() { @Override public int compare(Unit o1, Unit o2) { if (o1.getCollisionStrategy() instanceof Ground && o2.getCollisionStrategy() instanceof Flying) return -1; else if (o2.getCollisionStrategy() instanceof Ground && o1.getCollisionStrategy() instanceof Flying) return 1; else return 0; } }); return units; }
/** @return all the projectiles in the game state */ public List<Projectile> getProjectiles() { List<Projectile> projectiles = new ArrayList<Projectile>(); Lane[] lanes = map.getLanes(); for (Lane l : lanes) { Projectile[] ps = l.getProjectiles(); for (Projectile p : ps) projectiles.add(p); } return projectiles; }
/** @return all the buildings in the game state */ public List<Building> getBuildings() { List<Building> buildings = new ArrayList<Building>(); Node[] nodes = map.getNodes(); for (Node n : nodes) { Building[] bs = n.getContainedBuildings(); for (Building b : bs) buildings.add(b); } return buildings; }
public GameState(MapConfiguration mapConfig, List<PlayerData> players) { map = mapConfig.createMap(this); this.players = new HashMap<Integer, Player>(); timerTick = 0; this.races = new ArrayList<Race>(); for (int i = 0; i < players.size(); i++) { Race r = null; try { r = (Race) Configuration.copyConfiguration(players.get(i).getRace()); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } if (r == null) throw new RuntimeException("Error copying race"); this.races.add(r); Node[] startNodes = {map.getStartNode(players.get(i).getStartingSlot() - 1)}; Player p = new Player(this, startNodes, r, players.get(i).getName(), i); this.players.put(i, p); } // TODO this dummy player is for debugging purposes Race r = null; int i = this.players.size(); try { r = (Race) Configuration.copyConfiguration(players.get(0).getRace()); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } if (r == null) throw new RuntimeException("Error copying race"); this.races.add(r); Node[] nodes = this.getMap().getNodes(); List<Node> dummyStartNodes = new ArrayList<Node>(); for (Node n : nodes) if (n.getOwner() == null) dummyStartNodes.add(n); Player dummyPlayer = new Player(this, dummyStartNodes.toArray(new Node[0]), r, "dummy PLayer", i); this.players.put(i, dummyPlayer); }
/** @return all the command centers in the game state */ public List<Building> getCommandCenters() { ArrayList<Building> ccs = new ArrayList<Building>(); Node[] nodes = map.getNodes(); for (Node n : nodes) ccs.add(n.getCommandCenter()); return ccs; }
/** @return the dimensions of the map */ public Position getMapSize() { return map.getDimensions(); }