Compare commits
6 Commits
7c79a309e7
...
b13f87f77b
| Author | SHA1 | Date | |
|---|---|---|---|
| b13f87f77b | |||
| fcd686fad0 | |||
| be3c4deb3c | |||
| 3a4a0a1824 | |||
| 2e9e7cc45e | |||
| 61d9df04f9 |
@ -2,13 +2,11 @@ package se.urmo.game.collision;
|
|||||||
|
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import se.urmo.game.entities.MyPoint;
|
import se.urmo.game.util.MyPoint;
|
||||||
import se.urmo.game.main.GamePanel;
|
import se.urmo.game.main.GamePanel;
|
||||||
import se.urmo.game.util.Direction;
|
import se.urmo.game.util.Direction;
|
||||||
import se.urmo.game.map.GameMap;
|
import se.urmo.game.map.GameMap;
|
||||||
import se.urmo.game.util.Pair;
|
|
||||||
|
|
||||||
import java.awt.Point;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|||||||
@ -1,13 +1,11 @@
|
|||||||
package se.urmo.game.collision;
|
package se.urmo.game.collision;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import se.urmo.game.util.MyPoint;
|
||||||
import se.urmo.game.map.GameMap;
|
import se.urmo.game.map.GameMap;
|
||||||
import se.urmo.game.map.MapTile;
|
|
||||||
import se.urmo.game.util.Direction;
|
import se.urmo.game.util.Direction;
|
||||||
|
|
||||||
import java.awt.Point;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Stream;
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class GhostCollisionChecker {
|
public class GhostCollisionChecker {
|
||||||
@ -17,18 +15,20 @@ public class GhostCollisionChecker {
|
|||||||
this.map = map;
|
this.map = map;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Direction> calculateDirectionAlternatives(Point position) {
|
public List<Direction> calculateDirectionAlternatives(MyPoint position) {
|
||||||
List<Direction> intersection = map.directionAlternatives(position);
|
List<Direction> intersection = map.directionAlternatives((int) position.x, (int) position.y);
|
||||||
log.info("Possible travel directions: {}", intersection);
|
log.info("Possible travel directions: {}", intersection);
|
||||||
return intersection;
|
return intersection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public Point canMoveTo(Direction dir, Point pos) {
|
public MyPoint canMoveTo(Direction dir, double x, double y) {
|
||||||
// -1 is because else we endup in next tile
|
// -1 is because else we endup in next tile
|
||||||
Point pp = new Point(pos.x + dir.dx * (GameMap.MAP_TILESIZE/2 - 1), pos.y + dir.dy * (GameMap.MAP_TILESIZE/2 -1) );
|
//Point pp = new Point((int) (x + dir.dx * (GameMap.MAP_TILESIZE/2 - 1)), (int) (y + dir.dy * (GameMap.MAP_TILESIZE/2 -1)));
|
||||||
|
|
||||||
return ! map.isSolid(pp) ? pos : null;
|
return ! map.isSolidXY(
|
||||||
|
(int) (x) + dir.dx * (GameMap.MAP_TILESIZE/2 - 1),
|
||||||
|
(int) (y) + dir.dy * (GameMap.MAP_TILESIZE/2 - 1)) ? new MyPoint(x,y) : null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
8
src/main/java/se/urmo/game/entities/Animated.java
Normal file
8
src/main/java/se/urmo/game/entities/Animated.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package se.urmo.game.entities;
|
||||||
|
|
||||||
|
public interface Animated {
|
||||||
|
void updateAnimationTick();
|
||||||
|
int getAnimationSpeed();
|
||||||
|
boolean isPaused();
|
||||||
|
|
||||||
|
}
|
||||||
41
src/main/java/se/urmo/game/entities/BaseAnimated.java
Normal file
41
src/main/java/se/urmo/game/entities/BaseAnimated.java
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package se.urmo.game.entities;
|
||||||
|
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
abstract public class BaseAnimated implements Animated {
|
||||||
|
private final int animationSpeed;
|
||||||
|
private final int maxSpriteFrames;
|
||||||
|
@Setter
|
||||||
|
protected boolean paused = true;
|
||||||
|
private int aniTick = 0;
|
||||||
|
protected int aniIndex = 0;
|
||||||
|
|
||||||
|
protected BaseAnimated(int animationSpeed, int maxSpriteFrames) {
|
||||||
|
this.animationSpeed = animationSpeed;
|
||||||
|
this.maxSpriteFrames = maxSpriteFrames;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void updateAnimationTick() {
|
||||||
|
if (!paused) {
|
||||||
|
aniTick++;
|
||||||
|
if (aniTick >= animationSpeed) {
|
||||||
|
aniTick = 0;
|
||||||
|
aniIndex++;
|
||||||
|
if (aniIndex >= maxSpriteFrames) {
|
||||||
|
aniIndex = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getAnimationSpeed() {
|
||||||
|
return animationSpeed;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isPaused() {
|
||||||
|
return paused;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,7 +1,8 @@
|
|||||||
package se.urmo.game.entities;
|
package se.urmo.game.entities.collectibles;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import se.urmo.game.entities.pacman.PacMan;
|
||||||
import se.urmo.game.map.GameMap;
|
import se.urmo.game.map.GameMap;
|
||||||
import se.urmo.game.state.FruitType;
|
import se.urmo.game.state.FruitType;
|
||||||
|
|
||||||
@ -35,8 +36,6 @@ public class Fruit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean collidesWith(PacMan pacman) {
|
public boolean collidesWith(PacMan pacman) {
|
||||||
//return pacman.distanceTo(position) < GameMap.MAP_TILESIZE / 2.0;
|
|
||||||
|
|
||||||
Rectangle pacmanBounds = pacman.getBounds();
|
Rectangle pacmanBounds = pacman.getBounds();
|
||||||
Rectangle fruitBounds = new Rectangle(position.x, position.y, sprite.getWidth(), sprite.getHeight());
|
Rectangle fruitBounds = new Rectangle(position.x, position.y, sprite.getWidth(), sprite.getHeight());
|
||||||
return pacmanBounds.intersects(fruitBounds);
|
return pacmanBounds.intersects(fruitBounds);
|
||||||
@ -1,15 +1,20 @@
|
|||||||
package se.urmo.game.entities;
|
package se.urmo.game.entities.ghost;
|
||||||
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import se.urmo.game.collision.GhostCollisionChecker;
|
import se.urmo.game.collision.GhostCollisionChecker;
|
||||||
|
import se.urmo.game.entities.BaseAnimated;
|
||||||
|
import se.urmo.game.entities.ghost.strategy.FearStrategy;
|
||||||
|
import se.urmo.game.entities.ghost.strategy.GhostStrategy;
|
||||||
|
import se.urmo.game.entities.pacman.PacMan;
|
||||||
import se.urmo.game.graphics.SpriteLocation;
|
import se.urmo.game.graphics.SpriteLocation;
|
||||||
import se.urmo.game.graphics.SpriteSheetManager;
|
import se.urmo.game.graphics.SpriteSheetManager;
|
||||||
import se.urmo.game.main.Game;
|
import se.urmo.game.main.Game;
|
||||||
import se.urmo.game.map.GameMap;
|
import se.urmo.game.map.GameMap;
|
||||||
import se.urmo.game.state.GhostManager;
|
import se.urmo.game.state.GhostManager;
|
||||||
|
import se.urmo.game.state.LevelManager;
|
||||||
import se.urmo.game.util.Direction;
|
import se.urmo.game.util.Direction;
|
||||||
import se.urmo.game.util.MiscUtil;
|
import se.urmo.game.util.MiscUtil;
|
||||||
|
import se.urmo.game.util.MyPoint;
|
||||||
|
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.awt.Graphics;
|
import java.awt.Graphics;
|
||||||
@ -20,31 +25,24 @@ import java.util.Map;
|
|||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class Ghost {
|
public class Ghost extends BaseAnimated {
|
||||||
|
private static final double BASE_SPEED = 0.40;
|
||||||
private static final int WARNING_THRESHOLD = 180; // 3 seconds of warning
|
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;
|
public static final int GHOST_SIZE = 32;
|
||||||
private static final int ANIMATION_UPDATE_FREQUENCY = 25;
|
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 static final int FRIGHTENED_DURATION_TICKS = 10 * Game.UPS_SET;
|
||||||
|
|
||||||
private final GhostCollisionChecker collisionChecker;
|
private final GhostCollisionChecker collisionChecker;
|
||||||
private final GhostStrategy chaseStrategy;
|
private final GhostStrategy chaseStrategy;
|
||||||
private final Point startPos;
|
private final MyPoint startPos;
|
||||||
private final BufferedImage[] fearAnimation;
|
private final BufferedImage[] fearAnimation;
|
||||||
private final BufferedImage[] baseAnimation;
|
private final BufferedImage[] baseAnimation;
|
||||||
@Getter
|
private final LevelManager levelManager;
|
||||||
private Point position;
|
private MyPoint position;
|
||||||
|
|
||||||
private boolean moving = true;
|
|
||||||
private int aniTick = 0;
|
|
||||||
private int aniIndex = 0;
|
|
||||||
private final GhostStrategy scaterStrategy;
|
private final GhostStrategy scaterStrategy;
|
||||||
private GhostStrategy currentStrategy;
|
private GhostStrategy currentStrategy;
|
||||||
private BufferedImage[] animation;
|
private BufferedImage[] animation;
|
||||||
private int movementTick = 0;
|
|
||||||
private Direction direction;
|
private Direction direction;
|
||||||
private Direction prevDirection;
|
private Direction prevDirection;
|
||||||
private GhostMode mode;
|
private GhostMode mode;
|
||||||
@ -53,38 +51,34 @@ public class Ghost {
|
|||||||
private boolean isBlinking = false;
|
private boolean isBlinking = false;
|
||||||
|
|
||||||
|
|
||||||
|
public Ghost(GhostCollisionChecker collisionChecker, GhostStrategy strategy, GhostStrategy scaterStrategy, int animation, LevelManager levelManager) {
|
||||||
public Ghost(GhostCollisionChecker collisionChecker, GhostStrategy strategy, GhostStrategy scaterStrategy, int animation) {
|
super(ANIMATION_UPDATE_FREQUENCY, GhostManager.MAX_SPRITE_FRAMES);
|
||||||
this.collisionChecker = collisionChecker;
|
this.collisionChecker = collisionChecker;
|
||||||
this.chaseStrategy = strategy;
|
this.chaseStrategy = strategy;
|
||||||
this.scaterStrategy = scaterStrategy;
|
this.scaterStrategy = scaterStrategy;
|
||||||
this.baseAnimation = SpriteSheetManager.get(SpriteLocation.GHOST).getAnimation(animation);;
|
this.baseAnimation = SpriteSheetManager.get(SpriteLocation.GHOST).getAnimation(animation);
|
||||||
this.fearAnimation = SpriteSheetManager.get(SpriteLocation.GHOST).getAnimation(8);;
|
this.fearAnimation = SpriteSheetManager.get(SpriteLocation.GHOST).getAnimation(8);
|
||||||
|
|
||||||
position = new Point(
|
position = new MyPoint(
|
||||||
13 * GameMap.MAP_TILESIZE + GameMap.OFFSET_X + (GameMap.MAP_TILESIZE / 2),
|
13 * GameMap.MAP_TILESIZE + GameMap.OFFSET_X + ((double) GameMap.MAP_TILESIZE / 2),
|
||||||
4 * GameMap.MAP_TILESIZE + GameMap.OFFSET_Y + (GameMap.MAP_TILESIZE / 2) );
|
4 * GameMap.MAP_TILESIZE + GameMap.OFFSET_Y + ((double) GameMap.MAP_TILESIZE / 2));
|
||||||
startPos = position;
|
startPos = position;
|
||||||
this.currentStrategy = chaseStrategy;
|
this.currentStrategy = chaseStrategy;
|
||||||
this.animation = baseAnimation;
|
this.animation = baseAnimation;
|
||||||
|
this.levelManager = levelManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void draw(Graphics g) {
|
public void draw(Graphics g) {
|
||||||
g.drawImage(
|
g.drawImage(
|
||||||
animation[aniIndex],
|
animation[aniIndex],
|
||||||
position.x - GHOST_SIZE / 2,
|
(int) position.x - GHOST_SIZE / 2,
|
||||||
position.y - GHOST_SIZE / 2,
|
(int) position.y - GHOST_SIZE / 2,
|
||||||
GHOST_SIZE,
|
GHOST_SIZE,
|
||||||
GHOST_SIZE, null);
|
GHOST_SIZE, null);
|
||||||
g.drawImage(COLLISION_BOX,
|
|
||||||
position.x - COLLISION_BOX_OFFSET,
|
|
||||||
position.y - COLLISION_BOX_OFFSET,
|
|
||||||
COLLISION_BOX_SIZE,
|
|
||||||
COLLISION_BOX_SIZE, null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update(PacMan pacman, GameMap map) {
|
public void update(PacMan pacman, GameMap map) {
|
||||||
updateAnimationTick();
|
//updateAnimationTick();
|
||||||
if (mode == GhostMode.FRIGHTENED) {
|
if (mode == GhostMode.FRIGHTENED) {
|
||||||
updateInFrightendMode();
|
updateInFrightendMode();
|
||||||
}
|
}
|
||||||
@ -103,21 +97,16 @@ public class Ghost {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void updatePosition(PacMan pacman, GameMap map) {
|
private void updatePosition(PacMan pacman, GameMap map) {
|
||||||
// only move ghost on update interval - this is basically ghost speed;
|
if (map.isAligned(new Point((int) position.x, (int) position.y))) {
|
||||||
if (movementTick >= GHOST_MOVEMENT_UPDATE_FREQUENCY) {
|
log.info("Evaluating possible directions");
|
||||||
if (map.isAligned(position)) {
|
|
||||||
//log.info("Evaluating possible directions");
|
|
||||||
prevDirection = direction;
|
prevDirection = direction;
|
||||||
direction = chooseDirection(
|
direction = chooseDirection(
|
||||||
prioritize(collisionChecker.calculateDirectionAlternatives(position)),
|
prioritize(collisionChecker.calculateDirectionAlternatives(position)),
|
||||||
currentStrategy.chooseTarget(this, pacman, map));
|
currentStrategy.chooseTarget(this, pacman, map));
|
||||||
//log.info("selecting direction {}", direction);
|
log.info("selecting direction {}", direction);
|
||||||
}
|
}
|
||||||
|
|
||||||
moveTo(getNewPosition());
|
moveTo(getNewPosition());
|
||||||
|
|
||||||
movementTick = 0;
|
|
||||||
} else movementTick++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -126,23 +115,32 @@ public class Ghost {
|
|||||||
*
|
*
|
||||||
* @return new position
|
* @return new position
|
||||||
*/
|
*/
|
||||||
private Point getNewPosition() {
|
private MyPoint getNewPosition() {
|
||||||
Point point = new Point(
|
return new MyPoint(
|
||||||
position.x + direction.dx,
|
position.x + direction.dx * getSpeed(),
|
||||||
position.y + direction.dy);
|
position.y + direction.dy * getSpeed());
|
||||||
//log.debug("Next position {}", point);
|
|
||||||
return point;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void moveTo(Point newPosition) {
|
private double getSpeed() {
|
||||||
Point destination = collisionChecker.canMoveTo(direction, newPosition);
|
return BASE_SPEED * levelManager.getGhostSpeed();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void moveTo(MyPoint newPosition) {
|
||||||
|
MyPoint destination = collisionChecker.canMoveTo(direction, newPosition.x, newPosition.y);
|
||||||
if (destination != null) {
|
if (destination != null) {
|
||||||
position = destination;
|
position = destination;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a map of directions and their associated priority values based on the given list of directions.
|
||||||
|
* Directions with a value of {@code Direction.NONE} are excluded. If a direction is opposite to the
|
||||||
|
* previous direction, it is given a higher priority value.
|
||||||
|
*
|
||||||
|
* @param directions a list of potential movement directions
|
||||||
|
* @return a map where keys are valid directions and values are their associated priority
|
||||||
|
*/
|
||||||
private Map<Direction, Integer> prioritize(List<Direction> directions) {
|
private Map<Direction, Integer> prioritize(List<Direction> directions) {
|
||||||
return directions.stream()
|
return directions.stream()
|
||||||
.filter(d -> d != Direction.NONE)
|
.filter(d -> d != Direction.NONE)
|
||||||
@ -153,6 +151,16 @@ public class Ghost {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Selects the best movement direction for the ghost based on priority and distance to the target.
|
||||||
|
* The method evaluates the directions with the lowest priority value and chooses the one that
|
||||||
|
* minimizes the distance to the target point.
|
||||||
|
*
|
||||||
|
* @param options a map where keys represent potential movement directions and values
|
||||||
|
* represent their associated priority levels
|
||||||
|
* @param target the target point towards which the direction is evaluated
|
||||||
|
* @return the direction that has the lowest priority and minimizes the distance to the target
|
||||||
|
*/
|
||||||
private Direction chooseDirection(Map<Direction, Integer> options, Point target) {
|
private Direction chooseDirection(Map<Direction, Integer> options, Point target) {
|
||||||
// Find the lowest priority
|
// Find the lowest priority
|
||||||
int lowestPriority = options.values().stream()
|
int lowestPriority = options.values().stream()
|
||||||
@ -160,18 +168,19 @@ public class Ghost {
|
|||||||
.min()
|
.min()
|
||||||
.orElse(Integer.MAX_VALUE);
|
.orElse(Integer.MAX_VALUE);
|
||||||
|
|
||||||
// Return all directions that have this priority
|
// Collect all directions that have this priority
|
||||||
List<Direction> directions = options.entrySet().stream()
|
List<Direction> directions = options.entrySet().stream()
|
||||||
.filter(entry -> entry.getValue() == lowestPriority)
|
.filter(entry -> entry.getValue() == lowestPriority)
|
||||||
.map(Map.Entry::getKey)
|
.map(Map.Entry::getKey)
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
|
// Calculate the direction that has the lowest distance to the target
|
||||||
Direction best = directions.getFirst();
|
Direction best = directions.getFirst();
|
||||||
double bestDist = Double.MAX_VALUE;
|
double bestDist = Double.MAX_VALUE;
|
||||||
|
|
||||||
for (Direction d : directions) {
|
for (Direction d : directions) {
|
||||||
int nx = position.x + d.dx * GameMap.MAP_TILESIZE;
|
double nx = position.x + d.dx * GameMap.MAP_TILESIZE;
|
||||||
int ny = position.y + d.dy * GameMap.MAP_TILESIZE;
|
double ny = position.y + d.dy * GameMap.MAP_TILESIZE;
|
||||||
double dist = target.distance(nx, ny);
|
double dist = target.distance(nx, ny);
|
||||||
if (dist < bestDist) {
|
if (dist < bestDist) {
|
||||||
bestDist = dist;
|
bestDist = dist;
|
||||||
@ -181,20 +190,6 @@ public class Ghost {
|
|||||||
return best;
|
return best;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateAnimationTick() {
|
|
||||||
if (moving) {
|
|
||||||
aniTick++;
|
|
||||||
if (aniTick >= ANIMATION_UPDATE_FREQUENCY) {
|
|
||||||
aniTick = 0;
|
|
||||||
aniIndex++;
|
|
||||||
if (aniIndex >= GhostManager.MAX_SPRITE_FRAMES) {
|
|
||||||
aniIndex = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMode(GhostMode mode) {
|
public void setMode(GhostMode mode) {
|
||||||
this.mode = mode;
|
this.mode = mode;
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
@ -220,4 +215,12 @@ public class Ghost {
|
|||||||
public void resetPosition() {
|
public void resetPosition() {
|
||||||
position = startPos;
|
position = startPos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Point getPosition() {
|
||||||
|
return new Point((int) position.x, (int) position.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void resetModes() {
|
||||||
|
mode = GhostMode.CHASE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package se.urmo.game.entities;
|
package se.urmo.game.entities.ghost;
|
||||||
|
|
||||||
public enum GhostMode {
|
public enum GhostMode {
|
||||||
CHASE,
|
CHASE,
|
||||||
@ -1,5 +1,7 @@
|
|||||||
package se.urmo.game.entities;
|
package se.urmo.game.entities.ghost.strategy;
|
||||||
|
|
||||||
|
import se.urmo.game.entities.pacman.PacMan;
|
||||||
|
import se.urmo.game.entities.ghost.Ghost;
|
||||||
import se.urmo.game.map.GameMap;
|
import se.urmo.game.map.GameMap;
|
||||||
|
|
||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
@ -1,5 +1,7 @@
|
|||||||
package se.urmo.game.entities;
|
package se.urmo.game.entities.ghost.strategy;
|
||||||
|
|
||||||
|
import se.urmo.game.entities.pacman.PacMan;
|
||||||
|
import se.urmo.game.entities.ghost.Ghost;
|
||||||
import se.urmo.game.map.GameMap;
|
import se.urmo.game.map.GameMap;
|
||||||
|
|
||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
@ -1,5 +1,7 @@
|
|||||||
package se.urmo.game.entities;
|
package se.urmo.game.entities.ghost.strategy;
|
||||||
|
|
||||||
|
import se.urmo.game.entities.pacman.PacMan;
|
||||||
|
import se.urmo.game.entities.ghost.Ghost;
|
||||||
import se.urmo.game.map.GameMap;
|
import se.urmo.game.map.GameMap;
|
||||||
import se.urmo.game.util.Direction;
|
import se.urmo.game.util.Direction;
|
||||||
|
|
||||||
@ -14,7 +16,8 @@ public class FearStrategy implements GhostStrategy {
|
|||||||
public Point chooseTarget(Ghost ghost, PacMan pacman, GameMap map) {
|
public Point chooseTarget(Ghost ghost, PacMan pacman, GameMap map) {
|
||||||
// Frightened ghosts do not target Pacman.
|
// Frightened ghosts do not target Pacman.
|
||||||
// Instead, they pick a random adjacent valid tile.
|
// Instead, they pick a random adjacent valid tile.
|
||||||
List<Direction> neighbors = map.directionAlternatives(ghost.getPosition());
|
Point ghostPos = ghost.getPosition();
|
||||||
|
List<Direction> neighbors = map.directionAlternatives(ghostPos.x, ghostPos.y);
|
||||||
|
|
||||||
if (neighbors.isEmpty()) {
|
if (neighbors.isEmpty()) {
|
||||||
return ghost.getPosition(); // stuck
|
return ghost.getPosition(); // stuck
|
||||||
@ -1,5 +1,7 @@
|
|||||||
package se.urmo.game.entities;
|
package se.urmo.game.entities.ghost.strategy;
|
||||||
|
|
||||||
|
import se.urmo.game.entities.pacman.PacMan;
|
||||||
|
import se.urmo.game.entities.ghost.Ghost;
|
||||||
import se.urmo.game.map.GameMap;
|
import se.urmo.game.map.GameMap;
|
||||||
|
|
||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
@ -1,5 +1,7 @@
|
|||||||
package se.urmo.game.entities;
|
package se.urmo.game.entities.ghost.strategy;
|
||||||
|
|
||||||
|
import se.urmo.game.entities.pacman.PacMan;
|
||||||
|
import se.urmo.game.entities.ghost.Ghost;
|
||||||
import se.urmo.game.map.GameMap;
|
import se.urmo.game.map.GameMap;
|
||||||
import se.urmo.game.util.Direction;
|
import se.urmo.game.util.Direction;
|
||||||
|
|
||||||
@ -1,5 +1,7 @@
|
|||||||
package se.urmo.game.entities;
|
package se.urmo.game.entities.ghost.strategy;
|
||||||
|
|
||||||
|
import se.urmo.game.entities.pacman.PacMan;
|
||||||
|
import se.urmo.game.entities.ghost.Ghost;
|
||||||
import se.urmo.game.map.GameMap;
|
import se.urmo.game.map.GameMap;
|
||||||
import se.urmo.game.util.Direction;
|
import se.urmo.game.util.Direction;
|
||||||
|
|
||||||
@ -1,5 +1,7 @@
|
|||||||
package se.urmo.game.entities;
|
package se.urmo.game.entities.ghost.strategy;
|
||||||
|
|
||||||
|
import se.urmo.game.entities.pacman.PacMan;
|
||||||
|
import se.urmo.game.entities.ghost.Ghost;
|
||||||
import se.urmo.game.map.GameMap;
|
import se.urmo.game.map.GameMap;
|
||||||
|
|
||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
@ -1,5 +1,7 @@
|
|||||||
package se.urmo.game.entities;
|
package se.urmo.game.entities.ghost.strategy;
|
||||||
|
|
||||||
|
import se.urmo.game.entities.pacman.PacMan;
|
||||||
|
import se.urmo.game.entities.ghost.Ghost;
|
||||||
import se.urmo.game.map.GameMap;
|
import se.urmo.game.map.GameMap;
|
||||||
|
|
||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
@ -1,5 +1,7 @@
|
|||||||
package se.urmo.game.entities;
|
package se.urmo.game.entities.ghost.strategy;
|
||||||
|
|
||||||
|
import se.urmo.game.entities.pacman.PacMan;
|
||||||
|
import se.urmo.game.entities.ghost.Ghost;
|
||||||
import se.urmo.game.map.GameMap;
|
import se.urmo.game.map.GameMap;
|
||||||
|
|
||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
@ -1,5 +1,7 @@
|
|||||||
package se.urmo.game.entities;
|
package se.urmo.game.entities.ghost.strategy;
|
||||||
|
|
||||||
|
import se.urmo.game.entities.pacman.PacMan;
|
||||||
|
import se.urmo.game.entities.ghost.Ghost;
|
||||||
import se.urmo.game.map.GameMap;
|
import se.urmo.game.map.GameMap;
|
||||||
|
|
||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
@ -1,14 +1,15 @@
|
|||||||
package se.urmo.game.entities;
|
package se.urmo.game.entities.pacman;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import se.urmo.game.collision.CollisionChecker;
|
import se.urmo.game.collision.CollisionChecker;
|
||||||
|
import se.urmo.game.entities.BaseAnimated;
|
||||||
|
import se.urmo.game.state.LevelManager;
|
||||||
import se.urmo.game.util.Direction;
|
import se.urmo.game.util.Direction;
|
||||||
import se.urmo.game.main.Game;
|
|
||||||
import se.urmo.game.map.GameMap;
|
import se.urmo.game.map.GameMap;
|
||||||
import se.urmo.game.util.LoadSave;
|
import se.urmo.game.util.LoadSave;
|
||||||
import se.urmo.game.util.MiscUtil;
|
import se.urmo.game.util.MyPoint;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
@ -16,40 +17,39 @@ import java.util.Arrays;
|
|||||||
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class PacMan {
|
public class PacMan extends BaseAnimated {
|
||||||
public static final int PACMAN_SIZE = 32;
|
public static final int PACMAN_SIZE = 32;
|
||||||
public static final int PACMAN_OFFSET = PACMAN_SIZE / 2;
|
public static final int PACMAN_OFFSET = PACMAN_SIZE / 2;
|
||||||
private static final int COLLISION_BOX_SIZE = 16;
|
private static final int COLLISION_BOX_SIZE = 16;
|
||||||
private static final int COLLISION_BOX_OFFSET = (PACMAN_SIZE - COLLISION_BOX_SIZE) / 2;
|
private static final int COLLISION_BOX_OFFSET = (PACMAN_SIZE - COLLISION_BOX_SIZE) / 2;
|
||||||
private final Game game;
|
|
||||||
//private final Point startPosition;
|
|
||||||
private final MyPoint startPosition;
|
private final MyPoint startPosition;
|
||||||
private int aniTick = 0;
|
|
||||||
private int aniIndex = 0;
|
|
||||||
private static final int ANIMATION_UPDATE_FREQUENCY = 10;
|
private static final int ANIMATION_UPDATE_FREQUENCY = 10;
|
||||||
private static final double BASE_SPEED = 0.40;
|
private static final double BASE_SPEED = 0.40;
|
||||||
@Setter
|
private boolean moving = false;
|
||||||
private boolean moving;
|
private final BufferedImage[][] spriteSheets;
|
||||||
private final BufferedImage[][] movmentImages = new BufferedImage[4][4];
|
|
||||||
private MyPoint position;
|
private MyPoint position;
|
||||||
private static final BufferedImage COLLISION_BOX = MiscUtil.createOutlinedBox(COLLISION_BOX_SIZE, COLLISION_BOX_SIZE, Color.yellow, 2);
|
|
||||||
private final CollisionChecker collisionChecker;
|
private final CollisionChecker collisionChecker;
|
||||||
|
private final LevelManager levelManager;
|
||||||
@Setter
|
@Setter
|
||||||
@Getter
|
@Getter
|
||||||
private Direction direction = Direction.NONE;
|
private Direction direction = Direction.NONE;
|
||||||
|
private double pacmanLevelSpeed;
|
||||||
|
|
||||||
public PacMan(Game game, CollisionChecker collisionChecker) {
|
public PacMan(CollisionChecker collisionChecker, LevelManager levelManager) {
|
||||||
this.game = game;
|
super(ANIMATION_UPDATE_FREQUENCY, 4);
|
||||||
this.collisionChecker = collisionChecker;
|
this.collisionChecker = collisionChecker;
|
||||||
|
this.levelManager = levelManager;
|
||||||
this.position = new MyPoint(
|
this.position = new MyPoint(
|
||||||
26 * GameMap.MAP_TILESIZE + GameMap.OFFSET_X,
|
26 * GameMap.MAP_TILESIZE + GameMap.OFFSET_X,
|
||||||
13 * GameMap.MAP_TILESIZE + GameMap.OFFSET_Y + ((double) GameMap.MAP_TILESIZE / 2));
|
13 * GameMap.MAP_TILESIZE + GameMap.OFFSET_Y + ((double) GameMap.MAP_TILESIZE / 2));
|
||||||
this.startPosition = this.position;
|
this.startPosition = this.position;
|
||||||
loadAnimation();
|
this.spriteSheets = loadAnimation();
|
||||||
|
this.pacmanLevelSpeed = this.levelManager.getPacmanLevelSpeed();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadAnimation() {
|
private BufferedImage[][] loadAnimation() {
|
||||||
BufferedImage[][] image = new BufferedImage[3][4];
|
BufferedImage[][] image = new BufferedImage[3][4];
|
||||||
|
BufferedImage[][] spriteMap = new BufferedImage[4][4];;
|
||||||
|
|
||||||
BufferedImage img = LoadSave.GetSpriteAtlas("sprites/PacManAssets-PacMan.png");
|
BufferedImage img = LoadSave.GetSpriteAtlas("sprites/PacManAssets-PacMan.png");
|
||||||
for (int row = 0; row < 3; row++) {
|
for (int row = 0; row < 3; row++) {
|
||||||
@ -57,34 +57,30 @@ public class PacMan {
|
|||||||
image[row][col] = img.getSubimage(PACMAN_SIZE * col, PACMAN_SIZE * row, PACMAN_SIZE, PACMAN_SIZE);
|
image[row][col] = img.getSubimage(PACMAN_SIZE * col, PACMAN_SIZE * row, PACMAN_SIZE, PACMAN_SIZE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
movmentImages[Direction.RIGHT.ordinal()] = image[0];
|
spriteMap[Direction.RIGHT.ordinal()] = image[0];
|
||||||
movmentImages[Direction.LEFT.ordinal()] = Arrays.stream(image[0])
|
spriteMap[Direction.LEFT.ordinal()] = Arrays.stream(image[0])
|
||||||
.map(i -> LoadSave.rotate(i, 180))
|
.map(i -> LoadSave.rotate(i, 180))
|
||||||
.toArray(BufferedImage[]::new);
|
.toArray(BufferedImage[]::new);
|
||||||
movmentImages[Direction.DOWN.ordinal()] = Arrays.stream(image[0])
|
spriteMap[Direction.DOWN.ordinal()] = Arrays.stream(image[0])
|
||||||
.map(i -> LoadSave.rotate(i, 90))
|
.map(i -> LoadSave.rotate(i, 90))
|
||||||
.toArray(BufferedImage[]::new);
|
.toArray(BufferedImage[]::new);
|
||||||
movmentImages[Direction.UP.ordinal()] = Arrays.stream(image[0])
|
spriteMap[Direction.UP.ordinal()] = Arrays.stream(image[0])
|
||||||
.map(i -> LoadSave.rotate(i, 270))
|
.map(i -> LoadSave.rotate(i, 270))
|
||||||
.toArray(BufferedImage[]::new);
|
.toArray(BufferedImage[]::new);
|
||||||
|
return spriteMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void draw(Graphics g) {
|
public void draw(Graphics g) {
|
||||||
g.drawImage(
|
g.drawImage(
|
||||||
movmentImages[direction==Direction.NONE ? 0 : direction.ordinal()][aniIndex],
|
spriteSheets[direction==Direction.NONE ? 0 : direction.ordinal()][aniIndex],
|
||||||
(int) position.x - PACMAN_OFFSET,
|
(int) position.x - PACMAN_OFFSET,
|
||||||
(int) position.y - PACMAN_OFFSET,
|
(int) position.y - PACMAN_OFFSET,
|
||||||
PACMAN_SIZE,
|
PACMAN_SIZE,
|
||||||
PACMAN_SIZE, null);
|
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.fillRect(position.x-1, position.y-1, 3, 3);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update() {
|
public void update() {
|
||||||
updateAnimationTick();
|
|
||||||
if(moving) {
|
if(moving) {
|
||||||
log.debug("Moving to {}", direction);
|
|
||||||
MyPoint mpoint = switch (direction) {
|
MyPoint mpoint = switch (direction) {
|
||||||
case RIGHT -> new MyPoint(position.x + getSpeed(), position.y);
|
case RIGHT -> new MyPoint(position.x + getSpeed(), position.y);
|
||||||
case LEFT -> new MyPoint(position.x - getSpeed(), position.y);
|
case LEFT -> new MyPoint(position.x - getSpeed(), position.y);
|
||||||
@ -102,44 +98,40 @@ public class PacMan {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private double getSpeed() {
|
private double getSpeed() {
|
||||||
return BASE_SPEED * 0.8;
|
return BASE_SPEED * pacmanLevelSpeed;
|
||||||
}
|
|
||||||
|
|
||||||
private void updateAnimationTick() {
|
|
||||||
if (moving) {
|
|
||||||
aniTick++;
|
|
||||||
if (aniTick >= ANIMATION_UPDATE_FREQUENCY) {
|
|
||||||
aniTick = 0;
|
|
||||||
aniIndex++;
|
|
||||||
if (aniIndex >= 4) {
|
|
||||||
aniIndex = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public double distanceTo(Point point) {
|
public double distanceTo(Point point) {
|
||||||
return new Point((int) position.x, (int) position.y).distance(point);
|
return new Point((int) position.x, (int) position.y).distance(point);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loseLife() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void resetPosition() {
|
public void resetPosition() {
|
||||||
position = startPosition;
|
position = startPosition;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void reset() {
|
||||||
|
resetPosition();
|
||||||
|
aniIndex = 0; // reset animation to start
|
||||||
|
}
|
||||||
|
|
||||||
public Image getLifeIcon() {
|
public Image getLifeIcon() {
|
||||||
return movmentImages[0][1];
|
return spriteSheets[0][1];
|
||||||
}
|
}
|
||||||
|
|
||||||
public Rectangle getBounds() {
|
public Rectangle getBounds() {
|
||||||
return new Rectangle((int) (position.x - COLLISION_BOX_OFFSET), (int) (position.y - COLLISION_BOX_OFFSET), COLLISION_BOX_SIZE, COLLISION_BOX_SIZE);
|
return new Rectangle(
|
||||||
|
(int) (position.x - COLLISION_BOX_OFFSET),
|
||||||
|
(int) (position.y - COLLISION_BOX_OFFSET),
|
||||||
|
COLLISION_BOX_SIZE,
|
||||||
|
COLLISION_BOX_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Point getPosition() {
|
public Point getPosition() {
|
||||||
return new Point((int) position.x, (int) position.y);
|
return new Point((int) position.x, (int) position.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setMoving(boolean b) {
|
||||||
|
moving = b;
|
||||||
|
paused = !b;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -1,7 +1,7 @@
|
|||||||
package se.urmo.game.graphics;
|
package se.urmo.game.graphics;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import se.urmo.game.entities.Ghost;
|
import se.urmo.game.entities.ghost.Ghost;
|
||||||
import se.urmo.game.map.GameMap;
|
import se.urmo.game.map.GameMap;
|
||||||
import se.urmo.game.util.LoadSave;
|
import se.urmo.game.util.LoadSave;
|
||||||
|
|
||||||
|
|||||||
@ -1,12 +1,12 @@
|
|||||||
package se.urmo.game.map;
|
package se.urmo.game.map;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import se.urmo.game.graphics.SpriteSheetManager;
|
|
||||||
import se.urmo.game.util.Direction;
|
import se.urmo.game.util.Direction;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
@ -18,15 +18,36 @@ public class GameMap {
|
|||||||
public static final int MAP_ROW_SIZE = 30;
|
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_Y = 7 * MAP_TILESIZE; // 160px from top
|
||||||
public static final int OFFSET_X = MAP_TILESIZE; // 16px from left
|
public static final int OFFSET_X = MAP_TILESIZE; // 16px from left
|
||||||
private final MapTile[][] mapData;
|
private MapTile[][] mapData;
|
||||||
|
private final int[][] csvData;
|
||||||
|
private long numberOfDots;
|
||||||
|
|
||||||
|
|
||||||
public GameMap(String mapFilePath) {
|
public GameMap(String mapFilePath) {
|
||||||
this.mapData = loadMap(mapFilePath, MAP_ROW_SIZE, MAP_COL_SIZE);
|
this.csvData = readCSV(mapFilePath, MAP_ROW_SIZE, MAP_COL_SIZE);
|
||||||
|
|
||||||
|
this.mapData = createMap(csvData);
|
||||||
|
|
||||||
|
this.numberOfDots = Arrays.stream(mapData)
|
||||||
|
.flatMap(Arrays::stream)
|
||||||
|
.filter(tile -> tile != null &&
|
||||||
|
(tile.getTileType() == TileType.SMALL_PELLET ||
|
||||||
|
tile.getTileType() == TileType.LARGE_PELLET))
|
||||||
|
.count();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private MapTile[][] loadMap(String path, int mapRowSize, int mapColSize) {
|
private static MapTile[][] createMap(int[][] data2) {
|
||||||
|
return Arrays.stream(data2)
|
||||||
|
.map(a -> Arrays.stream(a)
|
||||||
|
.mapToObj(i -> MapTile.byType(TileType.fromValue(i)))
|
||||||
|
.toArray(MapTile[]::new))
|
||||||
|
.toArray(MapTile[][]::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
private int[][] readCSV(String path, int mapRowSize, int mapColSize) {
|
||||||
MapTile[][] data = new MapTile[mapRowSize][mapColSize];
|
MapTile[][] data = new MapTile[mapRowSize][mapColSize];
|
||||||
|
int[][] data2 = new int[mapRowSize][mapColSize];
|
||||||
|
|
||||||
try (InputStream is = getClass().getClassLoader().getResourceAsStream(path);
|
try (InputStream is = getClass().getClassLoader().getResourceAsStream(path);
|
||||||
BufferedReader br = new BufferedReader(new InputStreamReader(Objects.requireNonNull(is)))) {
|
BufferedReader br = new BufferedReader(new InputStreamReader(Objects.requireNonNull(is)))) {
|
||||||
@ -42,11 +63,15 @@ public class GameMap {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int col = 0; col < mapColSize; col++) {
|
data2[row++] = Arrays.stream(tokens)
|
||||||
int value = Integer.parseInt(tokens[col].trim());
|
.map(String::trim)
|
||||||
data[row][col] = MapTile.byType(TileType.fromValue(value));
|
.mapToInt(Integer::parseInt)
|
||||||
}
|
.toArray();
|
||||||
row++;
|
|
||||||
|
// for (int col = 0; col < mapColSize; col++) {
|
||||||
|
// int value = Integer.parseInt(tokens[col].trim());
|
||||||
|
// data[row][col] = MapTile.byType(TileType.fromValue(value));
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
if (row != mapRowSize) {
|
if (row != mapRowSize) {
|
||||||
@ -60,7 +85,7 @@ public class GameMap {
|
|||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error("Failed to read resource: {}", e.getMessage(), e);
|
log.error("Failed to read resource: {}", e.getMessage(), e);
|
||||||
}
|
}
|
||||||
return data;
|
return data2;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void draw(Graphics g) {
|
public void draw(Graphics g) {
|
||||||
@ -77,10 +102,6 @@ public class GameMap {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isPassable(List<Point> list){
|
|
||||||
return list.stream().allMatch(p -> isPassable(p.x, p.y));
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isPassable(int screenX, int screenY) {
|
public boolean isPassable(int screenX, int screenY) {
|
||||||
int row = screenToRow(screenY);
|
int row = screenToRow(screenY);
|
||||||
int col = screenToCol(screenX);
|
int col = screenToCol(screenX);
|
||||||
@ -94,8 +115,8 @@ public class GameMap {
|
|||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isSolid(Point pos) {
|
public boolean isSolidXY(int screenX, int screenY) {
|
||||||
return isSolid(screenToRow(pos), screenToCol(pos));
|
return isSolid(screenToRow(screenY), screenToCol(screenX));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -143,6 +164,7 @@ public class GameMap {
|
|||||||
public int columns() {
|
public int columns() {
|
||||||
return MAP_COL_SIZE;
|
return MAP_COL_SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int rows() {
|
public int rows() {
|
||||||
return MAP_ROW_SIZE;
|
return MAP_ROW_SIZE;
|
||||||
}
|
}
|
||||||
@ -186,6 +208,7 @@ public class GameMap {
|
|||||||
public static int colToScreen(int col) {
|
public static int colToScreen(int col) {
|
||||||
return col * MAP_TILESIZE + OFFSET_X;
|
return col * MAP_TILESIZE + OFFSET_X;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int rowToScreen(int row) {
|
public static int rowToScreen(int row) {
|
||||||
return row * MAP_TILESIZE + OFFSET_Y;
|
return row * MAP_TILESIZE + OFFSET_Y;
|
||||||
}
|
}
|
||||||
@ -198,11 +221,12 @@ public class GameMap {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Direction> directionAlternatives(Point screen) {
|
public List<Direction> directionAlternatives(int screenX, int screenY) {
|
||||||
int row = (screen.y - GameMap.OFFSET_Y) / GameMap.MAP_TILESIZE;
|
int row = (screenY - GameMap.OFFSET_Y) / GameMap.MAP_TILESIZE;
|
||||||
int col = (screen.x - GameMap.OFFSET_X) / GameMap.MAP_TILESIZE;
|
int col = (screenX - GameMap.OFFSET_X) / GameMap.MAP_TILESIZE;
|
||||||
|
|
||||||
record DirectionCheck(int rowOffset, int colOffset, Direction direction) {}
|
record DirectionCheck(int rowOffset, int colOffset, Direction direction) {
|
||||||
|
}
|
||||||
log.debug("At [{}][{}]", row, col);
|
log.debug("At [{}][{}]", row, col);
|
||||||
return Stream.of(
|
return Stream.of(
|
||||||
new DirectionCheck(0, 1, Direction.RIGHT),
|
new DirectionCheck(0, 1, Direction.RIGHT),
|
||||||
@ -226,4 +250,13 @@ public class GameMap {
|
|||||||
int col = pos.y % GameMap.MAP_TILESIZE;
|
int col = pos.y % GameMap.MAP_TILESIZE;
|
||||||
return row == GameMap.MAP_TILESIZE / 2 && col == GameMap.MAP_TILESIZE / 2;
|
return row == GameMap.MAP_TILESIZE / 2 && col == GameMap.MAP_TILESIZE / 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public long numberOfDots() {
|
||||||
|
//return this.numberOfDots;
|
||||||
|
return 50;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void reset() {
|
||||||
|
mapData = createMap(csvData);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
23
src/main/java/se/urmo/game/state/AnimationManager.java
Normal file
23
src/main/java/se/urmo/game/state/AnimationManager.java
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package se.urmo.game.state;
|
||||||
|
|
||||||
|
import se.urmo.game.entities.Animated;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class AnimationManager {
|
||||||
|
private final List<Animated> animatedEntities = new ArrayList<>();
|
||||||
|
|
||||||
|
public void register(Animated animated) {
|
||||||
|
animatedEntities.add(animated);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void unregister(Animated entity) {
|
||||||
|
animatedEntities.remove(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateAll() {
|
||||||
|
animatedEntities.forEach(Animated::updateAnimationTick);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,8 +1,8 @@
|
|||||||
package se.urmo.game.state;
|
package se.urmo.game.state;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import se.urmo.game.entities.Fruit;
|
import se.urmo.game.entities.collectibles.Fruit;
|
||||||
import se.urmo.game.entities.PacMan;
|
import se.urmo.game.entities.pacman.PacMan;
|
||||||
|
|
||||||
import java.awt.Graphics;
|
import java.awt.Graphics;
|
||||||
|
|
||||||
@ -10,16 +10,15 @@ import java.awt.Graphics;
|
|||||||
public class FruitManager {
|
public class FruitManager {
|
||||||
private final LevelManager levelManager;
|
private final LevelManager levelManager;
|
||||||
private Fruit activeFruit;
|
private Fruit activeFruit;
|
||||||
private int dotsEaten = 0;
|
|
||||||
|
|
||||||
public FruitManager(LevelManager levelManager) {
|
public FruitManager(LevelManager levelManager) {
|
||||||
this.levelManager = levelManager;
|
this.levelManager = levelManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void dotEaten() {
|
public void dotEaten(int dotsEaten) {
|
||||||
dotsEaten++;
|
dotsEaten++;
|
||||||
if (dotsEaten == 10 || dotsEaten == 170) {
|
if (dotsEaten == 10 || dotsEaten == 170) {
|
||||||
spawnFruit(levelManager.getLevel());
|
spawnFruit(levelManager.getCurrentLevel().getLevel());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,4 +47,8 @@ public class FruitManager {
|
|||||||
activeFruit.draw(g);
|
activeFruit.draw(g);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void reset() {
|
||||||
|
activeFruit = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,7 +9,9 @@ import java.util.Arrays;
|
|||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
public enum FruitType {
|
public enum FruitType {
|
||||||
CHERRY(1, SpriteSheetManager.get(SpriteLocation.ITEM).getSprite(0,0), 100);
|
CHERRY(1, SpriteSheetManager.get(SpriteLocation.ITEM).getSprite(0,0), 100),
|
||||||
|
CHERRY2(2, SpriteSheetManager.get(SpriteLocation.ITEM).getSprite(0,0), 100),
|
||||||
|
CHERRY3(3, SpriteSheetManager.get(SpriteLocation.ITEM).getSprite(0,0), 100);
|
||||||
|
|
||||||
private final int level;
|
private final int level;
|
||||||
private final BufferedImage sprite;
|
private final BufferedImage sprite;
|
||||||
|
|||||||
94
src/main/java/se/urmo/game/state/GameOverState.java
Normal file
94
src/main/java/se/urmo/game/state/GameOverState.java
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
package se.urmo.game.state;
|
||||||
|
|
||||||
|
import se.urmo.game.main.GamePanel;
|
||||||
|
import se.urmo.game.util.GameFonts;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.Graphics2D;
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
|
|
||||||
|
public class GameOverState implements GameState {
|
||||||
|
private final GameStateManager gsm;
|
||||||
|
private int finalScore = 0;
|
||||||
|
private int finalLevel = 0;
|
||||||
|
private final HighScoreManager highScores; // optional
|
||||||
|
|
||||||
|
private boolean saved = false;
|
||||||
|
private long startMs = System.currentTimeMillis();
|
||||||
|
|
||||||
|
public GameOverState(GameStateManager gsm, HighScoreManager highScores) {
|
||||||
|
this.gsm = gsm;
|
||||||
|
this.highScores = highScores;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update() {
|
||||||
|
if (!saved) {
|
||||||
|
if (highScores != null) highScores.submit(finalScore);
|
||||||
|
saved = true;
|
||||||
|
}
|
||||||
|
// could auto-return to menu after N seconds if you like
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void render(Graphics2D g) {
|
||||||
|
// Clear / dim background
|
||||||
|
g.setColor(new Color(0,0,0,200));
|
||||||
|
g.fillRect(0,0, GamePanel.SCREEN_WIDTH, GamePanel.SCREEN_HEIGHT);
|
||||||
|
|
||||||
|
// Title
|
||||||
|
g.setColor(Color.RED);
|
||||||
|
g.setFont(GameFonts.arcade(32f));
|
||||||
|
drawCentered(g, "GAME OVER", GamePanel.SCREEN_HEIGHT/2 - 40);
|
||||||
|
|
||||||
|
// Stats
|
||||||
|
g.setColor(Color.WHITE);
|
||||||
|
g.setFont(GameFonts.arcade(18f));
|
||||||
|
drawCentered(g, "SCORE: " + finalScore, GamePanel.SCREEN_HEIGHT/2);
|
||||||
|
drawCentered(g, "LEVEL: " + finalLevel, GamePanel.SCREEN_HEIGHT/2 + 24);
|
||||||
|
|
||||||
|
// Prompt
|
||||||
|
g.setFont(GameFonts.arcade(12f));
|
||||||
|
drawCentered(g, "Press..", GamePanel.SCREEN_HEIGHT/2 + 64);
|
||||||
|
drawCentered(g, "ENTER to restart • ESC for menu", GamePanel.SCREEN_HEIGHT/2 + 78);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawCentered(Graphics2D g, String text, int y) {
|
||||||
|
var fm = g.getFontMetrics();
|
||||||
|
int x = (GamePanel.SCREEN_WIDTH - fm.stringWidth(text)) / 2;
|
||||||
|
g.drawString(text, x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void keyPressed(KeyEvent e) {
|
||||||
|
switch (e.getKeyCode()) {
|
||||||
|
case KeyEvent.VK_ENTER -> restartNewGame();
|
||||||
|
case KeyEvent.VK_ESCAPE -> goToMenu();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void keyReleased(KeyEvent e) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void restartNewGame() {
|
||||||
|
// Build a fresh PlayingState with everything reset
|
||||||
|
gsm.setState(GameStateType.PLAYING);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void goToMenu() {
|
||||||
|
// MenuState menu = new MenuState(gsm);
|
||||||
|
// gsm.register(StateId.MENU, menu);
|
||||||
|
// gsm.set(StateId.MENU);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setScore(int score) {
|
||||||
|
this.finalScore = score;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLevel(int level) {
|
||||||
|
this.finalLevel = level;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -15,11 +15,13 @@ public class GameStateManager {
|
|||||||
|
|
||||||
public GameStateManager(Game game) {
|
public GameStateManager(Game game) {
|
||||||
this.game = game;
|
this.game = game;
|
||||||
states.put(GameStateType.PLAYING, new PlayingState(game, this));
|
GameOverState gameOverState = new GameOverState(this, new HighScoreManager());
|
||||||
setState(GameStateType.PLAYING);
|
states.put(GameStateType.PLAYING, new PlayingState(game, this, gameOverState));
|
||||||
|
states.put(GameStateType.GAME_OVER, gameOverState);
|
||||||
|
setState(GameStateType.GAME_OVER);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setState(GameStateType type) {
|
public void setState(GameStateType type) {
|
||||||
currentState = states.get(type);
|
currentState = states.get(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
package se.urmo.game.state;
|
package se.urmo.game.state;
|
||||||
|
|
||||||
public enum GameStateType {
|
public enum GameStateType {
|
||||||
PLAYING,
|
PLAYING, GAME_OVER,
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,28 +3,25 @@ package se.urmo.game.state;
|
|||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import se.urmo.game.collision.GhostCollisionChecker;
|
import se.urmo.game.collision.GhostCollisionChecker;
|
||||||
import se.urmo.game.entities.BlinkyStrategy;
|
import se.urmo.game.entities.ghost.strategy.BlinkyStrategy;
|
||||||
import se.urmo.game.entities.ClydeStrategy;
|
import se.urmo.game.entities.ghost.strategy.ClydeStrategy;
|
||||||
import se.urmo.game.entities.Ghost;
|
import se.urmo.game.entities.ghost.Ghost;
|
||||||
import se.urmo.game.entities.GhostMode;
|
import se.urmo.game.entities.ghost.GhostMode;
|
||||||
import se.urmo.game.entities.InkyStrategy;
|
import se.urmo.game.entities.ghost.strategy.InkyStrategy;
|
||||||
import se.urmo.game.entities.PacMan;
|
import se.urmo.game.entities.pacman.PacMan;
|
||||||
import se.urmo.game.entities.PinkyStrategy;
|
import se.urmo.game.entities.ghost.strategy.PinkyStrategy;
|
||||||
import se.urmo.game.entities.ScatterToBottomLeft;
|
import se.urmo.game.entities.ghost.strategy.ScatterToBottomLeft;
|
||||||
import se.urmo.game.entities.ScatterToBottomRight;
|
import se.urmo.game.entities.ghost.strategy.ScatterToBottomRight;
|
||||||
import se.urmo.game.entities.ScatterToTopLeft;
|
import se.urmo.game.entities.ghost.strategy.ScatterToTopLeft;
|
||||||
import se.urmo.game.entities.ScatterToTopRight;
|
import se.urmo.game.entities.ghost.strategy.ScatterToTopRight;
|
||||||
import se.urmo.game.map.GameMap;
|
import se.urmo.game.map.GameMap;
|
||||||
import se.urmo.game.util.LoadSave;
|
|
||||||
|
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
import java.awt.image.BufferedImage;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class GhostManager {
|
public class GhostManager {
|
||||||
public static final int SPRITE_SHEET_ROWS = 10;
|
|
||||||
public static final int MAX_SPRITE_FRAMES = 4;
|
public static final int MAX_SPRITE_FRAMES = 4;
|
||||||
public static final int BLINKY_ANIMATION = 0;
|
public static final int BLINKY_ANIMATION = 0;
|
||||||
public static final int PINKY_ANIMATION = 2;
|
public static final int PINKY_ANIMATION = 2;
|
||||||
@ -32,6 +29,7 @@ public class GhostManager {
|
|||||||
public static final int CLYDE_ANIMATION = 3;
|
public static final int CLYDE_ANIMATION = 3;
|
||||||
@Getter
|
@Getter
|
||||||
private final List<Ghost> ghosts = new ArrayList<>();
|
private final List<Ghost> ghosts = new ArrayList<>();
|
||||||
|
private final LevelManager levelManager;
|
||||||
|
|
||||||
private long lastModeSwitchTime;
|
private long lastModeSwitchTime;
|
||||||
private int phaseIndex = 0;
|
private int phaseIndex = 0;
|
||||||
@ -44,13 +42,17 @@ public class GhostManager {
|
|||||||
5000, Integer.MAX_VALUE // scatter 5s, then chase forever
|
5000, Integer.MAX_VALUE // scatter 5s, then chase forever
|
||||||
};
|
};
|
||||||
|
|
||||||
public GhostManager(GhostCollisionChecker ghostCollisionChecker) {
|
public GhostManager(GhostCollisionChecker ghostCollisionChecker, AnimationManager animationManager, LevelManager levelManager) {
|
||||||
|
this.levelManager = levelManager;
|
||||||
// Create ghosts with their strategies
|
// Create ghosts with their strategies
|
||||||
Ghost blinky = new Ghost(ghostCollisionChecker, new BlinkyStrategy(), new ScatterToTopRight(), BLINKY_ANIMATION);
|
Ghost blinky = new Ghost(ghostCollisionChecker, new BlinkyStrategy(), new ScatterToTopRight(), BLINKY_ANIMATION, levelManager);
|
||||||
ghosts.add(blinky);
|
ghosts.add(blinky);
|
||||||
// ghosts.add(new Ghost(ghostCollisionChecker, new PinkyStrategy(), new ScatterToTopLeft(), PINKY_ANIMATION));
|
ghosts.add(new Ghost(ghostCollisionChecker, new PinkyStrategy(), new ScatterToTopLeft(), PINKY_ANIMATION, levelManager));
|
||||||
// ghosts.add(new Ghost(ghostCollisionChecker,new InkyStrategy(blinky), new ScatterToBottomRight(), INKY_ANIMATION));
|
ghosts.add(new Ghost(ghostCollisionChecker,new InkyStrategy(blinky), new ScatterToBottomRight(), INKY_ANIMATION, levelManager));
|
||||||
// ghosts.add(new Ghost(ghostCollisionChecker, new ClydeStrategy(), new ScatterToBottomLeft(), CLYDE_ANIMATION));
|
ghosts.add(new Ghost(ghostCollisionChecker, new ClydeStrategy(), new ScatterToBottomLeft(), CLYDE_ANIMATION, levelManager));
|
||||||
|
|
||||||
|
ghosts.forEach(animationManager::register);
|
||||||
|
|
||||||
setMode(GhostMode.CHASE);
|
setMode(GhostMode.CHASE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,6 +89,12 @@ public class GhostManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setFrightMode() {
|
public void setFrightMode() {
|
||||||
this.setMode(GhostMode.FRIGHTENED);
|
setMode(GhostMode.FRIGHTENED);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void reset() {
|
||||||
|
phaseIndex = 0;
|
||||||
|
setMode(GhostMode.SCATTER);
|
||||||
|
ghosts.forEach(Ghost::resetPosition);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
7
src/main/java/se/urmo/game/state/HighScoreManager.java
Normal file
7
src/main/java/se/urmo/game/state/HighScoreManager.java
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package se.urmo.game.state;
|
||||||
|
|
||||||
|
public class HighScoreManager {
|
||||||
|
public void submit(int finalScore) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -3,6 +3,56 @@ package se.urmo.game.state;
|
|||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
||||||
public class LevelManager {
|
public class LevelManager {
|
||||||
@Getter
|
|
||||||
private int level = 1;
|
public void nextLevel() {
|
||||||
|
currentLevel = Level.forLevel(currentLevel.getLevel() + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public enum Level {
|
||||||
|
LEVEL1(1,6000, 0.75, 0.8, FruitType.CHERRY),
|
||||||
|
LEVEL2(2,3000, 0.85, 0.85, FruitType.CHERRY),
|
||||||
|
LEVEL3(3,1000, 0.95, 0.9, FruitType.CHERRY),
|
||||||
|
LEVEL4(4,6000, 0.75, 0.85, FruitType.CHERRY),
|
||||||
|
LEVEL5(5,6000, 0.75, 0.85, FruitType.CHERRY),
|
||||||
|
LEVEL6(6,6000, 0.75, 0.85, FruitType.CHERRY),
|
||||||
|
LEVEL7(7,6000, 0.75, 0.85, FruitType.CHERRY),
|
||||||
|
LEVEL8(8,6000, 0.75, 0.85, FruitType.CHERRY),
|
||||||
|
LEVEL9(9,6000, 0.75, 0.85, FruitType.CHERRY),
|
||||||
|
LEVEL10(10,6000, 0.75, 0.85, FruitType.CHERRY),
|
||||||
|
LEVEL11(11,6000, 0.75, 0.85, FruitType.CHERRY),
|
||||||
|
LEVEL12(12,6000, 0.75, 0.85, FruitType.CHERRY),
|
||||||
|
LEVEL13(13,6000, 0.75, 0.85, FruitType.CHERRY),
|
||||||
|
LEVEL14(14,6000, 0.75, 0.85, FruitType.CHERRY),
|
||||||
|
LEVEL15(15,6000, 0.75, 0.85, FruitType.CHERRY),
|
||||||
|
LEVEL16(16,6000, 0.75, 0.85, FruitType.CHERRY),
|
||||||
|
LEVEL17(17,6000, 0.75, 0.85, FruitType.CHERRY),
|
||||||
|
LEVEL18(18,6000, 0.75, 0.85, FruitType.CHERRY),
|
||||||
|
LEVEL19(19,6000, 0.75, 0.85, FruitType.CHERRY);
|
||||||
|
|
||||||
|
private final int frightendedDuration;
|
||||||
|
private final double ghostSpeed;
|
||||||
|
private final double pacmanSpeed;
|
||||||
|
private final FruitType fruitType;
|
||||||
|
private final int level;
|
||||||
|
|
||||||
|
Level(int level, int frightendedDuration, double ghostSpeed, double pacmanSpeed, FruitType fruitType) {
|
||||||
|
this.level = level;
|
||||||
|
this.frightendedDuration = frightendedDuration;
|
||||||
|
this.ghostSpeed = ghostSpeed;
|
||||||
|
this.pacmanSpeed = pacmanSpeed;
|
||||||
|
this.fruitType = fruitType;
|
||||||
|
}
|
||||||
|
public static Level forLevel(int i) {
|
||||||
|
return Level.values()[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Getter
|
||||||
|
private Level currentLevel = Level.LEVEL1;
|
||||||
|
public double getPacmanLevelSpeed() {
|
||||||
|
return currentLevel.pacmanSpeed;
|
||||||
|
}
|
||||||
|
public double getGhostSpeed() {
|
||||||
|
return currentLevel.ghostSpeed;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,52 +4,123 @@ import lombok.Getter;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import se.urmo.game.collision.CollisionChecker;
|
import se.urmo.game.collision.CollisionChecker;
|
||||||
import se.urmo.game.collision.GhostCollisionChecker;
|
import se.urmo.game.collision.GhostCollisionChecker;
|
||||||
import se.urmo.game.entities.Ghost;
|
import se.urmo.game.entities.ghost.Ghost;
|
||||||
import se.urmo.game.entities.GhostMode;
|
import se.urmo.game.entities.ghost.GhostMode;
|
||||||
import se.urmo.game.entities.PacMan;
|
import se.urmo.game.entities.pacman.PacMan;
|
||||||
import se.urmo.game.main.Game;
|
import se.urmo.game.main.Game;
|
||||||
import se.urmo.game.map.GameMap;
|
import se.urmo.game.map.GameMap;
|
||||||
import se.urmo.game.map.MapTile;
|
import se.urmo.game.map.MapTile;
|
||||||
import se.urmo.game.map.TileType;
|
import se.urmo.game.map.TileType;
|
||||||
import se.urmo.game.util.Direction;
|
import se.urmo.game.util.Direction;
|
||||||
|
import se.urmo.game.util.GameFonts;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
import java.io.InputStream;
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class PlayingState implements GameState {
|
public class PlayingState implements GameState {
|
||||||
|
public static final int REMAINING_LIVES = 0;
|
||||||
private final Game game;
|
private final Game game;
|
||||||
private final GameStateManager gameStateManager;
|
private final GameStateManager gameStateManager;
|
||||||
|
private final GameOverState gameOverState;
|
||||||
|
|
||||||
|
// Core components
|
||||||
private final GhostManager ghostManager;
|
private final GhostManager ghostManager;
|
||||||
private final Font arcadeFont;
|
|
||||||
private final FruitManager fruitManager;
|
private final FruitManager fruitManager;
|
||||||
private final LevelManager levelManager;
|
private final LevelManager levelManager;
|
||||||
|
private final AnimationManager animationManager;
|
||||||
private PacMan pacman;
|
private PacMan pacman;
|
||||||
@Getter
|
@Getter
|
||||||
private GameMap map;
|
private GameMap map;
|
||||||
private int score;
|
|
||||||
private int lives = 3;
|
|
||||||
|
|
||||||
public PlayingState(Game game, GameStateManager gameStateManager) {
|
// Durations (tune to taste)
|
||||||
|
private static final int READY_MS = 1500;
|
||||||
|
private static final int LEVEL_COMPLETE_MS = 1500;
|
||||||
|
private static final int LIFE_LOST_MS = 2000;
|
||||||
|
|
||||||
|
// Score/Lives
|
||||||
|
private int score = 0;
|
||||||
|
private int lives = 3;
|
||||||
|
private int dotsEaten = 0;
|
||||||
|
|
||||||
|
// Phase + timers
|
||||||
|
private RoundPhase phase = RoundPhase.PLAYING;
|
||||||
|
private long phaseStartMs = System.currentTimeMillis();
|
||||||
|
private boolean deathInProgress;
|
||||||
|
|
||||||
|
public PlayingState(Game game, GameStateManager gameStateManager, GameOverState gameOverState) {
|
||||||
this.game = game;
|
this.game = game;
|
||||||
this.gameStateManager = gameStateManager;
|
this.gameStateManager = gameStateManager;
|
||||||
|
this.gameOverState = gameOverState;
|
||||||
this.map = new GameMap("maps/map1.csv");
|
this.map = new GameMap("maps/map1.csv");
|
||||||
this.pacman = new PacMan(game, new CollisionChecker(map));
|
this.animationManager = new AnimationManager();
|
||||||
this.ghostManager = new GhostManager(new GhostCollisionChecker(map));
|
|
||||||
this.levelManager = new LevelManager();
|
this.levelManager = new LevelManager();
|
||||||
|
this.pacman = new PacMan(new CollisionChecker(map), levelManager);
|
||||||
|
this.animationManager.register(pacman);
|
||||||
|
this.ghostManager = new GhostManager(new GhostCollisionChecker(map), animationManager, levelManager);
|
||||||
this.fruitManager = new FruitManager(levelManager);
|
this.fruitManager = new FruitManager(levelManager);
|
||||||
this.arcadeFont = loadArcadeFont();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update() {
|
public void update() {
|
||||||
|
switch (phase) {
|
||||||
|
case READY -> {
|
||||||
|
// Freeze everything during READY
|
||||||
|
if (phaseElapsed() >= READY_MS) {
|
||||||
|
setPhase(RoundPhase.PLAYING);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case PLAYING -> {
|
||||||
|
animationManager.updateAll();
|
||||||
pacman.update();
|
pacman.update();
|
||||||
ghostManager.update(pacman, map);
|
ghostManager.update(pacman, map);
|
||||||
fruitManager.update(pacman, this);
|
fruitManager.update(pacman, this);
|
||||||
checkCollisions();
|
checkCollisions();
|
||||||
handleDots();
|
handleDots();
|
||||||
}
|
}
|
||||||
|
case LEVEL_COMPLETE -> {
|
||||||
|
// Freeze, then advance level
|
||||||
|
if (phaseElapsed() >= LEVEL_COMPLETE_MS) {
|
||||||
|
advanceLevel();
|
||||||
|
setPhase(RoundPhase.READY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case LIFE_LOST -> {
|
||||||
|
// Freeze, then reset round (keep dot state)
|
||||||
|
if (phaseElapsed() >= LIFE_LOST_MS) {
|
||||||
|
deathInProgress = false;
|
||||||
|
resetAfterLifeLost();
|
||||||
|
setPhase(RoundPhase.READY);
|
||||||
|
if (lives <= 0) {
|
||||||
|
endGame();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void resetAfterLifeLost() {
|
||||||
|
pacman.reset(); // to start tile, direction stopped
|
||||||
|
ghostManager.reset(); // to house
|
||||||
|
}
|
||||||
|
|
||||||
|
private void advanceLevel() {
|
||||||
|
levelManager.nextLevel();
|
||||||
|
map.reset();
|
||||||
|
ghostManager.reset();
|
||||||
|
fruitManager.reset();
|
||||||
|
pacman.reset();
|
||||||
|
dotsEaten = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setPhase(RoundPhase next) {
|
||||||
|
phase = next;
|
||||||
|
phaseStartMs = System.currentTimeMillis();
|
||||||
|
}
|
||||||
|
|
||||||
|
private long phaseElapsed() {
|
||||||
|
return System.currentTimeMillis() - phaseStartMs;
|
||||||
|
}
|
||||||
|
|
||||||
private void handleDots() {
|
private void handleDots() {
|
||||||
Point pacmanScreenPos = pacman.getPosition();
|
Point pacmanScreenPos = pacman.getPosition();
|
||||||
@ -59,8 +130,12 @@ public class PlayingState implements GameState {
|
|||||||
ghostManager.setFrightMode();
|
ghostManager.setFrightMode();
|
||||||
}
|
}
|
||||||
if (wasRemoved) {
|
if (wasRemoved) {
|
||||||
fruitManager.dotEaten();
|
dotsEaten++;
|
||||||
|
fruitManager.dotEaten(dotsEaten);
|
||||||
score += tile.getTileType().getScore();
|
score += tile.getTileType().getScore();
|
||||||
|
if (dotsEaten == map.numberOfDots()) {
|
||||||
|
setPhase(RoundPhase.LEVEL_COMPLETE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,11 +146,28 @@ public class PlayingState implements GameState {
|
|||||||
ghostManager.draw(g);
|
ghostManager.draw(g);
|
||||||
fruitManager.draw(g);
|
fruitManager.draw(g);
|
||||||
drawUI(g);
|
drawUI(g);
|
||||||
|
|
||||||
|
// Phase overlays
|
||||||
|
switch (phase) {
|
||||||
|
case READY -> drawCenterText(g, "READY!");
|
||||||
|
case LEVEL_COMPLETE -> drawCenterText(g, "LEVEL COMPLETE!");
|
||||||
|
case LIFE_LOST -> drawCenterText(g, "LIFE LOST");
|
||||||
|
default -> { /* no overlay */ }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawCenterText(Graphics2D g, String text) {
|
||||||
|
g.setFont(GameFonts.arcade(18F));
|
||||||
|
g.setColor(Color.YELLOW);
|
||||||
|
var fm = g.getFontMetrics();
|
||||||
|
int cx = GameMap.OFFSET_X + map.columns() * GameMap.MAP_TILESIZE / 2;
|
||||||
|
int cy = GameMap.OFFSET_Y + map.rows() * GameMap.MAP_TILESIZE / 2;
|
||||||
|
g.drawString(text, cx - fm.stringWidth(text) / 2, cy);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void drawUI(Graphics2D g) {
|
private void drawUI(Graphics2D g) {
|
||||||
g.setColor(Color.WHITE);
|
g.setColor(Color.WHITE);
|
||||||
g.setFont(arcadeFont);
|
g.setFont(GameFonts.arcade(18F));
|
||||||
|
|
||||||
// Score (above map, left)
|
// Score (above map, left)
|
||||||
g.drawString("Your Score", 48, 48);
|
g.drawString("Your Score", 48, 48);
|
||||||
@ -92,6 +184,7 @@ public class PlayingState implements GameState {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void keyPressed(KeyEvent e) {
|
public void keyPressed(KeyEvent e) {
|
||||||
|
if (phase != RoundPhase.PLAYING) return;
|
||||||
switch (e.getKeyCode()) {
|
switch (e.getKeyCode()) {
|
||||||
case KeyEvent.VK_W -> move(Direction.UP);
|
case KeyEvent.VK_W -> move(Direction.UP);
|
||||||
case KeyEvent.VK_S -> move(Direction.DOWN);
|
case KeyEvent.VK_S -> move(Direction.DOWN);
|
||||||
@ -112,6 +205,8 @@ public class PlayingState implements GameState {
|
|||||||
|
|
||||||
private void checkCollisions() {
|
private void checkCollisions() {
|
||||||
for (Ghost ghost : ghostManager.getGhosts()) {
|
for (Ghost ghost : ghostManager.getGhosts()) {
|
||||||
|
if (deathInProgress) return; // guard
|
||||||
|
//if(overlap(pacman, ghost)
|
||||||
double dist = pacman.distanceTo(ghost.getPosition());
|
double dist = pacman.distanceTo(ghost.getPosition());
|
||||||
if (dist < GameMap.MAP_TILESIZE / 2.0) {
|
if (dist < GameMap.MAP_TILESIZE / 2.0) {
|
||||||
if (ghost.isFrightened()) {
|
if (ghost.isFrightened()) {
|
||||||
@ -120,21 +215,27 @@ public class PlayingState implements GameState {
|
|||||||
ghost.resetPosition();
|
ghost.resetPosition();
|
||||||
ghost.setMode(GhostMode.CHASE); // end frightend
|
ghost.setMode(GhostMode.CHASE); // end frightend
|
||||||
} else {
|
} else {
|
||||||
|
deathInProgress = true;
|
||||||
// Pac-Man loses a life
|
// Pac-Man loses a life
|
||||||
lives--;
|
lives--;
|
||||||
if(lives == 0)System.exit(1);
|
setPhase(RoundPhase.LIFE_LOST);
|
||||||
else pacman.resetPosition();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Font loadArcadeFont() {
|
// private boolean overlaps(PacMan p, Ghost g) {
|
||||||
try (InputStream is = getClass().getResourceAsStream("/fonts/PressStart2P-Regular.ttf")) {
|
// // center-distance or AABB; center distance keeps the arcade feel
|
||||||
return Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(16f);
|
// double dx = p.getCenterX() - g.getCenterX();
|
||||||
} catch (Exception e) {
|
// double dy = p.getCenterY() - g.getCenterY();
|
||||||
return new Font("Monospaced", Font.BOLD, 16);
|
// double r = map.getTileSize() * 0.45; // tune threshold
|
||||||
}
|
// return (dx*dx + dy*dy) <= r*r;
|
||||||
|
// }
|
||||||
|
|
||||||
|
private void endGame() {
|
||||||
|
gameOverState.setScore(score);
|
||||||
|
gameOverState.setLevel(levelManager.getCurrentLevel().getLevel());
|
||||||
|
gameStateManager.setState(GameStateType.GAME_OVER);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setScore(int score) {
|
public void setScore(int score) {
|
||||||
|
|||||||
8
src/main/java/se/urmo/game/state/RoundPhase.java
Normal file
8
src/main/java/se/urmo/game/state/RoundPhase.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package se.urmo.game.state;
|
||||||
|
|
||||||
|
public enum RoundPhase {
|
||||||
|
READY, // "READY!" shown briefly before play starts
|
||||||
|
PLAYING, // normal gameplay
|
||||||
|
LEVEL_COMPLETE, // all dots eaten; freeze briefly then advance level
|
||||||
|
LIFE_LOST // Pac-Man hit; freeze briefly then reset positions}
|
||||||
|
}
|
||||||
26
src/main/java/se/urmo/game/util/GameFonts.java
Normal file
26
src/main/java/se/urmo/game/util/GameFonts.java
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package se.urmo.game.util;
|
||||||
|
|
||||||
|
import java.awt.Font;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
public enum GameFonts {
|
||||||
|
ARCADE_FONT("/fonts/PressStart2P-Regular.ttf");
|
||||||
|
|
||||||
|
private final Font font;
|
||||||
|
|
||||||
|
GameFonts(String s) {
|
||||||
|
font = loadArcadeFont(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Font loadArcadeFont(String name) {
|
||||||
|
try (InputStream is = GameFonts.class.getResourceAsStream(name)) {
|
||||||
|
return Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(16f);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return new Font("Monospaced", Font.BOLD, 16);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Font arcade(float textSize) {
|
||||||
|
return ARCADE_FONT.font.deriveFont(textSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package se.urmo.game.entities;
|
package se.urmo.game.util;
|
||||||
|
|
||||||
public class MyPoint {
|
public class MyPoint {
|
||||||
public final double x;
|
public final double x;
|
||||||
Reference in New Issue
Block a user