Advertisement
Guest User

Untitled

a guest
Dec 5th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.14 KB | None | 0 0
  1. float map[][], light[][];
  2. PShape tile;
  3. int scale = 5;
  4.  
  5. void setup() {
  6.   size(800, 600, P2D);
  7.   noStroke();
  8.  
  9.   tile = createShape(RECT, 0, 0, scale, scale);
  10.  
  11.   map = new float[width / scale][height / scale];
  12.   light = new float[width / scale][height / scale];
  13.  
  14.   for (int y = 0; y < height / scale; y++) { // generate random map
  15.     for (int x = 0; x < width / scale; x++) {
  16.       map[x][y] = noise(x * 0.05, y * 0.05);
  17.       light[x][y] = 0;
  18.     }
  19.   }
  20. }
  21.  
  22. void draw() {
  23.   // background(0, 0, 50);
  24.   updateLight();
  25.   for (int y = 0; y < height / scale; y++) {
  26.     for (int x = 0; x < width / scale; x++) {
  27.       pushMatrix();
  28.       translate(x * scale, y * scale);
  29.       tile.setFill(color(map[x][y] * light[x][y] * 255));
  30.       shape(tile);
  31.       popMatrix();
  32.     }
  33.   }
  34.   fill(255, 0, 0);
  35.   text("FPS: " + round(frameRate), width / 2, 20);
  36. }
  37.  
  38. void updateLight() {
  39.   for (int y = 0; y < height / scale; y++) { // set light level of each tile depending on mouse distance
  40.     for (int x = 0; x < width / scale; x++) {
  41.       light[x][y] = map(sqrt(sq(mouseX - x * scale) + sq(mouseY - y * scale)) * 3, 0, 1000, 1, 0);
  42.     }
  43.   }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement