Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.iMackshun.Games.PixelFormer.Objects;
- import com.badlogic.gdx.Input.Keys;
- import com.badlogic.gdx.InputAdapter;
- import com.badlogic.gdx.math.Vector2;
- import com.badlogic.gdx.physics.box2d.Body;
- import com.badlogic.gdx.physics.box2d.BodyDef;
- import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
- import com.badlogic.gdx.physics.box2d.Fixture;
- import com.badlogic.gdx.physics.box2d.FixtureDef;
- import com.badlogic.gdx.physics.box2d.PolygonShape;
- import com.badlogic.gdx.physics.box2d.World;
- public class Player extends InputAdapter{
- enum State{
- MOVINGLEFT,
- MOVINGRIGHT,
- JUMPING,
- IDLE,
- };
- //Variable Declarations
- public static Body PlayerBody;
- public static State state;
- public static int SPEED = 10;
- //Constructor
- public Player(World world, FixtureDef PlayerFixtureDef, float X, float Y, float Width, float Height){
- //Body Definition
- BodyDef bodyDef = new BodyDef();//Shape
- bodyDef.type = BodyType.DynamicBody;
- bodyDef.position.set(X,Y);
- //Create The Body and Disable Rotation
- PlayerBody = world.createBody(bodyDef);
- PlayerBody.setFixedRotation(true);
- // Shape
- PolygonShape PlayerShape = new PolygonShape();
- PlayerShape.setAsBox(Width, Height);
- //Fixture Definition
- FixtureDef fixtureDef = new FixtureDef();
- fixtureDef.shape = PlayerShape;
- fixtureDef.density = 0.5f;
- fixtureDef.friction = 0.0f;
- fixtureDef.restitution = 0.2f;
- // Create The Fixture and Bind the Shape to the Body
- Fixture PlayerFixture = PlayerBody.createFixture(fixtureDef);
- }
- public void update(){
- Vector2 Velocity = PlayerBody.getLinearVelocity();
- switch(state){
- case MOVINGLEFT:
- Velocity.x = -SPEED;
- break;
- case MOVINGRIGHT:
- Velocity.x = SPEED;
- break;
- case IDLE:
- Velocity.x = 0;
- break;
- }
- PlayerBody.setLinearVelocity(Velocity);
- }
- @Override
- public boolean keyDown(int keycode) {
- switch(keycode){
- case(Keys.LEFT):
- state = State.MOVINGLEFT;
- break;
- case(Keys.RIGHT):
- state = State.MOVINGRIGHT;
- break;
- case(Keys.SPACE):
- if(state != State.JUMPING){
- System.out.print("Jump");
- state = State.JUMPING;
- PlayerBody.applyForceToCenter(0, 5000, true);
- }
- break;
- }
- return true;
- }
- @Override
- public boolean keyUp(int keycode) {
- switch(keycode){
- case(Keys.LEFT):
- state = State.IDLE;
- break;
- case(Keys.RIGHT):
- state = State.IDLE;
- break;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment