128 lines
4.2 KiB
Java
128 lines
4.2 KiB
Java
import java.awt.*;
|
|
import java.awt.geom.Ellipse2D;
|
|
/**
|
|
* Beschreiben Sie hier die Klasse Spielfiguren.
|
|
*
|
|
* @author (Ihr Name)
|
|
* @version (eine Versionsnummer oder ein Datum)
|
|
*/
|
|
public abstract class Spielfiguren extends GameObject
|
|
{
|
|
protected final Spiel spiel;
|
|
protected double centerX;
|
|
protected double centerY;
|
|
protected final double radius;
|
|
protected final double Geschwindigkeit;
|
|
protected Color color;
|
|
|
|
protected int bevorzugteRichtungX;
|
|
protected int bevorzugteRichtungY;
|
|
protected int bewegteRichtungX;
|
|
protected int bewegteRichtungY;
|
|
|
|
private final double initialX;
|
|
private final double initialY;
|
|
|
|
public Spielfiguren(Spiel spiel, double centerX, double centerY, double radius, double Geschwindigkeit, Color color){
|
|
this.spiel = spiel;
|
|
this.centerX = centerX;
|
|
this.centerY = centerY;
|
|
this.radius = radius;
|
|
this.Geschwindigkeit = Geschwindigkeit;
|
|
this.color = color;
|
|
|
|
initialX = centerX;
|
|
initialY = centerY;
|
|
}
|
|
|
|
public void zuruecksetzen(){
|
|
centerX = initialX;
|
|
centerY = initialY;
|
|
bevorzugteRichtungX= 0;
|
|
bevorzugteRichtungY = 0;
|
|
bewegteRichtungX = 0;
|
|
bewegteRichtungY = 0;
|
|
}
|
|
|
|
public void tickBewegteRichtung(){
|
|
if(bewegteRichtungX == 0 && bewegteRichtungY == 0){
|
|
bewegteRichtungX = bevorzugteRichtungX;
|
|
bewegteRichtungY = bevorzugteRichtungY;
|
|
}else if(bewegteRichtungX != 0 && bevorzugteRichtungX != 0){
|
|
bewegteRichtungX = bevorzugteRichtungX;
|
|
}else if(bewegteRichtungY != 0 && bevorzugteRichtungY != 0){
|
|
bewegteRichtungY = bevorzugteRichtungY;
|
|
}
|
|
}
|
|
|
|
public void tickWallCollisions(){
|
|
Labyrinth labyrinth = spiel.getLabyrinth();
|
|
|
|
if(bewegteRichtungX == 1 && !labyrinth.isFrei((int) (centerX + 0.5), (int) centerY)){
|
|
centerX = (int) centerX + 0.5;
|
|
bewegteRichtungX = 0;
|
|
}else if(bewegteRichtungX == -1 && !labyrinth.isFrei((int) (centerX -0.5), (int) centerY)){
|
|
centerX = (int) centerX + 0.5;
|
|
bewegteRichtungX = 0;
|
|
}else if(bewegteRichtungY == 1 && !labyrinth.isFrei((int) centerX, (int) (centerY + 0.5))){
|
|
centerY = (int) centerY + 0.5;
|
|
bewegteRichtungY = 0;
|
|
}else if(bewegteRichtungY == -1 && !labyrinth.isFrei((int) centerX, (int) (centerY - 0.5))){
|
|
centerY = (int) centerY + 0.5;
|
|
bewegteRichtungY = 0;
|
|
}
|
|
}
|
|
|
|
|
|
private void tickTurn(boolean crossedCenterX, boolean crossedCenterY){
|
|
boolean turnXToY = crossedCenterX && bewegteRichtungX != 0 && bevorzugteRichtungY != 0 && spiel.getLabyrinth().isFrei((int) centerX, (int) (centerY + bevorzugteRichtungY));
|
|
boolean turnYToX = crossedCenterY && bewegteRichtungY != 0 && bevorzugteRichtungX != 0 && spiel.getLabyrinth().isFrei((int) (centerX + bevorzugteRichtungX), (int) centerY);
|
|
if(turnXToY){
|
|
centerX = (int) centerX + 0.5;
|
|
bewegteRichtungX = 0;
|
|
bewegteRichtungY = bevorzugteRichtungY;
|
|
}else if(turnYToX){
|
|
centerX = (int) centerX + 0.5;
|
|
bewegteRichtungY = 0;
|
|
bewegteRichtungX = bevorzugteRichtungX;
|
|
}
|
|
}
|
|
|
|
public void tick(){
|
|
tickBewegteRichtung();
|
|
|
|
double newX = centerX + bewegteRichtungX * Geschwindigkeit;
|
|
double newY = centerY + bewegteRichtungY * Geschwindigkeit;
|
|
|
|
boolean crossedCenterX = Math.abs((centerX - 0.5)%1.0 - (newX - 0.5) % 1.0)> 0.5;
|
|
boolean crossedCenterY = Math.abs((centerY - 0.5)%1.0 - (newY - 0.5) % 1.0)> 0.5;
|
|
|
|
centerX = newX;
|
|
centerY = newY;
|
|
|
|
|
|
tickTurn(crossedCenterX, crossedCenterY);
|
|
tickWallCollisions();
|
|
}
|
|
|
|
public double getCenterX(){
|
|
return centerX;
|
|
}
|
|
|
|
public double getCenterY(){
|
|
return centerY;
|
|
}
|
|
|
|
public double getRadius(){
|
|
return radius;
|
|
}
|
|
|
|
public int getBewegteRichtungX() {
|
|
return bewegteRichtungX;
|
|
}
|
|
|
|
public int getBewegteRichtungY() {
|
|
return bewegteRichtungY;
|
|
}
|
|
}
|