Praktikum 9

This commit is contained in:
Andreas Greiner 2021-06-07 15:30:36 +02:00
parent a10a43d7ed
commit 686f7442c8
2 changed files with 136 additions and 0 deletions

View File

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

View File

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