Beispiel #1
0
  private static List<Cell> retrieveNeighbouringCells(
      Cell[][] cells, Cell cell, Creature creature) {
    List<Cell> neighbours = new ArrayList<Cell>();

    int x = cell.getXCoor();
    int y = cell.getYCoor();

    // left
    if (x > 1) {
      // up
      if (y > 1) {
        neighbours.add(cells[x - 1][y - 1]);
        neighbours.add(cells[x][y - 1]);
      }

      // middle
      neighbours.add(cells[x - 1][y]);

      // down
      if (y + 1 < cells[0].length) {
        neighbours.add(cells[x - 1][y + 1]);
        neighbours.add(cells[x][y + 1]);
      }
    }

    // right
    if (x + 1 < cells.length) {
      // up
      if (y > 1) {
        neighbours.add(cells[x + 1][y - 1]);
      }

      // middle
      neighbours.add(cells[x + 1][y]);

      // down
      if (y + 1 < cells[0].length) {
        neighbours.add(cells[x + 1][y + 1]);
      }
    }

    List<Cell> finalNeighbours = new ArrayList<Cell>();

    for (Cell neighbour : neighbours) {
      if (neighbour.creatureAllowed(creature) && neighbour.getBase() == null) {
        finalNeighbours.add(neighbour);
      }
    }

    return finalNeighbours;
  }
Beispiel #2
0
  /** Business logic */
  public static MotionPath createMotionPath(
      TerrainQuad terrain, Cell[][] cells, Cell from, Cell to, Creature creature)
      throws NoReachablePathException {
    if (creature instanceof AirborneCreature) {
      AirborneCreature airCreature = (AirborneCreature) creature;
      airCreature.takeOff();
    }

    if (!to.creatureAllowed(creature)) {
      throw new NoReachablePathException();
    }

    MotionPath motionPath = new MotionPath();

    /** Different for air creatures */
    if (creature instanceof AirborneCreature) {
      // Add take off waypoint
      Vector3f takeOffWayPoint =
          new Vector3f(
              from.getWorldCoordinates().x, airCreatureHeight, from.getWorldCoordinates().z);

      motionPath.addWayPoint(creature.getModel().getLocalTranslation());
      motionPath.addWayPoint(takeOffWayPoint);

      int numSteps = 100;
      float diffX = (to.getWorldCoordinates().x - from.getWorldCoordinates().x) / (float) numSteps;
      float diffZ = (to.getWorldCoordinates().z - from.getWorldCoordinates().z) / (float) numSteps;

      for (int i = 0; i < numSteps; i++) {
        float x = from.getWorldCoordinates().x + i * diffX;
        float z = from.getWorldCoordinates().z + i * diffZ;
        // float y = terrain.getHeight(new Vector2f(x, z)) + airCreatureHeight;
        motionPath.addWayPoint(new Vector3f(x, airCreatureHeight, z));
      }
    } else {
      List<Cell> findPath = findPath(cells, from, to, creature);

      Cell previousCell = null;

      for (Cell cell : findPath) {
        if (previousCell != null) {
          // Smoothen path
          int numParts = 5;
          float xDiff =
              (cell.getWorldCoordinates().x - previousCell.getWorldCoordinates().x)
                  / (float) numParts;
          float zDiff =
              (cell.getWorldCoordinates().z - previousCell.getWorldCoordinates().z)
                  / (float) numParts;

          for (int i = 0; i < numParts; i++) {
            float x = previousCell.getWorldCoordinates().x + i * xDiff;
            float z = previousCell.getWorldCoordinates().z + i * zDiff;
            Vector3f part = null;

            if (creature instanceof SeaCreature) {
              System.out.println("test");
              part = new Vector3f(x, 0, z);
            } else {
              part = new Vector3f(x, terrain.getHeight(new Vector2f(x, z)) - 100, z);
            }

            motionPath.addWayPoint(part);
          }
        }

        if (creature instanceof SeaCreature) {
          Vector3f old = cell.getWorldCoordinates();
          motionPath.addWayPoint(
              new Vector3f(old.x, creature.getModel().getLocalTranslation().y, old.z));
        } else {
          motionPath.addWayPoint(cell.getWorldCoordinates());
        }

        previousCell = cell;
      }
    }

    motionPath.setCycle(false);
    return motionPath;
  }