Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SpriteSheet {
- private String path;
- public final int SIZE;
- public final int SPRITE_WIDTH, SPRITE_HEIGHT;
- private int width, height;
- public int[] pixels;
- public static SpriteSheet tiles = new SpriteSheet("/textures/spritesheet.png", 256);
- public static SpriteSheet projectiles = new SpriteSheet("/textures/projectiles.png", 48);
- public static SpriteSheet player = new SpriteSheet("/mobs/player_sheet.png", 128, 96);
- public static SpriteSheet player_down = new SpriteSheet(player, 0, 0, 1, 3, 32);
- public static SpriteSheet player_up = new SpriteSheet(player, 1, 0, 1, 3, 32);
- public static SpriteSheet player_left = new SpriteSheet(player, 2, 0, 1, 3, 32);
- public static SpriteSheet player_right = new SpriteSheet(player, 3, 0, 1, 3, 32);
- public static SpriteSheet king_cherno = new SpriteSheet("/mobs/king_cherno.png", 128, 96);
- public static SpriteSheet cherno_down = new SpriteSheet(king_cherno, 0, 0, 1, 3, 32);
- public static SpriteSheet cherno_up = new SpriteSheet(king_cherno, 1, 0, 1, 3, 32);
- public static SpriteSheet cherno_left = new SpriteSheet(king_cherno, 2, 0, 1, 3, 32);
- public static SpriteSheet cherno_right = new SpriteSheet(king_cherno, 3, 0, 1, 3, 32);
- public static SpriteSheet citizen = new SpriteSheet("/mobs/citizen_sheet.png", 128, 96);
- public static SpriteSheet citizen_down = new SpriteSheet(citizen, 0, 0, 1, 3, 32);
- public static SpriteSheet citizen_up = new SpriteSheet(citizen, 1, 0, 1, 3, 32);
- public static SpriteSheet citizen_left = new SpriteSheet(citizen, 2, 0, 1, 3, 32);
- public static SpriteSheet citizen_right = new SpriteSheet(citizen, 3, 0, 1, 3, 32);
- private Sprite[] sprites;
- public SpriteSheet(SpriteSheet sheet, int x, int y, int width, int height, int spriteSize) {
- int xx = x * spriteSize;
- int yy = y * spriteSize;
- int w = width * spriteSize;
- int h = height * spriteSize;
- if (width == height) SIZE = width;
- else SIZE = -1;
- SPRITE_WIDTH = w;
- SPRITE_HEIGHT = h;
- pixels = new int[w * h];
- for (int y0 = 0; y0 < h; y0++) {
- int yp = yy + y0;
- for (int x0 = 0; x0 < w; x0++) {
- int xp = xx + x0;
- pixels[x0 + y0 * w] = sheet.pixels[xp + yp * sheet.SPRITE_WIDTH];
- }
- }
- int frame = 0;
- sprites = new Sprite[width * height];
- for (int ya = 0; ya < height; ya++) {
- for (int xa = 0; xa < width; xa++) {
- int[] spritePixels = new int[spriteSize * spriteSize];
- for (int y0 = 0; y0 < spriteSize; y0++) {
- for (int x0 = 0; x0 < spriteSize; x0++) {
- spritePixels[x0 + y0 * spriteSize] = pixels[(x0 + xa * spriteSize) + (y0 + ya * spriteSize) * SPRITE_WIDTH];
- }
- }
- Sprite sprite = new Sprite(spritePixels, spriteSize, spriteSize);
- sprites[frame++] = sprite;
- }
- }
- }
- public SpriteSheet(String path, int size) {
- this.path = path;
- SIZE = size;
- SPRITE_WIDTH = size;
- SPRITE_HEIGHT = size;
- pixels = new int[SIZE * SIZE];
- load();
- }
- public SpriteSheet(String path, int width, int height) {
- this.path = path;
- SIZE = -1;
- SPRITE_WIDTH = width;
- SPRITE_HEIGHT = height;
- pixels = new int[SPRITE_WIDTH * SPRITE_HEIGHT];
- load();
- }
- public Sprite[] getSprites() {
- return sprites;
- }
- public int getWidth() {
- return width;
- }
- public int getHeight() {
- return height;
- }
- public int[] getPixels() {
- return pixels;
- }
- private void load() {
- try {
- System.out.print("Attempting to load: " + path + "...");
- BufferedImage image = ImageIO.read(SpriteSheet.class.getResource(path));
- System.out.println(" succeeded!");
- width = image.getWidth();
- height = image.getHeight();
- pixels = new int[width * height];
- image.getRGB(0, 0, width, height, pixels, 0, width);
- } catch (IOException e) {
- e.printStackTrace();
- } catch (Exception e) {
- System.err.println(" failed!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement