Advertisement
Guest User

GamePlay.java

a guest
Aug 17th, 2016
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1. package screens;
  2.  
  3. import com.badlogic.gdx.Gdx;
  4. import com.badlogic.gdx.Input;
  5. import com.badlogic.gdx.Screen;
  6. import com.badlogic.gdx.graphics.GL20;
  7. import com.badlogic.gdx.graphics.OrthographicCamera;
  8. import com.badlogic.gdx.math.Vector2;
  9. import com.badlogic.gdx.physics.box2d.*;
  10. import com.santivaldez.ropistgame.entities.Rope;
  11.  
  12. /**
  13.  * Created by SantiValdez on 10-Aug-16.
  14.  */
  15. public class GamePlay implements Screen{
  16.  
  17.     private OrthographicCamera cam;
  18.  
  19.     private Box2DDebugRenderer debugRenderer;
  20.     private World world;
  21.     private Body playerBody;
  22.     private Body ceilingBody;
  23.     private Rope rope;
  24.  
  25.     @Override
  26.     public void show() {
  27.         world = new World(new Vector2(0, -9.81f), true);
  28.         cam = new OrthographicCamera();
  29.         debugRenderer = new Box2DDebugRenderer();
  30.  
  31.         rope = new Rope(world);
  32.  
  33.         BodyDef bdef = new BodyDef();
  34.         FixtureDef fdef = new FixtureDef();
  35.  
  36.         //bodydef
  37.         bdef.type = BodyDef.BodyType.StaticBody;
  38.         bdef.position.set(0, 2.4f);
  39.  
  40.         //shape
  41.         PolygonShape ceilingShape = new PolygonShape();
  42.         ceilingShape.setAsBox(15, 0.25f);
  43.  
  44.         //fixturedef
  45.         fdef.shape = ceilingShape;
  46.  
  47.         //create it
  48.         ceilingBody = world.createBody(bdef);
  49.         ceilingBody.createFixture(fdef);
  50.  
  51.         bdef.position.set(0, -10);
  52.         world.createBody(bdef).createFixture(fdef); // TEMPORARY FLOOR
  53.  
  54.         //reuse for player
  55.         bdef.type = BodyDef.BodyType.DynamicBody;
  56.         bdef.position.set(-5, 1.5f);
  57.  
  58.         CircleShape playerShape = new CircleShape();
  59.         playerShape.setRadius(0.15f); //half
  60.  
  61.         fdef.shape = playerShape;
  62.         fdef.density = 0.1f;
  63.         fdef.restitution = 0.5f;
  64.         fdef.friction = 0.2f;
  65.  
  66.         playerBody = world.createBody(bdef);
  67.         playerBody.createFixture(fdef);
  68.  
  69.         rope.create(ceilingBody, playerBody, 10);
  70.     }
  71.  
  72.     @Override
  73.     public void render(float delta) {
  74.         Gdx.gl.glClearColor(0f, 0.4f, 0.7f, 1f);
  75.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  76.  
  77.  
  78.  
  79.         world.step(1f / 60f, 8, 3);
  80.         cam.update();
  81.         debugRenderer.render(world, cam.combined);
  82.  
  83.         if(Gdx.input.isKeyPressed(Input.Keys.SPACE)){
  84.             playerBody.applyLinearImpulse(0.008f, 0.008f, 0, 0, true);
  85.             //world.destroyBody(playerBody);
  86.             if(!world.isLocked()) {
  87.                 rope.destroyRope();
  88.             }
  89.         }
  90.     }
  91.  
  92.     @Override
  93.     public void dispose() {
  94.         world.dispose();
  95.         debugRenderer.dispose();
  96.     }
  97.  
  98.     @Override
  99.     public void pause() {}
  100.  
  101.     @Override
  102.     public void resize(int width, int height) {
  103.         cam.viewportWidth = width / 100;    // 1pixel >> 100m pixel ratio
  104.         cam.viewportHeight = height / 100;
  105.     }
  106.  
  107.     @Override
  108.     public void resume() {}
  109.  
  110.     @Override
  111.     public void hide() {}
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement