diff --git a/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/base/Bubble.java b/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/base/Bubble.java index c33064c..364cff6 100644 --- a/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/base/Bubble.java +++ b/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/base/Bubble.java @@ -29,11 +29,6 @@ public abstract class Bubble extends CollidingGameObject implements MovingGameOb flyFromLeftToRight = true; } - @Override - public String toString() { - return "Bubble:" + getPosition().toString(); - } - /** * Moves the bubble a tick further. */ diff --git a/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/base/CollidableGameObject.java b/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/base/CollidableGameObject.java index c459829..cc412d6 100644 --- a/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/base/CollidableGameObject.java +++ b/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/base/CollidableGameObject.java @@ -3,6 +3,7 @@ package de.thdeg.greiner.superpangworld.graphics.base; import de.thdeg.greiner.superpangworld.gameview.GameView; import java.awt.*; +import java.util.Objects; /** * Represents all game objects that are able to collide with something. @@ -39,4 +40,23 @@ public abstract class CollidableGameObject extends GameObject { * @param otherObject The other GameObject that is involved in the collision. */ public abstract void reactToCollision(CollidableGameObject otherObject); + + @Override + public CollidableGameObject clone() { + return (CollidableGameObject) super.clone(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + CollidableGameObject that = (CollidableGameObject) o; + return Objects.equals(hitBox, that.hitBox); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), hitBox); + } } diff --git a/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/base/GameObject.java b/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/base/GameObject.java index 1960fcc..4ff61f2 100644 --- a/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/base/GameObject.java +++ b/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/base/GameObject.java @@ -3,15 +3,17 @@ package de.thdeg.greiner.superpangworld.graphics.base; import de.thdeg.greiner.superpangworld.game.managers.GamePlayManager; import de.thdeg.greiner.superpangworld.gameview.GameView; +import java.util.Objects; + /** * A basic game object. */ -public abstract class GameObject { +public abstract class GameObject implements Cloneable{ /** The game view to display the game object on. */ protected final GameView gameView; /** The position of the game object. */ - protected final Position position; + protected Position position; /** The size of the game object. */ protected double size; /** The speed in pixel per tick of the game object. */ @@ -82,4 +84,36 @@ public abstract class GameObject { position.y += adaptY; } + @Override + public GameObject clone() { + GameObject gameObject = null; + try { + gameObject = (GameObject) super.clone(); + gameObject.position = position.clone(); + } catch (CloneNotSupportedException ignored) { + } + return gameObject; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + GameObject that = (GameObject) o; + return Double.compare(that.speedInPixel, speedInPixel) == 0 + && Double.compare(that.rotation, rotation) == 0 + && Double.compare(that.size, size) == 0 && width == that.width + && height == that.height && position.equals(that.position); + } + + @Override + public int hashCode() { + return Objects.hash(position, speedInPixel, rotation, size, width, height); + } + + @Override + public String toString() { + return getClass().getSimpleName() + ": " + position; + } + } diff --git a/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/base/Position.java b/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/base/Position.java index 7e631e1..9d35668 100644 --- a/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/base/Position.java +++ b/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/base/Position.java @@ -1,9 +1,11 @@ package de.thdeg.greiner.superpangworld.graphics.base; +import java.util.Objects; + /** * Die Position eines Objekts auf einem Fenster anhand zweidimensionaler Koordinaten. */ -public class Position { +public class Position implements Cloneable{ /** Die x-Koordinate */ public double x; @@ -102,5 +104,27 @@ public class Position { public String toString() { return "Position(" + (int) Math.round(x) + ", " + (int) Math.round(y) + ')'; } - + + @Override + public Position clone() { + Position other = null; + try{ + other = (Position) super.clone(); + }catch(CloneNotSupportedException ignored){ + } + return other; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Position position = (Position) o; + return Double.compare(position.x, x) == 0 && Double.compare(position.y, y) == 0; + } + + @Override + public int hashCode() { + return Objects.hash(x, y); + } } diff --git a/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/immovable/LevelLabel.java b/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/immovable/LevelLabel.java index 98482ff..1e46d39 100644 --- a/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/immovable/LevelLabel.java +++ b/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/immovable/LevelLabel.java @@ -38,9 +38,4 @@ public class LevelLabel extends GameObject { public void updateStatus() { } - - @Override - public String toString() { - return "LevelLabel:" +getPosition().toString(); - } } diff --git a/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/immovable/LevelProgressBar.java b/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/immovable/LevelProgressBar.java index f4badf2..0b4edc9 100644 --- a/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/immovable/LevelProgressBar.java +++ b/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/immovable/LevelProgressBar.java @@ -44,11 +44,6 @@ public class LevelProgressBar extends GameObject { } - @Override - public String toString() { - return "LevelProgressBar:" +getPosition().toString(); - } - /** * Set the progress of the level. * @param levelProgress the progress diff --git a/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/moveable/Harpoon.java b/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/moveable/Harpoon.java index c7180cc..f926687 100644 --- a/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/moveable/Harpoon.java +++ b/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/moveable/Harpoon.java @@ -59,11 +59,6 @@ public class Harpoon extends CollidingGameObject implements MovingGameObject { gameView.addRectangleToCanvas(hitBox.x, hitBox.y, hitBox.width, hitBox.height,1,false, Color.red); } - @Override - public String toString() { - return "Harpoon:" +getPosition().toString(); - } - /** * Moves the harpoon a tick further upwards. */ diff --git a/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/moveable/Player.java b/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/moveable/Player.java index 91fccb5..8dcaeb5 100644 --- a/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/moveable/Player.java +++ b/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/moveable/Player.java @@ -6,6 +6,7 @@ import de.thdeg.greiner.superpangworld.graphics.base.GameObject; import de.thdeg.greiner.superpangworld.graphics.base.MovingGameObject; import java.awt.*; +import java.util.Objects; /** * The game character controlled by the player. @@ -101,4 +102,23 @@ public class Player extends CollidableGameObject { shooting = true; gamePlayManager.shootHarpoon(this.position); } + + @Override + public Player clone() { + return (Player) super.clone(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + Player player = (Player) o; + return shooting == player.shooting; + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), shooting); + } } diff --git a/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/moveable/SpecialBubble.java b/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/moveable/SpecialBubble.java index 6bad40c..7349b68 100644 --- a/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/moveable/SpecialBubble.java +++ b/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/moveable/SpecialBubble.java @@ -27,6 +27,8 @@ public class SpecialBubble extends Bubble { " GGGGGWWG \n"+ " GGGWWG \n"+ " GGGG \n"; + /** Flag if the time is stopped or all bubbles are popped when this bubble gets popped. */ + private Status status; /** * Create a special bubble with default values. @@ -40,6 +42,8 @@ public class SpecialBubble extends Bubble { Random rand = new Random(); position.setTo(rand.nextInt(GameView.WIDTH),rand.nextInt(GameView.HEIGHT)); + status = Status.FREEZE; + this.hitBox.width = width-20; this.hitBox.height = height-20; } @@ -74,4 +78,12 @@ public class SpecialBubble extends Bubble { private void activateEffect(){ } + + /** + * Enumeration to decide if when the bubble is popped the time is stopped or all bubbles are popped. + */ + private enum Status{ + FREEZE, + POP + } }