Advertisement
Guest User

Shitty Java class

a guest
Aug 28th, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.05 KB | None | 0 0
  1. package com.sholnk.box2dtry;
  2.  
  3. import com.badlogic.gdx.graphics.Texture;
  4. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  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.CircleShape;
  10. import com.badlogic.gdx.physics.box2d.Fixture;
  11. import com.badlogic.gdx.physics.box2d.FixtureDef;
  12. import com.badlogic.gdx.physics.box2d.World;
  13.  
  14. public abstract class EntityBase {
  15.    
  16.     protected BodyDef bodyDef = new BodyDef();
  17.     protected Body body;
  18.     protected Texture texture;
  19.     protected CircleShape circle = new CircleShape();
  20.     protected FixtureDef fixtureDef = new FixtureDef();
  21.     protected Fixture fixture;
  22.     protected Vector2 vel = new Vector2();
  23.     protected Vector2 pos = new Vector2();
  24.     protected Vector2 dimensions = new Vector2();
  25.  
  26.     public EntityBase(World world, Texture texture) {
  27.         this.texture = texture;
  28.         bodyDef.type = BodyType.DynamicBody;
  29.         bodyDef.position.set(0, 0);
  30.         body = world.createBody(bodyDef);
  31.         circle.setRadius(texture.getWidth() / 2f);
  32.         dimensions.x = circle.getRadius() * 2;
  33.         dimensions.y = circle.getRadius() * 2;
  34.         fixtureDef.shape = circle;
  35.         fixtureDef.density = 0.5f;
  36.         fixtureDef.friction = 0.4f;
  37.         fixtureDef.restitution = 0.6f; // Make it bounce a little bit
  38.         fixture = body.createFixture(fixtureDef);
  39.     }
  40.    
  41.     public void render(SpriteBatch batch) {
  42.         batch.draw(texture, body.getPosition().x, body.getPosition().y);
  43.     }
  44.    
  45.     public abstract void update();
  46.    
  47.     public Vector2 getPosition() {
  48.         return body.getPosition();
  49.     }
  50.    
  51.     public float getWidth() {
  52.         return dimensions.x;
  53.     }
  54.    
  55.     public float getHeight() {
  56.         return dimensions.y;
  57.     }
  58.    
  59.     public void setPosition(Vector2 pos) {
  60.         body.setTransform(pos, 0);
  61.         this.pos = pos;
  62.     }
  63.    
  64.     public void setPosition(float x, float y) {
  65.         pos.x = x; pos.y = y;
  66.         body.setTransform(pos, 0);
  67.     }
  68.    
  69.     public Body getBody() {
  70.         return body;
  71.     }
  72.    
  73.     public void dispose() {
  74.         circle.dispose();
  75.     }
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement