Finishing map-graphics

This commit is contained in:
Urban Modig
2025-08-15 11:26:50 +02:00
parent 80cd0c242f
commit fe2eb8cb48
9 changed files with 149 additions and 55 deletions

View File

@ -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;
}
}

View File

@ -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);
}
}
}

View File

@ -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() {

View File

@ -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);