Advertisement
Guest User

wolf.pde

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