Browse Source

Merge branch 'ghostsbehaviour'

remotes/origin/playerRefactoring
fdai2751 11 months ago
parent
commit
5bb3552356
  1. 5
      src/main/java/pacmanGame/GameManager.java
  2. 6
      src/main/java/pacmanGame/Ghost.java
  3. 2
      src/main/java/pacmanGame/GhostBehavior.java
  4. 8
      src/main/java/pacmanGame/GhostBehaviorChase.java
  5. 24
      src/main/java/pacmanGame/GhostBehaviorRandom.java
  6. 58
      src/main/java/pacmanGame/VisualizerPlainText.java

5
src/main/java/pacmanGame/GameManager.java

@ -54,12 +54,17 @@ public class GameManager {
public void Update() { public void Update() {
visualizer.Update(); visualizer.Update();
if(!isPaused) { if(!isPaused) {
if(time%moveSpeed == 0) { if(time%moveSpeed == 0) {
player.Move(); player.Move();
for(int i = 0; i<ghostCount; i++) {
ghosts[i].move();
}
} }
time++; time++;
} }
if(time == 300) { if(time == 300) {
spawnFruit(); spawnFruit();
} }

6
src/main/java/pacmanGame/Ghost.java

@ -4,12 +4,13 @@ public class Ghost {
public Vector2 position; public Vector2 position;
public final GameManager gameManager; public final GameManager gameManager;
public final int ghostNumber; public final int ghostNumber;
public GhostBehavior behavior;
public Ghost(GameManager gameManager, int ghostNumber) { public Ghost(GameManager gameManager, int ghostNumber) {
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();
} }
public void setPosition(Vector2 newPosition) { public void setPosition(Vector2 newPosition) {
@ -23,4 +24,7 @@ public class Ghost {
public void spawn() { public void spawn() {
this.position = gameManager.map.ghostSpawns[ghostNumber].Clone(); this.position = gameManager.map.ghostSpawns[ghostNumber].Clone();
} }
public void move() {
move(behavior.GetDirection(this));
}
} }

2
src/main/java/pacmanGame/GhostBehavior.java

@ -1,7 +1,7 @@
package pacmanGame; package pacmanGame;
public interface GhostBehavior { public interface GhostBehavior {
public Vector2 GetDirection();
public Vector2 GetDirection(Ghost ghost);
} }

8
src/main/java/pacmanGame/GhostBehaviorChase.java

@ -1,14 +1,10 @@
package pacmanGame; package pacmanGame;
public class GhostBehaviorChase implements GhostBehavior { public class GhostBehaviorChase implements GhostBehavior {
public final Ghost ghost;
public GhostBehaviorChase(Ghost ghost) {
this.ghost = ghost;
}
@Override @Override
public Vector2 GetDirection() {
public Vector2 GetDirection(Ghost ghost) {
return null; return null;
} }

24
src/main/java/pacmanGame/GhostBehaviorRandom.java

@ -0,0 +1,24 @@
package pacmanGame;
import java.util.Random;
public class GhostBehaviorRandom implements GhostBehavior {
@Override
public Vector2 GetDirection(Ghost ghost) {
Random random = new Random();
int direction = random.nextInt(4);
switch(direction) {
case 0: //oben
return new Vector2(0,1);
case 1: //unten
return new Vector2(0,-1);
case 2:// links
return new Vector2(-1,0);
case 3: //rechts
return new Vector2(1,0);
default:
break;
}
return null;
}
}

58
src/main/java/pacmanGame/VisualizerPlainText.java

@ -56,6 +56,7 @@ public class VisualizerPlainText implements Visualizer {
for(int x = 0; x < map.size.x; x++) { for(int x = 0; x < map.size.x; x++) {
Cell cell = map.GetCell(new Vector2(x, map.size.y - 1 - y)); Cell cell = map.GetCell(new Vector2(x, map.size.y - 1 - y));
if(gameManager.isPaused && 5 < y && y <= 10){ if(gameManager.isPaused && 5 < y && y <= 10){
if(x == 0) { if(x == 0) {
if(y == 8){ if(y == 8){
@ -66,8 +67,16 @@ public class VisualizerPlainText implements Visualizer {
} }
} }
} }
else {
boolean containsGhosts = false; boolean containsGhosts = false;
boolean containsGhosts1 = false;
boolean containsGhosts2 = false;
boolean containsGhosts3 = false;
boolean containsGhosts4 = false;
boolean containsGhosts5 = false;
boolean containsGhosts6 = false;
boolean containsGhosts7 = false;
boolean containsGhosts8 = false;
boolean containsPlayer = false; boolean containsPlayer = false;
for( int i = 0; i < gameManager.ghosts.length; i++) { for( int i = 0; i < gameManager.ghosts.length; i++) {
if(gameManager.ghosts[i].position.equals(cell.pos)) { if(gameManager.ghosts[i].position.equals(cell.pos)) {
@ -78,6 +87,53 @@ public class VisualizerPlainText implements Visualizer {
containsPlayer = true; containsPlayer = true;
} }
if(containsPlayer) {
output += playerSprite;
}
else if(containsGhosts) {
output += ghostSprite;
}
else if (containsGhosts1) {
output += ghostSprite1;
}
else if (containsGhosts2) {
output += ghostSprite2;
}
else if (containsGhosts3) {
output += ghostSprite3;
}
else if (containsGhosts4) {
output += ghostSprite4;
}
else if (containsGhosts5) {
output += ghostSprite5;
}
else if (containsGhosts6) {
output += ghostSprite6;
}
else if (containsGhosts7) {
output += ghostSprite7;
}
else if (containsGhosts8) {
output += ghostSprite8;
}
else if (sprites.containsKey(cell.type)) {
output += sprites.get(cell.type);
}
else {
containsGhosts = false;
containsPlayer = false;
for( int i = 0; i < gameManager.ghosts.length; i++) {
if(gameManager.ghosts[i].position.equals(cell.pos)) {
containsGhosts = true;
}
}
if(gameManager.player.position.equals(cell.pos)) {
containsPlayer = true;
}
if(containsPlayer) { if(containsPlayer) {
output += playerSprite; output += playerSprite;
} }

Loading…
Cancel
Save