示例#1
0
  public GameServer(int port) throws IOException {
    webSocketServer = new WebSocketServer(port);
    long lastId = OwnableManager.getLastId();
    Ownable.setLastId(lastId);
    map = new Map();

    // If I don't write this, the static fields in Item subclasses are
    // created in the wrong order. I don't understand why.

    FightScene.getAll();
  }
示例#2
0
  public void heroAttacks(Player player, int i) {
    Hero player2 = heroes.get(i);
    if (player2 == null) return;
    // Converting to list of Hero.
    List<Hero> heroes = new ArrayList<Hero>();
    heroes.add(player2);

    // Getting the appropriate fight scene.
    FightScene scene = FightScene.getFightSceneFor(player, heroes);

    // Adding the initiator in the list of fighters.
    heroes.add(player);
    Fight fight = new Fight(heroes, scene, this);
    fights.add(fight);

    fight.startFight();
  }
示例#3
0
  /**
   * 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();
  }