/** * Handles the controls for playing and also the controls when in the Pause and Game over menu. */ public void handle_controls() { if (gameStatus.equalsIgnoreCase(GameConstants.GAME_STATUS_PLAYING)) { if (Gdx.input.isKeyPressed(Input.Keys.LEFT) || Gdx.input.isKeyPressed(Input.Keys.A)) { // System.out.println("Left!"); player1.moveLeft(); } if (Gdx.input.isKeyPressed(Input.Keys.RIGHT) || Gdx.input.isKeyPressed(Input.Keys.D)) { // System.out.println("Right!"); player1.moveRight(); } if (!Gdx.input.isKeyPressed(Input.Keys.RIGHT) && !Gdx.input.isKeyPressed(Input.Keys.LEFT)) { player1.stand_still(); } if (Gdx.input.isKeyPressed(Input.Keys.SPACE)) { long current_time = System.currentTimeMillis() - startTime; if (current_time >= lastShot + shotTime) { // once every second (1000millisec) lastShot = current_time; projectiles.add( new Projectile( player1.x + (player1.width / 2), player1.y + player1.height, 10, 20, imageHandler.getTexture(ImageConstants.PROJECTILE))); } } } if (Gdx.input.isKeyJustPressed(Input.Keys.P)) { if (gameStatus.equalsIgnoreCase(GameConstants.GAME_STATUS_PAUSED)) pause(false); else if (gameStatus.equalsIgnoreCase(GameConstants.GAME_STATUS_PLAYING)) pause(true); } if (gameStatus.equalsIgnoreCase(GameConstants.GAME_STATUS_PAUSED)) { String result = pauseMenu.handle_controls(); if (result != null) { if (result.equalsIgnoreCase("Continue")) { gameStatus = GameConstants.GAME_STATUS_PLAYING; long current_time = System.currentTimeMillis() - startTime; startTime = startTime + (current_time - pauseTime); } else if (result.equalsIgnoreCase("Quit")) { gameStatus = GameConstants.GAME_STATUS_RETURN; } } } if (gameStatus.equalsIgnoreCase(GameConstants.GAME_STATUS_GAME_OVER)) { String result = gameOverMenu.handle_controls(); if (result != null) { if (result.equalsIgnoreCase("Retry")) { gameStatus = GameConstants.GAME_STATUS_RETRY; } else if (result.equalsIgnoreCase("Quit")) { gameStatus = GameConstants.GAME_STATUS_RETURN; } } } }
/** * 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; } } }
/** Player can't go through the walls */ private void calculatePlayerVsWall() { if (player1.x < 0) player1.set_x(0); if (player1.x + player1.width > GameConstants.WINDOW_WIDTH) player1.set_x(GameConstants.WINDOW_WIDTH - player1.width); }