Advertisement
Guest User

wolf.pde

a guest
Mar 12th, 2018
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.42 KB | None | 0 0
  1. import com.jogamp.newt.opengl.GLWindow;
  2.  
  3. int mapsizex = 20;
  4. int mapsizey = 20;
  5.  
  6. PVector intersect = new PVector(0, 0);
  7. ArrayList<PVector> intersects = new ArrayList<PVector>();
  8.  
  9. int[][] playfield = new int[mapsizex+5][mapsizey+5];
  10. float xhit1, xhit2, yhit1, yhit2, height1, height2, dist;
  11.  
  12. int fov = 90; // not used
  13.  
  14. boolean view = true;
  15. Player player1;
  16.  
  17. boolean [] keys = new boolean[256];
  18. boolean [] lastKeys = new boolean[256];
  19.  
  20. PVector hitFwd = new PVector(0, 0);
  21. PVector hitRev = new PVector(0, 0);
  22. PVector hitLeft = new PVector(0, 0);
  23. PVector hitRight = new PVector(0, 0);
  24.  
  25. int split = 0;//width/2; // must set after setup
  26.  
  27. void setup() {
  28.   size (800, 600, P3D);
  29.   split = width/2;
  30.   int biggerAxis = 25;// playfield larger than map? could be max(mapsizex, mapsizey);
  31.   for (int i = 0; i < biggerAxis; i++) {
  32.     intersects.add(new PVector(0, 0));
  33.   }
  34.  
  35.   for (int i = 1; i < mapsizex+1; i++) {
  36.     playfield[i][1] = 1;
  37.     playfield[1][i] = 1;
  38.     playfield[mapsizex][i] = 1;
  39.     playfield[i][mapsizey] = 1;
  40.     playfield[i][2] = 1;
  41.     playfield[2][i] = 1;
  42.     playfield[mapsizex-1][i] = 1;
  43.     playfield[i][mapsizey-1] = 1;
  44.   }
  45.   for (int i = 1; i < 5; i++) {
  46.     playfield[i][5] = 1;
  47.     playfield[7][5+i] = 1;
  48.     playfield[i][15] = 2;
  49.     playfield[12][5+i] = 4;
  50.     playfield[i][11] = 3;
  51.     playfield[2][9+i] = 1;
  52.   }
  53.   player1 = new Player(10, 10.5, 145.001/360);
  54.   player1.loc.add(new PVector( 0.1*sin(player1.direction), 0.1*cos(player1.direction)));
  55.   player1.scanAngle(0);
  56. }
  57.  
  58. void keyPressed() {
  59.   int rawKey = key;
  60.   if (rawKey < 256) {
  61.     if ((rawKey>64)&&(rawKey<91)) rawKey+=32;
  62.     keys[rawKey] = true;
  63.   }
  64. }
  65.  
  66. void keyReleased() {
  67.   int rawKey = key;
  68.   if (rawKey < 256) {
  69.     if ((rawKey>64)&&(rawKey<91)) rawKey+=32;
  70.     keys[rawKey] = false;
  71.   }
  72. }
  73.  
  74. float mouseMove = 0;
  75. boolean mouseMoved = false;
  76. void mouseMoved() {
  77.   GLWindow r = (GLWindow)surface.getNative();
  78.   mouseMoved = false;
  79.   if (view) {
  80.     r.confinePointer(true);
  81.     r.setPointerVisible(false);
  82.     PVector center = new PVector(width/2, height/2);
  83.     PVector mouse = new PVector(mouseX, mouseY);
  84.     if (center!= mouse) mouseMoved = true;
  85.     mouse.sub(center);
  86.     mouseMove = mouse.x;
  87.     r.warpPointer(width/2, height/2);
  88.   } else {
  89.     r.confinePointer(false);
  90.     r.setPointerVisible(true);
  91.   }
  92. }
  93.  
  94. void draw2DGrid() {
  95.   for (int j = 1; j < mapsizex+1; j++) {
  96.     for ( int i = 1; i < mapsizey+1; i++) {
  97.       fill(playfield[j][i]*63);
  98.       rect(j*30-30, i*30-30, 30, 30);
  99.     }
  100.   }
  101.   ellipse(player1.loc.x*30-30, player1.loc.y*30-30, 10, 10);
  102.   line (player1.loc.x*30-30, player1.loc.y*30-30, player1.loc.x*30-30+500*sin((player1.direction)), player1.loc.y*30-30+500*cos((player1.direction)));
  103. }
  104.  
  105. void draw2DGridOverlay() {
  106.   pushMatrix();
  107.   //noStroke();
  108.   stroke(30);
  109.   noFill();
  110.    translate(600,420,100);
  111.   scale(0.20,0.20);
  112.   fill(0);
  113.   rect(0,0,600,600);
  114.  
  115.   render2D();
  116.   popMatrix();
  117. }
  118.  
  119. void render3D() {
  120.   fill (64, 128, 255);
  121.   rect(0, 0, 800, 300);
  122.   for (float i = split; i > -split; i--) {
  123.     float strip = i/((float)width);// (fov/360.0 * TWO_PI);
  124.     player1.scanAngle(strip);  
  125.     calculateIntersects();
  126.     dist = cos((strip))*sqrt((player1.loc.x-xhit1)*(player1.loc.x-xhit1)+(player1.loc.y-yhit1)*(player1.loc.y-yhit1));
  127.     if (dist < 2) {
  128.       stroke(255);
  129.       line(400-i, 0, 400-i, 600.0);
  130.     } else {
  131.       stroke(int(255/(dist/2)));
  132.       line(400-i, 300-(600.0/dist)*height1, 400-i, 300+600.0/dist);
  133.     }
  134.     stroke(int(255/(dist/2)));
  135.   }
  136.   draw2DGridOverlay();
  137. }
  138.  
  139. PVector colAtAng(float ang) {
  140.   player1.scanAngle(ang);  
  141.   calculateIntersects();
  142.   return new PVector(xhit1, yhit1);
  143. }
  144.  
  145. void getCollisionDistances() {
  146.   hitFwd = colAtAng(0);
  147.   hitRev = colAtAng(PI);
  148.   hitLeft = colAtAng(HALF_PI);
  149.   hitRight = colAtAng(-HALF_PI);
  150. }
  151.  
  152. void drawCollisions() {
  153.   fill(255,0,0);
  154.   ellipse(hitFwd.x*30-30, hitFwd.y*30-30, 16, 16);
  155.   ellipse(hitRev.x*30-30, hitRev.y*30-30, 6, 6);
  156.   ellipse(hitLeft.x*30-30, hitLeft.y*30-30, 6, 6);
  157.   ellipse(hitRight.x*30-30, hitRight.y*30-30, 6, 6);
  158. }
  159.  
  160. void render2D() {
  161.   draw2DGrid();
  162.   drawCollisions();
  163.   for (float i = split; i > -split; i--) {
  164.     float strip = i/((float)width);// (fov/360.0 * TWO_PI);
  165.     player1.scanAngle(strip);  
  166.     calculateIntersects();
  167.     stroke(0, 255, 0);
  168.     ellipse(xhit1*30-30, yhit1*30-30, 3, 3);
  169.     if (i==0) {
  170.       ellipse(xhit1*30-30, yhit1*30-30, 6, 6);
  171.     }
  172.   }
  173. }
  174.  
  175. void draw() {
  176.   mouseMoved(); // wasn't being called if mouseButtons were pressed
  177.   background(0);
  178.   stroke(255);
  179.   ellipseMode(CENTER);
  180.   if (keys['v'] && !lastKeys['v']) view = !view;
  181.  
  182.   player1.playerMovement();
  183.  
  184.   getCollisionDistances();
  185.  
  186.   if (!view) render2D();
  187.   else render3D();
  188.  
  189.   lastKeys = keys.clone();
  190.   fill(255);
  191.   text(frameRate, 0, 10);
  192. }
  193.  
  194. void calculateIntersects() {
  195.   if (((player1.vdirection >= 0) && (player1.vdirection < HALF_PI)) || ((player1.vdirection >= (HALF_PI + PI)) && (player1.vdirection < TWO_PI))) {
  196.     intersect.x = player1.loc.x+(1-player1.dypos())*tan((player1.vdirection));
  197.   } else {
  198.     intersect.x = player1.loc.x-(player1.dypos())*tan((player1.vdirection));
  199.   }
  200.   if ((player1.vdirection >= 0) && (player1.vdirection < PI)) {
  201.     intersect.y = player1.loc.y+(1-player1.dxpos())/tan((player1.vdirection));
  202.   } else {
  203.     intersect.y = player1.loc.y-(player1.dxpos())/tan((player1.vdirection));
  204.   }
  205.  
  206.  
  207.   for (int i = 0; i <= mapsizex; i++) {
  208.     if ((player1.vdirection > HALF_PI) && (player1.vdirection < (HALF_PI + PI))) {
  209.       intersects.get(i).x = player1.loc.x+(intersect.x-player1.loc.x)/player1.dypos()*(player1.dypos()+i);
  210.       if (intersects.get(i).x<0) {
  211.         yhit1 = -100;
  212.         break;
  213.       }
  214.       if (intersects.get(i).x>mapsizex) {
  215.  
  216.         yhit1 = 100;
  217.         break;
  218.       }
  219.       if (playfield[int(intersects.get(i).x)][int(player1.loc.y-player1.dypos()-i-1)]>0) {
  220.         xhit1 = intersects.get(i).x;
  221.         yhit1 = player1.loc.y-player1.dypos()-i;
  222.         height1 = playfield[int(intersects.get(i).x)][int(player1.loc.y-player1.dypos()-i-1)];
  223.         break;
  224.       }
  225.     } else {
  226.       intersects.get(i).x=player1.loc.x+(intersect.x-player1.loc.x)/(1-player1.dypos())*(1-player1.dypos()+i);
  227.  
  228.       if (intersects.get(i).x>mapsizex) {
  229.         yhit1 = 100;
  230.         break;
  231.       }
  232.       if (intersects.get(i).x<0) {
  233.         yhit1 = -100;
  234.         break;
  235.       }
  236.       if (playfield[int(intersects.get(i).x)][int(player1.loc.y-player1.dypos()+i+1)]>0) {
  237.         xhit1 = intersects.get(i).x;
  238.         yhit1 = player1.loc.y-player1.dypos()+i+1;
  239.         height1 = playfield[int(intersects.get(i).x)][int(player1.loc.y-player1.dypos()+i+1)];
  240.         break;
  241.       }
  242.     }
  243.   }
  244.  
  245.   for (int i = 0; i <= mapsizey; i++) {
  246.     if ((player1.vdirection > 0) && (player1.vdirection < PI)) {
  247.       intersects.get(i).y = player1.loc.y+(intersect.y-player1.loc.y)/(1-player1.dxpos())*(1-player1.dxpos()+i);
  248.  
  249.       if (intersects.get(i).y<0) {
  250.         xhit2 = -100;
  251.         break;
  252.       }
  253.       if (intersects.get(i).y>mapsizey) {
  254.         xhit2 = 100;
  255.         break;
  256.       }
  257.  
  258.       int k = int(player1.loc.x-player1.dxpos()+i+1);
  259.       if (k<=mapsizex) {
  260.         if (playfield[int(player1.loc.x-player1.dxpos()+i+1)][int(intersects.get(i).y)]>0) {
  261.           xhit2 = player1.loc.x-player1.dxpos()+i+1;
  262.           yhit2 = intersects.get(i).y;
  263.           height2 = playfield[int(player1.loc.x-player1.dxpos()+i+1)][int(intersects.get(i).y)];
  264.           break;
  265.         }
  266.       }
  267.     } else {
  268.       intersects.get(i).y = player1.loc.y+(intersect.y-player1.loc.y)/player1.dxpos()*(player1.dxpos()+i);
  269.       if (intersects.get(i).y<0) {
  270.         xhit2 = -100;
  271.         break;
  272.       }
  273.       if (intersects.get(i).y>mapsizey) {
  274.         xhit2 = 100;
  275.         break;
  276.       }
  277.  
  278.       int k = int(player1.loc.x-player1.dxpos()-i-1);
  279.       if (k>=0) {
  280.         if (playfield[int(player1.loc.x-player1.dxpos()-i-1)][int(intersects.get(i).y)]>0) {
  281.           xhit2 = player1.loc.x-player1.dxpos()-i;
  282.           yhit2 = intersects.get(i).y;
  283.           height2 = playfield[int(player1.loc.x-player1.dxpos()-i-1)][int(intersects.get(i).y)];
  284.           break;
  285.         }
  286.       }
  287.     }
  288.   }
  289.  
  290.   if (((player1.loc.x-xhit2)*(player1.loc.x-xhit2)+(player1.loc.y-yhit2)*(player1.loc.y-yhit2))<(player1.loc.x-xhit1)*(player1.loc.x-xhit1)+(player1.loc.y-yhit1)*(player1.loc.y-yhit1)) {
  291.     xhit1 = xhit2;
  292.     yhit1 = yhit2;
  293.     height1 = height2;
  294.   }
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement