#1 Neue Physik für die Bubbles implementiert

This commit is contained in:
Andreas Greiner 2021-06-23 22:56:15 +02:00
parent a887d0a9f0
commit 11b7799168
9 changed files with 63 additions and 28 deletions

View File

@ -31,6 +31,7 @@ public class GamePlayManager {
/** Der Spieler */
private final Player player;
/**
* Create the manager handling the gameplay.
*
@ -58,7 +59,7 @@ public class GamePlayManager {
RoundBubble roundBubble = new RoundBubble(gameView, new ArrayList<>(Collections.singletonList(gameObjectManager.getPlayerObject())));
roundBubble.setGamePlayManager(this);
gameObjectManager.getBubbles().add(roundBubble);
gameView.setTimer("SpawnBubble","GamePlayManager",3000);
gameView.setTimer("SpawnBubble","GamePlayManager",30000000);
}
}

View File

@ -1,9 +1,11 @@
package de.thdeg.greiner.superpangworld.graphics.base;
import de.thdeg.greiner.superpangworld.gameview.GameView;
import de.thdeg.greiner.superpangworld.graphics.helper.HelperValues;
import de.thdeg.greiner.superpangworld.graphics.moveable.Harpoon;
import de.thdeg.greiner.superpangworld.graphics.moveable.PlayerObject;
import java.awt.geom.Point2D;
import java.util.*;
/**
@ -11,6 +13,9 @@ import java.util.*;
*/
public abstract class Bubble extends CollidingGameObject implements MovingGameObject{
private static final Point2D VELOCITY_RIGHT = new Point2D.Double(3,1);
private static final Point2D VELOCITY_LEFT = new Point2D.Double(-3,1);
/** Random generator */
private final Random random;
/** Flag, if the bubble is in the spawning phase */
@ -22,6 +27,8 @@ public abstract class Bubble extends CollidingGameObject implements MovingGameOb
/** Temporary movement action */
private Position nextPosition;
private Point2D velocity;
/**
* Create a bubble with default values.
*
@ -43,7 +50,8 @@ public abstract class Bubble extends CollidingGameObject implements MovingGameOb
spawnSpeed = 500;
gameView.setTimer("spawn"+spawnID,"bubble"+spawnID,spawnSpeed);
nextPosition = new Position(random.nextInt(GameView.WIDTH-width),random.nextInt(GameView.HEIGHT-width));
velocity = new Point2D.Double(-3.5,1.4);
}
/**
@ -55,8 +63,9 @@ public abstract class Bubble extends CollidingGameObject implements MovingGameOb
* @param speedInPixel the speed in pixel per tick
* @param position the position
* @param spawning flag, if the bubble is in the spawning phase
* @param moveLeftToRight flag, if the bubble is moving left to right
*/
public Bubble(GameView gameView, ArrayList<CollidableGameObject> objectsToCollideWith, double size, double speedInPixel, Position position, boolean spawning){
public Bubble(GameView gameView, ArrayList<CollidableGameObject> objectsToCollideWith, double size, double speedInPixel, Position position, boolean spawning, boolean moveLeftToRight){
super(gameView,objectsToCollideWith);
rotation = 90;
this.size = size;
@ -72,7 +81,7 @@ public abstract class Bubble extends CollidingGameObject implements MovingGameOb
gameView.setTimer("spawn"+spawnID,"bubble"+spawnID,spawnSpeed);
}
nextPosition = new Position(random.nextInt(GameView.WIDTH-width),random.nextInt(GameView.HEIGHT-width));
velocity = new Point2D.Double(3.5,1.4);
}
/**
@ -81,23 +90,34 @@ public abstract class Bubble extends CollidingGameObject implements MovingGameOb
@Override
public void updatePosition(){
if(spawning){
if(position.y >= 0){
if(position.y >= HelperValues.FRAME_BORDER_WIDTH){
spawning = false;
} else if(gameView.timerExpired("spawn"+spawnID,"bubble"+spawnID)){
position.down(width/4.0);
position.down((width + HelperValues.FRAME_BORDER_WIDTH)/4.0);
gameView.setTimer("spawn"+spawnID,"bubble"+spawnID,spawnSpeed);
}
}else{
double distance = position.distance(nextPosition);
if (distance >= speedInPixel) {
position.right((nextPosition.x - position.x) / distance * speedInPixel);
position.down((nextPosition.y - position.y) / distance * speedInPixel);
} else {
nextPosition = new Position(random.nextInt(GameView.WIDTH-width),random.nextInt(GameView.HEIGHT-width));
}
Point2D newLocation = new Point2D.Double(position.x,position.y);
double speedFactor = 0.2;
newLocation = new Point2D.Double(newLocation.getX() + (velocity.getX() * speedFactor), newLocation.getY() + (velocity.getY() * speedFactor));
velocity = new Point2D.Double(velocity.getX() + HelperValues.BUBBLE_GRAVITY.getX(), velocity.getY() + HelperValues.BUBBLE_GRAVITY.getY());
// Wall bounce
if((newLocation.getX() >= GameView.WIDTH - width - HelperValues.FRAME_BORDER_WIDTH) || newLocation.getX() <= HelperValues.FRAME_BORDER_WIDTH){
velocity = new Point2D.Double(velocity.getX() * -1.0, velocity.getY());
}
// Bottom bounce
if(newLocation.getY() >= HelperValues.FRAME_WINDOW_HEIGHT + HelperValues.FRAME_BORDER_WIDTH - width){
velocity = new Point2D.Double(velocity.getX(), velocity.getY() * -0.95);
newLocation = new Point2D.Double(newLocation.getX(), HelperValues.FRAME_WINDOW_HEIGHT + HelperValues.FRAME_BORDER_WIDTH - width);
}
position.setTo(newLocation.getX(), newLocation.getY());
}
}
/**
@ -127,11 +147,20 @@ public abstract class Bubble extends CollidingGameObject implements MovingGameOb
return speedInPixel;
}
/**
* Get the flag if the bubble is in its spawning phase.
*
* @return <code>true</code> if in spawning phase, else <code>false</code>
*/
public boolean isSpawning(){
return spawning;
}
@Override
public void reactToCollision(CollidableGameObject otherObject) {
if(otherObject instanceof PlayerObject){
gamePlayManager.looseLife();
}else if(otherObject instanceof Harpoon){
}else if(otherObject instanceof Harpoon && !spawning){
gamePlayManager.destroy((Harpoon) otherObject);
gamePlayManager.destroy(this);
}

View File

@ -1,2 +1,12 @@
package de.thdeg.greiner.superpangworld.graphics.helper;public class HelperValues {
package de.thdeg.greiner.superpangworld.graphics.helper;
import java.awt.geom.Point2D;
public class HelperValues {
public static final int FRAME_BORDER_WIDTH = 8;
public static final int FRAME_WINDOW_HEIGHT = 432;
public static Point2D BUBBLE_GRAVITY = new Point2D.Double(0,0.2);
}

View File

@ -18,8 +18,6 @@ public class Harpoon extends CollidingGameObject implements MovingGameObject {
" kkkkk \n"+
"k kkk k\n"+
"k kkk k\n";
private boolean stuckAtRoof;
private final long harpoonID;
/**
* Create a harpoon with default values.
@ -37,8 +35,6 @@ public class Harpoon extends CollidingGameObject implements MovingGameObject {
getPosition().setTo(300,300);
this.hitBox.width = width;
this.hitBox.height = (int) (440 - position.y);
stuckAtRoof = false;
harpoonID = UUID.randomUUID().getLeastSignificantBits();
}
@Override
@ -50,9 +46,12 @@ public class Harpoon extends CollidingGameObject implements MovingGameObject {
@Override
public void reactToCollision(CollidableGameObject otherObject) {
gamePlayManager.destroy(this);
if(Bubble.class.isAssignableFrom(otherObject.getClass())){
Bubble bubble = (Bubble) otherObject;
if(!bubble.isSpawning()){
gamePlayManager.destroy((Bubble)otherObject);
gamePlayManager.destroy(this);
}
}
}
@ -72,9 +71,8 @@ public class Harpoon extends CollidingGameObject implements MovingGameObject {
public void updatePosition(){
if(position.y > 8){
getPosition().up(Math.min(speedInPixel, position.y-8));
}else if(!stuckAtRoof){
stuckAtRoof = true;
gameView.setTimer("stuckAtRoof"+harpoonID,"harpoon"+harpoonID,1000);
}else{
gamePlayManager.destroy(this);
}
}
@ -83,8 +81,5 @@ public class Harpoon extends CollidingGameObject implements MovingGameObject {
*/
@Override
public void updateStatus() {
if(stuckAtRoof && gameView.timerExpired("stuckAtRoof"+harpoonID,"harpoon"+harpoonID)){
gamePlayManager.destroy(this);
}
}
}