Pac-Man/src/Ghost.java

107 lines
2.8 KiB
Java

// Diese Klasse stellt die einzelnen Geister dar.
public class Ghost extends Picture { // Die übergeordnete Klasse ist Picture
private static final long serialVersionUID = -5352006665147359473L;
public float xPos;
public float yPos;
public boolean up;
public boolean down;
public boolean left;
public boolean right;
public boolean feared = false;
public static int fearedTimer;
private double HCost;
private int type;
public Ghost(int type) {
super("Pinky"); // Aufrufen der übergeordneten Klasse
this.type = type;
// xPos = 150 + 40 * index;
this.ogSauce();
}
public void ogSauce() {
if (type == 0) {
this.changeSauce("Blinky");
} else if (type == 2) {
this.changeSauce("Inky");
} else if (type == 3) {
this.changeSauce("Clyde");
} else if (type == 1) {
this.changeSauce("Pinky");
}
}
public void setPos(char coordinate, int newPos) {
if (coordinate == 'x') {
xPos = newPos;
} else if (coordinate == 'y') {
yPos = newPos;
}
}
public int getHCost(Player player, int mod_x, int mod_y) {
if (!feared) {
if (type == 0) {
HCost = (int) Math.sqrt(Math.pow((((xPos - 10) / 20) + mod_x) - ((player.getPos('x', 0) - 10) / 20), 2)
+ Math.pow((((yPos - 10) / 20) + mod_y) - ((player.getPos('y', 0) - 10) / 20), 2));
} else if (type == 1) {
HCost = (int) Math
.sqrt(Math.pow((((xPos - 10) / 20) + mod_x) - ((player.getPos('x', 0) - 10 + 80) / 20), 2)
+ Math.pow((((yPos - 10) / 20) + mod_y) - ((player.getPos('y', 0) - 10) / 20), 2));
} else if (type == 2) {
HCost = (int) Math.sqrt(Math.pow((((xPos - 10) / 20) + mod_x) - ((300 - 10 + 80) / 20), 2)
+ Math.pow((((yPos - 10) / 20) + mod_y) - ((100 - 10) / 20), 2));
} else if (type == 3) {
HCost = (int) Math.sqrt(Math.pow((((xPos - 10) / 20) + mod_x) - ((player.getPos('x', 0) - 10) / 20), 2)
+ Math.pow((((yPos - 10) / 20) + mod_y) - ((player.getPos('y', 0) + 100 - 10) / 20), 2));
}
} else {
HCost = 10 * (1 / (Math.sqrt(Math.pow((((xPos - 10) / 20) + mod_x) - ((player.getPos('x', 0) - 10) / 20), 2)
+ Math.pow((((yPos - 10) / 20) + mod_y) - ((player.getPos('y', 0) - 10) / 20), 2))));
}
return (int) HCost;
}
public int getPos(char coordinate, long dt) { // Hier kommt die zuvor erwähnte delta time ins Spiel
if (coordinate == 'x') { // Auslesen der 'x' - Koordinate:
if (left && dt != 0 && Game.frames % 4 == 0) {
xPos -= 1;
} else if (right && dt != 0 && Game.frames % 4 == 0) {
xPos += 1;
}
return (int) xPos;
} else if (coordinate == 'y') { // Auslesen der 'y' - Koordinate:
if (down && dt != 0 && Game.frames % 4 == 0) {
yPos += 1;
} else if (up && dt != 0 && Game.frames % 4 == 0) {
yPos -= 1;
}
return (int) yPos; // (int)
} else {
return -1;
}
}
}