Minor fixes
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
package se.urmo.game.collision;
|
package se.urmo.game.collision;
|
||||||
|
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import se.urmo.game.util.Direction;
|
import se.urmo.game.util.Direction;
|
||||||
import se.urmo.game.map.GameMap;
|
import se.urmo.game.map.GameMap;
|
||||||
|
|
||||||
@ -8,6 +9,7 @@ import java.awt.Point;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
public class CollisionChecker {
|
public class CollisionChecker {
|
||||||
private GameMap map;
|
private GameMap map;
|
||||||
|
|
||||||
@ -17,7 +19,7 @@ public class CollisionChecker {
|
|||||||
|
|
||||||
public Point getValidDestination(Direction direction, Point position, int agent_width, int agent_height) {
|
public Point getValidDestination(Direction direction, Point position, int agent_width, int agent_height) {
|
||||||
|
|
||||||
List<Point> list = switch (direction) {
|
List<Point> boundaries = switch (direction) {
|
||||||
case RIGHT -> List.of(
|
case RIGHT -> List.of(
|
||||||
new Point(position.x + agent_width, position.y), // TOPRIGHT
|
new Point(position.x + agent_width, position.y), // TOPRIGHT
|
||||||
new Point(position.x + agent_width, position.y + agent_height)); // BOTTOMRIGHT
|
new Point(position.x + agent_width, position.y + agent_height)); // BOTTOMRIGHT
|
||||||
@ -33,13 +35,13 @@ public class CollisionChecker {
|
|||||||
default -> Collections.EMPTY_LIST;
|
default -> Collections.EMPTY_LIST;
|
||||||
};
|
};
|
||||||
|
|
||||||
System.out.println( direction + " bounderies for " + position + " are " + list);
|
log.debug("{} boundaries for {} are {}", direction, position, boundaries);
|
||||||
|
|
||||||
List<Point> list2 = list.stream()
|
List<Point> normalized = boundaries.stream()
|
||||||
.map(p -> normalizePosition(direction, p, agent_width, agent_height))
|
.map(p -> normalizePosition(direction, p, agent_width, agent_height))
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
if (map.isPassable(list2)) {
|
if (map.isPassable(normalized)) {
|
||||||
return normalizePosition(direction, position, agent_width, agent_height);
|
return normalizePosition(direction, position, agent_width, agent_height);
|
||||||
}
|
}
|
||||||
return null; // Blocked
|
return null; // Blocked
|
||||||
|
|||||||
@ -2,6 +2,7 @@ package se.urmo.game.entities;
|
|||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import se.urmo.game.collision.CollisionChecker;
|
import se.urmo.game.collision.CollisionChecker;
|
||||||
import se.urmo.game.util.Direction;
|
import se.urmo.game.util.Direction;
|
||||||
import se.urmo.game.main.Game;
|
import se.urmo.game.main.Game;
|
||||||
@ -14,6 +15,7 @@ import java.awt.image.BufferedImage;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
public class PacMan {
|
public class PacMan {
|
||||||
public static final int PACMAN_SIZE = 32;
|
public static final int PACMAN_SIZE = 32;
|
||||||
private static final int COLLISION_BOX_SIZE = 16;
|
private static final int COLLISION_BOX_SIZE = 16;
|
||||||
@ -36,7 +38,7 @@ public class PacMan {
|
|||||||
public PacMan(Game game, CollisionChecker collisionChecker) {
|
public PacMan(Game game, CollisionChecker collisionChecker) {
|
||||||
this.game = game;
|
this.game = game;
|
||||||
this.collisionChecker = collisionChecker;
|
this.collisionChecker = collisionChecker;
|
||||||
position = new Point(26 * 16 + 8 + GameMap.OFFSET_X, 13 * 16 + GameMap.OFFSET_Y);
|
position = new Point(26 * GameMap.MAP_TILESIZE + 8 + GameMap.OFFSET_X, 13 * GameMap.MAP_TILESIZE + GameMap.OFFSET_Y);
|
||||||
loadAnimation();
|
loadAnimation();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,7 +48,7 @@ public class PacMan {
|
|||||||
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++) {
|
||||||
for (int col = 0; col < 4; col++) {
|
for (int col = 0; col < 4; col++) {
|
||||||
image[row][col] = img.getSubimage(32 * col, 32 * 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];
|
movmentImages[Direction.RIGHT.ordinal()] = image[0];
|
||||||
@ -83,7 +85,7 @@ public class PacMan {
|
|||||||
case DOWN -> new Point(position.x, position.y + speed);
|
case DOWN -> new Point(position.x, position.y + speed);
|
||||||
default -> throw new IllegalStateException("Unexpected value: " + direction);
|
default -> throw new IllegalStateException("Unexpected value: " + direction);
|
||||||
};
|
};
|
||||||
System.out.println("At: " + position+ ",trying to move " + direction.name() + " to " + newPosition);
|
log.debug("At: {},trying to move {} to {}", position, direction.name(), newPosition);
|
||||||
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) {
|
||||||
@ -99,7 +101,7 @@ public class PacMan {
|
|||||||
if (aniTick >= ANIMATION_UPDATE_FREQUENCY) {
|
if (aniTick >= ANIMATION_UPDATE_FREQUENCY) {
|
||||||
aniTick = 0;
|
aniTick = 0;
|
||||||
aniIndex++;
|
aniIndex++;
|
||||||
if (aniIndex >= 3) {
|
if (aniIndex >= 4) {
|
||||||
aniIndex = 0;
|
aniIndex = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,9 +1,12 @@
|
|||||||
package se.urmo.game.main;
|
package se.urmo.game.main;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import se.urmo.game.state.GameStateManager;
|
import se.urmo.game.state.GameStateManager;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
public class Game implements Runnable {
|
public class Game implements Runnable {
|
||||||
public final static int FPS_SET = 120;
|
public final static int FPS_SET = 120;
|
||||||
public final static int UPS_SET = 120;
|
public final static int UPS_SET = 120;
|
||||||
@ -11,9 +14,11 @@ public class Game implements Runnable {
|
|||||||
private final static double timePerUpdate = 1000000000.0 / UPS_SET;
|
private final static double timePerUpdate = 1000000000.0 / UPS_SET;
|
||||||
|
|
||||||
|
|
||||||
|
@Getter
|
||||||
private final GameStateManager gameStateManager;
|
private final GameStateManager gameStateManager;
|
||||||
private Thread gameThread;
|
private Thread gameThread;
|
||||||
private final JFrame window = new JFrame();
|
private final JFrame window = new JFrame();
|
||||||
|
@Getter
|
||||||
private final GamePanel gamePanel;
|
private final GamePanel gamePanel;
|
||||||
|
|
||||||
public Game() {
|
public Game() {
|
||||||
@ -69,18 +74,10 @@ public class Game implements Runnable {
|
|||||||
|
|
||||||
if (System.currentTimeMillis() - lastCheck >= 1000) {
|
if (System.currentTimeMillis() - lastCheck >= 1000) {
|
||||||
lastCheck = System.currentTimeMillis();
|
lastCheck = System.currentTimeMillis();
|
||||||
//System.out.println("FPS: " + frames + " | UPS: " + updates);
|
log.debug("FPS: " + frames + " | UPS: " + updates);
|
||||||
frames = 0;
|
frames = 0;
|
||||||
updates = 0;
|
updates = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public GamePanel getGamePanel() {
|
|
||||||
return gamePanel;
|
|
||||||
}
|
|
||||||
|
|
||||||
public GameStateManager getGameStateManager() {
|
|
||||||
return gameStateManager;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -148,11 +148,11 @@ public class GameMap {
|
|||||||
int col = (x - OFFSET_X) / MAP_TILESIZE;
|
int col = (x - OFFSET_X) / MAP_TILESIZE;
|
||||||
int tileY = (y - OFFSET_Y) % MAP_TILESIZE;
|
int tileY = (y - OFFSET_Y) % MAP_TILESIZE;
|
||||||
int tileX = (x - OFFSET_X) % MAP_TILESIZE;
|
int tileX = (x - OFFSET_X) % MAP_TILESIZE;
|
||||||
System.out.print("Point[x="+x+",y="+y+"] is row="+ row + ", col=" + col + " with reminder x=" +tileX+",y=" +tileY);
|
log.trace("Point[x="+x+",y="+y+"] is row="+ row + ", col=" + col + " with reminder x=" +tileX+",y=" +tileY);
|
||||||
|
|
||||||
boolean[][] mask = mapData[row][col].getCollisionMask();
|
boolean[][] mask = mapData[row][col].getCollisionMask();
|
||||||
boolean b = mask == null || !mask[tileY][tileX];
|
boolean b = mask == null || !mask[tileY][tileX];
|
||||||
System.out.println(b?" - passable":" - not passable");
|
log.trace(b?" - passable":" - not passable");
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -195,16 +195,12 @@ public class GameMap {
|
|||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isSolid(List<Point> points) {
|
|
||||||
return points.stream().allMatch(p -> isSolid(p.x, p.y));
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isSolid(int x, int y) {
|
public boolean isSolid(int x, int y) {
|
||||||
int row = (y - OFFSET_Y) / MAP_TILESIZE;
|
int row = (y - OFFSET_Y) / MAP_TILESIZE;
|
||||||
int col = (x - OFFSET_X) / MAP_TILESIZE;
|
int col = (x - OFFSET_X) / MAP_TILESIZE;
|
||||||
MapTile mapTile = mapData[row][col];
|
MapTile mapTile = mapData[row][col];
|
||||||
boolean solid = mapTile.isSolid();
|
boolean solid = mapTile.isSolid();
|
||||||
log.debug("["+row+"]["+col+"] is " + (solid?"solid":" not solid") + " (" + mapTile.getValue() + ")");
|
log.debug("[{}][{}] is {} ({})", row, col, solid ? "solid" : " not solid", mapTile.getValue());
|
||||||
return solid;
|
return solid;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,8 +25,8 @@ public class GhostManager {
|
|||||||
public GhostManager(GhostCollisionChecker ghostCollisionChecker) {
|
public GhostManager(GhostCollisionChecker ghostCollisionChecker) {
|
||||||
loadAnimation();
|
loadAnimation();
|
||||||
// Create ghosts with their strategies
|
// Create ghosts with their strategies
|
||||||
ghosts.add(new Ghost(ghostCollisionChecker, new BlinkyStrategy(),image[0]));
|
//ghosts.add(new Ghost(ghostCollisionChecker, new BlinkyStrategy(),image[0]));
|
||||||
ghosts.add(new Ghost(ghostCollisionChecker, new PinkyStrategy(), image[1]));
|
//ghosts.add(new Ghost(ghostCollisionChecker, new PinkyStrategy(), image[1]));
|
||||||
//ghosts.add(new Ghost(240, 200, new InkyStrategy(), loader.getSprite("inky")));
|
//ghosts.add(new Ghost(240, 200, new InkyStrategy(), loader.getSprite("inky")));
|
||||||
//ghosts.add(new Ghost(260, 200, new ClydeStrategy(), loader.getSprite("clyde")));
|
//ghosts.add(new Ghost(260, 200, new ClydeStrategy(), loader.getSprite("clyde")));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user