public Hero getHero(String name) { synchronized (players) { for (Hero p : players.values()) { if (p.getName().equals(name)) return p; } } return null; }
public void sendChatToAll(Hero from, String text) { List<Hero> heroesClone = null; synchronized (heroes) { heroesClone = new ArrayList<Hero>(players.values()); } for (Hero h : heroesClone) { h.receiveChat(from, text); } }
public void sendChatToHero(Hero from, Hero to, String text) { if (to == null || from == null) { return; // No such hero. } to.receiveChat(from, text); }
/** * 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(); }
@Override public void unequip(Hero hero) { hero.addDamage(-damage); hero.setAttackRange(1); }
@Override public void equip(Hero hero) { hero.addDamage(damage); hero.setAttackRange(range); }