Advertisement
Guest User

Untitled

a guest
Jul 25th, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.31 KB | None | 0 0
  1. package com.somethinglogical.someRPG;
  2.  
  3. import com.badlogic.gdx.Gdx;
  4. import com.badlogic.gdx.graphics.Texture;
  5. import com.badlogic.gdx.math.Rectangle;
  6. import com.badlogic.gdx.math.Vector2;
  7. import com.badlogic.gdx.physics.box2d.Body;
  8. import com.badlogic.gdx.physics.box2d.BodyDef;
  9. import com.badlogic.gdx.physics.box2d.World;
  10.  
  11. /**
  12.  * Created by ryan on 21/07/14.
  13.  */
  14. public class Player {
  15.  
  16.     boolean leftMove;
  17.     boolean rightMove;
  18.     boolean upMove;
  19.     boolean downMove;
  20.  
  21.     float xPosition;
  22.     float yPosition;
  23.  
  24.     BodyDef playerBodyDef;
  25.     Body playerBody;
  26.     Texture playerLook;
  27.     Vector2 velocity;
  28.     Vector2 position;
  29.  
  30.     private final static float _maxVelocity = 5;
  31.     static float _playerAcceleration = 0.3f;
  32.     static float _playerDecceleration = -0.1f;
  33.  
  34.  
  35.     void Create(World world) {
  36.  
  37.         playerLook = Assets.playerCharacter;
  38.         this.setupDynamicBody(world);
  39.  
  40.     }
  41.  
  42.     void updateMotion() {
  43.  
  44.         updatePhysics();
  45.         addMovement();
  46.         addFriction();
  47.  
  48.  
  49.     }
  50.  
  51.     void addMovement(){
  52.  
  53.         if (rightMove && velocity.x < _maxVelocity) {
  54.             this.playerBody.applyLinearImpulse(_playerAcceleration, 0, position.x, position.y, true);
  55.         }
  56.         if (leftMove && velocity.x > -_maxVelocity) {
  57.             this.playerBody.applyLinearImpulse(-_playerAcceleration, 0, position.x, position.y, true);
  58.         }
  59.         if (upMove && velocity.y < _maxVelocity) {
  60.             this.playerBody.applyLinearImpulse(0, _playerAcceleration, position.x, position.y, true);
  61.         }
  62.         if (downMove && velocity.y > -_maxVelocity) {
  63.             this.playerBody.applyLinearImpulse(0, -_playerAcceleration, position.x, position.y, true);
  64.         }
  65.     }
  66.  
  67.     void addFriction() {
  68.         if (velocity.x > 0){
  69.             this.playerBody.applyLinearImpulse(_playerDecceleration, 0, position.x, position.y, true);
  70.         }
  71.         if (velocity.x < 0){
  72.             this.playerBody.applyLinearImpulse(-_playerDecceleration, 0, position.x, position.y, true);
  73.         }
  74.         if (velocity.y > 0){
  75.             this.playerBody.applyLinearImpulse(0, _playerDecceleration, position.x, position.y, true);
  76.         }
  77.         if (velocity.y < 0){
  78.             this.playerBody.applyLinearImpulse(0, -_playerDecceleration, position.x, position.y, true);
  79.         }
  80.     }
  81.  
  82.     void updatePhysics(){
  83.  
  84.         velocity = this.playerBody.getLinearVelocity();
  85.         position = this.playerBody.getPosition();
  86.  
  87.         xPosition = this.playerBody.getPosition().x;
  88.         yPosition = this.playerBody.getPosition().y;
  89.  
  90.     }
  91.  
  92.     void setupDynamicBody(World world){
  93.         playerBodyDef = new BodyDef();
  94.         playerBodyDef.type = BodyDef.BodyType.DynamicBody;
  95.         playerBodyDef.position.set(xPosition, yPosition);
  96.  
  97.  
  98.         playerBody = world.createBody(playerBodyDef);
  99.  
  100.     }
  101.  
  102.     public void setLeftMove(boolean t) {
  103.         if (rightMove && t) leftMove = false;
  104.         leftMove = t;
  105.     }
  106.  
  107.     public void setRightMove(boolean t) {
  108.         if (leftMove && t) rightMove = false;
  109.         rightMove = t;
  110.     }
  111.  
  112.     public void setUpMove(boolean t) {
  113.         if (downMove && t) upMove = false;
  114.         upMove = t;
  115.     }
  116.  
  117.     public void setDownMove(boolean t) {
  118.         if (upMove && t) downMove = false;
  119.         downMove = t;
  120.     }
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement