Guest User

player.pde

a guest
Mar 11th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 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.  
  13.   PVector loc;
  14.   PVector vel;
  15.  
  16.   Player (float xpos_, float ypos_, float direction_) {
  17.     loc = new PVector(xpos_, ypos_);
  18.     vel = new PVector(0, 0);//.rotate(direction_);
  19.     direction = direction_;
  20.   }
  21.  
  22.   void playerMovement() {
  23.     float strafe = 0,mov = 0;
  24.     PVector hitLocFwd = new PVector(0, 0), hitLocSides = new PVector(0, 0), hitLoc = new PVector(0, 0);
  25.  
  26.     if (!view) { //2D a/d turns player
  27.       if (keys['a']) turn(turnSpeed);
  28.       if (keys['d']) turn(-turnSpeed);
  29.     } else {     //3D a/d strafes player
  30.       if (keys['a']) {
  31.         strafe = moveSpeed;
  32.         hitLocSides = hitLeft;
  33.       } else if (keys['d']) {
  34.         hitLocSides = hitRight;
  35.         strafe = -moveSpeed;
  36.       }
  37.       if (mouseMoved) turn(mouseMove*mouseTurnSpeed);
  38.     }
  39.  
  40.     if (keys['w']) {
  41.       hitLocFwd = hitFwd;
  42.       mov = 1;
  43.     } else if (keys['s']) {
  44.       hitLocFwd = hitRev;
  45.       mov = -1;
  46.     }
  47.  
  48.     //set hitLoc to most relevant intersection
  49.     float dF = hitLocFwd.dist(loc);
  50.     float dS = hitLocSides.dist(loc);
  51.     if (dF > dS) hitLoc = hitLocSides;
  52.     else hitLoc = hitLocFwd;
  53.  
  54.     // requested movement vector from controls
  55.     PVector requestedMove = new PVector(strafe, mov*moveSpeed).rotate(-direction);
  56.  
  57.     //Collision detection
  58.     PVector playerLoc = player1.loc.copy();//new PVector( player1.xpos, player1.ypos);
  59.     float dist = (playerLoc.copy().add(vel)).dist(hitLoc);
  60.     float distInv =  1-dist/minWallDist;
  61.     if (dist < minWallDist) {
  62.       requestedMove.mult(-distInv);
  63.       vel.setMag(0);
  64.     }
  65.    
  66.     // apply acceleration, velocity, deceleration
  67.     vel.add(requestedMove.mult(acceleration));
  68.     vel.limit(maxSpeed);
  69.     if (requestedMove.magSq() == 0) vel.mult(slowDown);
  70.     loc.add(vel);
  71.   }
  72.  
  73.   int xcell() {
  74.     return floor(loc.x);
  75.   }
  76.  
  77.   int ycell() {
  78.     return floor(loc.y);
  79.   }
  80.  
  81.   float dxpos() {
  82.     return loc.x-xcell();
  83.   }
  84.  
  85.   float dypos() {
  86.     return loc.y-ycell();
  87.   }
  88.  
  89.   void turn (float val_) {
  90.     direction = radianWrap(direction + val_);
  91.   }
  92.  
  93.   void scanAngle (float val_) {
  94.     vdirection = radianWrap(direction + val_);
  95.   }
  96.  
  97.   float radianWrap(float k) {
  98.     if (k > TWO_PI)  k = k -TWO_PI;
  99.     if (k < 0) k = TWO_PI + k;
  100.     return k;
  101.   }
  102.  
  103. }
Add Comment
Please, Sign In to add comment