Browse Source

Implemented GhostBehaviorChase

main
fdai7012 11 months ago
parent
commit
4d2bc56ace
  1. 2
      src/main/java/pacmanGame/Ghost.java
  2. 60
      src/main/java/pacmanGame/GhostBehaviorChase.java
  3. 7
      src/main/java/pacmanGame/Map.java

2
src/main/java/pacmanGame/Ghost.java

@ -10,7 +10,7 @@ public class Ghost {
this.gameManager = gameManager; this.gameManager = gameManager;
this.ghostNumber = ghostNumber; this.ghostNumber = ghostNumber;
this.position = new Vector2(-1, -1); this.position = new Vector2(-1, -1);
behavior = new GhostBehaviorRandom();
behavior = new GhostBehaviorChase();
} }
public void setPosition(Vector2 newPosition) { public void setPosition(Vector2 newPosition) {

60
src/main/java/pacmanGame/GhostBehaviorChase.java

@ -1,12 +1,70 @@
package pacmanGame; package pacmanGame;
import java.util.Random;
public class GhostBehaviorChase implements GhostBehavior { public class GhostBehaviorChase implements GhostBehavior {
@Override @Override
public Vector2 GetDirection(Ghost ghost) { public Vector2 GetDirection(Ghost ghost) {
for(int attempts = 10; attempts > 0; attempts--) {
Vector2 direction = ComputeDirection(ghost);
boolean isValied = DirectionIsValid(ghost, direction);
if(isValied) {
return direction;
}
}
return new Vector2(0,0);
}
return null;
public boolean DirectionIsValid(Ghost ghost, Vector2 direction) {
return !ghost.gameManager.map.getCell(ghost.position.Add(direction)).type.equals("wall");
} }
public Vector2 ComputeDirection(Ghost ghost) {
Vector2 from = ghost.position;
Vector2 to = ghost.gameManager.player.position;
String path = ghost.gameManager.map.FindPath(from, to);
int distanceToPlayer = Math.abs(from.x - to.x) + Math.abs(from.y - to.y);
if(distanceToPlayer < 5) {
int random = new Random().nextInt(4);
if(random == 0) {
return new Vector2(0, 1);
}
if(random == 1) {
return new Vector2(0, -1);
}
if(random == 2) {
return new Vector2(1, 0);
}
else {
return new Vector2(-1, 0);
}
}
if(path.length() == 0) {
return new Vector2(0,0);
}
char nextDir = path.charAt(0);
if(nextDir == 'u') {
return new Vector2(0, 1);
}
else if(nextDir == 'd') {
return new Vector2(0, -1);
}
else if(nextDir == 'l') {
return new Vector2(-1, 0);
}
else if(nextDir == 'r') {
return new Vector2(1, 0);
}
return null;
}
} }

7
src/main/java/pacmanGame/Map.java

@ -19,7 +19,7 @@ public class Map {
"wwwwwww.wwwwwewwewwwww.wwwwwww", "wwwwwww.wwwwwewwewwwww.wwwwwww",
"wwwwwww.wwwwwewwewwwww.wwwwwww", "wwwwwww.wwwwwewwewwwww.wwwwwww",
"wwwwwww.wweeeeeeeeeeww.wwwwwww", "wwwwwww.wweeeeeeeeeeww.wwwwwww",
"wwwwwww.wwewwwwwwwweww.wwwwwww",
"wwwwwww.wwewwweewwweww.wwwwwww",
"wwwwwww.wweweeeeeeweww.wwwwwww", "wwwwwww.wweweeeeeeweww.wwwwwww",
"Teeeeee.eeew804629weee.eeeeeet", "Teeeeee.eeew804629weee.eeeeeet",
"wwwwwww.wwewe1573eweww.wwwwwww", "wwwwwww.wwewe1573eweww.wwwwwww",
@ -162,10 +162,9 @@ public class Map {
for(int x = 0; x < size.x; x++) { for(int x = 0; x < size.x; x++) {
for(int y = 0; y < size.y; y++) { for(int y = 0; y < size.y; y++) {
Vector2 pos = new Vector2(x,y);
if(tempMap[x][y] == 'e' && (pos.Add(new Vector2(-1,-1))).isWithin(size.Add(new Vector2(-2,-2)))) {
if(tempMap[x][y] == 'e') {
Vector2 pos = new Vector2(x,y);
char newChar = 'x'; char newChar = 'x';
if(targetChars.indexOf(tempMap[x + 1][y]) >= 0) { if(targetChars.indexOf(tempMap[x + 1][y]) >= 0) {

Loading…
Cancel
Save