Compare commits

..

4 Commits

Author SHA1 Message Date
f001cf0a41 Implemented frightmode 2025-08-29 13:28:09 +02:00
09e0396cc3 Removed collisionmask for large pellets 2025-08-28 17:39:49 +02:00
76cb35c955 Score Large Pellet 2025-08-28 16:36:18 +02:00
cbcc0af47c Refactored GameMap 2025-08-27 20:03:59 +02:00
15 changed files with 391 additions and 153 deletions

View File

@ -5,6 +5,7 @@ import lombok.extern.slf4j.Slf4j;
import se.urmo.game.main.GamePanel;
import se.urmo.game.util.Direction;
import se.urmo.game.map.GameMap;
import se.urmo.game.util.Pair;
import java.awt.Point;
import java.util.Collections;
@ -31,7 +32,8 @@ public class CollisionChecker {
);
};
log.debug("{} boundaries for {} are {}", direction, position, boundaries);
List<Pair> bs = boundaries.stream().map(p -> new Pair(p.x, p.y, GameMap.screenToRow(p.y), GameMap.screenToCol(p.x))).toList();
log.debug("{} boundaries for {} are {}", direction, position, bs);
List<Point> normalized = boundaries.stream()
.map(p -> normalizePosition(direction, p, agent_width, agent_height))

View File

@ -18,33 +18,12 @@ public class GhostCollisionChecker {
}
public List<Direction> calculateDirectionAlternatives(Point position) {
List<Direction> intersection = directionAlternatives(position);
List<Direction> intersection = map.directionAlternatives(position);
log.info("Possible travel directions: {}", intersection);
return intersection;
}
public List<Direction> directionAlternatives(Point position) {
int row = (position.y - GameMap.OFFSET_Y) / GameMap.MAP_TILESIZE;
int col = (position.x - GameMap.OFFSET_X) / GameMap.MAP_TILESIZE;
record DirectionCheck(int rowOffset, int colOffset, Direction direction) {}
log.debug("At [{}][{}]", row, col);
return Stream.of(
new DirectionCheck(0, 1, Direction.RIGHT),
new DirectionCheck(0, -1, Direction.LEFT),
new DirectionCheck(1, 0, Direction.DOWN),
new DirectionCheck(-1, 0, Direction.UP)
)
.filter(dc -> {
int r = row + dc.rowOffset;
int c = col + dc.colOffset;
boolean solid = map.isSolid(r, c);
log.debug("[{}][{}] {} is {}", r, c, dc.direction, solid ? "solid" : " not solid");
return !solid;
})
.map(DirectionCheck::direction)
.toList();
}
public Point canMoveTo(Direction dir, Point pos) {
// -1 is because else we endup in next tile

View File

@ -0,0 +1,32 @@
package se.urmo.game.entities;
import se.urmo.game.map.GameMap;
import se.urmo.game.util.Direction;
import java.awt.Point;
import java.util.List;
import java.util.Random;
public class FearStrategy implements GhostStrategy {
private final Random random = new Random();
@Override
public Point chooseTarget(Ghost ghost, PacMan pacman, GameMap map) {
// Frightened ghosts do not target Pacman.
// Instead, they pick a random adjacent valid tile.
List<Direction> neighbors = map.directionAlternatives(ghost.getPosition());
if (neighbors.isEmpty()) {
return ghost.getPosition(); // stuck
}
//Transform directions to actual Points
List<Point> potentialTargets = neighbors.stream()
.map(d -> new Point(
ghost.getPosition().x + d.dx * GameMap.MAP_TILESIZE,
ghost.getPosition().y + d.dy * GameMap.MAP_TILESIZE)).toList();
// Pick a random valid neighbor
return potentialTargets.get(random.nextInt(neighbors.size()));
}
}

View File

@ -3,6 +3,7 @@ package se.urmo.game.entities;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import se.urmo.game.collision.GhostCollisionChecker;
import se.urmo.game.main.Game;
import se.urmo.game.map.GameMap;
import se.urmo.game.state.GhostManager;
import se.urmo.game.util.Direction;
@ -18,16 +19,20 @@ import java.util.stream.Collectors;
@Slf4j
public class Ghost {
private static final int WARNING_THRESHOLD = 180; // 3 seconds of warning
private static final int COLLISION_BOX_SIZE = 16;
private static final int GHOST_MOVEMENT_UPDATE_FREQUENCY = 2;
public static final int GHOST_SIZE = 32;
private static final int ANIMATION_UPDATE_FREQUENCY = 25;
private static final int COLLISION_BOX_OFFSET = COLLISION_BOX_SIZE / 2;
private static final BufferedImage COLLISION_BOX = MiscUtil.createOutlinedBox(COLLISION_BOX_SIZE, COLLISION_BOX_SIZE, Color.black, 2);
private static final int FRIGHTENED_DURATION_TICKS = 10 * Game.UPS_SET;
private final GhostCollisionChecker collisionChecker;
private final GhostStrategy chaseStrategy;
private final Point startPos;
private final BufferedImage[] fearAnimation;
private final BufferedImage[] baseAnimation;
@Getter
private Point position;
@ -36,23 +41,29 @@ public class Ghost {
private int aniIndex = 0;
private final GhostStrategy scaterStrategy;
private GhostStrategy currentStrategy;
private final BufferedImage[] animation;
private BufferedImage[] animation;
private int movementTick = 0;
private Direction direction;
private Direction prevDirection;
private GhostMode mode;
private final GhostStrategy fearStrategy = new FearStrategy();
private int frightenedTimer = 0;
private boolean isBlinking = false;
public Ghost(GhostCollisionChecker collisionChecker, GhostStrategy strategy, GhostStrategy scaterStrategy, BufferedImage[] animation) {
public Ghost(GhostCollisionChecker collisionChecker, GhostStrategy strategy, GhostStrategy scaterStrategy, BufferedImage[] animation, BufferedImage[] fearAnimation) {
this.collisionChecker = collisionChecker;
this.chaseStrategy = strategy;
this.scaterStrategy = scaterStrategy;
this.animation = animation;
this.baseAnimation = animation;
this.fearAnimation = fearAnimation;
position = new Point(
13 * GameMap.MAP_TILESIZE + GameMap.OFFSET_X + (GameMap.MAP_TILESIZE / 2),
4 * GameMap.MAP_TILESIZE + GameMap.OFFSET_Y + (GameMap.MAP_TILESIZE / 2) );
startPos = position;
this.currentStrategy = chaseStrategy;
this.animation = baseAnimation;
}
public void draw(Graphics g) {
@ -71,17 +82,36 @@ public class Ghost {
public void update(PacMan pacman, GameMap map) {
updateAnimationTick();
if(mode == GhostMode.FRIGHTENED){
updateInFrightendMode();
}
updatePosition(pacman, map);
}
private void updateInFrightendMode() {
frightenedTimer--;
if(frightenedTimer <= WARNING_THRESHOLD){
isBlinking = (frightenedTimer / 25) % 2 == 0;
animation = isBlinking ? fearAnimation : baseAnimation;
}
if(frightenedTimer <= 0){
setMode(GhostMode.CHASE);
}
}
private void updatePosition(PacMan pacman, GameMap map) {
// only move ghost on update interval - this is basically ghost speed;
if (movementTick >= GHOST_MOVEMENT_UPDATE_FREQUENCY) {
chooseDirection(pacman, map);
if (map.isAligned(position)) {
//log.info("Evaluating possible directions");
prevDirection = direction;
direction = chooseDirection(
prioritize(collisionChecker.calculateDirectionAlternatives(position)),
currentStrategy.chooseTarget(this, pacman, map));
//log.info("selecting direction {}", direction);
}
Point newPosition = getNewPosition();
move(newPosition);
moveTo(getNewPosition());
movementTick = 0;
} else movementTick++;
@ -89,6 +119,7 @@ public class Ghost {
/**
* Given a position and a direction - calculate the new position
* Moves one pixel in the given direction
*
* @return new position
*/
@ -101,25 +132,7 @@ public class Ghost {
}
/**
* Choose a new direction when 'aligned' ie when in the exact middle of a tile
* else continue with the existing direction.
*
* @param pacman
* @param map
*/
private void chooseDirection(PacMan pacman, GameMap map) {
if (isAlligned(position)) {
log.info("Evaluating possible directions");
prevDirection = direction;
direction = chooseDirection(
prioritize(collisionChecker.calculateDirectionAlternatives(position)),
currentStrategy.chooseTarget(this, pacman, map));
log.info("selecting direction {}", direction);
}
}
private void move(Point newPosition) {
private void moveTo(Point newPosition) {
Point destination = collisionChecker.canMoveTo(direction, newPosition);
if (destination != null) {
@ -137,14 +150,7 @@ public class Ghost {
}
private boolean isAlligned(Point pos) {
int row = pos.x % GameMap.MAP_TILESIZE;
int col = pos.y % GameMap.MAP_TILESIZE;
return row == GameMap.MAP_TILESIZE / 2 && col == GameMap.MAP_TILESIZE / 2;
}
private Direction chooseDirection(Map<Direction, Integer> options, Point target) {
// Find the lowest priority
int lowestPriority = options.values().stream()
.mapToInt(Integer::intValue)
@ -189,15 +195,23 @@ public class Ghost {
public void setMode(GhostMode mode) {
this.mode = mode;
switch (mode) {
case CHASE -> currentStrategy = chaseStrategy;
case CHASE -> {
animation = baseAnimation;
currentStrategy = chaseStrategy;
}
case SCATTER -> currentStrategy = scaterStrategy;
case FRIGHTENED -> currentStrategy = null;
case FRIGHTENED -> {
frightenedTimer = FRIGHTENED_DURATION_TICKS;
isBlinking = false;
animation = fearAnimation;
currentStrategy = fearStrategy;
}
case EATEN -> currentStrategy = null;
}
}
public boolean isFrightened() {
return false;
return mode == GhostMode.FRIGHTENED;
}
public void resetPosition() {

View File

@ -77,13 +77,12 @@ public class PacMan {
PACMAN_SIZE,
PACMAN_SIZE, null);
g.drawImage(COLLISION_BOX, position.x - COLLISION_BOX_OFFSET, position.y - COLLISION_BOX_OFFSET, COLLISION_BOX_SIZE, COLLISION_BOX_SIZE, null);
g.setColor(Color.BLUE);
//g.setColor(Color.BLUE);
//g.fillRect(position.x-1, position.y-1, 3, 3);
}
public void update() {
updateAnimationTick();
//if(direction == Direction.NONE) return;
if(moving) {
Point newPosition = switch (direction) {
case RIGHT -> new Point(position.x + speed, position.y);

View File

@ -1,13 +1,14 @@
package se.urmo.game.map;
import lombok.extern.slf4j.Slf4j;
import se.urmo.game.util.LoadSave;
import se.urmo.game.util.Direction;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;
@Slf4j
public class GameMap {
@ -16,14 +17,11 @@ public class GameMap {
public static final int MAP_ROW_SIZE = 30;
public static final int OFFSET_Y = 7 * MAP_TILESIZE; // 160px from top
public static final int OFFSET_X = MAP_TILESIZE; // 16px from left
private final BufferedImage[][] mapSpriteBuffer;
private final MapTile[][] mapData;
private final BufferedImage[][] mapItemSpriteBuffer;
public GameMap() {
this.mapSpriteBuffer = LoadSave.loadSprites("sprites/PacMan-custom-spritemap-0-3.png", 5, 11, MAP_TILESIZE);
this.mapItemSpriteBuffer = LoadSave.loadSprites("sprites/PacManAssets-Items.png", 2, 8, MAP_TILESIZE);
this.mapData = loadMap("maps/map1.csv", MAP_ROW_SIZE, MAP_COL_SIZE);
public GameMap(String mapFilePath) {
this.mapData = loadMap(mapFilePath, MAP_ROW_SIZE, MAP_COL_SIZE);
}
private MapTile[][] loadMap(String path, int mapRowSize, int mapColSize) {
@ -45,7 +43,9 @@ public class GameMap {
for (int col = 0; col < mapColSize; col++) {
int value = Integer.parseInt(tokens[col].trim());
data[rowIndex][col] = new MapTile(getSprite(value), value);
TileType type = TileType.fromValue(value);
BufferedImage sprite = SpriteSheetManager.get(type.getSpriteSheet()).getSprite(type);
data[rowIndex][col] = new MapTile(sprite, type);
}
rowIndex++;
}
@ -64,69 +64,6 @@ public class GameMap {
return data;
}
private BufferedImage getSprite(int value) {
return switch (value){
case 0 -> mapSpriteBuffer[1][1];
case 1 -> mapSpriteBuffer[0][0];
case 2 -> mapSpriteBuffer[0][1];
case 3 -> mapSpriteBuffer[0][2];
case 4 -> mapSpriteBuffer[0][3];
case 5 -> mapSpriteBuffer[0][4];
case 6 -> mapSpriteBuffer[0][5];
case 7 -> mapSpriteBuffer[0][6];
case 8 -> mapSpriteBuffer[0][7];
case 9 -> mapSpriteBuffer[0][8];
case 10 -> mapSpriteBuffer[0][9];
case 11 -> mapSpriteBuffer[0][10];
case 12 -> mapSpriteBuffer[1][0];
case 13 -> mapSpriteBuffer[1][1];
case 14 -> mapSpriteBuffer[1][2];
case 15 -> mapSpriteBuffer[1][3];
case 16 -> mapSpriteBuffer[1][4];
case 17 -> mapSpriteBuffer[1][5];
case 18 -> mapSpriteBuffer[1][6];
case 19 -> mapSpriteBuffer[1][7];
case 20 -> mapSpriteBuffer[1][8];
case 21 -> mapSpriteBuffer[1][9];
case 22 -> mapSpriteBuffer[1][10];
case 23 -> mapSpriteBuffer[2][0];
case 24 -> mapSpriteBuffer[2][1];
case 25 -> mapSpriteBuffer[2][2];
case 26 -> mapSpriteBuffer[2][3];
case 27 -> mapSpriteBuffer[2][4];
case 28 -> mapSpriteBuffer[2][5];
case 29 -> mapSpriteBuffer[2][6];
case 30 -> mapSpriteBuffer[2][7];
case 31 -> mapSpriteBuffer[2][8];
case 32 -> mapSpriteBuffer[2][9];
case 33 -> mapSpriteBuffer[2][10];
case 34 -> mapSpriteBuffer[3][0];
case 35 -> mapSpriteBuffer[3][1];
case 36 -> mapSpriteBuffer[3][2];
case 37 -> mapSpriteBuffer[3][3];
case 38 -> mapSpriteBuffer[3][4];
case 39 -> mapSpriteBuffer[3][5];
case 40 -> mapSpriteBuffer[3][6];
case 41 -> mapSpriteBuffer[3][7];
case 42 -> mapSpriteBuffer[3][8];
case 43 -> mapSpriteBuffer[3][9];
case 44 -> mapSpriteBuffer[3][10];
case 45 -> mapSpriteBuffer[4][0];
case 46 -> mapSpriteBuffer[4][1];
case 47 -> mapSpriteBuffer[4][2];
case 48 -> mapSpriteBuffer[4][3];
case 49 -> mapSpriteBuffer[4][4];
case 50 -> mapSpriteBuffer[4][5];
case 51 -> mapSpriteBuffer[4][6];
case 52 -> mapSpriteBuffer[4][7];
case 53 -> mapSpriteBuffer[4][8];
case 54 -> mapSpriteBuffer[4][9];
case 55 -> mapSpriteBuffer[4][10];
default -> null;
};
}
public void draw(Graphics g) {
for (int row = 0; row < mapData.length; row++) {
for (int col = 0; col < mapData[row].length; col++) {
@ -141,6 +78,11 @@ public class GameMap {
}
}
private BufferedImage getSprite(int value) {
TileType type = TileType.fromValue(value);
return SpriteSheetManager.get(type.getSpriteSheet()).getSprite(type);
}
public boolean isPassable(List<Point> list){
return list.stream().allMatch(p -> isPassable(p.x, p.y));
}
@ -159,17 +101,16 @@ public class GameMap {
}
public boolean isSolid(Point pos) {
int row = screenToRow(pos);
int col = screenToCol(pos);
return isSolid(row,col);
return isSolid(screenToRow(pos), screenToCol(pos));
}
public boolean isSolid(int row, int col) {
if (col >= columns() || col < 0 ) return true;
if(row >= rows() || row < 0) return true;
MapTile mapTile = mapData[row][col];
boolean solid = mapTile.isSolid();
// log.debug("[{}][{}] is {} ({})", row, col, solid ? "solid" : " not solid", mapTile.getValue());
log.debug("[{}][{}] {}", row, col, mapTile.getTileType());
return solid;
}
@ -186,11 +127,13 @@ public class GameMap {
}
MapTile tile = mapData[row][col];
if (tile != null && tile.getValue() == 0 && tile.getImage() != null) {
TileType type = tile.getTileType();
if (type.isRemovable() && tile.getImage() != null) {
log.debug("Removing tile {}", tile);
tile.setImage(null);
return true;
}
return false;
}
@ -246,4 +189,40 @@ public class GameMap {
return (screenY - OFFSET_Y) / MAP_TILESIZE;
}
public MapTile getTile(Point screenPos) {
int r = screenToRow(screenPos);
int c = screenToCol(screenPos);
return mapData[r][c];
}
public List<Direction> directionAlternatives(Point screen) {
int row = (screen.y - GameMap.OFFSET_Y) / GameMap.MAP_TILESIZE;
int col = (screen.x - GameMap.OFFSET_X) / GameMap.MAP_TILESIZE;
record DirectionCheck(int rowOffset, int colOffset, Direction direction) {}
log.debug("At [{}][{}]", row, col);
return Stream.of(
new DirectionCheck(0, 1, Direction.RIGHT),
new DirectionCheck(0, -1, Direction.LEFT),
new DirectionCheck(1, 0, Direction.DOWN),
new DirectionCheck(-1, 0, Direction.UP)
)
.filter(dc -> {
int r = row + dc.rowOffset;
int c = col + dc.colOffset;
boolean solid = isSolid(r, c);
log.debug("[{}][{}] {} is {}", r, c, dc.direction, solid ? "solid" : " not solid");
return !solid;
})
.map(DirectionCheck::direction)
.toList();
}
public boolean isAligned(Point pos) {
int row = pos.x % GameMap.MAP_TILESIZE;
int col = pos.y % GameMap.MAP_TILESIZE;
return row == GameMap.MAP_TILESIZE / 2 && col == GameMap.MAP_TILESIZE / 2;
}
}

View File

@ -8,16 +8,14 @@ import java.awt.image.BufferedImage;
@Getter
@Setter
public class MapTile {
private final int value;
private final TileType tileType;
private BufferedImage image;
private final boolean solid;
private final boolean[][] collisionMask;
public MapTile(BufferedImage image, int value) {
this.value = value;
public MapTile(BufferedImage image, TileType tileType) {
this.tileType = tileType;
this.image = image;
this.solid = value != 0 && value != 99;
this.collisionMask = value != 0 ? createCollisionMask(image) : null;
this.collisionMask = tileType.isSolid() ? createCollisionMask(image) : null;
}
private boolean[][] createCollisionMask(BufferedImage img) {
@ -34,4 +32,14 @@ public class MapTile {
return mask;
}
public boolean isSolid() {
return tileType.isSolid();
}
@Override
public String toString() {
return "MapTile{" +
"tileType=" + tileType +
'}';
}
}

View File

@ -0,0 +1,32 @@
package se.urmo.game.map;
import lombok.Getter;
import se.urmo.game.util.LoadSave;
import java.awt.image.BufferedImage;
@Getter
public enum SpriteLocation {
MAP("sprites/PacMan-custom-spritemap-0-3.png", 5, 11),
ITEM("sprites/PacManAssets-Items.png", 2, 8),
NONE("", 0, 0) { // Special case for tiles without sprites
@Override
public BufferedImage[][] loadSprites(int tileSize) {
return new BufferedImage[][] {{ null }}; // Single null sprite
}
};
private final String path;
private final int rows;
private final int cols;
SpriteLocation(String path, int rows, int cols) {
this.path = path;
this.rows = rows;
this.cols = cols;
}
public BufferedImage[][] loadSprites(int tileSize) {
return LoadSave.loadSprites(path, rows, cols, tileSize);
}
}

View File

@ -0,0 +1,34 @@
package se.urmo.game.map;
import lombok.Getter;
import java.awt.image.BufferedImage;
public class SpriteSheet {
private final BufferedImage[][] spriteSheet;
@Getter
private final SpriteLocation location;
public SpriteSheet(SpriteLocation location) {
this.location = location;
this.spriteSheet = location.loadSprites(GameMap.MAP_TILESIZE);
}
public BufferedImage getSprite(TileType tileType) {
if (tileType.getSpriteSheet() != location) {
throw new IllegalArgumentException("Tile type belongs to different sprite sheet");
}
// NONE will always return null without additional checks
return getSprite(tileType.getRow(), tileType.getCol());
}
public BufferedImage getSprite(int row, int col) {
if (location == SpriteLocation.NONE) {
return null;
}
if (row >= spriteSheet.length || col >= spriteSheet[0].length) {
return null;
}
return spriteSheet[row][col];
}
}

View File

@ -0,0 +1,19 @@
package se.urmo.game.map;
import java.util.EnumMap;
import java.util.Map;
public class SpriteSheetManager {
private static final Map<SpriteLocation, SpriteSheet> spriteSheets = new EnumMap<>(SpriteLocation.class);
public static SpriteSheet get(SpriteLocation location) {
return spriteSheets.computeIfAbsent(location, SpriteSheet::new);
}
// Optionally add methods like:
public static void reloadAll() {
spriteSheets.clear();
}
}

View File

@ -0,0 +1,105 @@
package se.urmo.game.map;
import lombok.Getter;
@Getter
public enum TileType {
SMALL_PELLET(0, false, SpriteLocation.ITEM, 1, 0, true, 10),
TILE_1(1, true, SpriteLocation.MAP, 0, 0, false, 0),
TILE_2(2, true, SpriteLocation.MAP, 0, 1, false, 0),
TILE_3(3, true, SpriteLocation.MAP, 0, 2, false, 0),
TILE_4(4, true, SpriteLocation.MAP, 0, 3, false, 0),
TILE_5(5, true, SpriteLocation.MAP, 0, 4, false, 0),
TILE_6(6, true, SpriteLocation.MAP, 0, 5, false, 0),
TILE_7(7, true, SpriteLocation.MAP, 0, 6, false, 0),
TILE_8(8, true, SpriteLocation.MAP, 0, 7, false, 0),
TILE_9(9, true, SpriteLocation.MAP, 0, 8, false ,0),
TILE_10(10, true, SpriteLocation.MAP, 0, 9, false ,0),
TILE_11(11, true, SpriteLocation.MAP, 0, 10, false, 0),
TILE_12(12, true, SpriteLocation.MAP, 1, 0, false ,0),
TILE_13(13, true, SpriteLocation.MAP, 1, 1, false ,0),
TILE_14(14, true, SpriteLocation.MAP, 1, 2, false ,0),
TILE_15(15, true, SpriteLocation.MAP, 1, 3, false ,0),
TILE_16(16, true, SpriteLocation.MAP, 1, 4, false ,0),
TILE_17(17, true, SpriteLocation.MAP, 1, 5, false ,0),
TILE_18(18, true, SpriteLocation.MAP, 1, 6, false ,0),
TILE_19(19, true, SpriteLocation.MAP, 1, 7, false ,0),
TILE_20(20, true, SpriteLocation.MAP, 1, 8, false ,0),
TILE_21(21, true, SpriteLocation.MAP, 1, 9, false ,0),
TILE_22(22, true, SpriteLocation.MAP, 1, 10, false ,0),
TILE_23(23, true, SpriteLocation.MAP, 2, 0, false ,0),
TILE_24(24, true, SpriteLocation.MAP, 2, 1, false ,0),
TILE_25(25, true, SpriteLocation.MAP, 2, 2, false ,0),
TILE_26(26, true, SpriteLocation.MAP, 2, 3, false ,0),
TILE_27(27, true, SpriteLocation.MAP, 2, 4, false ,0),
TILE_28(28, true, SpriteLocation.MAP, 2, 5, false ,0),
TILE_29(29, true, SpriteLocation.MAP, 2, 6, false ,0),
TILE_30(30, true, SpriteLocation.MAP, 2, 7, false ,0),
TILE_31(31, true, SpriteLocation.MAP, 2, 8, false ,0),
TILE_32(32, true, SpriteLocation.MAP, 2, 9, false ,0),
TILE_33(33, true, SpriteLocation.MAP, 2, 10, false ,0),
TILE_34(34, true, SpriteLocation.MAP, 3, 0, false ,0),
TILE_35(35, true, SpriteLocation.MAP, 3, 1, false ,0),
TILE_36(36, true, SpriteLocation.MAP, 3, 2, false ,0),
TILE_37(37, true, SpriteLocation.MAP, 3, 3, false ,0),
TILE_38(38, true, SpriteLocation.MAP, 3, 4, false ,0),
TILE_39(39, true, SpriteLocation.MAP, 3, 5, false ,0),
TILE_40(40, true, SpriteLocation.MAP, 3, 6, false ,0),
TILE_41(41, true, SpriteLocation.MAP, 3, 7, false ,0),
TILE_42(42, true, SpriteLocation.MAP, 3, 8, false ,0),
TILE_43(43, true, SpriteLocation.MAP, 3, 9, false ,0),
TILE_44(44, true, SpriteLocation.MAP, 3, 10, false ,0),
TILE_45(45, true, SpriteLocation.MAP, 4, 0, false ,0),
TILE_46(46, true, SpriteLocation.MAP, 4, 1, false ,0),
TILE_47(47, true, SpriteLocation.MAP, 4, 2, false ,0),
TILE_48(48, true, SpriteLocation.MAP, 4, 3, false ,0),
TILE_49(49, true, SpriteLocation.MAP, 4, 4, false ,0),
TILE_50(50, true, SpriteLocation.MAP, 4, 5, false ,0),
TILE_51(51, true, SpriteLocation.MAP, 4, 6, false ,0),
TILE_52(52, true, SpriteLocation.MAP, 4, 7, false ,0),
TILE_53(53, true, SpriteLocation.MAP, 4, 8, false ,0),
TILE_54(54, true, SpriteLocation.MAP, 4, 9, false ,0),
TILE_55(55, true, SpriteLocation.MAP, 4, 10, false ,0),
LARGE_PELLET(56, false, SpriteLocation.ITEM, 1 ,1,true, 50),
EMPTY(99, false, SpriteLocation.NONE, 0, 0, false, 0); // No sprite associated with empty tiles
private final int value;
private final boolean solid;
private final SpriteLocation spriteSheet;
private final int row;
private final int col;
private final boolean removable;
private final int score;
TileType(int value, boolean solid, SpriteLocation spriteSheet, int row, int col, boolean removable, int score) {
this.value = value;
this.solid = solid;
this.spriteSheet = spriteSheet;
this.row = row;
this.col = col;
this.removable = removable;
this.score = score;
}
public static TileType fromValue(int value) {
for (TileType type : values()) {
if (type.value == value) return type;
}
return EMPTY;
}
public boolean hasSprite() {
return spriteSheet != null;
}
@Override
public String toString() {
return "TileType{" +
"value=" + value +
", removable=" + removable +
", score=" + score +
", solid=" + solid +
'}';
}
}

View File

@ -46,11 +46,11 @@ public class GhostManager {
loadAnimation();
// Create ghosts with their strategies
Ghost blinky = new Ghost(ghostCollisionChecker, new BlinkyStrategy(), new ScatterToTopRight(), image[0]);
Ghost blinky = new Ghost(ghostCollisionChecker, new BlinkyStrategy(), new ScatterToTopRight(), image[0], image[9]);
ghosts.add(blinky);
ghosts.add(new Ghost(ghostCollisionChecker, new PinkyStrategy(),new ScatterToTopLeft(), image[2]));
ghosts.add(new Ghost(ghostCollisionChecker,new InkyStrategy(blinky), new ScatterToBottomRight(), image[1]));
ghosts.add(new Ghost(ghostCollisionChecker, new ClydeStrategy(), new ScatterToBottomLeft(), image[3]));
//ghosts.add(new Ghost(ghostCollisionChecker, new PinkyStrategy(),new ScatterToTopLeft(), image[2], image[9]));
//ghosts.add(new Ghost(ghostCollisionChecker,new InkyStrategy(blinky), new ScatterToBottomRight(), image[1], image[9]));
//ghosts.add(new Ghost(ghostCollisionChecker, new ClydeStrategy(), new ScatterToBottomLeft(), image[3], image[8]));
setMode(GhostMode.CHASE);
}
@ -67,7 +67,7 @@ public class GhostManager {
public void setMode(GhostMode mode) {
this.globalMode = mode;
log.info("Mode changed to {}", globalMode);
log.debug("Mode changed to {}", globalMode);
for (Ghost g : ghosts) {
g.setMode(mode);
}
@ -97,4 +97,8 @@ public class GhostManager {
ghost.draw(g);
}
}
public void setFrightMode() {
this.setMode(GhostMode.FRIGHTENED);
}
}

View File

@ -5,9 +5,12 @@ import lombok.extern.slf4j.Slf4j;
import se.urmo.game.collision.CollisionChecker;
import se.urmo.game.collision.GhostCollisionChecker;
import se.urmo.game.entities.Ghost;
import se.urmo.game.entities.GhostMode;
import se.urmo.game.entities.PacMan;
import se.urmo.game.main.Game;
import se.urmo.game.map.GameMap;
import se.urmo.game.map.MapTile;
import se.urmo.game.map.TileType;
import se.urmo.game.util.Direction;
import java.awt.*;
@ -29,7 +32,7 @@ public class PlayingState implements GameState {
public PlayingState(Game game, GameStateManager gameStateManager) {
this.game = game;
this.gameStateManager = gameStateManager;
this.map = new GameMap();
this.map = new GameMap("maps/map1.csv");
this.pacman = new PacMan(game, new CollisionChecker(map));
this.ghostManager = new GhostManager(new GhostCollisionChecker(map));
this.arcadeFont = loadArcadeFont();
@ -45,10 +48,13 @@ public class PlayingState implements GameState {
private void handleDots() {
Point pacmanScreenPos = pacman.getPosition();
MapTile tile = map.getTile(pacmanScreenPos);
boolean wasRemoved = map.removeTileImage(pacmanScreenPos);
if(wasRemoved && tile.getTileType() == TileType.LARGE_PELLET){
ghostManager.setFrightMode();
}
if(wasRemoved){
score+=10;
score+=tile.getTileType().getScore();
}
}
@ -105,6 +111,7 @@ public class PlayingState implements GameState {
// Pac-Man eats ghost
score += 200;
ghost.resetPosition();
ghost.setMode(GhostMode.CHASE); // end frightend
} else {
// Pac-Man loses a life
lives--;

View File

@ -0,0 +1,24 @@
package se.urmo.game.util;
public class Pair{
private final int x;
private final int y;
int row;
int col;
public Pair(int x, int y, int row, int col){
this.x = x;
this.y = y;
this.row = row;
this.col = col;
}
@Override
public String toString() {
return "Pair{" +
"x=" + x +
", y=" + y +
", row=" + row +
", col=" + col +
'}';
}
}

View File

@ -1,7 +1,7 @@
1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,45,46, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3
12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,15,17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,14
12, 0, 4, 5, 5, 6, 0, 4, 5, 5, 5, 6, 0,15,17, 0, 4, 5, 5, 5, 6, 0, 4, 5, 5, 6, 0,14
12, 0,26,27,27,28, 0,26,27,27,27,28, 0,26,28, 0,26,27,27,27,28, 0,26,27,27,28, 0,14
12,56,26,27,27,28, 0,26,27,27,27,28, 0,26,28, 0,26,27,27,27,28, 0,26,27,27,28,56,14
12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,14
12, 0, 4, 5, 5, 6, 0, 4, 6, 0, 4, 5, 5, 5, 5, 5, 5, 6, 0, 4, 6, 0, 4, 5, 5, 6, 0,14
12, 0,26,27,27,28, 0,15,17, 0,26,27,27, 7, 8,27,27,28, 0,15,17, 0,26,27,27,28, 0,14

1 1 2 2 2 2 2 2 2 2 2 2 2 2 45 46 2 2 2 2 2 2 2 2 2 2 2 2 3
2 12 0 0 0 0 0 0 0 0 0 0 0 0 15 17 0 0 0 0 0 0 0 0 0 0 0 0 14
3 12 0 4 5 5 6 0 4 5 5 5 6 0 15 17 0 4 5 5 5 6 0 4 5 5 6 0 14
4 12 0 56 26 27 27 28 0 26 27 27 27 28 0 26 28 0 26 27 27 27 28 0 26 27 27 28 0 56 14
5 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 14
6 12 0 4 5 5 6 0 4 6 0 4 5 5 5 5 5 5 6 0 4 6 0 4 5 5 6 0 14
7 12 0 26 27 27 28 0 15 17 0 26 27 27 7 8 27 27 28 0 15 17 0 26 27 27 28 0 14