SHARE
TWEET

Untitled

a guest Jul 21st, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let height = 20;
  2. let width = 20;
  3. let squareSize = 20;
  4. let canvasWidth = 400;
  5. let canvasHeight = 400;
  6. let tileArr = [];
  7. let imgArr;
  8. let player;
  9. let distProp;
  10.  
  11. function setup() {
  12.   createCanvas(canvasWidth,canvasHeight);
  13.   pixelDensity(1)
  14.   distProp = Math.sqrt(
  15.     ((.5 * canvasHeight) * (.5 * canvasHeight))+
  16.     ((.5 * canvasWidth) * (.5 * canvasWidth)));
  17.   imgArr = [
  18.     loadImage('../images/160cobble.png'),
  19.     loadImage('../images/160mario.png'),
  20.     loadImage('../images/160black.png')
  21.     ];
  22.  
  23.   for(let i = 0; i < width; i++){
  24.     tileArr[i] = [];
  25.     for(let j = 0; j < height; j++){
  26.       tileArr[i][j] = new Tiles(i * squareSize, j * squareSize, imgArr[0], squareSize);
  27.     }
  28.   }
  29.   tileArr[5][6].show(imgArr[2]);
  30.   tileArr[8][9].show(imgArr[2]);
  31.   tileArr[3][4].show(imgArr[2]);
  32.  
  33.   player = new LightSource(200,200,255)
  34. }
  35.  
  36. function draw() {
  37.   background(0)
  38.   for(let i = 0; i < width; i++){
  39.     for(let j = 0; j < height; j++){
  40.       tileArr[i][j].draw();
  41.     }
  42.   }
  43.  
  44.   drawLines(tileArr[5][6],player)
  45.   drawLines(tileArr[8][9],player)
  46.   drawLines(tileArr[3][4],player)
  47.  
  48.  
  49.   player.draw()
  50.  
  51.  
  52.   loadPixels();
  53.  
  54.  
  55.   for(let i = 0; i < canvasWidth; i++){
  56.     for(let j = 0; j < canvasHeight; j++){
  57.       dimColor(i,j,distForm(player.x,i,player.y,j),pixels)
  58.     }
  59.   }
  60.  
  61.  
  62.   updatePixels();
  63.  
  64.  
  65. }
  66.  
  67. class Tiles{
  68.   constructor(x,y,img,size){
  69.     this.x = x;
  70.     this.y = y;
  71.     this.img = img;
  72.     this.block = false;
  73.     this.squareSize = size;
  74.   }
  75.   show(img){
  76.     this.block = true;
  77.     this.img = img;
  78.   }
  79.   erase(){
  80.     this.block = false;
  81.     this.img = imgArr[1];
  82.   }
  83.   draw(){
  84.     image(this.img,this.x,this.y,squareSize,squareSize);
  85.   }
  86. }
  87.  
  88. class LightSource{
  89.   constructor(x,y,brightness){
  90.     this.x = x;
  91.     this.y = y;
  92.     this.brightness = brightness;
  93.   }
  94.  
  95.   draw(){
  96.     fill('#FFFF00')
  97.     ellipse(this.x,this.y,5)
  98.   }
  99. }
  100.  
  101.  
  102.  
  103. function getPixelColor(x,y,p){
  104.   let index = 4 * y * canvasWidth + (4 * x);
  105.   return [p[index], p[index+1], p[index+2]]
  106. }
  107.  
  108. function setPixelColor(x,y,col,p){
  109.   let index = 4 * y * canvasWidth + (4 * x);
  110.   p[index] = col[0]
  111.   p[index+1] = col[1]
  112.   p[index+2] = col[2]
  113. }
  114.  
  115. function dimColor(x,y,dist,p){
  116.   index = 4 * y * canvasWidth + (4 * x);
  117.   setPixelColor(x,y,[p[index]-((dist/distProp)*255),p[index+1]-((dist/distProp)*255),p[index+2]-((dist/distProp)*255)],p)
  118. }
  119.  
  120. function keyPressed() {
  121.   switch(keyCode){
  122.     case LEFT_ARROW:
  123.         player.x -= 10
  124.       break;
  125.     case RIGHT_ARROW:
  126.         player.x += 10
  127.       break;
  128.     case UP_ARROW:
  129.         player.y -= 10
  130.       break;
  131.     case DOWN_ARROW:
  132.         player.y+= 10
  133.       break;
  134.   }
  135. }
  136.  
  137. function distForm(x1,x2,y1,y2){
  138.   return int(Math.sqrt(((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1))));
  139. }
  140.  
  141. function drawLines(square,player){
  142.   let [p1, p2]= getSquareCorners(square,player)
  143.   let x1 = Math.sqrt(2)+(.5*canvasWidth)*Math.cos(Math.atan2(player.y-p1[1],player.x-p1[0]))
  144.   let y1 = Math.sqrt(2)+(.5*canvasWidth)*Math.sin(Math.atan2(player.y-p1[1],player.x-p1[0]))
  145.   let x2 = Math.sqrt(2)+(.5*canvasWidth)*Math.cos(Math.atan2(player.y-p2[1],player.x-p2[0]))
  146.   let y2 = Math.sqrt(2)+(.5*canvasWidth)*Math.sin(Math.atan2(player.y-p2[1],player.x-p2[0]))
  147.  
  148.   // line(player.x,player.y,x1,y1);
  149.   // line(player.x,player.y,x2,y2);
  150.   fill(120)
  151.   beginShape();
  152.   vertex(x1, y1);
  153.   vertex(x2, y2);
  154.   vertex(p2[0], p2[1]);
  155.   vertex(p1[0], p1[1]);
  156.  
  157.   endShape(CLOSE);
  158. }
  159.  
  160. function getSlope(x1,x2,y1,y2){
  161.     if(x1 == x2) return null;
  162.     return (y2-y1)/(x2-x1);
  163. }
  164.  
  165. function getSquareCorners(square, playerPos) {
  166.   let topLeft = [square.x, square.y]
  167.   let topRight = [square.x + square.squareSize, square.y]
  168.   let bottomLeft = [square.x, square.y + square.squareSize]
  169.   let bottomRight = [square.x + square.squareSize, square.y + square.squareSize]
  170.  
  171.   let squareCenter = [square.x + (square.squareSize / 2), square.y + (square.squareSize / 2)]
  172.  
  173.   let direction
  174.   if (squareCenter[0] < playerPos.x) direciton = "left"
  175.   else direction = "right"
  176.  
  177.   // top right quadrant or bottom left quadrant
  178.   if ((squareCenter[0] > playerPos.x && squareCenter[1] > playerPos.y) ||
  179.     (squareCenter[0] < playerPos.x && squareCenter[1] < playerPos.y)) {
  180.     return [topLeft, bottomRight]
  181.   }
  182.  
  183.   // bottom right quadrant or top left quadrant
  184.   if ((squareCenter[0] > playerPos.x && squareCenter[1] < playerPos.y) ||
  185.     (squareCenter[0] < playerPos.x && squareCenter[1] > playerPos.y)) {
  186.     return [topRight, bottomLeft]
  187.   }
  188.  
  189.   // if straight up
  190.   if (squareCenter[0] == playerPos.x && squareCenter[1] > playerPos.y){
  191.     return [bottomLeft, bottomRight]
  192.   }
  193.  
  194.   // if straight down
  195.   if (squareCenter[0] == playerPos.x && squareCenter[1] < playerPos.y){
  196.     return [topRight, topLeft]
  197.   }
  198.  
  199.   // if straight right
  200.   if (square.x < playerPos.x && square.y == playerPos.y){
  201.     return [topLeft, bottomLeft]
  202.   }
  203.  
  204.   // if straight left
  205.   if (square.x > playerPos.x && square.y == playerPos.y){
  206.     return [topRight, bottomRight]
  207.   }
  208.  
  209.   console.log("NO THING FOUND")
  210.   return
  211. }
  212. /*
  213. function drawShadow(square,player){
  214.   let [p1,p2,p3,p4] = getCanvasTouchPoints(square,player);
  215.   fill(0);
  216.   beginShape();
  217.   if(p3[0] != p4[0] && p3[1] != p4[1]){
  218.     if(p3[0]==0 && p4[1]==0){
  219.       vertex(0,canvasHeight);
  220.     }else if(p4[0]==0 && p3[1]==0){
  221.       vertex(0,canvasHeight);
  222.     }else if(p3[0]==0 && p4[1]==canvasHeight){
  223.       vertex();
  224.     }else if(){
  225.  
  226.     }
  227.  
  228.   }
  229.  
  230.   endShape(CLOSED)
  231. }
  232. */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top