示例#1
0
文件: Duck.java 项目: barthoz/ogogame
  /** Constructor */
  public Duck(Spatial model, Game game, Cell location) {
    this.model = model;
    this.location = location;
    this.controller = new DuckControl(model, game, this);

    model.setLocalTranslation(location.getWorldCoordinates());
  }
示例#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;
  }