Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.somethinglogical.someRPG;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.math.Rectangle;
- 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.World;
- /**
- * Created by ryan on 21/07/14.
- */
- public class Player {
- boolean leftMove;
- boolean rightMove;
- boolean upMove;
- boolean downMove;
- float xPosition;
- float yPosition;
- BodyDef playerBodyDef;
- Body playerBody;
- Texture playerLook;
- Vector2 velocity;
- Vector2 position;
- private final static float _maxVelocity = 5;
- static float _playerAcceleration = 0.3f;
- static float _playerDecceleration = -0.1f;
- void Create(World world) {
- playerLook = Assets.playerCharacter;
- this.setupDynamicBody(world);
- }
- void updateMotion() {
- updatePhysics();
- addMovement();
- addFriction();
- }
- void addMovement(){
- if (rightMove && velocity.x < _maxVelocity) {
- this.playerBody.applyLinearImpulse(_playerAcceleration, 0, position.x, position.y, true);
- }
- if (leftMove && velocity.x > -_maxVelocity) {
- this.playerBody.applyLinearImpulse(-_playerAcceleration, 0, position.x, position.y, true);
- }
- if (upMove && velocity.y < _maxVelocity) {
- this.playerBody.applyLinearImpulse(0, _playerAcceleration, position.x, position.y, true);
- }
- if (downMove && velocity.y > -_maxVelocity) {
- this.playerBody.applyLinearImpulse(0, -_playerAcceleration, position.x, position.y, true);
- }
- }
- void addFriction() {
- if (velocity.x > 0){
- this.playerBody.applyLinearImpulse(_playerDecceleration, 0, position.x, position.y, true);
- }
- if (velocity.x < 0){
- this.playerBody.applyLinearImpulse(-_playerDecceleration, 0, position.x, position.y, true);
- }
- if (velocity.y > 0){
- this.playerBody.applyLinearImpulse(0, _playerDecceleration, position.x, position.y, true);
- }
- if (velocity.y < 0){
- this.playerBody.applyLinearImpulse(0, -_playerDecceleration, position.x, position.y, true);
- }
- }
- void updatePhysics(){
- velocity = this.playerBody.getLinearVelocity();
- position = this.playerBody.getPosition();
- xPosition = this.playerBody.getPosition().x;
- yPosition = this.playerBody.getPosition().y;
- }
- void setupDynamicBody(World world){
- playerBodyDef = new BodyDef();
- playerBodyDef.type = BodyDef.BodyType.DynamicBody;
- playerBodyDef.position.set(xPosition, yPosition);
- playerBody = world.createBody(playerBodyDef);
- }
- public void setLeftMove(boolean t) {
- if (rightMove && t) leftMove = false;
- leftMove = t;
- }
- public void setRightMove(boolean t) {
- if (leftMove && t) rightMove = false;
- rightMove = t;
- }
- public void setUpMove(boolean t) {
- if (downMove && t) upMove = false;
- upMove = t;
- }
- public void setDownMove(boolean t) {
- if (upMove && t) downMove = false;
- downMove = t;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement