예제 #1
0
  /**
   * Method that calculates the enemies movement speed and direction
   *
   * <p>The speed is calculated by finding out time left and number of steps left. This is dynamic
   * since enemies tend to get killed.
   */
  private void moveEnemies() {
    if (enemies.isEmpty()) return;

    long current_time = System.currentTimeMillis() - startTime;

    if (animationFrame > numberOfAnimationUnits) {
      if (current_time > lastEnemyUpdate + enemyPauseTime) animationFrame = 1;
      return;
    }

    int width = currentLevel.getEnemyWidth();
    int height = currentLevel.getEnemyHeight();

    if (animationFrame == 1) {
      boolean goDown = false;
      int jumpGoal = width;
      calculationTime = current_time;
      if (enemies.get(0).goingRight) {
        if (EnemyHandler.getLargestX(enemies) > GameConstants.WINDOW_WIDTH - (width * 2)) {
          goDown = true;
          jumpGoal = height;
        }
      } else {
        if (EnemyHandler.getSmallestX(enemies) < width) {
          goDown = true;
          jumpGoal = height;
        }
      }
      for (Enemy badGuy : enemies) {
        badGuy.goingDown = goDown;
        badGuy.setJumpGoal(jumpGoal);
      }
    }

    int moveDistance = width / numberOfAnimationUnits;
    for (Enemy badGuy : enemies) {
      badGuy.move(moveDistance);
    }

    animationFrame++;
    if (animationFrame > numberOfAnimationUnits) {
      for (Enemy badGuy : enemies) {
        badGuy.goToJumpGoal();
      }

      enemyPauseTime = calculateEnemyJumpTime(current_time, current_time - calculationTime);
      lastEnemyUpdate = current_time;
      System.out.println("Pause time: " + enemyPauseTime);
      System.out.println("Run Time: " + current_time / 1000);
    }
  }
예제 #2
0
  /**
   * Calculates the minimun number of steps left for enemies to reach player
   *
   * @return
   */
  private int calculateStepsLeft() {
    boolean goingRight = enemies.get(0).goingRight;
    int smallestX = EnemyHandler.getSmallestX(enemies);
    int largestX = EnemyHandler.getLargestX(enemies);
    int smallestY = EnemyHandler.getSmallestY(enemies);
    int width = currentLevel.getEnemyWidth();
    int height = currentLevel.getEnemyHeight();
    int stepsLeftOnRow = 0;

    if (goingRight) {
      stepsLeftOnRow = (GameConstants.WINDOW_WIDTH - (largestX + width)) / width;
    } else {
      stepsLeftOnRow = smallestX / width;
    }

    int stepsOnEachRow = (GameConstants.WINDOW_WIDTH - (largestX + width - smallestX)) / width;

    int rowsLeft = (smallestY - player1.height) / height;

    return stepsLeftOnRow + (stepsOnEachRow * rowsLeft) + rowsLeft;
  }
예제 #3
0
  /**
   * Calculates how long the enemies can pause before the next jump. I no enemies die the pause time
   * will stay almost the same. If they do get killed the pause time is reduced.
   *
   * @param current_time
   * @param calculation_time
   * @return
   */
  private long calculateEnemyJumpTime(long current_time, long calculation_time) {

    int totalStepsLeft = calculateStepsLeft();

    if (totalStepsLeft < 1) return 0;

    long timeLeft = currentLevel.getTimeInMiliSec() - current_time;

    long stepTime = (timeLeft - (calculation_time * totalStepsLeft)) / totalStepsLeft;

    return stepTime;
  }
예제 #4
0
  /**
   * Sets (or resets) game variables And loads level data
   *
   * @param levelNo
   */
  public void loadLevel(int levelNo) {
    score = 0;
    startTime = System.currentTimeMillis();
    lastShot = -shotTime;
    lastEnemyUpdate = 0;
    projectiles = new ArrayList<>();
    animationFrame = 1;
    /*if(enemies != null)
    enemies.clear();*/
    enemies = new ArrayList<>();

    if (levelNo == 1) {
      currentLevel = new Level1Data();
      enemies = spawnPit.getEnemies(currentLevel, imageHandler);
    }

    explosions = new ArrayList<>();

    int pWidth = currentLevel.getPlayerWidth();
    int pHeight = currentLevel.getPlayerHeight();
    player1 =
        new Player(
            (GameConstants.WINDOW_WIDTH / 2) - pWidth / 2,
            10,
            pWidth,
            pHeight,
            imageHandler.getTexture(ImageConstants.PLAYER),
            imageHandler.getTexture(ImageConstants.PLAYER_LEFT),
            imageHandler.getTexture(ImageConstants.PLAYER_RIGHT));

    gameStatus = GameConstants.GAME_STATUS_INTRO;
    int smallY = EnemyHandler.getSmallestY(enemies);
    intro_enemy_offset = GameConstants.WINDOW_HEIGHT - smallY + intro_speed;
    intro_player_offset = player1.y + 1 + player1.height;

    if (enemy_hero != null) {
      enemy_hero = null;
    }
  }