38 lines
1.0 KiB
Java
38 lines
1.0 KiB
Java
package se.urmo.game.map;
|
|
|
|
import lombok.Getter;
|
|
import lombok.Setter;
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
@Getter
|
|
@Setter
|
|
public class MapTile {
|
|
private final int value;
|
|
private BufferedImage image;
|
|
private final boolean solid;
|
|
private final boolean[][] collisionMask;
|
|
|
|
public MapTile(BufferedImage image, int value) {
|
|
this.value = value;
|
|
this.image = image;
|
|
this.solid = value != 0 && value != 99 && value != 56;
|
|
this.collisionMask = value != 0 ? createCollisionMask(image) : null;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
}
|