From 686f7442c8c7442019158bf337866e17c97a8d6a Mon Sep 17 00:00:00 2001 From: agreiner Date: Mon, 7 Jun 2021 15:30:36 +0200 Subject: [PATCH] Praktikum 9 --- .../graphics/moveable/FollowerBall.java | 79 +++++++++++++++++++ .../graphics/moveable/RandomBall.java | 57 +++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/moveable/FollowerBall.java create mode 100644 SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/moveable/RandomBall.java diff --git a/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/moveable/FollowerBall.java b/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/moveable/FollowerBall.java new file mode 100644 index 0000000..504bb1c --- /dev/null +++ b/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/moveable/FollowerBall.java @@ -0,0 +1,79 @@ +package de.thdeg.berl.ufofight.graphics.movingobjects; + +import de.thdeg.berl.ufofight.gameview.GameView; +import de.thdeg.berl.ufofight.graphics.base.GameObject; +import de.thdeg.berl.ufofight.graphics.base.MovingGameObject; +import de.thdeg.berl.ufofight.graphics.base.Position; + +import java.awt.*; + +/** This ball follows another ball. */ +public class FollowerBall extends GameObject implements MovingGameObject { + + private enum Status {FOLLOW, CENTER, HIDE} + + private final RandomBall followMe; + private Position targetPosition; + private Status status; + + /** + * Creates the GameObject with the GameView to be displayed on. + * + * @param gameView Window to show the GameObject on. + * @param followMe The RandomBall to follow. + */ + public FollowerBall(GameView gameView, RandomBall followMe) { + super(gameView); + this.status = Status.CENTER; + this.followMe = followMe; + this.position = new Position(GameView.WIDTH, GameView.HEIGHT); + this.size = 50; + this.speedInPixel = 2; + + } + + @Override + public void updatePosition() { + if (gameView.timerExpired("Status", "FollowerBall")) { + gameView.setTimer("Status", "FollowerBall", 3000); + switch (status) { + case HIDE: + status = Status.CENTER; + break; + case CENTER: + status = Status.FOLLOW; + break; + case FOLLOW: + status = Status.HIDE; + break; + } + } + if (status == Status.FOLLOW) { + targetPosition = followMe.getPosition().clone(); + targetPosition.y += 25; + } else if (status == Status.CENTER) { + targetPosition = new Position(GameView.WIDTH / 2d, GameView.HEIGHT / 2d); + } + if (status != Status.HIDE) { + double distance = position.distance(targetPosition); + if (distance >= speedInPixel) { + position.right((targetPosition.x - position.x) / distance * speedInPixel); + position.down((targetPosition.y - position.y) / distance * speedInPixel); + } + } + } + + @Override + protected void updateStatus() { + + } + + @Override + public void addToCanvas() { + if (status == Status.HIDE) { + gameView.addOvalToCanvas(position.x, position.y, size, size, 2, true, Color.WHITE); + } else { + gameView.addOvalToCanvas(position.x, position.y, size, size, 2, true, Color.GREEN); + } + } +} diff --git a/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/moveable/RandomBall.java b/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/moveable/RandomBall.java new file mode 100644 index 0000000..76c5110 --- /dev/null +++ b/SuperPangWorld/src/de/thdeg/greiner/superpangworld/graphics/moveable/RandomBall.java @@ -0,0 +1,57 @@ +package de.thdeg.berl.ufofight.graphics.movingobjects; + +import de.thdeg.berl.ufofight.gameview.GameView; +import de.thdeg.berl.ufofight.graphics.base.GameObject; +import de.thdeg.berl.ufofight.graphics.base.MovingGameObject; +import de.thdeg.berl.ufofight.graphics.base.Position; + +import java.awt.*; +import java.util.Random; + +/** Ball with random movement. */ +public class RandomBall extends GameObject implements MovingGameObject { + private final Position targetPosition; + private final Random random; + + /** + * Creates the GameObject with the GameView to be displayed on. + * + * @param gameView Window to show the GameObject on. + */ + public RandomBall(GameView gameView) { + super(gameView); + this.targetPosition = new Position(800, 200); + this.random = new Random(); + this.size = 50; + this.speedInPixel = 4; + } + + @Override + public void updatePosition() { + double distance = position.distance(targetPosition); + if (distance >= speedInPixel) { + position.right((targetPosition.x - position.x) / distance * speedInPixel); + position.down((targetPosition.y - position.y) / distance * speedInPixel); + } else { + setRandomTargetPosition(); + } + } + + + @Override + protected void updateStatus() { + + } + + /** Set position to aim at */ + public void setRandomTargetPosition() { + targetPosition.x = random.nextInt(GameView.WIDTH); + targetPosition.y = random.nextInt(GameView.HEIGHT); + } + + @Override + public void addToCanvas() { + gameView.addOvalToCanvas(position.x, position.y, size, size, 2, true, Color.YELLOW); + gameView.addOvalToCanvas(targetPosition.x, targetPosition.y, size, size, 2, false, Color.WHITE); + } +}