Basic score count working
update takes movement into account
This commit is contained in:
@ -65,8 +65,4 @@ public class CollisionChecker {
|
|||||||
|
|
||||||
return new Point(x, y);
|
return new Point(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeTile(Point destination) {
|
|
||||||
map.removeTileImage(destination);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,6 +7,6 @@ import java.awt.Point;
|
|||||||
public class BlinkyStrategy implements GhostStrategy {
|
public class BlinkyStrategy implements GhostStrategy {
|
||||||
@Override
|
@Override
|
||||||
public Point chooseTarget(Ghost ghost, PacMan pacman, GameMap map) {
|
public Point chooseTarget(Ghost ghost, PacMan pacman, GameMap map) {
|
||||||
return pacman.getTilePosition();
|
return pacman.getPosition();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,7 +7,7 @@ import java.awt.Point;
|
|||||||
public class ClydeStrategy implements GhostStrategy {
|
public class ClydeStrategy implements GhostStrategy {
|
||||||
@Override
|
@Override
|
||||||
public Point chooseTarget(Ghost clyde, PacMan pacman, GameMap map) {
|
public Point chooseTarget(Ghost clyde, PacMan pacman, GameMap map) {
|
||||||
Point pacTile = pacman.getTilePosition();
|
Point pacTile = pacman.getPosition();
|
||||||
Point clydeTile = clyde.getPosition(); // ghost’s current tile
|
Point clydeTile = clyde.getPosition(); // ghost’s current tile
|
||||||
|
|
||||||
double distance = pacTile.distance(clydeTile);
|
double distance = pacTile.distance(clydeTile);
|
||||||
|
|||||||
@ -96,7 +96,7 @@ public class Ghost {
|
|||||||
Point point = new Point(
|
Point point = new Point(
|
||||||
position.x + direction.dx,
|
position.x + direction.dx,
|
||||||
position.y + direction.dy);
|
position.y + direction.dy);
|
||||||
log.debug("Next position {}", point);
|
//log.debug("Next position {}", point);
|
||||||
return point;
|
return point;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,7 +15,7 @@ public class InkyStrategy implements GhostStrategy {
|
|||||||
public Point chooseTarget(Ghost ghost, PacMan pacman, GameMap map) {
|
public Point chooseTarget(Ghost ghost, PacMan pacman, GameMap map) {
|
||||||
// 1. Two tiles ahead of pacman
|
// 1. Two tiles ahead of pacman
|
||||||
Direction pacmanDir = pacman.getDirection();
|
Direction pacmanDir = pacman.getDirection();
|
||||||
Point pacmanPos = pacman.getTilePosition();
|
Point pacmanPos = pacman.getPosition();
|
||||||
Point ahead = switch (pacmanDir){
|
Point ahead = switch (pacmanDir){
|
||||||
case RIGHT -> new Point(pacmanPos.x + 8 * GameMap.MAP_TILESIZE, pacmanPos.y);
|
case RIGHT -> new Point(pacmanPos.x + 8 * GameMap.MAP_TILESIZE, pacmanPos.y);
|
||||||
case LEFT -> new Point(pacmanPos.x - 8 * GameMap.MAP_TILESIZE, pacmanPos.y);
|
case LEFT -> new Point(pacmanPos.x - 8 * GameMap.MAP_TILESIZE, pacmanPos.y);
|
||||||
|
|||||||
@ -30,6 +30,7 @@ public class PacMan {
|
|||||||
@Setter
|
@Setter
|
||||||
private boolean moving;
|
private boolean moving;
|
||||||
private final BufferedImage[][] movmentImages = new BufferedImage[4][4];
|
private final BufferedImage[][] movmentImages = new BufferedImage[4][4];
|
||||||
|
@Getter
|
||||||
private Point position;
|
private Point position;
|
||||||
private static final BufferedImage COLLISION_BOX = MiscUtil.createOutlinedBox(COLLISION_BOX_SIZE, COLLISION_BOX_SIZE, Color.yellow, 2);
|
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;
|
||||||
@ -82,8 +83,8 @@ public class PacMan {
|
|||||||
|
|
||||||
public void update() {
|
public void update() {
|
||||||
updateAnimationTick();
|
updateAnimationTick();
|
||||||
if(direction == Direction.NONE) return;
|
//if(direction == Direction.NONE) return;
|
||||||
|
if(moving) {
|
||||||
Point newPosition = switch (direction) {
|
Point newPosition = switch (direction) {
|
||||||
case RIGHT -> new Point(position.x + speed, position.y);
|
case RIGHT -> new Point(position.x + speed, position.y);
|
||||||
case LEFT -> new Point(position.x - speed, position.y);
|
case LEFT -> new Point(position.x - speed, position.y);
|
||||||
@ -95,10 +96,10 @@ public class PacMan {
|
|||||||
Point destination = collisionChecker.getValidDestination(direction, newPosition, COLLISION_BOX_SIZE, COLLISION_BOX_SIZE);
|
Point destination = collisionChecker.getValidDestination(direction, newPosition, COLLISION_BOX_SIZE, COLLISION_BOX_SIZE);
|
||||||
|
|
||||||
if (destination != null) {
|
if (destination != null) {
|
||||||
collisionChecker.removeTile(destination);
|
|
||||||
position = destination;
|
position = destination;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void updateAnimationTick() {
|
private void updateAnimationTick() {
|
||||||
if (moving) {
|
if (moving) {
|
||||||
@ -114,10 +115,6 @@ public class PacMan {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Point getTilePosition() {
|
|
||||||
return position;
|
|
||||||
}
|
|
||||||
|
|
||||||
public double distanceTo(Point point) {
|
public double distanceTo(Point point) {
|
||||||
return position.distance(point);
|
return position.distance(point);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,7 +9,7 @@ public class PinkyStrategy implements GhostStrategy{
|
|||||||
@Override
|
@Override
|
||||||
public Point chooseTarget(Ghost ghost, PacMan pacman, GameMap map) {
|
public Point chooseTarget(Ghost ghost, PacMan pacman, GameMap map) {
|
||||||
Direction pacmanDir = pacman.getDirection();
|
Direction pacmanDir = pacman.getDirection();
|
||||||
Point pacmanPos = pacman.getTilePosition();
|
Point pacmanPos = pacman.getPosition();
|
||||||
return switch (pacmanDir){
|
return switch (pacmanDir){
|
||||||
case RIGHT -> new Point(pacmanPos.x + 8 * GameMap.MAP_TILESIZE, pacmanPos.y);
|
case RIGHT -> new Point(pacmanPos.x + 8 * GameMap.MAP_TILESIZE, pacmanPos.y);
|
||||||
case LEFT -> new Point(pacmanPos.x - 8 * GameMap.MAP_TILESIZE, pacmanPos.y);
|
case LEFT -> new Point(pacmanPos.x - 8 * GameMap.MAP_TILESIZE, pacmanPos.y);
|
||||||
|
|||||||
@ -176,15 +176,30 @@ public class GameMap {
|
|||||||
if (col >= columns() || col < 0 ) return true;
|
if (col >= columns() || col < 0 ) return true;
|
||||||
MapTile mapTile = mapData[row][col];
|
MapTile mapTile = mapData[row][col];
|
||||||
boolean solid = mapTile.isSolid();
|
boolean solid = mapTile.isSolid();
|
||||||
log.debug("[{}][{}] is {} ({})", row, col, solid ? "solid" : " not solid", mapTile.getValue());
|
// log.debug("[{}][{}] is {} ({})", row, col, solid ? "solid" : " not solid", mapTile.getValue());
|
||||||
return solid;
|
return solid;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeTileImage(Point destination) {
|
public boolean removeTileImage(Point screen) {
|
||||||
int row = screenToRow(destination);
|
if (screen == null || mapData == null) {
|
||||||
int col = screenToCol(destination);
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int row = screenToRow(screen);
|
||||||
|
int col = screenToCol(screen);
|
||||||
|
|
||||||
|
if (row < 0 || row >= mapData.length || col < 0 || col >= mapData[0].length) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
MapTile tile = mapData[row][col];
|
MapTile tile = mapData[row][col];
|
||||||
if(tile.getValue() == 0) tile.setImage(null);
|
if (tile != null && tile.getValue() == 0 && tile.getImage() != null) {
|
||||||
|
tile.setImage(null);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int screenToCol(Point point) {
|
private static int screenToCol(Point point) {
|
||||||
@ -230,11 +245,12 @@ public class GameMap {
|
|||||||
return new Point(screen.x - OFFSET_X, screen.y - OFFSET_Y);
|
return new Point(screen.x - OFFSET_X, screen.y - OFFSET_Y);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int screenToCol(int screenX) {
|
public static int screenToCol(int screenX) {
|
||||||
return (screenX - OFFSET_X) / MAP_TILESIZE;
|
return (screenX - OFFSET_X) / MAP_TILESIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int screenToRow(int screenY) {
|
public static int screenToRow(int screenY) {
|
||||||
return (screenY - OFFSET_Y) / MAP_TILESIZE;
|
return (screenY - OFFSET_Y) / MAP_TILESIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
package se.urmo.game.state;
|
package se.urmo.game.state;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
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.BlinkyStrategy;
|
|
||||||
import se.urmo.game.entities.Ghost;
|
import se.urmo.game.entities.Ghost;
|
||||||
import se.urmo.game.entities.PacMan;
|
import se.urmo.game.entities.PacMan;
|
||||||
import se.urmo.game.main.Game;
|
import se.urmo.game.main.Game;
|
||||||
@ -14,6 +14,7 @@ import java.awt.*;
|
|||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
public class PlayingState implements GameState {
|
public class PlayingState implements GameState {
|
||||||
private final Game game;
|
private final Game game;
|
||||||
private final GameStateManager gameStateManager;
|
private final GameStateManager gameStateManager;
|
||||||
@ -39,6 +40,16 @@ public class PlayingState implements GameState {
|
|||||||
pacman.update();
|
pacman.update();
|
||||||
ghostManager.update(pacman, map);
|
ghostManager.update(pacman, map);
|
||||||
checkCollisions();
|
checkCollisions();
|
||||||
|
handleDots();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleDots() {
|
||||||
|
Point pacmanScreenPos = pacman.getPosition();
|
||||||
|
boolean wasRemoved = map.removeTileImage(pacmanScreenPos);
|
||||||
|
|
||||||
|
if(wasRemoved){
|
||||||
|
score+=10;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -84,7 +95,6 @@ public class PlayingState implements GameState {
|
|||||||
@Override
|
@Override
|
||||||
public void keyReleased(KeyEvent e) {
|
public void keyReleased(KeyEvent e) {
|
||||||
pacman.setMoving(false);
|
pacman.setMoving(false);
|
||||||
pacman.setDirection(Direction.NONE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkCollisions() {
|
private void checkCollisions() {
|
||||||
|
|||||||
Reference in New Issue
Block a user