Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Sprite {
- public final int SIZE;
- private int x, y;
- private int width, height;
- public int[] pixels;
- protected static SpriteSheet sheet;
- public static Sprite grass = new Sprite(16, 0, 0, SpriteSheet.tiles);
- public static Sprite flower = new Sprite(16, 1, 0, SpriteSheet.tiles);
- public static Sprite rock = new Sprite(16, 2, 0, SpriteSheet.tiles);
- public static Sprite tree = new Sprite(16, 3, 0, SpriteSheet.tiles);
- public static Sprite wall = new Sprite(16, 0, 1, SpriteSheet.tiles);
- public static Sprite brick = new Sprite(16, 1, 1, SpriteSheet.tiles);
- public static Sprite wood_floor = new Sprite(16, 2, 1, SpriteSheet.tiles);
- public static Sprite water = new Sprite(16, 0, 2, SpriteSheet.tiles);
- public static Sprite deep_water = new Sprite(16, 1, 2, SpriteSheet.tiles);
- public static Sprite voidSprite = new Sprite(16, 0x0080FF);
- //Projectile Sprites here:
- public static Sprite projectile_rock = new Sprite(16, 0, 0, SpriteSheet.projectiles);
- //Particle:
- public static Sprite particle_normal = new Sprite(3, 0xAAAAAA);
- protected Sprite(SpriteSheet sheet, int width, int height) {
- SIZE = (width == height) ? width : -1;
- this.width = width;
- this.height = height;
- this.sheet = sheet;
- }
- public Sprite(int size, int x, int y, SpriteSheet sheet) {
- SIZE = size;
- this.width = size;
- this.height = size;
- pixels = new int[SIZE * SIZE];
- this.x = x * size;
- this.y = y * size;
- this.sheet = sheet;
- load();
- }
- public Sprite(int width, int height, int color) {
- SIZE = -1;
- this.width = width;
- this.height = height;
- pixels = new int[width * height];
- setColor(color);
- }
- public Sprite(int size, int color) {
- SIZE = size;
- this.width = size;
- this.height = size;
- pixels = new int[SIZE * SIZE];
- setColor(color);
- }
- public Sprite(int[] pixels, int width, int height) {
- SIZE = (width == height) ? width : -1;
- this.width = width;
- this.height = height;
- this.pixels = new int[pixels.length];
- for (int i = 0; i < pixels.length; i++) {
- this.pixels[i] = pixels[i];
- }
- }
- public static Sprite[] split(SpriteSheet sheet) {
- int amount = (sheet.getWidth() * sheet.getHeight()) / (sheet.SPRITE_WIDTH * sheet.SPRITE_HEIGHT);
- Sprite[] sprites = new Sprite[amount];
- int current = 0;
- int[] pixels = new int[sheet.SPRITE_WIDTH * sheet.SPRITE_HEIGHT];
- for (int yp = 0; yp < sheet.getHeight() / sheet.SPRITE_HEIGHT; yp++) {
- for (int xp = 0; xp < sheet.getWidth() / sheet.SPRITE_WIDTH; xp++) {
- for (int y = 0; y < sheet.SPRITE_HEIGHT; y++) {
- for (int x = 0; x < sheet.SPRITE_WIDTH; x++) {
- int xo = x + xp * sheet.SPRITE_WIDTH;
- int yo = y + yp * sheet.SPRITE_HEIGHT;
- pixels[x + y * sheet.SPRITE_WIDTH] = sheet.getPixels()[xo + yo * sheet.getWidth()];
- }
- }
- sprites[current++] = new Sprite(pixels, sheet.SPRITE_WIDTH, sheet.SPRITE_HEIGHT);
- }
- }
- return sprites;
- }
- private void setColor(int colour) {
- for (int i = 0; i < width * height; i++) {
- pixels[i] = colour;
- }
- }
- public int getWidth() {
- return width;
- }
- public int getHeight() {
- return height;
- }
- private void load() {
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- pixels[x + y * width] = sheet.pixels[(x + this.x) + (y + this.y) * sheet.SPRITE_WIDTH];
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement