Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.jogamp.newt.opengl.GLWindow;
- int mapsizex = 20;
- int mapsizey = 20;
- PVector intersect = new PVector(0, 0);
- ArrayList<PVector> intersects = new ArrayList<PVector>();
- int[][] playfield = new int[mapsizex+5][mapsizey+5];
- float xhit1, xhit2, yhit1, yhit2, height1, height2, dist;
- int fov = 90; // not used
- boolean view = true;
- Player player1;
- boolean [] keys = new boolean[256];
- boolean [] lastKeys = new boolean[256];
- PVector hitFwd = new PVector(0, 0);
- PVector hitRev = new PVector(0, 0);
- PVector hitLeft = new PVector(0, 0);
- PVector hitRight = new PVector(0, 0);
- int split = 0;//width/2; // must set after setup
- void setup() {
- size (800, 600, P3D);
- split = width/2;
- int biggerAxis = 25;// playfield larger than map? could be max(mapsizex, mapsizey);
- for (int i = 0; i < biggerAxis; i++) {
- intersects.add(new PVector(0, 0));
- }
- for (int i = 1; i < mapsizex+1; i++) {
- playfield[i][1] = 1;
- playfield[1][i] = 1;
- playfield[mapsizex][i] = 1;
- playfield[i][mapsizey] = 1;
- playfield[i][2] = 1;
- playfield[2][i] = 1;
- playfield[mapsizex-1][i] = 1;
- playfield[i][mapsizey-1] = 1;
- }
- for (int i = 1; i < 5; i++) {
- playfield[i][5] = 1;
- playfield[7][5+i] = 1;
- playfield[i][15] = 2;
- playfield[12][5+i] = 4;
- playfield[i][11] = 3;
- playfield[2][9+i] = 1;
- }
- player1 = new Player(10, 10.5, 145.001/360);
- player1.loc.add(new PVector( 0.1*sin(player1.direction), 0.1*cos(player1.direction)));
- player1.scanAngle(0);
- }
- void keyPressed() {
- int rawKey = key;
- if (rawKey < 256) {
- if ((rawKey>64)&&(rawKey<91)) rawKey+=32;
- keys[rawKey] = true;
- }
- }
- void keyReleased() {
- int rawKey = key;
- if (rawKey < 256) {
- if ((rawKey>64)&&(rawKey<91)) rawKey+=32;
- keys[rawKey] = false;
- }
- }
- float mouseMove = 0;
- boolean mouseMoved = false;
- void mouseMoved() {
- GLWindow r = (GLWindow)surface.getNative();
- mouseMoved = false;
- if (view) {
- r.confinePointer(true);
- r.setPointerVisible(false);
- PVector center = new PVector(width/2, height/2);
- PVector mouse = new PVector(mouseX, mouseY);
- if (center!= mouse) mouseMoved = true;
- mouse.sub(center);
- mouseMove = mouse.x;
- r.warpPointer(width/2, height/2);
- } else {
- r.confinePointer(false);
- r.setPointerVisible(true);
- }
- }
- void draw2DGrid() {
- for (int j = 1; j < mapsizex+1; j++) {
- for ( int i = 1; i < mapsizey+1; i++) {
- fill(playfield[j][i]*63);
- rect(j*30-30, i*30-30, 30, 30);
- }
- }
- ellipse(player1.loc.x*30-30, player1.loc.y*30-30, 10, 10);
- 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)));
- }
- void draw2DGridOverlay() {
- pushMatrix();
- //noStroke();
- stroke(30);
- noFill();
- translate(600,420,100);
- scale(0.20,0.20);
- fill(0);
- rect(0,0,600,600);
- render2D();
- popMatrix();
- }
- void render3D() {
- fill (64, 128, 255);
- rect(0, 0, 800, 300);
- for (float i = split; i > -split; i--) {
- float strip = i/((float)width);// (fov/360.0 * TWO_PI);
- player1.scanAngle(strip);
- calculateIntersects();
- dist = cos((strip))*sqrt((player1.loc.x-xhit1)*(player1.loc.x-xhit1)+(player1.loc.y-yhit1)*(player1.loc.y-yhit1));
- if (dist < 2) {
- stroke(255);
- line(400-i, 0, 400-i, 600.0);
- } else {
- stroke(int(255/(dist/2)));
- line(400-i, 300-(600.0/dist)*height1, 400-i, 300+600.0/dist);
- }
- stroke(int(255/(dist/2)));
- }
- draw2DGridOverlay();
- }
- PVector colAtAng(float ang) {
- player1.scanAngle(ang);
- calculateIntersects();
- return new PVector(xhit1, yhit1);
- }
- void getCollisionDistances() {
- hitFwd = colAtAng(0);
- hitRev = colAtAng(PI);
- hitLeft = colAtAng(HALF_PI);
- hitRight = colAtAng(-HALF_PI);
- }
- void drawCollisions() {
- fill(255,0,0);
- ellipse(hitFwd.x*30-30, hitFwd.y*30-30, 16, 16);
- ellipse(hitRev.x*30-30, hitRev.y*30-30, 6, 6);
- ellipse(hitLeft.x*30-30, hitLeft.y*30-30, 6, 6);
- ellipse(hitRight.x*30-30, hitRight.y*30-30, 6, 6);
- }
- void render2D() {
- draw2DGrid();
- drawCollisions();
- for (float i = split; i > -split; i--) {
- float strip = i/((float)width);// (fov/360.0 * TWO_PI);
- player1.scanAngle(strip);
- calculateIntersects();
- stroke(0, 255, 0);
- ellipse(xhit1*30-30, yhit1*30-30, 3, 3);
- if (i==0) {
- ellipse(xhit1*30-30, yhit1*30-30, 6, 6);
- }
- }
- }
- void draw() {
- mouseMoved(); // wasn't being called if mouseButtons were pressed
- background(0);
- stroke(255);
- ellipseMode(CENTER);
- if (keys['v'] && !lastKeys['v']) view = !view;
- player1.playerMovement();
- getCollisionDistances();
- if (!view) render2D();
- else render3D();
- lastKeys = keys.clone();
- fill(255);
- text(frameRate, 0, 10);
- }
- void calculateIntersects() {
- if (((player1.vdirection >= 0) && (player1.vdirection < HALF_PI)) || ((player1.vdirection >= (HALF_PI + PI)) && (player1.vdirection < TWO_PI))) {
- intersect.x = player1.loc.x+(1-player1.dypos())*tan((player1.vdirection));
- } else {
- intersect.x = player1.loc.x-(player1.dypos())*tan((player1.vdirection));
- }
- if ((player1.vdirection >= 0) && (player1.vdirection < PI)) {
- intersect.y = player1.loc.y+(1-player1.dxpos())/tan((player1.vdirection));
- } else {
- intersect.y = player1.loc.y-(player1.dxpos())/tan((player1.vdirection));
- }
- for (int i = 0; i <= mapsizex; i++) {
- if ((player1.vdirection > HALF_PI) && (player1.vdirection < (HALF_PI + PI))) {
- intersects.get(i).x = player1.loc.x+(intersect.x-player1.loc.x)/player1.dypos()*(player1.dypos()+i);
- if (intersects.get(i).x<0) {
- yhit1 = -100;
- break;
- }
- if (intersects.get(i).x>mapsizex) {
- yhit1 = 100;
- break;
- }
- if (playfield[int(intersects.get(i).x)][int(player1.loc.y-player1.dypos()-i-1)]>0) {
- xhit1 = intersects.get(i).x;
- yhit1 = player1.loc.y-player1.dypos()-i;
- height1 = playfield[int(intersects.get(i).x)][int(player1.loc.y-player1.dypos()-i-1)];
- break;
- }
- } else {
- intersects.get(i).x=player1.loc.x+(intersect.x-player1.loc.x)/(1-player1.dypos())*(1-player1.dypos()+i);
- if (intersects.get(i).x>mapsizex) {
- yhit1 = 100;
- break;
- }
- if (intersects.get(i).x<0) {
- yhit1 = -100;
- break;
- }
- if (playfield[int(intersects.get(i).x)][int(player1.loc.y-player1.dypos()+i+1)]>0) {
- xhit1 = intersects.get(i).x;
- yhit1 = player1.loc.y-player1.dypos()+i+1;
- height1 = playfield[int(intersects.get(i).x)][int(player1.loc.y-player1.dypos()+i+1)];
- break;
- }
- }
- }
- for (int i = 0; i <= mapsizey; i++) {
- if ((player1.vdirection > 0) && (player1.vdirection < PI)) {
- intersects.get(i).y = player1.loc.y+(intersect.y-player1.loc.y)/(1-player1.dxpos())*(1-player1.dxpos()+i);
- if (intersects.get(i).y<0) {
- xhit2 = -100;
- break;
- }
- if (intersects.get(i).y>mapsizey) {
- xhit2 = 100;
- break;
- }
- int k = int(player1.loc.x-player1.dxpos()+i+1);
- if (k<=mapsizex) {
- if (playfield[int(player1.loc.x-player1.dxpos()+i+1)][int(intersects.get(i).y)]>0) {
- xhit2 = player1.loc.x-player1.dxpos()+i+1;
- yhit2 = intersects.get(i).y;
- height2 = playfield[int(player1.loc.x-player1.dxpos()+i+1)][int(intersects.get(i).y)];
- break;
- }
- }
- } else {
- intersects.get(i).y = player1.loc.y+(intersect.y-player1.loc.y)/player1.dxpos()*(player1.dxpos()+i);
- if (intersects.get(i).y<0) {
- xhit2 = -100;
- break;
- }
- if (intersects.get(i).y>mapsizey) {
- xhit2 = 100;
- break;
- }
- int k = int(player1.loc.x-player1.dxpos()-i-1);
- if (k>=0) {
- if (playfield[int(player1.loc.x-player1.dxpos()-i-1)][int(intersects.get(i).y)]>0) {
- xhit2 = player1.loc.x-player1.dxpos()-i;
- yhit2 = intersects.get(i).y;
- height2 = playfield[int(player1.loc.x-player1.dxpos()-i-1)][int(intersects.get(i).y)];
- break;
- }
- }
- }
- }
- 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)) {
- xhit1 = xhit2;
- yhit1 = yhit2;
- height1 = height2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement