/** * 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); } }
/** * 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; }