Advertisement
Guest User

SpriteSheet class

a guest
Dec 4th, 2015
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.79 KB | None | 0 0
  1. public class SpriteSheet {
  2.  
  3.     private String path;
  4.     public final int SIZE;
  5.     public final int SPRITE_WIDTH, SPRITE_HEIGHT;
  6.     private int width, height;
  7.     public int[] pixels;
  8.  
  9.     public static SpriteSheet tiles = new SpriteSheet("/textures/spritesheet.png", 256);
  10.     public static SpriteSheet projectiles = new SpriteSheet("/textures/projectiles.png", 48);
  11.  
  12.     public static SpriteSheet player = new SpriteSheet("/mobs/player_sheet.png", 128, 96);
  13.     public static SpriteSheet player_down = new SpriteSheet(player, 0, 0, 1, 3, 32);
  14.     public static SpriteSheet player_up = new SpriteSheet(player, 1, 0, 1, 3, 32);
  15.     public static SpriteSheet player_left = new SpriteSheet(player, 2, 0, 1, 3, 32);
  16.     public static SpriteSheet player_right = new SpriteSheet(player, 3, 0, 1, 3, 32);
  17.  
  18.     public static SpriteSheet king_cherno = new SpriteSheet("/mobs/king_cherno.png", 128, 96);
  19.     public static SpriteSheet cherno_down = new SpriteSheet(king_cherno, 0, 0, 1, 3, 32);
  20.     public static SpriteSheet cherno_up = new SpriteSheet(king_cherno, 1, 0, 1, 3, 32);
  21.     public static SpriteSheet cherno_left = new SpriteSheet(king_cherno, 2, 0, 1, 3, 32);
  22.     public static SpriteSheet cherno_right = new SpriteSheet(king_cherno, 3, 0, 1, 3, 32);
  23.  
  24.     public static SpriteSheet citizen = new SpriteSheet("/mobs/citizen_sheet.png", 128, 96);
  25.     public static SpriteSheet citizen_down = new SpriteSheet(citizen, 0, 0, 1, 3, 32);
  26.     public static SpriteSheet citizen_up = new SpriteSheet(citizen, 1, 0, 1, 3, 32);
  27.     public static SpriteSheet citizen_left = new SpriteSheet(citizen, 2, 0, 1, 3, 32);
  28.     public static SpriteSheet citizen_right = new SpriteSheet(citizen, 3, 0, 1, 3, 32);
  29.  
  30.     private Sprite[] sprites;
  31.  
  32.     public SpriteSheet(SpriteSheet sheet, int x, int y, int width, int height, int spriteSize) {
  33.         int xx = x * spriteSize;
  34.         int yy = y * spriteSize;
  35.         int w = width * spriteSize;
  36.         int h = height * spriteSize;
  37.         if (width == height) SIZE = width;
  38.         else SIZE = -1;
  39.         SPRITE_WIDTH = w;
  40.         SPRITE_HEIGHT = h;
  41.         pixels = new int[w * h];
  42.         for (int y0 = 0; y0 < h; y0++) {
  43.             int yp = yy + y0;
  44.             for (int x0 = 0; x0 < w; x0++) {
  45.                 int xp = xx + x0;
  46.                 pixels[x0 + y0 * w] = sheet.pixels[xp + yp * sheet.SPRITE_WIDTH];
  47.             }
  48.         }
  49.         int frame = 0;
  50.         sprites = new Sprite[width * height];
  51.         for (int ya = 0; ya < height; ya++) {
  52.             for (int xa = 0; xa < width; xa++) {
  53.                 int[] spritePixels = new int[spriteSize * spriteSize];
  54.                 for (int y0 = 0; y0 < spriteSize; y0++) {
  55.                     for (int x0 = 0; x0 < spriteSize; x0++) {
  56.                         spritePixels[x0 + y0 * spriteSize] = pixels[(x0 + xa * spriteSize) + (y0 + ya * spriteSize) * SPRITE_WIDTH];
  57.                     }
  58.                 }
  59.                 Sprite sprite = new Sprite(spritePixels, spriteSize, spriteSize);
  60.                 sprites[frame++] = sprite;
  61.             }
  62.         }
  63.     }
  64.  
  65.     public SpriteSheet(String path, int size) {
  66.         this.path = path;
  67.         SIZE = size;
  68.         SPRITE_WIDTH = size;
  69.         SPRITE_HEIGHT = size;
  70.         pixels = new int[SIZE * SIZE];
  71.         load();
  72.     }
  73.  
  74.     public SpriteSheet(String path, int width, int height) {
  75.         this.path = path;
  76.         SIZE = -1;
  77.         SPRITE_WIDTH = width;
  78.         SPRITE_HEIGHT = height;
  79.         pixels = new int[SPRITE_WIDTH * SPRITE_HEIGHT];
  80.         load();
  81.     }
  82.  
  83.     public Sprite[] getSprites() {
  84.         return sprites;
  85.     }
  86.  
  87.     public int getWidth() {
  88.         return width;
  89.     }
  90.  
  91.     public int getHeight() {
  92.         return height;
  93.     }
  94.  
  95.     public int[] getPixels() {
  96.         return pixels;
  97.     }
  98.  
  99.     private void load() {
  100.         try {
  101.             System.out.print("Attempting to load: " + path + "...");
  102.             BufferedImage image = ImageIO.read(SpriteSheet.class.getResource(path));
  103.             System.out.println(" succeeded!");
  104.             width = image.getWidth();
  105.             height = image.getHeight();
  106.             pixels = new int[width * height];
  107.             image.getRGB(0, 0, width, height, pixels, 0, width);
  108.         } catch (IOException e) {
  109.             e.printStackTrace();
  110.         } catch (Exception e) {
  111.             System.err.println(" failed!");
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement