Pac-Man/src/Ghost.java

128 lines
3.1 KiB
Java
Raw Normal View History

2020-02-17 16:29:50 +01:00
// Diese Klasse stellt die einzelnen Geister dar.
public class Ghost extends Picture { // Die <20>bergeordnete Klasse ist Picture
private static final long serialVersionUID = -5352006665147359473L;
2020-06-06 14:51:42 +02:00
public float xPos;
public float yPos;
2020-06-07 16:27:50 +02:00
2020-06-04 19:54:41 +02:00
public boolean up;
public boolean down;
public boolean left;
public boolean right;
2020-06-07 16:27:50 +02:00
2020-06-07 16:12:00 +02:00
public int dire;
2020-06-07 14:09:46 +02:00
2020-06-07 16:27:50 +02:00
public static int offFrames = 4;
2020-06-07 15:42:46 +02:00
public boolean feared;
2020-06-07 13:47:17 +02:00
public static int fearedTimer;
2020-06-07 15:42:46 +02:00
private int deathTimer;
private boolean isDead;
2020-06-07 13:47:17 +02:00
private double HCost;
2020-06-06 16:14:03 +02:00
private int type;
2020-06-07 14:09:46 +02:00
2020-06-06 16:14:03 +02:00
public Ghost(int type) {
2020-06-07 14:09:46 +02:00
2020-02-17 16:29:50 +01:00
super("Pinky"); // Aufrufen der <20>bergeordneten Klasse
2020-06-06 16:14:03 +02:00
this.type = type;
2020-04-30 21:18:21 +02:00
// xPos = 150 + 40 * index;
2020-02-17 16:29:50 +01:00
2020-06-07 14:09:46 +02:00
this.ogSauce();
}
public void ogSauce() {
2020-06-06 16:14:03 +02:00
if (type == 0) {
2020-02-17 16:29:50 +01:00
this.changeSauce("Blinky");
2020-06-06 16:14:03 +02:00
} else if (type == 2) {
2020-02-17 16:29:50 +01:00
this.changeSauce("Inky");
2020-06-06 16:14:03 +02:00
} else if (type == 3) {
2020-02-17 16:29:50 +01:00
this.changeSauce("Clyde");
2020-06-07 14:09:46 +02:00
} else if (type == 1) {
this.changeSauce("Pinky");
2020-02-17 16:29:50 +01:00
}
2020-06-07 15:42:46 +02:00
2020-02-17 16:29:50 +01:00
}
public void setPos(char coordinate, int newPos) {
if (coordinate == 'x') {
xPos = newPos;
2020-04-30 21:18:21 +02:00
} else if (coordinate == 'y') {
2020-02-17 16:29:50 +01:00
yPos = newPos;
}
}
2020-06-06 14:51:42 +02:00
2020-06-04 19:54:41 +02:00
public int getHCost(Player player, int mod_x, int mod_y) {
2020-06-07 13:47:17 +02:00
if (!feared) {
2020-06-07 14:09:46 +02:00
if (type == 0) {
2020-06-06 16:14:03 +02:00
2020-06-07 14:09:46 +02:00
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) {
2020-06-07 16:12:00 +02:00
HCost = (int) Math.sqrt(Math.pow((((xPos - 10) / 20) + mod_x) - ((200 - 10 + 80) / 20), 2)
+ Math.pow((((yPos - 10) / 20) + mod_y) - ((200 - 10) / 20), 2));
2020-06-07 14:09:46 +02:00
} 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)
2020-06-07 13:47:17 +02:00
+ Math.pow((((yPos - 10) / 20) + mod_y) - ((player.getPos('y', 0) - 10) / 20), 2))));
}
2020-06-07 14:09:46 +02:00
return (int) HCost;
}
2020-06-07 14:09:46 +02:00
2020-06-07 15:42:46 +02:00
public void setDeathTimer(int amount) {
this.deathTimer = amount;
}
public int getDeathTimer() {
return (int) this.deathTimer;
}
public void setIsDead(boolean b) {
this.isDead = b;
}
public boolean getIsDead() {
return this.isDead;
}
2020-06-04 19:54:41 +02:00
public int getPos(char coordinate, long dt) { // Hier kommt die zuvor erw<72>hnte delta time ins Spiel
if (coordinate == 'x') { // Auslesen der 'x' - Koordinate:
2020-06-07 16:27:50 +02:00
if (left && dt != 0 && Game.frames % offFrames == 0) {
2020-06-07 14:09:46 +02:00
xPos -= 1;
2020-06-07 16:27:50 +02:00
} else if (right && dt != 0 && Game.frames % offFrames == 0) {
2020-06-07 14:09:46 +02:00
xPos += 1;
2020-06-04 19:54:41 +02:00
}
return (int) xPos;
} else if (coordinate == 'y') { // Auslesen der 'y' - Koordinate:
2020-06-07 16:27:50 +02:00
if (down && dt != 0 && Game.frames % offFrames == 0) {
2020-06-07 14:09:46 +02:00
yPos += 1;
2020-06-07 16:27:50 +02:00
} else if (up && dt != 0 && Game.frames % offFrames == 0) {
2020-06-07 14:09:46 +02:00
yPos -= 1;
2020-06-04 19:54:41 +02:00
}
2020-06-06 14:51:42 +02:00
return (int) yPos; // (int)
2020-06-04 19:54:41 +02:00
} else {
return -1;
}
}
2020-02-17 16:29:50 +01:00
}