pathfinding_complete

master
leventius imperatus 2020-06-04 19:54:41 +02:00
parent d6c82f4e4c
commit 5c4d039a30
11 changed files with 169 additions and 89 deletions

View File

@ -1,36 +1,17 @@
############################# #####
# # # # 0 #
###################################
# #
# ############ ###### ########### #
# # # ## ### #####
# # # ########## ### ##########
# # # ################# ##### #####
# # # # # #### ####
# # # # #################### ######
# # # # # P####### ######
# # # # # ################## ######
# # # # # # ######## ######
# # # # # # ####### ## ##
# ###### #
# ############ ###### ########### #
# ############ ########### #
# ## ### #
# ############ 0123 ########### #
# ########## ###### ######### #
# # # # # ############# ## ##
# # # # ################### # ## ##
# # ################# ##
#######################123#########
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
Es werden nur die ersten 35 Zeilen und Zeichen dieses Dokuments benutzt.
# # # P # # #
# ############ # ## # ########### #
# # ## # #
# ############ ## ########### #
# ############# ############ #
# ############# #### ############ #
# #
###################################

1
bin/.gitignore vendored
View File

@ -1 +0,0 @@
/assets/

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.

View File

@ -1,8 +1,12 @@
import java.awt.Color;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.lang.Math;
// In dieser Klasse findet der größte Teil des Spiels statt:
// Rendern von Map, Geistern, Pac-Man etc.
// Überprüfung von Kollisionen verschiedener Elemente (der komplizierteste Teil des Programms)
@ -20,7 +24,7 @@ public class Game {
private long dt;
private long lastT;
private int delaytimer = 9;
private int delaytimer = 10;
@SuppressWarnings("unused")
private long fps = 60;
@ -34,6 +38,9 @@ public class Game {
// nach rechts --> x wird größer
// nach unten --> y wird größer
int minn;
int[] HCost = new int[4];
public Game() { // Erstellen des Konstruktors (Was soll passieren, sobald dieses Klasse
// aufgerufen wird?)
@ -41,12 +48,9 @@ public class Game {
panel = new JPanel(); // darauf werden alle sichtbaren Elemente gespeichert
ghosts = new Ghost[1]; // 4 Geister
ghosts = new Ghost[4]; // 4 Geister
boolean[] ghost_up_possible = new boolean[ghosts.length];
boolean[] ghost_down_possible = new boolean[ghosts.length];
boolean[] ghost_left_possible = new boolean[ghosts.length];
boolean[] ghost_right_possible = new boolean[ghosts.length];
boolean[][] ghost_possible = new boolean[ghosts.length][4];
player = new Player(); // Pac-Man
@ -104,37 +108,109 @@ public class Game {
player.setLocation(player.getPos('x', dt), player.getPos('y', dt));
// Aktualiesieren der Positionen der Geister:
for (int i = 0; i < ghosts.length; i++) {
ghosts[i].setLocation(ghosts[i].getPos('x', dt), ghosts[i].getPos('y', dt));
}
//for (int i = 0; i < ghosts.length; i++) {
// ghosts[i].setLocation(ghosts[i].getPos('x', dt), ghosts[i].getPos('y', dt));
//}
for (int i = 0; i < ghosts.length; i++) {
if (Map.bricks[((ghosts[i].getPos('y', 0) - 10) / 20) - 1][(ghosts[i].getPos('x', 0) - 10)
/ 20] == null) {
ghost_up_possible[i] = true;
}
if (Map.bricks[((ghosts[i].getPos('y', 0) - 10) / 20) + 1][(ghosts[i].getPos('x', 0) - 10)
/ 20] == null) {
ghost_down_possible[i] = true;
ghost_possible[i][0] = false;
ghost_possible[i][1] = false;
ghost_possible[i][2] = false;
ghost_possible[i][3] = false;
if ((Map.bricks[conv(ghosts[i].yPos)][conv(ghosts[i].xPos) + 1] == null && Map.bricks[conv(ghosts[i].yPos + 19)][conv(ghosts[i].xPos) + 1] == null) || (ghosts[i].xPos + 10) % 20 != 0 ){
ghost_possible[i][0] = true;
}
if (Map.bricks[(ghosts[i].getPos('y', 0) - 10) / 20][((ghosts[i].getPos('x', 0) - 10) / 20)
- 1] == null) {
ghost_left_possible[i] = true;
}
if (Map.bricks[(ghosts[i].getPos('y', 0) - 10) / 20][((ghosts[i].getPos('x', 0) - 10) / 20)
+ 1] == null) {
ghost_right_possible[i] = true;
}
ghost_up_possible[i] = false;
ghost_down_possible[i] = false;
ghost_left_possible[i] = false;
ghost_right_possible[i] = false;
if ((Map.bricks[conv(ghosts[i].yPos)][conv(ghosts[i].xPos) - 1] == null && Map.bricks[conv(ghosts[i].yPos + 19)][conv(ghosts[i].xPos) - 1] == null) || (ghosts[i].xPos + 10) % 20 != 0 ){
ghost_possible[i][1] = true;
}
System.out.println(ghosts[0].getHCost(player));
if ((Map.bricks[conv(ghosts[i].yPos) - 1][conv(ghosts[i].xPos)] == null && Map.bricks[conv(ghosts[i].yPos) - 1][conv(ghosts[i].xPos + 19)] == null) || (ghosts[i].yPos + 10) % 20 != 0 ){
ghost_possible[i][2] = true;
}
if ((Map.bricks[conv(ghosts[i].yPos) + 1][conv(ghosts[i].xPos)] == null && Map.bricks[conv(ghosts[i].yPos) + 1][conv(ghosts[i].xPos + 19)] == null) || (ghosts[i].yPos + 10) % 20 != 0 ){
ghost_possible[i][3] = true;
}
System.out.println(HCost[0] + " " + HCost[1] + " " + HCost[2] + " " + HCost[3]);
HCost[0] = ghosts[i].getHCost(player, 1, 0);
HCost[1] = ghosts[i].getHCost(player, -1, 0);
HCost[2] = ghosts[i].getHCost(player, 0, -1);
HCost[3] = ghosts[i].getHCost(player, 0, 1);
//
int minn = 10000;
if (ghosts[i].up == true) {
HCost[3] = 10000;
}
if (ghosts[i].down == true) {
HCost[2] = 10000;
}
if (ghosts[i].left == true) {
HCost[0] = 10000;
}
if (ghosts[i].right == true) {
HCost[1] = 10000;
}
for (int j = 0; j < 4; j++) {
if (ghost_possible[i][j] == false) {
HCost[j] = 10500;
}
}
for (int j = 0; j < 4; j++) {
if (minn > HCost[j]) {
minn = HCost[j];
}
}
//System.out.println(minn);
//
for (int j = 0; j < 4; j++) {
if (HCost[j] == minn) {
if (j == 0) {
ghosts[i].right = true;
ghosts[i].left = false;
ghosts[i].down = false;
ghosts[i].up = false;
break;
}
if (j == 1) {
ghosts[i].left = true;
ghosts[i].right = false;
ghosts[i].down = false;
ghosts[i].up = false;
break;
}
if (j == 2) {
ghosts[i].up = true;
ghosts[i].right = false;
ghosts[i].left = false;
ghosts[i].down = false;
break;
}
if (j == 3) {
ghosts[i].down = true;
ghosts[i].right = false;
ghosts[i].left = false;
ghosts[i].up = false;
break;
}
}
}
}
// Kollision von Pac-Man mit Bricks:
for (int i = 0; i < 35; i++) { // für jeden Brick
@ -170,6 +246,7 @@ public class Game {
player.calcDir(0); // Berechnen wo Pac-Man als nächstes hin soll
for (int i = 0; i < ghosts.length; i++) {
ghosts[i].setLocation(ghosts[i].getPos('x', dt), ghosts[i].getPos('y', dt));
if (player.getPos('x', 0) < ghosts[i].getPos('x', 0) + 20
&& player.getPos('x', 0) > ghosts[i].getPos('x', 0) - 20
&& player.getPos('y', 0) < ghosts[i].getPos('y', 0) + 20
@ -198,4 +275,10 @@ public class Game {
e.printStackTrace();
}
}
private int conv(float a) { //90.0 --> 4
int b = (int) (a - 10 )/ 20;
return b;
}
}

View File

@ -4,9 +4,16 @@ public class Ghost extends Picture { // Die
private static final long serialVersionUID = -5352006665147359473L;
private int xPos;
private int yPos;
public int xPos;
public int yPos;
public boolean up;
public boolean down;
public boolean left;
public boolean right;
//private float speed = 0.00000007f;
private int HCost;
public Ghost(int index) {
@ -25,14 +32,7 @@ public class Ghost extends Picture { // Die
}
public int getPos(char coordinate, long dt) {
if (coordinate == 'x') {
return xPos;
} else if (coordinate == 'y') {
return yPos;
} else
return -1;
}
public void setPos(char coordinate, int newPos) {
if (coordinate == 'x') {
@ -42,28 +42,45 @@ public class Ghost extends Picture { // Die
}
}
public void moveUp() {
/*
public void setDirection(String dir) {
direction = dir;
}
public void moveDown() {
public String getDirection() {
return direction;
}
*/
public int getHCost(Player player, int mod_x, int mod_y) {
public void moveRight() {
}
public void moveLeft() {
}
public int getHCost(Player player) {
HCost = (int) Math.sqrt(Math.pow(xPos - player.getPos('x', 0), 2) + Math.pow(yPos - player.getPos('y', 0), 2));
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));
return 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) {
// xPos -= speed * dt;
xPos -= 1;
} else if (right && dt != 0) {
// xPos += speed * dt;
xPos += 1;
}
return (int) xPos;
} else if (coordinate == 'y') { // Auslesen der 'y' - Koordinate:
if (down && dt != 0) {
// yPos += speed * dt;
yPos += 1;
} else if (up && dt != 0) {
// yPos -= speed * dt;
yPos -= 1;
}
return (int) yPos; //(int)
} else {
return -1;
}
}
}