Aufgaben 9

This commit is contained in:
Andreas Greiner 2021-05-30 19:51:35 +02:00
parent 2803a4f048
commit a10a43d7ed
9 changed files with 114 additions and 24 deletions

View File

@ -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.
*/

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -38,9 +38,4 @@ public class LevelLabel extends GameObject {
public void updateStatus() {
}
@Override
public String toString() {
return "LevelLabel:" +getPosition().toString();
}
}

View File

@ -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

View File

@ -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.
*/

View File

@ -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);
}
}

View File

@ -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
}
}