/** * C++ callback function. * * <p>Notifies the event listener that a game has started. */ public void gameStarted() { try { // get the players self = null; allies.clear(); allyIDs.clear(); enemies.clear(); enemyIDs.clear(); players.clear(); int[] playerData = getPlayersData(); for (int index = 0; index < playerData.length; index += Player.numAttributes) { String name = new String(getPlayerName(playerData[index])); Player player = new Player(playerData, index, name); players.put(player.getID(), player); if (player.isSelf()) { self = player; } else if (player.isAlly()) { allies.add(player); allyIDs.add(player.getID()); } else if (player.isEnemy()) { enemies.add(player); enemyIDs.add(player.getID()); } else if (player.isNeutral()) { neutralPlayer = player; } } // get unit data units.clear(); playerUnits.clear(); alliedUnits.clear(); enemyUnits.clear(); neutralUnits.clear(); int[] unitData = getAllUnitsData(); for (int index = 0; index < unitData.length; index += Unit.numAttributes) { int id = unitData[index]; Unit unit = new Unit(id); unit.update(unitData, index); units.put(id, unit); if (self != null && unit.getPlayerID() == self.getID()) { playerUnits.add(unit); } else if (allyIDs.contains(unit.getPlayerID())) { alliedUnits.add(unit); } else if (enemyIDs.contains(unit.getPlayerID())) { enemyUnits.add(unit); } else { neutralUnits.add(unit); } } listener.gameStarted(); } catch (Error e) { e.printStackTrace(); } }
public List<Unit> getUnits(Player p) { List<Unit> pUnits = new ArrayList<Unit>(); for (Unit u : units.values()) { if (u.getPlayerID() == p.getID()) { pUnits.add(u); } } return pUnits; }
/** * C++ callback function. * * <p>Notifies the event listener that a game update occurred. */ private void gameUpdate() { try { // update game state gameFrame = getFrame(); if (!isReplay()) { self.update(getPlayerUpdate(self.getID())); self.updateResearch(getResearchStatus(self.getID()), getUpgradeStatus(self.getID())); } else { for (Integer playerID : players.keySet()) { players.get(playerID).update(getPlayerUpdate(playerID)); players .get(playerID) .updateResearch(getResearchStatus(playerID), getUpgradeStatus(playerID)); } } // update units int[] unitData = getAllUnitsData(); int[] bulletData = getAllBulletsData(); HashSet<Integer> deadUnits = new HashSet<Integer>(units.keySet()); ArrayList<Unit> playerList = new ArrayList<Unit>(); ArrayList<Unit> alliedList = new ArrayList<Unit>(); ArrayList<Unit> enemyList = new ArrayList<Unit>(); ArrayList<Unit> neutralList = new ArrayList<Unit>(); for (int index = 0; index < bulletData.length; index += Bullet.numAttributes) { int id = bulletData[index]; Bullet bullet = bullets.get(id); if (bullet == null) { bullet = new Bullet(id); bullets.put(id, bullet); } bullet.update(bulletData, index); } for (int index = 0; index < unitData.length; index += Unit.numAttributes) { int id = unitData[index]; // bugfix - unit list was emptying itself every second frame deadUnits.remove(id); Unit unit = units.get(id); if (unit == null) { unit = new Unit(id); units.put(id, unit); } unit.update(unitData, index); if (self != null) { if (unit.getPlayerID() == self.getID()) { playerList.add(unit); } else if (allyIDs.contains(unit.getPlayerID())) { alliedList.add(unit); } else if (enemyIDs.contains(unit.getPlayerID())) { enemyList.add(unit); } else { neutralList.add(unit); } } else if (allyIDs.contains(unit.getPlayerID())) { alliedList.add(unit); } else if (enemyIDs.contains(unit.getPlayerID())) { enemyList.add(unit); } else { neutralList.add(unit); } } // update the unit lists playerUnits = playerList; alliedUnits = alliedList; enemyUnits = enemyList; neutralUnits = neutralList; for (Integer unitID : deadUnits) { units.get(unitID).setDestroyed(); units.remove(unitID); } listener.gameUpdate(); } catch (Error e) { e.printStackTrace(); } }
public boolean isVisibleToPlayer(Unit u, Player p) { return isVisibleToPlayer(u.getID(), p.getID()); }