From fb556c1b90c597f101397e8b52a9cbedd9cf8441 Mon Sep 17 00:00:00 2001 From: Luca Nickel Date: Tue, 14 Dec 2021 23:03:39 +0900 Subject: [PATCH 1/3] Adds setColor method to Entity class Adds tests for setter and getter of Color for the Entity class --- src/entity/Entity.java | 8 ++++++++ test/entity/EntityTest.java | 31 +++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 test/entity/EntityTest.java diff --git a/src/entity/Entity.java b/src/entity/Entity.java index 9fe604fdf..7183cd1ba 100644 --- a/src/entity/Entity.java +++ b/src/entity/Entity.java @@ -57,6 +57,14 @@ public final Color getColor() { return color; } + /** + * Setter for the color of the entity. + * + * @param color + * New color of the entity. + */ + public final void setColor(Color color) { this.color = color; } + /** * Getter for the X axis position of the entity. * diff --git a/test/entity/EntityTest.java b/test/entity/EntityTest.java new file mode 100644 index 000000000..614cf5556 --- /dev/null +++ b/test/entity/EntityTest.java @@ -0,0 +1,31 @@ +package entity; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.awt.*; + +import static org.junit.jupiter.api.Assertions.*; + +class EntityTest { + + Entity testEntity; + + @BeforeEach + void setUp() { + // positionX, positionY, width and height are just placeholders + // they are not necessary for our tests + testEntity = new Entity(0,0,0,0, Color.WHITE); + } + + @Test + void getColor() { + assertEquals(Color.WHITE, testEntity.getColor()); + } + + @Test + void setColor() { + testEntity.setColor(Color.GREEN); + assertEquals(Color.GREEN, testEntity.getColor()); + } +} \ No newline at end of file From c80ffc9eecfdc7c2d2fceecd472232cb7003eefe Mon Sep 17 00:00:00 2001 From: Luca Nickel Date: Tue, 14 Dec 2021 23:06:57 +0900 Subject: [PATCH 2/3] Adds health, HEALTH_COLOR_CODE, getHealth() and reduceHealth() to the EnemyShip class Adds the corresponding tests for getHealth() and reduceHealth() Adjusts the call of the EnemyShip constructor in EnemyShipFormation --- src/entity/EnemyShip.java | 38 +++++++++++++++++++++++++--- src/entity/EnemyShipFormation.java | 4 +-- test/entity/EnemyShipTest.java | 40 ++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 test/entity/EnemyShipTest.java diff --git a/src/entity/EnemyShip.java b/src/entity/EnemyShip.java index 19d9958ad..33b756917 100644 --- a/src/entity/EnemyShip.java +++ b/src/entity/EnemyShip.java @@ -1,6 +1,7 @@ package entity; import java.awt.Color; +import java.util.HashMap; import engine.Cooldown; import engine.Core; @@ -22,6 +23,13 @@ public class EnemyShip extends Entity { private static final int C_TYPE_POINTS = 30; /** Point value of a bonus enemy. */ private static final int BONUS_TYPE_POINTS = 100; + /** Different health values with their corresponding colors. */ + private static final HashMap HEALTH_COLOR_CODES = + new HashMap(){{ + put(1, Color.WHITE); + put(3, Color.GREEN); + put(5, Color.BLUE); + }}; /** Cooldown between sprite changes. */ private Cooldown animationCooldown; @@ -29,6 +37,8 @@ public class EnemyShip extends Entity { private boolean isDestroyed; /** Values of the ship, in points, when destroyed. */ private int pointValue; + /** Health value of the ship, when spawned */ + private int health; /** * Constructor, establishes the ship's properties. @@ -39,14 +49,17 @@ public class EnemyShip extends Entity { * Initial position of the ship in the Y axis. * @param spriteType * Sprite type, image corresponding to the ship. + * @param health + * Initial health of the ship. */ public EnemyShip(final int positionX, final int positionY, - final SpriteType spriteType) { - super(positionX, positionY, 12 * 2, 8 * 2, Color.WHITE); + final SpriteType spriteType, final int health) { + super(positionX, positionY, 12 * 2, 8 * 2, HEALTH_COLOR_CODES.get(health)); this.spriteType = spriteType; this.animationCooldown = Core.getCooldown(500); this.isDestroyed = false; + this.health = health; switch (this.spriteType) { case EnemyShipA1: @@ -141,6 +154,16 @@ public final void destroy() { this.spriteType = SpriteType.Explosion; } + /** + * Reduces the health of the ship by 1 and changes the + * color of the ship when a specific health was reached. + */ + public final void reduceHealth() { + this.health--; + if (HEALTH_COLOR_CODES.containsKey(this.health)) + this.setColor(HEALTH_COLOR_CODES.get(this.health)); + } + /** * Checks if the ship has been destroyed. * @@ -149,4 +172,13 @@ public final void destroy() { public final boolean isDestroyed() { return this.isDestroyed; } -} + + /** + * Returns the current remaining health. + * + * @return Integer value of the health field. + */ + public final int getHealth() { + return this.health; + } +} \ No newline at end of file diff --git a/src/entity/EnemyShipFormation.java b/src/entity/EnemyShipFormation.java index 04bf96326..59e9bf61d 100644 --- a/src/entity/EnemyShipFormation.java +++ b/src/entity/EnemyShipFormation.java @@ -103,7 +103,7 @@ private enum Direction { LEFT, /** Movement to the bottom of the screen. */ DOWN - }; + } /** * Constructor, sets the initial conditions. @@ -149,7 +149,7 @@ else if (i / (float) this.nShipsHigh < PROPORTION_B column.add(new EnemyShip((SEPARATION_DISTANCE * this.enemyShips.indexOf(column)) + positionX, (SEPARATION_DISTANCE * i) - + positionY, spriteType)); + + positionY, spriteType, 1)); this.shipCount++; } } diff --git a/test/entity/EnemyShipTest.java b/test/entity/EnemyShipTest.java new file mode 100644 index 000000000..a8083e740 --- /dev/null +++ b/test/entity/EnemyShipTest.java @@ -0,0 +1,40 @@ +package entity; + +import engine.DrawManager.SpriteType; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.awt.*; + +import static org.junit.jupiter.api.Assertions.*; + +class EnemyShipTest { + + EnemyShip testShip; + + @BeforeEach + void setUp() { + // positionX, positionY and spriteType are just placeholders + // they are not necessary for our tests + testShip = new EnemyShip(0, 0, SpriteType.EnemyShipA1, 5); + } + + @Test + void getHealth() { + assertEquals(5, testShip.getHealth()); + } + + @Test + void reduceHealthWithoutColorChange() { + testShip.reduceHealth(); + assertEquals(4, testShip.getHealth()); + } + + @Test + void reduceHealthWithColorChange() { + testShip.reduceHealth(); + testShip.reduceHealth(); + assertEquals(3, testShip.getHealth()); + assertEquals(Color.GREEN, testShip.getColor()); + } +} \ No newline at end of file From ed1d44feeb97b4eb7f691da4ff60b31e0fef1a57 Mon Sep 17 00:00:00 2001 From: Luca Nickel Date: Tue, 14 Dec 2021 23:08:58 +0900 Subject: [PATCH 3/3] Removes GameScreenTest (was only for testing purposes during the lab session about testing) Adjusts manageCollisions() in GameScreen to reduce the health of a hit ship and to only destroy it when health reached 0 --- src/screen/GameScreen.java | 11 +++++---- test/screen/GameScreenTest.java | 42 --------------------------------- 2 files changed, 7 insertions(+), 46 deletions(-) delete mode 100644 test/screen/GameScreenTest.java diff --git a/src/screen/GameScreen.java b/src/screen/GameScreen.java index 46b4bfb96..e5d3e93a0 100644 --- a/src/screen/GameScreen.java +++ b/src/screen/GameScreen.java @@ -82,7 +82,7 @@ public class GameScreen extends Screen { * Current game state. * @param gameSettings * Current game settings. - * @param bonnusLife + * @param bonusLife * Checks if a bonus life is awarded this level. * @param width * Screen width. @@ -318,9 +318,12 @@ private void manageCollisions() { for (EnemyShip enemyShip : this.enemyShipFormation) if (!enemyShip.isDestroyed() && checkCollision(bullet, enemyShip)) { - this.score += enemyShip.getPointValue(); - this.shipsDestroyed++; - this.enemyShipFormation.destroy(enemyShip); + enemyShip.reduceHealth(); + if (enemyShip.getHealth() == 0) { + this.score += enemyShip.getPointValue(); + this.shipsDestroyed++; + this.enemyShipFormation.destroy(enemyShip); + } recyclable.add(bullet); } if (this.enemyShipSpecial != null diff --git a/test/screen/GameScreenTest.java b/test/screen/GameScreenTest.java deleted file mode 100644 index cc34c235a..000000000 --- a/test/screen/GameScreenTest.java +++ /dev/null @@ -1,42 +0,0 @@ -package screen; - -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.*; - -class GameScreenTest { - - @BeforeEach - void setUp() { - } - - @AfterEach - void tearDown() { - } - - @Test - void initialize() { - } - - @Test - void run() { - } - - @Test - void isPaused() { - } - - @Test - void pause() { - } - - @Test - void update() { - } - - @Test - void getGameState() { - } -} \ No newline at end of file