Advertisement
Guest User

player.pde

a guest
Mar 12th, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 KB | None | 0 0
  1. class Player {
  2.   float maxSpeed = 0.1;
  3.   float slowDown = 0.7;
  4.   float acceleration = 0.2;
  5.   float turnSpeed = 0.025;
  6.   float mouseTurnSpeed = -turnSpeed/8;
  7.   float moveSpeed = 0.1;
  8.   float minWallDist = 0.2;
  9.  
  10.   float direction;
  11.   float vdirection;
  12.   float vdtan;
  13.   PVector loc;
  14.   PVector vel;
  15.  
  16.   int cellx, celly;
  17.   float dx, dy;
  18.  
  19.   PVector hitFwd = new PVector(0, 0);
  20.   PVector hitRev = new PVector(0, 0);
  21.   PVector hitLeft = new PVector(0, 0);
  22.   PVector hitRight = new PVector(0, 0);
  23.  
  24.  
  25.   Player (float xpos_, float ypos_, float direction_) {
  26.     loc = new PVector(xpos_, ypos_);
  27.     vel = new PVector(0, 0);//.rotate(direction_);
  28.     direction = direction_;
  29.   }
  30.  
  31.   void playerMovement() {
  32.  
  33.     float strafe = 0, mov = 0;
  34.     PVector hitLocFwd = new PVector(0, 0), hitLocSides = new PVector(0, 0), hitLoc = new PVector(0, 0);
  35.  
  36.     if (!view) { //2D a/d turns player
  37.       if (keys['a']) turn(turnSpeed);
  38.       if (keys['d']) turn(-turnSpeed);
  39.     } else {     //3D a/d strafes player
  40.       if (keys['a']) {
  41.         strafe = moveSpeed;
  42.         hitLocSides = hitLeft;
  43.       } else if (keys['d']) {
  44.         hitLocSides = hitRight;
  45.         strafe = -moveSpeed;
  46.       }
  47.       if (mouseMoved) turn(mouseMove*mouseTurnSpeed);
  48.     }
  49.  
  50.     if (keys['w']) {
  51.       hitLocFwd = hitFwd;
  52.       mov = 1;
  53.     } else if (keys['s']) {
  54.       hitLocFwd = hitRev;
  55.       mov = -1;
  56.     }
  57.  
  58.     //set hitLoc to most relevant intersection
  59.     float dF = hitLocFwd.dist(loc);
  60.     float dS = hitLocSides.dist(loc);
  61.     if (dF > dS) hitLoc = hitLocSides;
  62.     else hitLoc = hitLocFwd;
  63.  
  64.     // requested movement vector from controls
  65.     PVector requestedMove = new PVector(strafe, mov*moveSpeed).rotate(-direction);
  66.  
  67.     //Collision detection
  68.  
  69.     PVector playerLoc = player1.loc.copy();//new PVector( player1.xpos, player1.ypos);
  70.     float dist = (playerLoc.copy().add(vel)).dist(hitLoc);
  71.     float distInv =  1-dist/minWallDist;
  72.     if (dist < minWallDist) {
  73.       requestedMove.mult(-distInv);
  74.       vel.setMag(0);
  75.     }
  76.  
  77.     // apply acceleration, velocity, deceleration
  78.     vel.add(requestedMove.mult(acceleration));
  79.     vel.limit(maxSpeed);
  80.     if (requestedMove.magSq() == 0) vel.mult(slowDown);
  81.     loc.add(vel);
  82.  
  83.     cellx = floor(loc.x);
  84.     celly = floor(loc.y);
  85.     dx = loc.x-cellx;
  86.     dy = loc.y-celly;
  87.    
  88.     getCollisionDistances();
  89.   }
  90.  
  91.   void turn (float val_) {
  92.     direction = radianWrap(direction + val_);
  93.   }
  94.  
  95.   void scanAngle (float val_) {
  96.     vdirection = radianWrap(direction + val_);
  97.     vdtan = tan(vdirection);
  98.   }
  99.  
  100.   float radianWrap(float k) {
  101.     if (k > TWO_PI)  k = k -TWO_PI;
  102.     if (k < 0) k = TWO_PI + k;
  103.     return k;
  104.   }
  105.  
  106.   void getCollisionDistances() {
  107.     hitFwd = calcIntersects(0);
  108.     hitRev = calcIntersects(PI);
  109.     hitLeft = calcIntersects(HALF_PI);
  110.     hitRight = calcIntersects(-HALF_PI);
  111.   }
  112.  
  113.   void drawCollisions() {
  114.     fill(255, 0, 0);
  115.     noStroke();
  116.     ellipse(hitFwd.x, hitFwd.y, 0.3, 0.3);
  117.     ellipse(hitRev.x, hitRev.y, 0.2, 0.2);
  118.     ellipse(hitLeft.x, hitLeft.y, 0.2, 0.2);
  119.     ellipse(hitRight.x, hitRight.y, 0.2, 0.2);
  120.   }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement