/** * TODO: Think about how the heroes should be locked so that they can't be attacked by any other * hero while this is being processed. */ public synchronized void heroAttacks(Hero initiator) { List<MapObserver> reachable = map.getNeighborsWithinAttackRange(initiator); // Return if there's no one to attack. if (reachable.isEmpty()) { if (initiator instanceof Player) { sendMessageToPlayer((Player) initiator, "There is no one to attack.", true); } return; } // Converting to list of Hero. List<Hero> heroes = new ArrayList<Hero>(); for (MapObserver o : reachable) { heroes.add(o.getHero()); } // Checking if there are to many fighters. int i; if (heroes.size() >= Fight.MAX_FIGHTERS) { // Sorting by distance. Util.sortHeroesByDistance(initiator.getLocation(), heroes); while ((i = heroes.size()) >= Fight.MAX_FIGHTERS) heroes.remove(i - 1); // Removing the most distant fighters over maxOpponents. /*for (int i = heroes.size() - 1; i > maxOpponents; i--) { heroes.remove(i); }*/ } // Getting the appropriate fight scene. FightScene scene = FightScene.getFightSceneFor(initiator, heroes); // Adding the initiator in the list of fighters. heroes.add(initiator); // Starting the fight. Fight fight = new Fight(heroes, scene, this); fights.add(fight); fight.startFight(); }
public void startMob(Mob m) { heroes.put(m.getId(), m); map.addObserver(m); aiThread.addMob(m); }