fdai7012
11 months ago
3 changed files with 63 additions and 6 deletions
-
2src/main/java/pacmanGame/Ghost.java
-
60src/main/java/pacmanGame/GhostBehaviorChase.java
-
7src/main/java/pacmanGame/Map.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); |
|||
} |
|||
|
|||
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; |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue