Skip to content

Commit

Permalink
Refac
Browse files Browse the repository at this point in the history
  • Loading branch information
armin-reichert committed Aug 21, 2024
1 parent e9b1e67 commit cbd82e5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
12 changes: 0 additions & 12 deletions core/src/main/java/de/amr/games/pacman/model/GameWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.List;
import java.util.stream.Stream;

import static de.amr.games.pacman.lib.Globals.*;
Expand Down Expand Up @@ -46,7 +45,6 @@ public class GameWorld {
private final Direction[] ghostDirections = new Direction[4];
private final Vector2i[] energizerTiles;
private final Portal[] portals;
private final List<Vector2i> cannotMoveUpTiles = new ArrayList<>();

private Vector2i houseTopLeftTile;
private Vector2i houseSize;
Expand Down Expand Up @@ -129,16 +127,6 @@ public boolean containsPoint(double x, double y) {
return 0 <= x && x <= map.terrain().numCols() * TS && 0 <= y && y <= map.terrain().numRows() * TS;
}

public void setCannotMoveUpTiles(List<Vector2i> tiles) {
checkNotNull(map);
cannotMoveUpTiles.clear();
cannotMoveUpTiles.addAll(tiles);
}

public List<Vector2i> cannotMoveUpTiles() {
return cannotMoveUpTiles;
}

public Stream<Vector2i> energizerTiles() {
return Arrays.stream(energizerTiles);
}
Expand Down
13 changes: 12 additions & 1 deletion core/src/main/java/de/amr/games/pacman/model/actors/Ghost.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import de.amr.games.pacman.model.GameWorld;
import org.tinylog.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

Expand Down Expand Up @@ -46,6 +48,7 @@ public class Ghost extends Creature {
private float speedInsideHouse;
private Animations animations;
private Consumer<Ghost> huntingBehaviour = game -> {};
private List<Vector2i> cannotMoveUpTiles = List.of();

public Ghost(byte id) {
this(id, null);
Expand Down Expand Up @@ -110,6 +113,14 @@ public void setRevivalPosition(Vector2f position) {
revivalPosition = position;
}

public void setCannotMoveUpTiles(List<Vector2i> tiles) {
cannotMoveUpTiles = new ArrayList<>(tiles);
}

public List<Vector2i> cannotMoveUpTiles() {
return cannotMoveUpTiles;
}

public void setSpeedReturningHome(float pixelsPerTick) {
speedReturningToHouse = pixelsPerTick;
}
Expand Down Expand Up @@ -163,7 +174,7 @@ public boolean canAccessTile(Vector2i tile) {
// hunting ghosts cannot move up at certain tiles in Pac-Man game
if (state == HUNTING_PAC) {
var currentTile = tile();
if (world.cannotMoveUpTiles().contains(currentTile)) {
if (cannotMoveUpTiles.contains(currentTile)) {
if (currentTile.plus(UP.vector()).equals(tile)) {
Logger.trace("Hunting {} cannot move up at {}", name, currentTile);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ public void buildRegularLevel(int levelNumber) {
pac.setName("Pac-Man");
pac.setAutopilot(new RuleBasedPacSteering(this));
pac.setUseAutopilot(false);
ghosts().forEach(ghost -> ghost.setHuntingBehaviour(this::ghostHuntingBehaviour));
ghosts().forEach(ghost -> {
ghost.setHuntingBehaviour(this::ghostHuntingBehaviour);
ghost.setCannotMoveUpTiles(CANNOT_MOVE_UP_TILES);
});
}

@Override
Expand All @@ -114,7 +117,10 @@ public void buildDemoLevel() {
pac.setName("Pac-Man");
pac.setAutopilot(new RouteBasedSteering(List.of(PACMAN_DEMO_LEVEL_ROUTE)));
pac.setUseAutopilot(true);
ghosts().forEach(ghost -> ghost.setHuntingBehaviour(this::ghostHuntingBehaviour));
ghosts().forEach(ghost -> {
ghost.setHuntingBehaviour(this::ghostHuntingBehaviour);
ghost.setCannotMoveUpTiles(CANNOT_MOVE_UP_TILES);
});
}

@Override
Expand Down Expand Up @@ -168,7 +174,6 @@ protected void ghostHuntingBehaviour(Ghost ghost) {
private GameWorld createWorld() {
var world = new GameWorld(WORLD_MAP);
world.createArcadeHouse(10, 15);
world.setCannotMoveUpTiles(CANNOT_MOVE_UP_TILES);
return world;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,14 @@ protected void drawSceneInfo() {
var game = context.game();
drawTileGrid();
if (game.variant() == GameVariant.PACMAN && game.world() != null) {
game.world().cannotMoveUpTiles().forEach(tile -> {
g.setFill(Color.RED);
g.fillOval(s(t(tile.x())), s(t(tile.y() - 1)), s(TS), s(TS));
g.setFill(Color.WHITE);
g.fillRect(s(t(tile.x()) + 1), s(t(tile.y()) - HTS - 1), s(TS - 2), s(2));
game.ghosts().forEach(ghost -> {
// Are currently the same for each ghost, but who knows what comes...
ghost.cannotMoveUpTiles().forEach(tile -> {
g.setFill(Color.RED);
g.fillOval(s(t(tile.x())), s(t(tile.y() - 1)), s(TS), s(TS));
g.setFill(Color.WHITE);
g.fillRect(s(t(tile.x()) + 1), s(t(tile.y()) - HTS - 1), s(TS - 2), s(2));
});
});
}
g.setFill(Color.YELLOW);
Expand Down

0 comments on commit cbd82e5

Please sign in to comment.