Advertisement
Guest User

Untitled

a guest
Dec 21st, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.54 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.awt.Color;
  3.  
  4. public class Screen {
  5.         public int[][] map;
  6.         public int mapWidth, mapHeight, width, height;
  7.         public ArrayList textures;
  8.        
  9.         public Screen(int[][] m, ArrayList tex, int w, int h) {
  10.             map = m;
  11.             textures = tex;
  12.             width = w;
  13.             height = h;
  14.         }
  15.        
  16.         public int[] update(Camera camera, int[] pixels) {
  17.             for(int n=0; n<pixels.length/2; n++) {
  18.                 if(pixels[n] != Color.DARK_GRAY.getRGB()) pixels[n] = Color.DARK_GRAY.getRGB();
  19.             }
  20.             for(int i=pixels.length/2; i<pixels.length; i++) {
  21.                 if(pixels[i] != Color.gray.getRGB()) pixels[i] = Color.gray.getRGB();
  22.             }
  23.            
  24.             for(int x=0; x<width; x=x+1) {
  25.                 double cameraX = 2 * x / (double)(width) -1;
  26.                 double rayDirX = camera.xDir + camera.xPlane * cameraX;
  27.                 double rayDirY = camera.yDir + camera.yPlane * cameraX;
  28.                 //Map position
  29.                 int mapX = (int)camera.xPos;
  30.                 int mapY = (int)camera.yPos;
  31.                 //length of ray from current position to next x or y-side
  32.                 double sideDistX;
  33.                 double sideDistY;
  34.                 //Length of ray from one side to next in map
  35.                 double deltaDistX = Math.sqrt(1 + (rayDirY*rayDirY) / (rayDirX*rayDirX));
  36.                 double deltaDistY = Math.sqrt(1 + (rayDirX*rayDirX) / (rayDirY*rayDirY));
  37.                 double perpWallDist;
  38.                 //Direction to go in x and y
  39.                 int stepX, stepY;
  40.                 boolean hit = false;//was a wall hit
  41.                 int side=0;//was the wall vertical or horizontal
  42.                
  43.               //Figure out the step direction and initial distance to a side
  44.                 if (rayDirX < 0)
  45.                 {
  46.                     stepX = -1;
  47.                     sideDistX = (camera.xPos - mapX) * deltaDistX;
  48.                 }
  49.                 else
  50.                 {
  51.                     stepX = 1;
  52.                     sideDistX = (mapX + 1.0 - camera.xPos) * deltaDistX;
  53.                 }
  54.                 if (rayDirY < 0)
  55.                 {
  56.                     stepY = -1;
  57.                     sideDistY = (camera.yPos - mapY) * deltaDistY;
  58.                 }
  59.                 else
  60.                 {
  61.                     stepY = 1;
  62.                     sideDistY = (mapY + 1.0 - camera.yPos) * deltaDistY;
  63.                 }
  64.                
  65.               //Loop to find where the ray hits a wall
  66.                 while(!hit) {
  67.                     //Jump to next square
  68.                     if (sideDistX < sideDistY)
  69.                     {
  70.                         sideDistX += deltaDistX;
  71.                         mapX += stepX;
  72.                         side = 0;
  73.                     }
  74.                     else
  75.                     {
  76.                         sideDistY += deltaDistY;
  77.                         mapY += stepY;
  78.                         side = 1;
  79.                     }
  80.                     //Check if ray has hit a wall
  81.                     if(map[mapX][mapY] > 0) hit = true;
  82.                 }
  83.                
  84.               //Calculate distance to the point of impact
  85.                 if(side==0)
  86.                     perpWallDist = Math.abs((mapX - camera.xPos + (1 - stepX) / 2) / rayDirX);
  87.                 else
  88.                     perpWallDist = Math.abs((mapY - camera.yPos + (1 - stepY) / 2) / rayDirY);    
  89.                 //Now calculate the height of the wall based on the distance from the camera
  90.                 int lineHeight;
  91.                 if(perpWallDist > 0) lineHeight = Math.abs((int)(height / perpWallDist));
  92.                 else lineHeight = height;
  93.                 //calculate lowest and highest pixel to fill in current stripe
  94.                 int drawStart = -lineHeight/2+ height/2;
  95.                 if(drawStart < 0)
  96.                     drawStart = 0;
  97.                 int drawEnd = lineHeight/2 + height/2;
  98.                 if(drawEnd >= height)
  99.                     drawEnd = height - 1;
  100.  
  101.                
  102.               //add a texture
  103.                 int texNum = map[mapX][mapY] - 1;
  104.                 double wallX;//Exact position of where wall was hit
  105.                 if(side==1) {//If its a y-axis wall
  106.                     wallX = (camera.xPos + ((mapY - camera.yPos + (1 - stepY) / 2) / rayDirY) * rayDirX);
  107.                 } else {//X-axis wall
  108.                     wallX = (camera.yPos + ((mapX - camera.xPos + (1 - stepX) / 2) / rayDirX) * rayDirY);
  109.                 }
  110.                 wallX-=Math.floor(wallX);
  111.                 //x coordinate on the texture
  112.                 int texX = (int)(wallX * (textures.get(texNum).SIZE));
  113.                 if(side == 0 && rayDirX > 0) texX = textures.get(texNum).SIZE - texX - 1;
  114.                 if(side == 1 && rayDirY < 0) texX = textures.get(texNum).SIZE - texX - 1;
  115.                
  116.               //calculate y coordinate on texture
  117.                 for(int y=drawStart; y<drawEnd; y++) {
  118.                     int texY = (((y*2 - height + lineHeight) << 6) / lineHeight) / 2;
  119.                     int color;
  120.                     if(side==0) color = textures.get(texNum).pixels[texX + (texY * textures.get(texNum).SIZE)];
  121.                     else color = (textures.get(texNum).pixels[texX + (texY * textures.get(texNum).SIZE)]>>1) & 8355711;//Make y sides darker
  122.                     pixels[x + y*(width)] = color;
  123.                 }
  124.  
  125.             }
  126.         }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement