Advertisement
Guest User

Sprite class

a guest
Dec 4th, 2015
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.33 KB | None | 0 0
  1. public class Sprite {
  2.  
  3.     public final int SIZE;
  4.     private int x, y;
  5.     private int width, height;
  6.     public int[] pixels;
  7.     protected static SpriteSheet sheet;
  8.  
  9.     public static Sprite grass = new Sprite(16, 0, 0, SpriteSheet.tiles);
  10.     public static Sprite flower = new Sprite(16, 1, 0, SpriteSheet.tiles);
  11.     public static Sprite rock = new Sprite(16, 2, 0, SpriteSheet.tiles);
  12.     public static Sprite tree = new Sprite(16, 3, 0, SpriteSheet.tiles);
  13.     public static Sprite wall = new Sprite(16, 0, 1, SpriteSheet.tiles);
  14.     public static Sprite brick = new Sprite(16, 1, 1, SpriteSheet.tiles);
  15.     public static Sprite wood_floor = new Sprite(16, 2, 1, SpriteSheet.tiles);
  16.     public static Sprite water = new Sprite(16, 0, 2, SpriteSheet.tiles);
  17.     public static Sprite deep_water = new Sprite(16, 1, 2, SpriteSheet.tiles);
  18.     public static Sprite voidSprite = new Sprite(16, 0x0080FF);
  19.  
  20.     //Projectile Sprites here:
  21.     public static Sprite projectile_rock = new Sprite(16, 0, 0, SpriteSheet.projectiles);
  22.  
  23.     //Particle:
  24.     public static Sprite particle_normal = new Sprite(3, 0xAAAAAA);
  25.  
  26.     protected Sprite(SpriteSheet sheet, int width, int height) {
  27.         SIZE = (width == height) ? width : -1;
  28.         this.width = width;
  29.         this.height = height;
  30.         this.sheet = sheet;
  31.     }
  32.  
  33.     public Sprite(int size, int x, int y, SpriteSheet sheet) {
  34.         SIZE = size;
  35.         this.width = size;
  36.         this.height = size;
  37.         pixels = new int[SIZE * SIZE];
  38.         this.x = x * size;
  39.         this.y = y * size;
  40.         this.sheet = sheet;
  41.         load();
  42.     }
  43.  
  44.     public Sprite(int width, int height, int color) {
  45.         SIZE = -1;
  46.         this.width = width;
  47.         this.height = height;
  48.         pixels = new int[width * height];
  49.         setColor(color);
  50.     }
  51.  
  52.     public Sprite(int size, int color) {
  53.         SIZE = size;
  54.         this.width = size;
  55.         this.height = size;
  56.         pixels = new int[SIZE * SIZE];
  57.         setColor(color);
  58.     }
  59.  
  60.     public Sprite(int[] pixels, int width, int height) {
  61.         SIZE = (width == height) ? width : -1;
  62.         this.width = width;
  63.         this.height = height;
  64.         this.pixels = new int[pixels.length];
  65.         for (int i = 0; i < pixels.length; i++) {
  66.             this.pixels[i] = pixels[i];
  67.         }
  68.     }
  69.  
  70.     public static Sprite[] split(SpriteSheet sheet) {
  71.         int amount = (sheet.getWidth() * sheet.getHeight()) / (sheet.SPRITE_WIDTH * sheet.SPRITE_HEIGHT);
  72.         Sprite[] sprites = new Sprite[amount];
  73.         int current = 0;
  74.         int[] pixels = new int[sheet.SPRITE_WIDTH * sheet.SPRITE_HEIGHT];
  75.         for (int yp = 0; yp < sheet.getHeight() / sheet.SPRITE_HEIGHT; yp++) {
  76.             for (int xp = 0; xp < sheet.getWidth() / sheet.SPRITE_WIDTH; xp++) {
  77.  
  78.                 for (int y = 0; y < sheet.SPRITE_HEIGHT; y++) {
  79.                     for (int x = 0; x < sheet.SPRITE_WIDTH; x++) {
  80.                         int xo = x + xp * sheet.SPRITE_WIDTH;
  81.                         int yo = y + yp * sheet.SPRITE_HEIGHT;
  82.                         pixels[x + y * sheet.SPRITE_WIDTH] = sheet.getPixels()[xo + yo * sheet.getWidth()];
  83.                     }
  84.                 }
  85.  
  86.                 sprites[current++] = new Sprite(pixels, sheet.SPRITE_WIDTH, sheet.SPRITE_HEIGHT);
  87.             }
  88.         }
  89.         return sprites;
  90.     }
  91.  
  92.     private void setColor(int colour) {
  93.         for (int i = 0; i < width * height; i++) {
  94.             pixels[i] = colour;
  95.         }
  96.     }
  97.  
  98.     public int getWidth() {
  99.         return width;
  100.     }
  101.  
  102.     public int getHeight() {
  103.         return height;
  104.     }
  105.  
  106.     private void load() {
  107.         for (int y = 0; y < height; y++) {
  108.             for (int x = 0; x < width; x++) {
  109.                 pixels[x + y * width] = sheet.pixels[(x + this.x) + (y + this.y) * sheet.SPRITE_WIDTH];
  110.             }
  111.         }
  112.     }
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement