Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.37 KB | None | 0 0
  1. package com.willie.compsci;
  2.  
  3. import java.util.HashMap;
  4.  
  5. import com.badlogic.gdx.Gdx;
  6. import com.badlogic.gdx.graphics.Texture;
  7. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  8. import com.badlogic.gdx.math.Vector2;
  9. import com.badlogic.gdx.physics.box2d.Body;
  10. import com.badlogic.gdx.physics.box2d.BodyDef;
  11. import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
  12. import com.badlogic.gdx.physics.box2d.Contact;
  13. import com.badlogic.gdx.physics.box2d.Fixture;
  14. import com.badlogic.gdx.physics.box2d.FixtureDef;
  15. import com.badlogic.gdx.physics.box2d.PolygonShape;
  16. import com.badlogic.gdx.physics.box2d.World;
  17. import com.badlogic.gdx.utils.Array;
  18.  
  19. public class Character
  20. {
  21.  
  22.     private final int MAX_HEALTH;
  23.     private int health;
  24.     private int currentHealth;
  25.     private Vector2 position;
  26.     private HashMap<Control, Integer> controls;
  27.     private BodyDef bodyDef;
  28.     private Body body;
  29.     private Fixture fixture;
  30.     private World world;
  31.     private final float PIXELS_TO_METERS = 100f;
  32.     private Array<Contact> contacts;
  33.     private boolean canJump;
  34.  
  35.     // will be changed to TextureRegion for use with a sprite sheet
  36.     private Texture texture;
  37.  
  38.     public Character(float x, float y, int maxHealth, Texture texture, HashMap<Control, Integer> controls, World world)
  39.     {
  40.         MAX_HEALTH = maxHealth;
  41.         this.texture = texture;
  42.         this.controls = controls;
  43.         position = new Vector2(x / PIXELS_TO_METERS, y / PIXELS_TO_METERS);
  44.         this.world = world;
  45.  
  46.         bodyDef = new BodyDef();
  47.         bodyDef.type = BodyType.DynamicBody;
  48.         bodyDef.position.set(position);
  49.         bodyDef.fixedRotation = true;
  50.         body = world.createBody(bodyDef);
  51.  
  52.         PolygonShape rectangle = new PolygonShape();
  53.         rectangle.setAsBox((texture.getWidth() / 2) / PIXELS_TO_METERS, (texture.getHeight() / 2) / PIXELS_TO_METERS);
  54.  
  55.         FixtureDef fixtureDef = new FixtureDef();
  56.         fixtureDef.shape = rectangle;
  57.         fixtureDef.density = 0.75f;
  58.         fixtureDef.friction = 1.5f;
  59.         fixtureDef.restitution = 0;
  60.  
  61.         fixture = body.createFixture(fixtureDef);
  62.  
  63.         canJump = true;
  64.     }
  65.  
  66.     // TODO: You slide down walls (don't stick to them)
  67.     // TODO: Collition detection
  68.  
  69.     private final float MAX_VELOCITY = 3;
  70.     private final float ACCELERATION = 0.15f;
  71.  
  72.     public void update(float delta)
  73.     {
  74.         contacts = world.getContactList();
  75.  
  76.         for (Contact c : contacts)
  77.             if (fixture.equals(c.getFixtureA()) || fixture.equals(c.getFixtureB()))
  78.             {
  79.                 System.out.println("canJump");
  80.                 canJump = true;
  81.             }
  82.  
  83.         if (Gdx.input.isKeyPressed(getControl(Control.LEFT)) && canMoveLeft() && body.getLinearVelocity().x > -MAX_VELOCITY)
  84.             body.applyLinearImpulse(-ACCELERATION, 0, body.getPosition().x, body.getPosition().y, true);
  85.  
  86.         if (Gdx.input.isKeyPressed(getControl(Control.RIGHT)) && canMoveRight() && body.getLinearVelocity().x < MAX_VELOCITY)
  87.             body.applyLinearImpulse(ACCELERATION, 0, body.getPosition().x, body.getPosition().y, true);
  88.  
  89.         if (Gdx.input.isKeyJustPressed(getControl(Control.JUMP)) && canJump)
  90.         {
  91.             body.applyForceToCenter(new Vector2(0.0f, 100f), true);
  92.             canJump = false;
  93.         }
  94.     }
  95.  
  96.     public void draw(SpriteBatch batch)
  97.     {
  98.         batch.draw(texture, body.getPosition().x * PIXELS_TO_METERS - texture.getWidth() / 2, body.getPosition().y * PIXELS_TO_METERS - texture.getHeight() / 2);
  99.     }
  100.  
  101.     public boolean canFall()
  102.     {
  103.         return (getY() >= 0);
  104.     }
  105.  
  106.     //
  107.     public boolean canJump()
  108.     {
  109.         // TODO: implement
  110.         return true;
  111.     }
  112.  
  113.     public int getControl(Control control)
  114.     {
  115.         return controls.get(control);
  116.     }
  117.  
  118.     public boolean canMoveLeft()
  119.     {
  120.         return !(position.x <= 0);
  121.     }
  122.  
  123.     public boolean canMoveRight()
  124.     {
  125.         return !(position.x + texture.getWidth() >= Gdx.graphics.getWidth());
  126.     }
  127.  
  128.     public int getCurrentHealth()
  129.     {
  130.         return currentHealth;
  131.     }
  132.  
  133.     public void setCurrentHealth(int currentHealth)
  134.     {
  135.         this.currentHealth = currentHealth;
  136.     }
  137.  
  138.     public float getX()
  139.     {
  140.         return position.x;
  141.     }
  142.  
  143.     public void setX(float x)
  144.     {
  145.         position.x = x;
  146.     }
  147.  
  148.     public float getY()
  149.     {
  150.         return position.y;
  151.     }
  152.  
  153.     public void setY(float y)
  154.     {
  155.         position.y = y;
  156.     }
  157.  
  158.     public Texture getTexture()
  159.     {
  160.         return texture;
  161.     }
  162.  
  163.     public int getHealth()
  164.     {
  165.         return health;
  166.     }
  167.  
  168.     public void setHealth(int health)
  169.     {
  170.         this.health = health;
  171.     }
  172.  
  173.     public int getMaxHealth()
  174.     {
  175.         return MAX_HEALTH;
  176.     }
  177.  
  178.     public Vector2 getPosition()
  179.     {
  180.         return body.getPosition();
  181.     }
  182.  
  183.     public void setPosition(Vector2 position)
  184.     {
  185.         body.getPosition().set(position);
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement