Advertisement
Guest User

Untitled

a guest
May 13th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  1. // Outer most class
  2. public class GameScreen {
  3.     GameRenderer renderer;
  4.     GameWorld world;
  5.     // Called every frame. delta is the time since last frame.
  6.     public void render(float delta) {
  7.         float dt = world.update(delta);
  8.         renderer.render(dt);
  9.     }
  10. }
  11.  
  12. public class GameRenderer {
  13.     // Interpolation code (smooth states)
  14.         static void interpolate(Vector2 newPosition, Vector2 oldPosition, float fraction, Vector2 vertex) {
  15.                 vertex.x = (1 - fraction) * oldPosition.x + fraction * newPosition.x;
  16.                 vertex.y = (1 - fraction) * oldPosition.y + fraction * newPosition.y;
  17.         }
  18.     // Main render method
  19.     public void render(float delta) {
  20.         batcher.begin();
  21.         for (int i = 0; i < world.bodies.size(); i++) {
  22.             GameCircle body = world.bodies.get(i);
  23.             renderCircle(body, delta / GameWorld.timeDelta);
  24.         }
  25.         batcher.end();
  26.     }
  27.         private void renderCircle(GameCircle circle, float fraction) {
  28.                 tmpVertex3.x = circle.getX();
  29.                 tmpVertex3.y = circle.getY();
  30.                 GameRenderer.interpolate(tmpVertex3, circle._lastPosition, fraction, tmpVertex1);
  31.                 mapCoordinate(tmpVertex1.x, tmpVertex1.y, tmpVertex2);
  32.                 float radius = scale(circle.getRadius());
  33.                 sprite.setBounds(tmpVertex2.x - radius, tmpVertex2.y - radius, radius * 2, radius * 2);
  34.                 sprite.draw(batcher);
  35.         }
  36. }
  37.  
  38. // Physics updates
  39. public class GameWorld {
  40.         // delta is time elapsed since last frame.
  41.         // this is called every frame.
  42.         // It returns the accumulator
  43.         public float update(float delta) {
  44.                 physicsDelta += Math.min(delta, 0.25f);
  45.                 while (physicsDelta >= GameWorld.timeDelta) {
  46.                         rememberWorld();
  47.                         protagonist.body.applyForceToCenter(force, true);
  48.                         world.step(GameWorld.timeDelta, 8, 3);
  49.                         physicsDelta -= GameWorld.timeDelta;
  50.                 }
  51.                 return physicsDelta;
  52.         }
  53.     // Remembers the current state of the world
  54.         public void rememberWorld() {
  55.                 for (int i = bodies.size() - 1; i >= 0; i--) {
  56.                         GameCircle body = bodies.get(i);
  57.                         body._lastPosition.x = body.getX();
  58.                         body._lastPosition.y = body.getY();
  59.                 }
  60.         }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement