Praktikum 8
This commit is contained in:
parent
937d74a0db
commit
2803a4f048
@ -1,12 +1,17 @@
|
||||
package de.thdeg.greiner.superpangworld.game.managers;
|
||||
|
||||
import de.thdeg.greiner.superpangworld.gameview.GameView;
|
||||
import de.thdeg.greiner.superpangworld.graphics.base.Bubble;
|
||||
import de.thdeg.greiner.superpangworld.graphics.base.CollidableGameObject;
|
||||
import de.thdeg.greiner.superpangworld.graphics.base.Position;
|
||||
import de.thdeg.greiner.superpangworld.graphics.moveable.Harpoon;
|
||||
import de.thdeg.greiner.superpangworld.graphics.moveable.HexagonalBubble;
|
||||
import de.thdeg.greiner.superpangworld.graphics.moveable.RoundBubble;
|
||||
import de.thdeg.greiner.superpangworld.graphics.moveable.SpecialBubble;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
@ -75,11 +80,17 @@ public class GamePlayManager {
|
||||
private void addRandomBubble(){
|
||||
int randomNumber = random.nextInt(100);
|
||||
if(randomNumber<60){
|
||||
gameObjectManager.getBubbles().add(new RoundBubble(gameView));
|
||||
RoundBubble roundBubble = new RoundBubble(gameView, new ArrayList<>(Arrays.asList(gameObjectManager.getPlayer())));
|
||||
roundBubble.setGamePlayManager(this);
|
||||
gameObjectManager.getBubbles().add(roundBubble);
|
||||
}else if(randomNumber<90){
|
||||
gameObjectManager.getBubbles().add(new HexagonalBubble(gameView));
|
||||
HexagonalBubble hexagonalBubble = new HexagonalBubble(gameView, new ArrayList<>(Arrays.asList(gameObjectManager.getPlayer())));
|
||||
hexagonalBubble.setGamePlayManager(this);
|
||||
gameObjectManager.getBubbles().add(hexagonalBubble);
|
||||
}else{
|
||||
gameObjectManager.getBubbles().add(new SpecialBubble(gameView));
|
||||
SpecialBubble specialBubble = new SpecialBubble(gameView, new ArrayList<>(Arrays.asList(gameObjectManager.getPlayer())));
|
||||
specialBubble.setGamePlayManager(this);
|
||||
gameObjectManager.getBubbles().add(specialBubble);
|
||||
}
|
||||
}
|
||||
|
||||
@ -89,7 +100,9 @@ public class GamePlayManager {
|
||||
*/
|
||||
public void shootHarpoon(Position startPosition){
|
||||
if(gameView.timerExpired("ShootHarpoon","GamePlayManager")){
|
||||
Harpoon harpoon = new Harpoon(gameView);
|
||||
ArrayList<CollidableGameObject> collidableGameObjects = new ArrayList<>();
|
||||
collidableGameObjects.addAll(gameObjectManager.getBubbles());
|
||||
Harpoon harpoon = new Harpoon(gameView,collidableGameObjects);
|
||||
harpoon.getPosition().setTo(startPosition.x, startPosition.y);
|
||||
harpoon.setGamePlayManager(this);
|
||||
gameObjectManager.getHarpoons().add(harpoon);
|
||||
@ -104,22 +117,12 @@ public class GamePlayManager {
|
||||
public void destroy(Harpoon harpoon){
|
||||
gameObjectManager.getHarpoons().remove(harpoon);
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves the World to the left.
|
||||
*
|
||||
* @param speedInPixel Speed to move the World
|
||||
* Remove a bubble from the game.
|
||||
* @param bubble the bubble to remove
|
||||
*/
|
||||
public void playerMovingRight(double speedInPixel) {
|
||||
gameObjectManager.moveWorld(-speedInPixel, 0);
|
||||
public void destroy(Bubble bubble){
|
||||
gameObjectManager.getBubbles().remove(bubble);
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves the World to the right.
|
||||
*
|
||||
* @param speedInPixel Speed to move the World
|
||||
*/
|
||||
public void playerMovingLeft(double speedInPixel) {
|
||||
gameObjectManager.moveWorld(speedInPixel, 0);
|
||||
}
|
||||
}
|
||||
|
@ -1,25 +1,30 @@
|
||||
package de.thdeg.greiner.superpangworld.graphics.base;
|
||||
|
||||
import de.thdeg.greiner.superpangworld.gameview.GameView;
|
||||
import de.thdeg.greiner.superpangworld.graphics.moveable.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* A Bubble, which moves around on the screen and can be shot by the player.
|
||||
*/
|
||||
public abstract class Bubble extends GameObject implements MovingGameObject{
|
||||
public abstract class Bubble extends CollidingGameObject implements MovingGameObject{
|
||||
|
||||
/** Flag, if the bubble flies from left to right */
|
||||
private boolean flyFromLeftToRight;
|
||||
|
||||
/**
|
||||
* Create a bubble with default values.
|
||||
*
|
||||
* @param gameView the {@link GameView} to display the bubble
|
||||
* @param objectsToCollideWith the list of {@link CollidableGameObject} the bubble can collide with
|
||||
*/
|
||||
public Bubble(GameView gameView){
|
||||
super(gameView);
|
||||
public Bubble(GameView gameView, ArrayList<CollidableGameObject> objectsToCollideWith){
|
||||
super(gameView,objectsToCollideWith);
|
||||
rotation = 90;
|
||||
size = 5;
|
||||
width = 12;
|
||||
height = 12;
|
||||
width = (int) (12 * size);
|
||||
height = (int) (12 * size);
|
||||
speedInPixel = 5;
|
||||
flyFromLeftToRight = true;
|
||||
}
|
||||
@ -55,4 +60,11 @@ public abstract class Bubble extends GameObject implements MovingGameObject{
|
||||
* Freezes the time.
|
||||
*/
|
||||
private void freezeTime(){}
|
||||
|
||||
@Override
|
||||
public void reactToCollision(CollidableGameObject otherObject) {
|
||||
if(otherObject instanceof Player){
|
||||
System.out.println("You died!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,42 @@
|
||||
package de.thdeg.greiner.superpangworld.graphics.base;
|
||||
|
||||
import de.thdeg.greiner.superpangworld.gameview.GameView;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* Represents all game objects that are able to collide with something.
|
||||
*/
|
||||
public abstract class CollidableGameObject extends GameObject {
|
||||
|
||||
protected Rectangle hitBox;
|
||||
|
||||
protected CollidableGameObject(GameView gameView) {
|
||||
super(gameView);
|
||||
this.hitBox = new Rectangle(-100_000, -100_000, 0, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
super.update();
|
||||
updateHitBoxPosition();
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to update the hitBox position of the game object.
|
||||
*/
|
||||
protected abstract void updateHitBoxPosition();
|
||||
|
||||
@Override
|
||||
public void adaptPosition(double adaptX, double adaptY) {
|
||||
super.adaptPosition(adaptX, adaptY);
|
||||
updateHitBoxPosition();
|
||||
}
|
||||
|
||||
/**
|
||||
* If a GameObject has collided with something, it is able to react to the collision
|
||||
*
|
||||
* @param otherObject The other GameObject that is involved in the collision.
|
||||
*/
|
||||
public abstract void reactToCollision(CollidableGameObject otherObject);
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package de.thdeg.greiner.superpangworld.graphics.base;
|
||||
|
||||
import de.thdeg.greiner.superpangworld.gameview.GameView;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* An object that can actively collide with other objects, e.g. a shot.
|
||||
*/
|
||||
public abstract class CollidingGameObject extends CollidableGameObject {
|
||||
|
||||
protected final ArrayList<CollidableGameObject> objectsToCollideWith;
|
||||
|
||||
protected CollidingGameObject(GameView gameView, ArrayList<CollidableGameObject> objectsToCollideWith) {
|
||||
super(gameView);
|
||||
this.objectsToCollideWith = objectsToCollideWith;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
super.update();
|
||||
checkCollisions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to check possible collisions that are actively caused by this game object. Both parties are notified
|
||||
* about the collision.
|
||||
*/
|
||||
private void checkCollisions() {
|
||||
for (CollidableGameObject collidableGameObject : objectsToCollideWith) {
|
||||
if (collidesWith(collidableGameObject)) {
|
||||
reactToCollision(collidableGameObject);
|
||||
collidableGameObject.reactToCollision(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if this game object is collided with the other game object.
|
||||
*
|
||||
* @param other The other game object.
|
||||
* @return <code>true</code> if the there was a collision.
|
||||
*/
|
||||
protected final boolean collidesWith(CollidableGameObject other) {
|
||||
return this.hitBox.intersects(other.hitBox);
|
||||
}
|
||||
}
|
@ -1,14 +1,15 @@
|
||||
package de.thdeg.greiner.superpangworld.graphics.moveable;
|
||||
|
||||
import de.thdeg.greiner.superpangworld.gameview.GameView;
|
||||
import de.thdeg.greiner.superpangworld.graphics.base.Bubble;
|
||||
import de.thdeg.greiner.superpangworld.graphics.base.GameObject;
|
||||
import de.thdeg.greiner.superpangworld.graphics.base.MovingGameObject;
|
||||
import de.thdeg.greiner.superpangworld.graphics.base.*;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* A harpoon which can be fired upwards by the player.
|
||||
*/
|
||||
public class Harpoon extends GameObject implements MovingGameObject {
|
||||
public class Harpoon extends CollidingGameObject implements MovingGameObject {
|
||||
|
||||
private final static String HARPOON =
|
||||
" B \n"+
|
||||
@ -19,16 +20,34 @@ public class Harpoon extends GameObject implements MovingGameObject {
|
||||
|
||||
/**
|
||||
* Create a harpoon with default values
|
||||
*
|
||||
* @param gameView the {@link GameView} to display the bubble
|
||||
* @param objectsToCollideWith the list of {@link CollidableGameObject} the harpoon can collide with
|
||||
*/
|
||||
public Harpoon(GameView gameView){
|
||||
super(gameView);
|
||||
public Harpoon(GameView gameView, ArrayList<CollidableGameObject> objectsToCollideWith){
|
||||
super(gameView, objectsToCollideWith);
|
||||
speedInPixel = 1;
|
||||
size = 3;
|
||||
width = 11;
|
||||
height = 11;
|
||||
width = (int) (7 * size);
|
||||
height = (int) (5 * size);
|
||||
rotation = 0;
|
||||
getPosition().setTo(300,300);
|
||||
this.hitBox.width = (int) (width);
|
||||
this.hitBox.height = (int) (height);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateHitBoxPosition() {
|
||||
hitBox.x = (int) (position.x);
|
||||
hitBox.y = (int) (position.y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reactToCollision(CollidableGameObject otherObject) {
|
||||
gamePlayManager.destroy(this);
|
||||
if(Bubble.class.isAssignableFrom(otherObject.getClass())){
|
||||
gamePlayManager.destroy((Bubble)otherObject);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -37,6 +56,7 @@ public class Harpoon extends GameObject implements MovingGameObject {
|
||||
@Override
|
||||
public void addToCanvas(){
|
||||
gameView.addBlockImageToCanvas(HARPOON, getPosition().x, getPosition().y,size, rotation);
|
||||
gameView.addRectangleToCanvas(hitBox.x, hitBox.y, hitBox.width, hitBox.height,1,false, Color.red);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -2,7 +2,10 @@ package de.thdeg.greiner.superpangworld.graphics.moveable;
|
||||
|
||||
import de.thdeg.greiner.superpangworld.gameview.GameView;
|
||||
import de.thdeg.greiner.superpangworld.graphics.base.Bubble;
|
||||
import de.thdeg.greiner.superpangworld.graphics.base.CollidableGameObject;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
@ -14,20 +17,34 @@ public class HexagonalBubble extends Bubble {
|
||||
* Create a hexagonal bubble with default values.
|
||||
*
|
||||
* @param gameView the {@link GameView} to display the bubble
|
||||
* @param objectsToCollideWith the list of {@link CollidableGameObject} the bubble can collide with
|
||||
*/
|
||||
public HexagonalBubble(GameView gameView) {
|
||||
super(gameView);
|
||||
public HexagonalBubble(GameView gameView, ArrayList<CollidableGameObject> objectsToCollideWith) {
|
||||
super(gameView,objectsToCollideWith);
|
||||
|
||||
Random rand = new Random();
|
||||
position.setTo(rand.nextInt(GameView.WIDTH),rand.nextInt(GameView.HEIGHT));
|
||||
position.setTo(rand.nextInt(GameView.WIDTH/2),rand.nextInt(GameView.HEIGHT/2));
|
||||
|
||||
size=0.6;
|
||||
speedInPixel = 2;
|
||||
|
||||
width = (int) (128 * size);
|
||||
height = (int) (128 * size);
|
||||
|
||||
this.hitBox.width = (int)(width-20);
|
||||
this.hitBox.height = (int)(height-20);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateHitBoxPosition() {
|
||||
hitBox.x = (int) (position.x+10);
|
||||
hitBox.y = (int) (position.y+10);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addToCanvas() {
|
||||
gameView.addImageToCanvas("hexagon.png",position.x,position.y,size,rotation);
|
||||
gameView.addRectangleToCanvas(hitBox.x, hitBox.y, hitBox.width, hitBox.height,1,false, Color.red);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,6 +1,7 @@
|
||||
package de.thdeg.greiner.superpangworld.graphics.moveable;
|
||||
|
||||
import de.thdeg.greiner.superpangworld.gameview.GameView;
|
||||
import de.thdeg.greiner.superpangworld.graphics.base.CollidableGameObject;
|
||||
import de.thdeg.greiner.superpangworld.graphics.base.GameObject;
|
||||
import de.thdeg.greiner.superpangworld.graphics.base.MovingGameObject;
|
||||
|
||||
@ -9,7 +10,7 @@ import java.awt.*;
|
||||
/**
|
||||
* The game character controlled by the player.
|
||||
*/
|
||||
public class Player extends GameObject {
|
||||
public class Player extends CollidableGameObject {
|
||||
|
||||
/** Flag, if the player is shooting */
|
||||
private boolean shooting;
|
||||
@ -44,9 +45,23 @@ public class Player extends GameObject {
|
||||
position.setTo(GameView.WIDTH/2, GameView.HEIGHT/1.5);
|
||||
shooting = false;
|
||||
size = 3;
|
||||
width = (int) size * 12;
|
||||
height = (int) size * 12;
|
||||
width = (int) size * 11;
|
||||
height = (int) size * 17;
|
||||
speedInPixel = 5;
|
||||
|
||||
this.hitBox.width = (int) (width - (2*size));
|
||||
this.hitBox.height = (int) (height - (1*size));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateHitBoxPosition() {
|
||||
hitBox.x = (int) (position.x + (1*size));
|
||||
hitBox.y = (int) (position.y + (1*size));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reactToCollision(CollidableGameObject otherObject) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -57,6 +72,7 @@ public class Player extends GameObject {
|
||||
gameView.addBlockImageToCanvas(SPIELFIGUR, position.x, position.y, size, rotation);
|
||||
}
|
||||
shooting = false;
|
||||
gameView.addRectangleToCanvas(hitBox.x, hitBox.y, hitBox.width, hitBox.height,1,false, Color.red);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -66,19 +82,11 @@ public class Player extends GameObject {
|
||||
|
||||
/** Moves the character to the left */
|
||||
public void left(){
|
||||
if (position.x > 300) {
|
||||
position.left(speedInPixel);
|
||||
} else {
|
||||
gamePlayManager.playerMovingLeft(speedInPixel);
|
||||
}
|
||||
}
|
||||
/** Moves the character to the right */
|
||||
public void right(){
|
||||
if (position.x < GameView.WIDTH - width - 300) {
|
||||
position.right(speedInPixel);
|
||||
} else {
|
||||
gamePlayManager.playerMovingRight(speedInPixel);
|
||||
}
|
||||
}
|
||||
/** Moves the character upwards */
|
||||
public void up(){
|
||||
|
@ -2,7 +2,10 @@ package de.thdeg.greiner.superpangworld.graphics.moveable;
|
||||
|
||||
import de.thdeg.greiner.superpangworld.gameview.GameView;
|
||||
import de.thdeg.greiner.superpangworld.graphics.base.Bubble;
|
||||
import de.thdeg.greiner.superpangworld.graphics.base.CollidableGameObject;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
|
||||
@ -30,12 +33,22 @@ public class RoundBubble extends Bubble {
|
||||
* Create a round bubble with default values.
|
||||
*
|
||||
* @param gameView the {@link GameView} to display the bubble
|
||||
* @param objectsToCollideWith the list of {@link CollidableGameObject} the bubble can collide with
|
||||
*/
|
||||
public RoundBubble(GameView gameView) {
|
||||
super(gameView);
|
||||
public RoundBubble(GameView gameView, ArrayList<CollidableGameObject> objectsToCollideWith) {
|
||||
super(gameView,objectsToCollideWith);
|
||||
|
||||
Random rand = new Random();
|
||||
position.setTo(rand.nextInt(GameView.WIDTH),rand.nextInt(GameView.HEIGHT));
|
||||
|
||||
this.hitBox.width = width-20;
|
||||
this.hitBox.height = height-20;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateHitBoxPosition() {
|
||||
hitBox.x = (int) (position.x+10);
|
||||
hitBox.y = (int) (position.y+10);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -44,6 +57,7 @@ public class RoundBubble extends Bubble {
|
||||
@Override
|
||||
public void addToCanvas(){
|
||||
gameView.addBlockImageToCanvas(RED_BUBBLE, getPosition().x, getPosition().y,size, rotation);
|
||||
gameView.addRectangleToCanvas(hitBox.x, hitBox.y, hitBox.width, hitBox.height,1,false, Color.yellow);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -2,7 +2,10 @@ package de.thdeg.greiner.superpangworld.graphics.moveable;
|
||||
|
||||
import de.thdeg.greiner.superpangworld.gameview.GameView;
|
||||
import de.thdeg.greiner.superpangworld.graphics.base.Bubble;
|
||||
import de.thdeg.greiner.superpangworld.graphics.base.CollidableGameObject;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
@ -29,17 +32,28 @@ public class SpecialBubble extends Bubble {
|
||||
* Create a special bubble with default values.
|
||||
*
|
||||
* @param gameView the {@link GameView} to display the bubble
|
||||
* @param objectsToCollideWith the list of {@link CollidableGameObject} the bubble can collide with
|
||||
*/
|
||||
public SpecialBubble(GameView gameView) {
|
||||
super(gameView);
|
||||
public SpecialBubble(GameView gameView, ArrayList<CollidableGameObject> objectsToCollideWith) {
|
||||
super(gameView,objectsToCollideWith);
|
||||
|
||||
Random rand = new Random();
|
||||
position.setTo(rand.nextInt(GameView.WIDTH),rand.nextInt(GameView.HEIGHT));
|
||||
|
||||
this.hitBox.width = width-20;
|
||||
this.hitBox.height = height-20;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateHitBoxPosition() {
|
||||
hitBox.x = (int) (position.x+10);
|
||||
hitBox.y = (int) (position.y+10);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addToCanvas() {
|
||||
gameView.addBlockImageToCanvas(GREEN_BUBBLE,position.x,position.y,size, rotation);
|
||||
gameView.addRectangleToCanvas(hitBox.x, hitBox.y, hitBox.width, hitBox.height,1,false, Color.red);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user