/** * 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; }