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 height1, height2, dist;
- int fov = 90; // not used
- boolean fpsLock = false;
- boolean view = true;
- Player player1;
- boolean [] keys = new boolean[256];
- boolean [] lastKeys = new boolean[256];
- int split = 0;//width/2; // must set after setup
- float mouseMove = 0;
- boolean mouseMoved = false;
- final float scaleSize = 30.0;
- GLWindow r;
- void setup() {
- size (800, 600, P3D);
- r = (GLWindow)surface.getNative();
- setCursorState();
- r.warpPointer(width/2, height/2);
- frameRate(1000);
- 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);
- stroke(255);
- ellipseMode(CENTER);
- }
- 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;
- }
- }
- void mouseMoved() {
- mouseCheck();
- if (view) r.warpPointer(width/2, height/2);
- }
- void mouseCheck() {
- mouseMoved = false;
- if (view) {
- 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;
- }
- }
- void drawMinimap() {
- pushMatrix();
- translate(600, 420, 100);
- scale(0.20, 0.20);
- render2D();
- popMatrix();
- }
- void render3D() {
- fill (64, 128, 255);
- rect(0, 0, 800, 300);
- noStroke();
- pushMatrix();
- translate(split, 0);
- scale(-1, 1);
- strokeWeight(1);
- beginShape(LINES);
- for (float i = split; i > -split; i-=1) {
- float strip = i/width;// (fov/360.0 * TWO_PI);
- PVector hit = calcIntersects(strip);
- hit = player1.loc.copy().sub(hit);
- dist = cos(strip)*hit.mag();
- if (dist < 2) {
- stroke(255);
- vertex(i, 1);
- vertex(i, 600-1);
- } else {
- stroke(int(255/(dist/2)));
- float dw = 600.0/dist;
- vertex(i, 300-dw*height1);
- vertex(i, 300+dw);
- }
- }
- endShape();
- popMatrix();
- drawMinimap();
- }
- void draw2DGrid() {
- strokeWeight(1/6.0);
- stroke(30);
- for (int j = 1; j < mapsizex+1; j++) {
- for ( int i = 1; i < mapsizey+1; i++) {
- fill(playfield[j][i]*63);
- rect(j, i, 1, 1);
- }
- }
- noStroke();
- ellipse(player1.loc.x, player1.loc.y, 0.5, 0.5);
- stroke(255);
- line (player1.loc.x, player1.loc.y, player1.loc.x+10*sin((player1.direction)), player1.loc.y+10*cos((player1.direction)));
- }
- void draw2DWallHighlight() {
- stroke(0, 255, 0);
- noFill();
- beginShape();
- strokeWeight(1/6.0);
- vertex(player1.loc.x, player1.loc.y);
- for (float i = split; i > -split; i-=12) {
- PVector hit = calcIntersects(i/width);
- if (i==0) vertex(hit.x, hit.y);//ellipse(hit.x*30-30, hit.y*30-30, 6, 6);
- else vertex(hit.x, hit.y);//ellipse(hit.x*30-30, hit.y*30-30, 3, 3);
- }
- vertex(player1.loc.x, player1.loc.y);
- endShape();
- }
- void render2D() {
- pushMatrix();
- translate(-scaleSize, -scaleSize);
- scale(scaleSize, scaleSize);
- draw2DGrid();
- draw2DWallHighlight();
- player1.drawCollisions();
- popMatrix();
- }
- void setCursorState() {
- r = (GLWindow)surface.getNative();
- if (view) {
- r.confinePointer(true);
- r.setPointerVisible(false);
- } else {
- r.confinePointer(false);
- r.setPointerVisible(true);
- }
- }
- long frameTime = System.nanoTime();
- void gameFrame() {
- if (keys['v'] && !lastKeys['v']) {
- view = !view;
- setCursorState();
- }
- if (keys['f'] && !lastKeys['f']) {
- fpsLock = !fpsLock;
- if (fpsLock) frameRate(60);
- else frameRate(1000);
- }
- mouseCheck(); // mouseMove wasn't being called if mouseButtons were pressed
- player1.playerMovement();
- lastKeys = keys.clone();
- }
- void draw() {
- if ((fpsLock)||(System.nanoTime()-frameTime>13333333)) {
- gameFrame();
- frameTime = System.nanoTime();
- }
- background(0);
- if (view) render3D();
- else render2D();
- fill(255);
- text("'v' for view, 'f' for 60fps, FPS: " + int(frameRate), 4, 12);
- }
- PVector calcIntersects(float ang) {
- player1.scanAngle(ang);
- PVector hit1 = new PVector(0, 0), hit2 = new PVector(0, 0);
- if (((player1.vdirection >= 0) && (player1.vdirection < HALF_PI))
- || ((player1.vdirection >= (HALF_PI + PI)) && (player1.vdirection < TWO_PI)))
- intersect.x = player1.loc.x+(1-player1.dy)*(player1.vdtan);
- else intersect.x = player1.loc.x-(player1.dy)*(player1.vdtan);
- if ((player1.vdirection >= 0) && (player1.vdirection < PI))
- intersect.y = player1.loc.y+(1-player1.dx)/(player1.vdtan);
- else intersect.y = player1.loc.y-(player1.dx)/(player1.vdtan);
- for (int i = 0; i <= mapsizex; i++) {
- PVector inti = intersects.get(i);
- if ((player1.vdirection > HALF_PI) && (player1.vdirection < (HALF_PI + PI))) {
- inti.x = player1.loc.x+(intersect.x-player1.loc.x)/player1.dy*(player1.dy+i);
- if (inti.x<0) {
- hit1.y = -100;
- break;
- }
- if (inti.x>mapsizex) {
- hit1.y = 100;
- break;
- }
- if (playfield[int(inti.x)][int(player1.loc.y-player1.dy-i-1)]>0) {
- hit1 = new PVector(inti.x, player1.loc.y-player1.dy-i);
- height1 = playfield[int(inti.x)][int(player1.loc.y-player1.dy-i-1)];
- break;
- }
- } else {
- inti.x=player1.loc.x+(intersect.x-player1.loc.x)/(1-player1.dy)*(1-player1.dy+i);
- if (inti.x>mapsizex) {
- hit1.y = 100;
- break;
- }
- if (inti.x<0) {
- hit1.y = -100;
- break;
- }
- if (playfield[int(inti.x)][int(player1.loc.y-player1.dy+i+1)]>0) {
- hit1 = new PVector(inti.x, player1.loc.y-player1.dy+i+1);
- height1 = playfield[int(inti.x)][int(player1.loc.y-player1.dy+i+1)];
- break;
- }
- }
- }
- for (int i = 0; i <= mapsizey; i++) {
- PVector inti = intersects.get(i);
- if ((player1.vdirection > 0) && (player1.vdirection < PI)) {
- inti.y = player1.loc.y+(intersect.y-player1.loc.y)/(1-player1.dx)*(1-player1.dx+i);
- if (inti.y<0) {
- hit2.x = -100;
- break;
- }
- if (inti.y>mapsizey) {
- hit2.x = 100;
- break;
- }
- if (int(player1.loc.x-player1.dx+i+1)<=mapsizex) {
- if (playfield[int(player1.loc.x-player1.dx+i+1)][int(inti.y)]>0) {
- hit2 = new PVector(player1.loc.x-player1.dx+i+1, inti.y);
- height2 = playfield[int(player1.loc.x-player1.dx+i+1)][int(inti.y)];
- break;
- }
- }
- } else {
- inti.y = player1.loc.y+(intersect.y-player1.loc.y)/player1.dx*(player1.dx+i);
- if (inti.y<0) {
- hit2.x = -100;
- break;
- }
- if (inti.y>mapsizey) {
- hit2.x = 100;
- break;
- }
- if (int(player1.loc.x-player1.dx-i-1)>=0) {
- if (playfield[int(player1.loc.x-player1.dx-i-1)][int(inti.y)]>0) {
- hit2 = new PVector(player1.loc.x-player1.dx-i, inti.y);
- height2 = playfield[int(player1.loc.x-player1.dx-i-1)][int(inti.y)];
- break;
- }
- }
- }
- }
- PVector dhit2 = player1.loc.copy().sub(hit2);
- PVector dhit1 = player1.loc.copy().sub(hit1);
- if (dhit2.dot(dhit2)<dhit1.dot(dhit1)) {
- hit1.x = hit2.x;
- hit1.y = hit2.y;
- height1 = height2;
- }
- return hit1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement