コード例 #1
0
ファイル: GameLogic.java プロジェクト: jspngh/Proj_AD
  private boolean executeMovementAgent(Agent a, int wantsToMove) {

    int currentLocation = a.getLocation();
    int xCurrent = this.calcXFromCoor(currentLocation);
    int yCurrent = this.calcYFromCoor(currentLocation);
    int xWants = this.calcXFromCoor(wantsToMove);
    int yWants = this.calcYFromCoor(wantsToMove);

    boolean isRed = a.isRed();

    // check if this is a valid movement request, otherwhise crash the game
    // check if wantsToMove is in bounds

    if (wantsToMove < 0 || wantsToMove >= this.gridIdentifier.length) {
      System.err.println("REQUESTED MOVE OUT OF BOUNDS. EXITING.");
      System.exit(-1);
    }

    // we can either move up, down, left or right, just check the four cases
    // crash the game if the move is not one of these choices
    if (!((xCurrent == xWants - 1 && yCurrent == yWants)
        || (xCurrent == xWants + 1 && yCurrent == yWants)
        || (yCurrent == yWants + 1 && xCurrent == xWants)
        || (yCurrent == yWants - 1 && xCurrent == xWants))) {

      System.err.println(xCurrent);
      System.err.println(yCurrent);
      System.err.println(xWants);
      System.err.println(yWants);
      System.err.println("REQUESTED MOVE NOT UP,DOWN,RIGHT OR LEFT.");
      System.exit(-1);
    }

    // move is not legal because there is a rock
    if (gridIdentifier[wantsToMove].getIdentifier().equals(Tile.ROCK_TILE)) {
      System.err.println("REQUESTED TO MOVE ON ROCK SQUARE.");
      System.exit(-1);
    }

    // move is also illegal if you're tyring to move into the enemies camp

    if ((gridIdentifier[wantsToMove].getIdentifier().equals(Tile.BLUE_CAMP_TILE) && isRed)
        || (gridIdentifier[wantsToMove].getIdentifier().equals(Tile.RED_CAMP_TILE) && !isRed)) {
      System.err.println("REQUESTED TO MOVE ON ENEMIES CAMP.");
      System.exit(-1);
    }

    // I guess if we get here, we can actually move!
    // Then there are three options, either the goto tile is a coin or else
    // its a grass field or its own camp

    // if its a camp, the sprite shouldnt move, but if is carrying a coin it
    // should drop of the coin and the movement list should go empty
    if (isRed && (gridIdentifier[wantsToMove].getIdentifier().equals(Tile.RED_CAMP_TILE))) {

      int coin = a.getCarrying();

      if (coin != -1) {
        coinDrop(isRed, a.getCarrying());
        a.setCarrying(-1);
      }
      a.clearMovementList();
      return false;
    } else if (!isRed
        && (gridIdentifier[wantsToMove].getIdentifier().equals(Tile.BLUE_CAMP_TILE))) {
      int coin = a.getCarrying();

      if (coin != -1) {
        coinDrop(isRed, a.getCarrying());
        a.setCarrying(-1);
      }

      a.clearMovementList();
      return false;
    }

    // if its a coin, two things can happen, either the actor is already
    // carrying a coin, in which case the coin being held is dropped at the
    // current location of the actor, then the actor moves to destination
    // and removed the coin there and puts it in inventory
    // else if he is not carrying anything, he removes the coin and moves to
    // the target square
    if ((gridIdentifier[wantsToMove].getIdentifier().equals(Tile.COIN_TILE))) {
      int carrying = a.getCarrying();

      // if he is carrying something, drop the coint
      if (carrying != -1) {
        // current location becomes a coin
        gridIdentifier[currentLocation] = mapper.get(Tile.COIN_TILE);
        // update coint location of coin dropped
        this.coinLocations.put(currentLocation, carrying);
      } else {
        gridIdentifier[currentLocation] = mapper.get(Tile.GRASS_TILE);
      }
      // targetlocation becomes sprite (red or blue)
      if (isRed) {
        gridIdentifier[wantsToMove] = mapper.get(Tile.RED_SPRITE_TILE);
      } else {
        gridIdentifier[wantsToMove] = mapper.get(Tile.BLUE_SPRITE_TILE);
      }

      // pick up the coin
      int pickup = this.coinLocations.get(wantsToMove);
      // give it to sprite
      a.setCarrying(pickup);
      // remove it from coint map
      this.coinLocations.remove(wantsToMove);
      // remove the action
      a.popMovement();
      a.setLocation(wantsToMove);

      return true;
    }

    if ((gridIdentifier[wantsToMove].getIdentifier().equals(Tile.GRASS_TILE))) {

      if (isRed) {
        gridIdentifier[wantsToMove] = mapper.get(Tile.RED_SPRITE_TILE);
      } else {
        gridIdentifier[wantsToMove] = mapper.get(Tile.BLUE_SPRITE_TILE);
      }

      gridIdentifier[currentLocation] = mapper.get(Tile.GRASS_TILE);
      a.popMovement();

      a.setLocation(wantsToMove);

      return true;
    }

    // this is still fatal if there is a head on collision, it is easier to
    // just allow for passing movements
    if (gridIdentifier[wantsToMove].getIdentifier().equals(Tile.BLUE_SPRITE_TILE)
        || gridIdentifier[wantsToMove].getIdentifier().equals(Tile.RED_SPRITE_TILE)) {

      if (isRed) {
        gridIdentifier[wantsToMove] = mapper.get(Tile.RED_SPRITE_TILE);
      } else {
        gridIdentifier[wantsToMove] = mapper.get(Tile.BLUE_SPRITE_TILE);
      }

      gridIdentifier[currentLocation] = mapper.get(Tile.GRASS_TILE);
      a.popMovement();
      a.setLocation(wantsToMove);

      return true;
    }

    throw new RuntimeErrorException(new Error("Ran out of options."));
  }
コード例 #2
0
ファイル: GameLogic.java プロジェクト: jspngh/Proj_AD
  private void readFromFile(Tile[] tbs, String file) {

    int height = squaresY;
    int width = squaresX;
    int countSprites = 0;

    BufferedReader reader = null;
    int i = 0;
    int lines = 0;
    try {
      reader = new BufferedReader(new FileReader(file));
      String line = reader.readLine();
      lines++;

      while (lines <= height) {
        String[] tokens = line.split("\t");
        if (tokens.length != width) {
          throw new IOException();
        }

        for (String x : tokens) {

          Tile tile = mapper.get(x);

          // if the tile did not map, it should be an int value (and
          // as such a coin)
          if (tile == null) {
            int value = Integer.parseInt(x);
            this.coinLocations.put(i, value);
            tile = mapper.get(Tile.COIN_TILE);
          } else if (tile.getIdentifier().equals(Tile.BLUE_CAMP_TILE)) {
            this.blueCamp = i;
          } else if (tile.getIdentifier().equals(Tile.RED_CAMP_TILE)) {
            this.redCamp = i;
          } else if (tile.getIdentifier().equals(Tile.BLUE_SPRITE_TILE)) {
            countSprites++;
            Agent a = null;
            if (this.blueTeamPathStrategy == AGENT_STRATEGY.RANDOM_WALKER) {
              a = new RandomWalker("" + countSprites, false, this, pathCredits);
            } else if (this.blueTeamPathStrategy == AGENT_STRATEGY.ASTARWALKER) {
              a = new AStarWalker("" + countSprites, false, this, pathCredits);
            }
            // ADD OTHER STRATEGIES FOR BLUE HERE
            else if (this.blueTeamPathStrategy == AGENT_STRATEGY.HIERARCHICALWALKER) {
              a = new HierarchicalWalker("" + countSprites, false, this, pathCredits);
            }

            // *****
            else {
              System.err.println("Illegal walker type");
              System.exit(-1);
            }
            a.setLocation(i);
            this.blueTeam.add(a);
            this.allSprites.add(a);
          } else if (tile.getIdentifier().equals(Tile.RED_SPRITE_TILE)) {
            countSprites++;
            Agent a = null;
            if (this.redTeamPathStrategy == AGENT_STRATEGY.RANDOM_WALKER) {
              a = new RandomWalker("" + countSprites, true, this, pathCredits);
            } else if (this.redTeamPathStrategy == AGENT_STRATEGY.ASTARWALKER) {
              a = new AStarWalker("" + countSprites, true, this, pathCredits);
            }
            // add other strategies for red team here

            // *****
            else {
              System.err.println("Illegal walker type");
              System.exit(-1);
            }
            this.redTeam.add(a);
            a.setLocation(i);
            this.allSprites.add(a);
          }
          tbs[i] = tile;
          i++;
        }
        line = reader.readLine();
        lines++;
      }

    } catch (IOException e) {
      System.err.println("Error reading  map file");
      e.printStackTrace();
      System.exit(-1);
    } finally {
      try {
        reader.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }