SHARE
TWEET

Basic DynamicEntity + Player class

ulfben Mar 14th, 2017 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class DynamicGameObject extends GameObject {
  2.     private static final String TAG = "DynamicGameObject";
  3.     private static final float MAX_DELTA = 0.48f; //maximum change in position over a single frame. keep < smallest object to avoid tunneling (1.0f = 1m)
  4.     protected static final float TERMINAL_VELOCITY = 16.0f;
  5.     protected static final float GRAVITATIONAL_ACCELERATION = 0.5f;
  6.     protected PointF mVelocity = new PointF(0.0f, 0.0f); //current velocity
  7.     protected PointF mTargetSpeed = new PointF(0.0f, 0.0f); //target velocity
  8.     protected PointF mAcceleration = new PointF(1.0f, 1.0f); //fake acceleration; how fast we approach targetspeed
  9.     protected float mGravitationalAccel = GRAVITATIONAL_ACCELERATION; //non-static member, some objects will not want gravity
  10.     protected float mFriction = 1.0f; //1 == no friction, 0 == no motion.
  11.  
  12.     public DynamicGameObject(final GameEngine engine, float x, float y, float width, float height, int type){
  13.         super(engine, x, y, width, height, type);
  14.     }
  15.     DynamicGameObject(final GameEngine engine, float x, float y, int type) {
  16.         super(engine, x, y, type);
  17.     }
  18.  
  19.     @Override
  20.     public void onCollision(final GameObject that){
  21.         GameObject.getOverlap(this, that, GameObject.overlap);
  22.         mWorldLocation.offset(GameObject.overlap.x, GameObject.overlap.y); //move us out of the collision
  23.         updateBounds();
  24.     }
  25.  
  26.     @Override
  27.     public void update(float dt){ //deltatime in seconds
  28.         mVelocity.x += (mAcceleration.x * mTargetSpeed.x);
  29.         if(Math.abs(mVelocity.x) > Math.abs(mTargetSpeed.x)){
  30.             mVelocity.x = mTargetSpeed.x; //instantaneous deceleration.
  31.         }
  32.         mVelocity.y += (mAcceleration.y * mTargetSpeed.y)+mGravitationalAccel;
  33.         if(mVelocity.y > TERMINAL_VELOCITY){
  34.             mVelocity.y = TERMINAL_VELOCITY;
  35.         }
  36.         mWorldLocation.x += Utils.clamp(mVelocity.x*dt, -MAX_DELTA, MAX_DELTA);
  37.         mWorldLocation.y += Utils.clamp(mVelocity.y*dt, -MAX_DELTA, MAX_DELTA);
  38.         updateBounds();
  39.     }
  40. }
  41.  
  42.  
  43. //Some values from my Player-class:
  44. private static final float PLAYER_RUN_SPEED = 6.0f; //meters per second
  45. private static final float PLAYER_ACCELERATION_X = 1f;
  46. private static final float PLAYER_ACCELERATION_Y = .9f;
  47. private static final float PLAYER_JUMP_DURATION = 0.120f;
  48. private static final float PLAYER_JUMP_IMPULSE = -(GRAVITATIONAL_ACCELERATION*4);
  49.  
  50. @Override
  51. public void update(float dt){
  52.     mAnim.update(dt);
  53.     float direction = mEngine.mControl.mHorizontalFactor;
  54.     mFacing = (direction < 0) ? LEFT : (direction > 0) ? RIGHT : mFacing;
  55.     mTargetSpeed.x = direction * PLAYER_RUN_SPEED;
  56.     if(mEngine.mControl.mJump && mJumpTime < PLAYER_JUMP_DURATION) {
  57.         mTargetSpeed.y = PLAYER_JUMP_IMPULSE;
  58.         mJumpTime += dt;
  59.     }else{
  60.         mTargetSpeed.y = 0; //gravity is added in parent class
  61.     }
  62.     super.update(dt);
  63. }
RAW Paste Data
Top