diff --git a/src/main/java/pacmanGame/Ghost.java b/src/main/java/pacmanGame/Ghost.java index 7261114..7884171 100644 --- a/src/main/java/pacmanGame/Ghost.java +++ b/src/main/java/pacmanGame/Ghost.java @@ -10,7 +10,7 @@ public class Ghost { this.gameManager = gameManager; this.ghostNumber = ghostNumber; this.position = new Vector2(-1, -1); - behavior = new GhostBehaviorRandom(); + behavior = new GhostBehaviorChase(); } public void setPosition(Vector2 newPosition) { diff --git a/src/main/java/pacmanGame/GhostBehaviorChase.java b/src/main/java/pacmanGame/GhostBehaviorChase.java index e505fca..ae4036e 100644 --- a/src/main/java/pacmanGame/GhostBehaviorChase.java +++ b/src/main/java/pacmanGame/GhostBehaviorChase.java @@ -1,12 +1,70 @@ package pacmanGame; +import java.util.Random; + public class GhostBehaviorChase implements GhostBehavior { @Override 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); + } + + 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; } - } diff --git a/src/main/java/pacmanGame/Map.java b/src/main/java/pacmanGame/Map.java index 8412837..605978e 100644 --- a/src/main/java/pacmanGame/Map.java +++ b/src/main/java/pacmanGame/Map.java @@ -19,7 +19,7 @@ public class Map { "wwwwwww.wwwwwewwewwwww.wwwwwww", "wwwwwww.wwwwwewwewwwww.wwwwwww", "wwwwwww.wweeeeeeeeeeww.wwwwwww", - "wwwwwww.wwewwwwwwwweww.wwwwwww", + "wwwwwww.wwewwweewwweww.wwwwwww", "wwwwwww.wweweeeeeeweww.wwwwwww", "Teeeeee.eeew804629weee.eeeeeet", "wwwwwww.wwewe1573eweww.wwwwwww", @@ -162,10 +162,9 @@ public class Map { for(int x = 0; x < size.x; x++) { for(int y = 0; y < size.y; y++) { - - if(tempMap[x][y] == 'e') { + Vector2 pos = new Vector2(x,y); + if(tempMap[x][y] == 'e' && (pos.Add(new Vector2(-1,-1))).isWithin(size.Add(new Vector2(-2,-2)))) { - Vector2 pos = new Vector2(x,y); char newChar = 'x'; if(targetChars.indexOf(tempMap[x + 1][y]) >= 0) {