diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/src/main/java/se/urmo/game/entities/PacMan.java b/src/main/java/se/urmo/game/entities/PacMan.java index 99f2e80..191c728 100644 --- a/src/main/java/se/urmo/game/entities/PacMan.java +++ b/src/main/java/se/urmo/game/entities/PacMan.java @@ -14,7 +14,9 @@ import java.util.Arrays; public class PacMan { - public static final int PACMAN_SIZE = GamePanel.TILE_SIZE; + public static final int PACMAN_SIZE = 32; + private static final int COLLISION_BOX_SIZE = 8; + private static final int COLLISION_BOX_OFFSET = (PACMAN_SIZE - COLLISION_BOX_SIZE) / 2; private final Game game; private int aniTick = 0; private int aniIndex = 0; @@ -22,9 +24,8 @@ public class PacMan { private int speed = 1; private boolean moving; private final BufferedImage[][] movmentImages = new BufferedImage[4][4]; - //private int xPos = 14 * 16 + 8, yPos = 29 * 16; // top left of object private Point position; - private static final BufferedImage innerBox = MiscUtil.createOutlinedBox(8, 8, Color.yellow, 2); + private static final BufferedImage COLLISION_BOX = MiscUtil.createOutlinedBox(COLLISION_BOX_SIZE, COLLISION_BOX_SIZE, Color.yellow, 2); private CollisionChecker collisionChecker; private Direction direction = Direction.NONE; @@ -58,10 +59,13 @@ public class PacMan { public void draw(Graphics g) { - g.drawImage(innerBox, position.x, position.y, PACMAN_SIZE, PACMAN_SIZE, null); - //g.setColor(Color.RED); - //g.drawLine(xPos, yPos, xPos, yPos); - //g.drawImage(movmentImages[direction][aniIndex], xPos, yPos, PACMAN_SIZE, PACMAN_SIZE, null); + g.drawImage( + movmentImages[direction==Direction.NONE?0:direction.ordinal()][aniIndex], + position.x - COLLISION_BOX_OFFSET, + position.y - COLLISION_BOX_OFFSET, + PACMAN_SIZE, + PACMAN_SIZE, null); + g.drawImage(COLLISION_BOX, position.x, position.y, COLLISION_BOX_SIZE, COLLISION_BOX_SIZE, null); } public void update() { @@ -76,7 +80,7 @@ public class PacMan { default -> throw new IllegalStateException("Unexpected value: " + direction); }; - Point destination = collisionChecker.getValidDestination(direction, newPosition, PACMAN_SIZE, PACMAN_SIZE); + Point destination = collisionChecker.getValidDestination(direction, newPosition, COLLISION_BOX_SIZE, COLLISION_BOX_SIZE); if(destination != null) { position = destination; @@ -104,7 +108,6 @@ public class PacMan { } public void setDirection(Direction direction) { - this.moving = true; this.direction = direction; } } diff --git a/src/main/java/se/urmo/game/map/GameMap.java b/src/main/java/se/urmo/game/map/GameMap.java index 783484c..72d44eb 100644 --- a/src/main/java/se/urmo/game/map/GameMap.java +++ b/src/main/java/se/urmo/game/map/GameMap.java @@ -15,12 +15,12 @@ public class GameMap { public static final int MAP_TILESIZE = 16;// 16px from left public static final int OFFSET_Y = 7 * MAP_TILESIZE; // 160px from top public static final int OFFSET_X = MAP_TILESIZE; // 16px from left - private BufferedImage[][] image = new BufferedImage[13][19]; + private final BufferedImage[][] images = new BufferedImage[13][19]; private MapTile[][] mapData; public GameMap() { - loadMap("maps/map1.csv"); loadSprites(); + loadMap("maps/map1.csv"); } private void loadMap(String path) { @@ -43,16 +43,78 @@ public class GameMap { } mapData = rows.stream() .map(row -> IntStream.of(row) - .mapToObj(MapTile::new) + .mapToObj(value -> new MapTile(getSprite(value), value)) .toArray(MapTile[]::new)) .toArray(MapTile[][]::new); } + private BufferedImage getSprite(int value) { + return switch (value){ + case 1 -> images[0][0]; + case 2 -> images[0][1]; + case 3 -> images[0][2]; + case 4 -> images[0][3]; + case 5 -> images[0][4]; + case 6 -> images[0][5]; + case 7 -> images[0][6]; + case 8 -> images[0][7]; + case 9 -> images[0][8]; + case 10 -> images[0][9]; + case 11 -> images[0][10]; + case 12 -> images[1][0]; + case 13 -> images[1][1]; + case 14 -> images[1][2]; + case 15 -> images[1][3]; + case 16 -> images[1][4]; + case 17 -> images[1][5]; + case 18 -> images[1][6]; + case 19 -> images[1][7]; + case 20 -> images[1][8]; + case 21 -> images[1][9]; + case 22 -> images[1][10]; + case 23 -> images[2][0]; + case 24 -> images[2][1]; + case 25 -> images[2][2]; + case 26 -> images[2][3]; + case 27 -> images[2][4]; + case 28 -> images[2][5]; + case 29 -> images[2][6]; + case 30 -> images[2][7]; + case 31 -> images[2][8]; + case 32 -> images[2][9]; + case 33 -> images[2][10]; + case 34 -> images[3][0]; + case 35 -> images[3][1]; + case 36 -> images[3][2]; + case 37 -> images[3][3]; + case 38 -> images[3][4]; + case 39 -> images[3][5]; + case 40 -> images[3][6]; + case 41 -> images[3][7]; + case 42 -> images[3][8]; + case 43 -> images[3][9]; + case 44 -> images[3][10]; + case 45 -> images[4][0]; + case 46 -> images[4][1]; + case 47 -> images[4][2]; + case 48 -> images[4][3]; + case 49 -> images[4][4]; + case 50 -> images[4][5]; + case 51 -> images[4][6]; + case 52 -> images[4][7]; + case 53 -> images[4][8]; + case 54 -> images[4][9]; + case 55 -> images[4][10]; + + default -> null; + }; + } + private void loadSprites() { - BufferedImage img = LoadSave.GetSpriteAtlas("sprites/PacManAssets_Map_TileSet2.png");//473B78 - for (int row = 0; row < 13; row++) { - for (int col = 0; col < 19; col++) { - image[row][col] = img.getSubimage(MAP_TILESIZE * col, MAP_TILESIZE * row, MAP_TILESIZE, MAP_TILESIZE); + BufferedImage img = LoadSave.GetSpriteAtlas("sprites/PacMan-custom-spritemap-0-1.png");//473B78 + for (int row = 0; row < 5; row++) { + for (int col = 0; col < 11; col++) { + images[row][col] = img.getSubimage(MAP_TILESIZE * col, MAP_TILESIZE * row, MAP_TILESIZE, MAP_TILESIZE); } } } diff --git a/src/main/java/se/urmo/game/map/MapTile.java b/src/main/java/se/urmo/game/map/MapTile.java index ade6967..9e9968b 100644 --- a/src/main/java/se/urmo/game/map/MapTile.java +++ b/src/main/java/se/urmo/game/map/MapTile.java @@ -1,19 +1,38 @@ package se.urmo.game.map; -import se.urmo.game.util.MiscUtil; - -import java.awt.*; import java.awt.image.BufferedImage; public class MapTile { private final int value; private final BufferedImage image; private final boolean solid; + private final boolean[][] collisionMask; - public MapTile(int value) { + public MapTile(BufferedImage image, int value) { this.value = value; - this.image = value != 0 ? MiscUtil.createOutlinedBox(16, 16, Color.blue, 2) : null; + this.image = image; +// this.image = value != 0 ? MiscUtil.createOutlinedBox(16, 16, Color.blue, 2) : null; + this.solid = value != 0; + this.collisionMask = createCollisionMask(image); + } + + public boolean[][] getCollisionMask() { + return collisionMask; + } + + private boolean[][] createCollisionMask(BufferedImage img) { + if(img == null) return null; + int w = img.getWidth(); + int h = img.getHeight(); + boolean[][] mask = new boolean[h][w]; + for (int y = 0; y < h; y++) { + for (int x = 0; x < w; x++) { + int alpha = (img.getRGB(x, y) >> 24) & 0xff; + mask[y][x] = alpha > 0; // solid if any pixel is visible + } + } + return mask; } public BufferedImage getImage() { diff --git a/src/main/java/se/urmo/game/state/PlayingState.java b/src/main/java/se/urmo/game/state/PlayingState.java index b532d8d..ca31bf0 100644 --- a/src/main/java/se/urmo/game/state/PlayingState.java +++ b/src/main/java/se/urmo/game/state/PlayingState.java @@ -35,13 +35,18 @@ public class PlayingState implements GameState { @Override public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { - case KeyEvent.VK_W -> pacman.setDirection(Direction.UP); - case KeyEvent.VK_S -> pacman.setDirection(Direction.DOWN); - case KeyEvent.VK_A -> pacman.setDirection(Direction.LEFT); - case KeyEvent.VK_D -> pacman.setDirection(Direction.RIGHT); + case KeyEvent.VK_W -> move(Direction.UP); + case KeyEvent.VK_S -> move(Direction.DOWN); + case KeyEvent.VK_A -> move(Direction.LEFT); + case KeyEvent.VK_D -> move(Direction.RIGHT); } } + private void move(Direction direction) { + pacman.setMoving(true); + pacman.setDirection(direction); + } + @Override public void keyReleased(KeyEvent e) { pacman.setMoving(false); diff --git a/src/main/resources/maps/map1.csv b/src/main/resources/maps/map1.csv index 28530d1..e9ac27d 100644 --- a/src/main/resources/maps/map1.csv +++ b/src/main/resources/maps/map1.csv @@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0, 0, 0, 0, 0, 0,15,17, 0, 0, 0, 0,15,17, 0, 0, 0, 0,15,17, 0, 0, 0, 0, 0, 0,14 +23,24,24,24,24,11, 0,15,19, 5, 5, 6, 0,15,17, 0, 4, 5, 5,18,17, 0, 9,10,10,10,10,25 + 0, 0, 0, 0, 0,22, 0,15, 8,27,27,28, 0,26,28, 0,26,27,27, 7,17, 0,20, 0, 0, 0, 0, 0 + 0, 0, 0, 0, 0,22, 0,15,17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,15,17, 0,20, 0, 0, 0, 0, 0 + 0, 0, 0, 0, 0,22, 0,15,17, 0,34,35,36,37,38,35,35,39, 0,15,17, 0,20, 0, 0, 0, 0, 0 +32,32,32,32,32,33, 0,26,28, 0,40, 0, 0, 0, 0, 0, 0,41, 0,26,28, 0,31,32,32,32,32,32 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,40, 0, 0, 0, 0, 0, 0,41, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +10,10,10,10,10,11, 0, 4, 6, 0,40, 0, 0, 0, 0, 0, 0,41, 0, 4, 6, 0, 9,10,10,10,10,10 + 0, 0, 0, 0, 0,22, 0,15,17, 0,42,43,43,43,43,43,43,44, 0,15,17, 0,20, 0, 0, 0, 0, 0 + 0, 0, 0, 0, 0,22, 0,15,17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,15,17, 0,20, 0, 0, 0, 0, 0 + 0, 0, 0, 0, 0,22, 0,15,17, 0, 4, 5, 5, 5, 5, 5, 5, 6, 0,15,17, 0,20, 0, 0, 0, 0, 0 + 1,32,32,32,32,33, 0,26,28, 0,26,27,27, 7, 8,27,27,28, 0,26,28, 0,31, 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, 7,17, 0,26,27,27,27,28, 0,26,28, 0,26,27,27,27,28, 0,15, 8,27,28, 0,14 +12, 0, 0, 0,15,17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,15,17, 0, 0, 0,14 +47, 5, 6, 0,15,17, 0, 4, 6, 0, 4, 5, 5, 5, 5, 5, 5, 6, 0, 4, 6, 0,15,17, 0, 4, 5,48 +49,27,28, 0,26,28, 0,15,17, 0,26,27,27, 7, 8,27,27,28, 0,15,17, 0,26,28, 0,26,27,50 +12, 0, 0, 0, 0, 0, 0,15,17, 0, 0, 0, 0,15,17, 0, 0, 0, 0,15,17, 0, 0, 0, 0, 0, 0,14 +12, 0, 4, 5, 5, 5, 5,18,19, 5, 5, 6, 0,15,17, 0, 4, 5, 5,18,19, 5, 5, 5, 5, 6, 0,14 +12, 0,26,27,27,27,27,27,27,27,27,28, 0,26,28, 0,26,27,27,27,27,27,27,27,27,28, 0,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 +23,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,25 \ No newline at end of file diff --git a/src/main/resources/sprites/PacMan-custom-spritemap-0-0.png b/src/main/resources/sprites/PacMan-custom-spritemap-0-0.png new file mode 100644 index 0000000..6530df4 Binary files /dev/null and b/src/main/resources/sprites/PacMan-custom-spritemap-0-0.png differ diff --git a/src/main/resources/sprites/PacMan-custom-spritemap-0-1.png b/src/main/resources/sprites/PacMan-custom-spritemap-0-1.png new file mode 100644 index 0000000..3455d3d Binary files /dev/null and b/src/main/resources/sprites/PacMan-custom-spritemap-0-1.png differ diff --git a/src/main/resources/sprites/PacMan-custom-spritemap-0-3.png b/src/main/resources/sprites/PacMan-custom-spritemap-0-3.png new file mode 100644 index 0000000..2cd9f50 Binary files /dev/null and b/src/main/resources/sprites/PacMan-custom-spritemap-0-3.png differ