iMackshun

Player

Oct 29th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. package com.iMackshun.Games.PixelFormer.Objects;
  2.  
  3. import com.badlogic.gdx.Input.Keys;
  4. import com.badlogic.gdx.InputAdapter;
  5. import com.badlogic.gdx.math.Vector2;
  6. import com.badlogic.gdx.physics.box2d.Body;
  7. import com.badlogic.gdx.physics.box2d.BodyDef;
  8. import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
  9. import com.badlogic.gdx.physics.box2d.Fixture;
  10. import com.badlogic.gdx.physics.box2d.FixtureDef;
  11. import com.badlogic.gdx.physics.box2d.PolygonShape;
  12. import com.badlogic.gdx.physics.box2d.World;
  13.  
  14. public class Player extends InputAdapter{
  15.     enum State{
  16.         MOVINGLEFT,
  17.         MOVINGRIGHT,
  18.         JUMPING,
  19.         IDLE,
  20.     };
  21.    
  22.     //Variable Declarations
  23.     public static Body PlayerBody;
  24.     public static State state;
  25.     public static int SPEED = 10;
  26.    
  27.     //Constructor
  28.     public Player(World world, FixtureDef PlayerFixtureDef, float X, float Y, float Width, float Height){
  29.         //Body Definition
  30.         BodyDef bodyDef = new BodyDef();//Shape
  31.         bodyDef.type = BodyType.DynamicBody;
  32.         bodyDef.position.set(X,Y);
  33.         //Create The Body and Disable Rotation
  34.         PlayerBody = world.createBody(bodyDef);
  35.         PlayerBody.setFixedRotation(true);
  36.         // Shape
  37.         PolygonShape PlayerShape = new PolygonShape();
  38.         PlayerShape.setAsBox(Width, Height);
  39.         //Fixture Definition
  40.         FixtureDef fixtureDef = new FixtureDef();
  41.         fixtureDef.shape = PlayerShape;
  42.         fixtureDef.density = 0.5f;
  43.         fixtureDef.friction = 0.0f;
  44.         fixtureDef.restitution = 0.2f;
  45.         // Create The Fixture and Bind the Shape to the Body
  46.         Fixture PlayerFixture = PlayerBody.createFixture(fixtureDef);
  47.     }
  48.     public void update(){
  49.         Vector2 Velocity = PlayerBody.getLinearVelocity();
  50.         switch(state){
  51.         case MOVINGLEFT:
  52.             Velocity.x = -SPEED;
  53.             break;
  54.         case MOVINGRIGHT:
  55.             Velocity.x = SPEED;
  56.             break;
  57.         case IDLE:
  58.             Velocity.x = 0;
  59.             break;
  60.         }
  61.         PlayerBody.setLinearVelocity(Velocity);
  62.     }
  63.     @Override
  64.     public boolean keyDown(int keycode) {
  65.         switch(keycode){
  66.         case(Keys.LEFT):
  67.             state = State.MOVINGLEFT;
  68.             break;
  69.         case(Keys.RIGHT):
  70.             state = State.MOVINGRIGHT;
  71.             break;
  72.         case(Keys.SPACE):
  73.             if(state != State.JUMPING){
  74.             System.out.print("Jump");
  75.             state = State.JUMPING;
  76.             PlayerBody.applyForceToCenter(0, 5000, true);
  77.             }
  78.             break;
  79.         }
  80.         return true;
  81.     }
  82.     @Override
  83.     public boolean keyUp(int keycode) {
  84.         switch(keycode){
  85.         case(Keys.LEFT):
  86.             state = State.IDLE;
  87.             break;
  88.         case(Keys.RIGHT):
  89.             state = State.IDLE;
  90.             break;
  91.         }
  92.         return true;
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment