Advertisement
Guest User

Untitled

a guest
Apr 17th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. public class GameObject {
  2. public final Vector2 position;
  3. public final Rectangle bounds;
  4.  
  5. public GameObject (float x, float y, float width, float height) {
  6. this.position = new Vector2(x, y);
  7. this.bounds = new Rectangle(x - width / 2, y - height / 2, width, height);
  8. }
  9.  
  10. public class DynamicGameObject extends GameObject {
  11. protected static Vector2 velocity;
  12. public final Vector2 accel;
  13.  
  14. public DynamicGameObject (float x, float y, float width, float height) {
  15. super(x, y, width, height);
  16. setVelocity(new Vector2());
  17. accel = new Vector2();
  18. }
  19.  
  20. public Vector2 getVelocity() {
  21. return velocity;
  22. }
  23.  
  24. public void setVelocity(Vector2 velocity) {
  25. DynamicGameObject.velocity = velocity;
  26. }
  27. }
  28.  
  29. public Ruben (float x, float y) {
  30. super(x, y, RUBEN_WIDTH, RUBEN_HEIGHT);
  31. state = RUBEN_STATE_IDLE;
  32. stateTime = 0;
  33. grounded = true;
  34. facingRight = true;
  35. }
  36.  
  37. public void update (float deltaTime) {
  38. //getVelocity().add(World.gravity.x * deltaTime, World.gravity.y * deltaTime);
  39. position.add(velocity.x * deltaTime, velocity.y * deltaTime);
  40. bounds.x = position.x - bounds.width / 2;
  41. bounds.y = position.y - bounds.height / 2;
  42.  
  43. if (position.x < 0) position.x = World.WORLD_WIDTH;
  44. if (position.x > World.WORLD_WIDTH) position.x = 0;
  45. stateTime += deltaTime;
  46. }
  47.  
  48. private void updateRuben (float deltaTime, float accelX) {
  49.  
  50.  
  51. if (ruben.getState() != Ruben.RUBEN_STATE_IDLE && ruben.position.y <= 0.5f) ruben.hitPlatform();
  52. if (ruben.getState() != Ruben.RUBEN_STATE_IDLE) ruben.getVelocity().x = -accelX / 10 * Ruben.RUBEN_MOVE_VELOCITY;
  53.  
  54. // clamp the velocity to the maximum, x-axis only
  55. if (Math.abs(ruben.getVelocity().x) > Ruben.RUBEN_MAX_VELOCITY) {
  56. ruben.getVelocity().x = Math.signum(ruben.getVelocity().x) * Ruben.RUBEN_MAX_VELOCITY;
  57. }
  58.  
  59. // clamp the velocity to 0 if it's < 1, and set the state to standing
  60. if (Math.abs(ruben.getVelocity().x) < 1) {
  61. ruben.getVelocity().x = 0;
  62. if (Ruben.isGrounded()) {
  63. Ruben.setState(Ruben.RUBEN_STATE_IDLE);
  64. }
  65. }
  66.  
  67. ruben.getVelocity().scl(deltaTime);
  68. ruben.update(deltaTime);
  69. heightSoFar = Math.max(ruben.position.y, heightSoFar);
  70. }
  71.  
  72. (GestureDetector gd = new GestureDetector(new GestureHandler(this));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement