Advertisement
Guest User

Screen

a guest
Jun 13th, 2013
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | None | 0 0
  1. package com.thecherno.rain.graphics;
  2.  
  3. import java.util.Random;
  4.  
  5. import com.thecherno.rain.entity.mob.Player;
  6. import com.thecherno.rain.level.tile.Tile;
  7.  
  8. public class Screen {
  9.    
  10.     public int width,height;
  11.     public int[] pixels;
  12.     public final int MAP_SIZE = 64;
  13.     public final int MAP_SIZE_MASK = MAP_SIZE - 1;
  14.     public int[] tiles = new int[MAP_SIZE * MAP_SIZE];
  15.    
  16.     public int xOffset, yOffset;
  17.    
  18.     private Random random = new Random();
  19.    
  20.     public Screen(int width, int height) {
  21.             this.width = width;
  22.             this.height = height;
  23.             pixels = new int[width * height];
  24.            
  25.             for (int i = 0; i < MAP_SIZE * MAP_SIZE; i++) {
  26.                     tiles[i] = random.nextInt(0xffffff);
  27.             }
  28.     }
  29.    
  30.     public void clear() {
  31.             for (int i = 0; i < pixels.length; i++) {
  32.                     pixels[i] = 0;
  33.             }
  34.     }
  35.  
  36.     public void renderTile(int xp, int yp, Tile tile) {
  37.         xp -= xOffset;
  38.         yp -= yOffset;
  39.         for (int y = 0; y < tile.sprite.SIZE; y++) {
  40.             int ya = y + yp;
  41.             for (int x = 0; x < tile.sprite.SIZE; x++) {
  42.                 int xa = x + xp;
  43.                 if (xa < -tile.sprite.SIZE || xa >= width || ya < 0 || ya >= height) break;
  44.                 if (xa < 0) xa = 0;
  45.                 pixels[xa+ya*width] = tile.sprite.pixels[x+y*tile.sprite.SIZE];
  46.                
  47.             }
  48.         }
  49.     }
  50.    
  51.     public void renderPlayer(int xp, int yp, Sprite sprite) {
  52.         xp -= xOffset;
  53.         yp -= yOffset;
  54.         for (int y = 0; y < 16; y++) {
  55.             int ya = y + yp;
  56.             for (int x = 0; x < 16; x++) {
  57.                 int xa = x + xp;
  58.                 if (xa < -16 || xa >= width || ya < 0 || ya >= height) break;
  59.                 if (xa < 0) xa = 0;
  60.                 pixels[xa+ya*width] = sprite.pixels[x+y*16];
  61.                
  62.             }
  63.         }
  64.     }
  65.     public void setOffset(int xOffset, int yOffset) {
  66.         this.xOffset = xOffset;
  67.         this.yOffset = yOffset;
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement