Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 83 additions & 50 deletions src/screen/GameScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public class GameScreen extends Screen {
/** Checks if a bonus life is received. */
private boolean bonusLife;

boolean paused = false;
private static final int SELECTION_TIME = 200;
private Cooldown selectionCooldown;

/**
* Constructor, establishes the properties of the screen.
*
Expand Down Expand Up @@ -101,6 +105,9 @@ public GameScreen(final GameState gameState,
this.lives++;
this.bulletsShot = gameState.getBulletsShot();
this.shipsDestroyed = gameState.getShipsDestroyed();

this.selectionCooldown = Core.getCooldown(SELECTION_TIME);
this.selectionCooldown.reset();
}

/**
Expand All @@ -125,6 +132,10 @@ public final void initialize() {
this.gameStartTime = System.currentTimeMillis();
this.inputDelay = Core.getCooldown(INPUT_DELAY);
this.inputDelay.reset();

this.gameStartTime = System.currentTimeMillis();
this.inputDelay = Core.getCooldown(INPUT_DELAY);
this.inputDelay.reset();
}

/**
Expand All @@ -141,73 +152,95 @@ public final int run() {
return this.returnCode;
}

public final boolean isPaused() {
return this.paused;
}

public final void pause() {
this.paused = !paused;
}
/**
* Updates the elements on screen and checks for events.
*/
protected final void update() {
super.update();

if (this.inputDelay.checkFinished() && !this.levelFinished) {
if (this.selectionCooldown.checkFinished() && this.inputDelay.checkFinished()) {
if (inputManager.isKeyDown(KeyEvent.VK_P) && this.isPaused()) {
pause();
this.logger.info("Game unpaused");
this.selectionCooldown.reset();
} else if (inputManager.isKeyDown(KeyEvent.VK_P)) {
pause();
this.logger.info("Game paused");
this.selectionCooldown.reset();
}
}

if (!this.ship.isDestroyed()) {
boolean moveRight = inputManager.isKeyDown(KeyEvent.VK_RIGHT)
|| inputManager.isKeyDown(KeyEvent.VK_D);
boolean moveLeft = inputManager.isKeyDown(KeyEvent.VK_LEFT)
|| inputManager.isKeyDown(KeyEvent.VK_A);
if(!this.isPaused())
{
if (this.inputDelay.checkFinished() && !this.levelFinished) {

boolean isRightBorder = this.ship.getPositionX()
+ this.ship.getWidth() + this.ship.getSpeed() > this.width - 1;
boolean isLeftBorder = this.ship.getPositionX()
- this.ship.getSpeed() < 1;
if (!this.ship.isDestroyed()) {
boolean moveRight = inputManager.isKeyDown(KeyEvent.VK_RIGHT)
|| inputManager.isKeyDown(KeyEvent.VK_D);
boolean moveLeft = inputManager.isKeyDown(KeyEvent.VK_LEFT)
|| inputManager.isKeyDown(KeyEvent.VK_A);

if (moveRight && !isRightBorder) {
this.ship.moveRight();
}
if (moveLeft && !isLeftBorder) {
this.ship.moveLeft();
boolean isRightBorder = this.ship.getPositionX()
+ this.ship.getWidth() + this.ship.getSpeed() > this.width - 1;
boolean isLeftBorder = this.ship.getPositionX()
- this.ship.getSpeed() < 1;

if (moveRight && !isRightBorder) {
this.ship.moveRight();
}
if (moveLeft && !isLeftBorder) {
this.ship.moveLeft();
}
if (inputManager.isKeyDown(KeyEvent.VK_SPACE))
if (this.ship.shoot(this.bullets))
this.bulletsShot++;
}
if (inputManager.isKeyDown(KeyEvent.VK_SPACE))
if (this.ship.shoot(this.bullets))
this.bulletsShot++;
}

if (this.enemyShipSpecial != null) {
if (!this.enemyShipSpecial.isDestroyed())
this.enemyShipSpecial.move(2, 0);
else if (this.enemyShipSpecialExplosionCooldown.checkFinished())
this.enemyShipSpecial = null;
if (this.enemyShipSpecial != null) {
if (!this.enemyShipSpecial.isDestroyed())
this.enemyShipSpecial.move(2, 0);

}
if (this.enemyShipSpecial == null
&& this.enemyShipSpecialCooldown.checkFinished()) {
this.enemyShipSpecial = new EnemyShip();
this.enemyShipSpecialCooldown.reset();
this.logger.info("A special ship appears");
}
if (this.enemyShipSpecial != null
&& this.enemyShipSpecial.getPositionX() > this.width) {
this.enemyShipSpecial = null;
this.logger.info("The special ship has escaped");
}
else if (this.enemyShipSpecialExplosionCooldown.checkFinished())
this.enemyShipSpecial = null;

this.ship.update();
this.enemyShipFormation.update();
this.enemyShipFormation.shoot(this.bullets);
}
}
if (this.enemyShipSpecial == null
&& this.enemyShipSpecialCooldown.checkFinished()) {
this.enemyShipSpecial = new EnemyShip();
this.enemyShipSpecialCooldown.reset();
this.logger.info("A special ship appears");
}
if (this.enemyShipSpecial != null
&& this.enemyShipSpecial.getPositionX() > this.width) {
this.enemyShipSpecial = null;
this.logger.info("The special ship has escaped");
}

manageCollisions();
cleanBullets();
draw();
this.ship.update();
this.enemyShipFormation.update();
this.enemyShipFormation.shoot(this.bullets);
}

if ((this.enemyShipFormation.isEmpty() || this.lives == 0)
&& !this.levelFinished) {
this.levelFinished = true;
this.screenFinishedCooldown.reset();
}
manageCollisions();
cleanBullets();
draw();

if (this.levelFinished && this.screenFinishedCooldown.checkFinished())
this.isRunning = false;
if ((this.enemyShipFormation.isEmpty() || this.lives == 0)
&& !this.levelFinished) {
this.levelFinished = true;
this.screenFinishedCooldown.reset();
}

if (this.levelFinished && this.screenFinishedCooldown.checkFinished())
this.isRunning = false;
}//pause
}

/**
Expand Down