/** * Draws the objects * * @param batch * @param imageHandler */ public void draw(SpriteBatch batch, ImageHandler imageHandler) { Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); batch.begin(); if (gameStatus.equalsIgnoreCase(GameConstants.GAME_STATUS_INTRO)) { player1.drawWithOffset(batch, 0, -intro_player_offset); for (Enemy badGuy : enemies) { badGuy.drawWithOffset(batch, 0, intro_enemy_offset); } } else { player1.draw(batch); for (Enemy badGuy : enemies) { badGuy.draw(batch); } for (Projectile proj : projectiles) { proj.draw(batch); } for (Explosion exp : explosions) { exp.draw(batch); } } scoreText.draw(batch, Integer.toString(score), 10, 750, 4); if (gameStatus.equalsIgnoreCase(GameConstants.GAME_STATUS_PAUSED)) { pauseMenu.draw(batch); } if (gameStatus.equalsIgnoreCase(GameConstants.GAME_STATUS_GAME_OVER)) { gameOverMenu.draw(batch); } if (gameStatus.equalsIgnoreCase(GameConstants.GAME_STATUS_INTRO_START)) { int draw_x = 450; int draw_y = 200; int size = 8; for (Sprite img : startText) { img.setPosition(draw_x, draw_y); img.setSize((int) (img.getRegionWidth() * size), (int) (img.getRegionHeight() * size)); img.draw(batch); draw_x += img.getRegionWidth() * size; } } }
/** * 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 movements and collisions between objects in play * * @return */ public String calculate() { if (gameStatus.equalsIgnoreCase(GameConstants.GAME_STATUS_PLAYING)) { /** *Movement** */ for (Projectile proj : projectiles) { proj.move(); } moveEnemies(); /** *Collisions** */ calculatePlayerVsWall(); calculateProjectileEscape(); calculateCollisionEnemyVsProjectile(); if (enemies.isEmpty()) { gameStatus = GameConstants.GAME_STATUS_GAME_OVER; } calculateEnemyReachingPlayer(); for (Explosion exp : explosions) { if (exp.isDead()) { exp.dispose(); explosions.remove(exp); break; } } } if (gameStatus.equalsIgnoreCase(GameConstants.GAME_STATUS_RETURN)) return GameConstants.APPLICATION_MENU; if (gameStatus.equalsIgnoreCase(GameConstants.GAME_STATUS_RETRY)) loadLevel(1); if (gameStatus.equalsIgnoreCase(GameConstants.GAME_STATUS_INTRO)) { intro_enemy_offset -= intro_speed; intro_player_offset--; if (intro_enemy_offset <= 0 && intro_player_offset <= 0) { gameStatus = GameConstants.GAME_STATUS_INTRO_START; intro_start_time = System.currentTimeMillis(); } else if (intro_enemy_offset < 0) { intro_enemy_offset = 0; } else if (intro_player_offset < 0) { intro_player_offset = 0; } } if (gameStatus.equalsIgnoreCase(GameConstants.GAME_STATUS_INTRO_START)) { long current = System.currentTimeMillis(); if (current - intro_start_time >= 1000) { startTime = System.currentTimeMillis(); gameStatus = GameConstants.GAME_STATUS_PLAYING; } } if (gameStatus.equalsIgnoreCase(GameConstants.GAME_STATUS_OUTRO)) { if (enemy_hero == null) { selectEnemy(); outro_distance_x = enemy_hero.x - player1.x; outro_distance_y = enemy_hero.y - player1.y; } else { if (calculateOverlap(enemy_hero, player1) || enemy_hero.y <= player1.y) { gameStatus = GameConstants.GAME_STATUS_GAME_OVER; } enemy_hero.x -= outro_distance_x / outro_distance_y; enemy_hero.y -= outro_distance_y / outro_distance_y; } } return null; }