Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. package com.mygdx.game;
  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.graphics.Texture;
  9. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  10. import com.badlogic.gdx.maps.Map;
  11. import com.badlogic.gdx.maps.MapObject;
  12. import com.badlogic.gdx.maps.objects.RectangleMapObject;
  13. import com.badlogic.gdx.maps.tiled.TiledMap;
  14. import com.badlogic.gdx.maps.tiled.TmxMapLoader;
  15. import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
  16. import com.badlogic.gdx.math.Rectangle;
  17. import com.badlogic.gdx.math.Vector2;
  18. import com.badlogic.gdx.physics.box2d.Body;
  19. import com.badlogic.gdx.physics.box2d.BodyDef;
  20. import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
  21. import com.badlogic.gdx.physics.box2d.FixtureDef;
  22. import com.badlogic.gdx.physics.box2d.PolygonShape;
  23. import com.badlogic.gdx.physics.box2d.World;
  24. import com.badlogic.gdx.utils.viewport.FitViewport;
  25. import com.badlogic.gdx.utils.viewport.Viewport;
  26. import com.mygdx.game.Scenes.Hud;
  27. import com.mygdx.game.Sprites.Bery;
  28. import com.sun.jndi.ldap.Ber;
  29.  
  30. public class PlayScreen implements Screen {
  31. private BeriGame game;
  32. private OrthographicCamera cam;
  33. private SpriteBatch batch;
  34. Texture texture;
  35. private Viewport gamePort;
  36. private Hud hud;
  37. private Bery bery;
  38.  
  39. private TmxMapLoader mapLoader;
  40. private OrthogonalTiledMapRenderer renderer;
  41. private TiledMap map;
  42.  
  43. private World world;
  44. private Box2DDebugRenderer b2dr;
  45.  
  46. public PlayScreen(BeriGame game) {
  47. this.game = game;
  48. texture = new Texture("badlogic.jpg");
  49. cam = new OrthographicCamera();
  50. gamePort = new FitViewport(BeriGame.V_WIDTH / BeriGame.PPM, BeriGame.V_HEIGHT / BeriGame.PPM, cam);
  51.  
  52. hud = new Hud(game.batch);
  53.  
  54. mapLoader = new TmxMapLoader();
  55. map = mapLoader.load("bery_map/beryMap.tmx");
  56. renderer = new OrthogonalTiledMapRenderer(map, 1 / BeriGame.PPM);
  57. cam.position.set(gamePort.getWorldWidth()/2, gamePort.getWorldHeight()/2, 0);
  58. world = new World(new Vector2(0, -10), true);
  59.  
  60. b2dr = new Box2DDebugRenderer();
  61.  
  62.  
  63. BodyDef bdef = new BodyDef();
  64. PolygonShape shape = new PolygonShape();
  65. FixtureDef fdef = new FixtureDef();
  66. Body body;
  67.  
  68. for(MapObject object : map.getLayers().get(2).getObjects().getByType(RectangleMapObject.class)) {
  69. Rectangle rect = ((RectangleMapObject) object).getRectangle();
  70. bdef.type = BodyDef.BodyType.StaticBody;
  71. bdef.position.set((rect.getX()+rect.getWidth()/2)/ BeriGame.PPM, (rect.getY()+rect.getHeight()/2) / BeriGame.PPM);
  72.  
  73. body = world.createBody(bdef);
  74.  
  75. shape.setAsBox(rect.getWidth()/2 / BeriGame.PPM, rect.getHeight()/2/ BeriGame.PPM);
  76. fdef.shape = shape;
  77.  
  78. body.createFixture(fdef);
  79. }
  80.  
  81. for(MapObject object : map.getLayers().get(3).getObjects().getByType(RectangleMapObject.class)) {
  82. Rectangle rect = ((RectangleMapObject) object).getRectangle();
  83. bdef.type = BodyDef.BodyType.StaticBody; // should be dynamic
  84. bdef.position.set((rect.getX()+rect.getWidth()/2)/ BeriGame.PPM, (rect.getY()+rect.getHeight()/2) / BeriGame.PPM);
  85.  
  86. body = world.createBody(bdef);
  87.  
  88. shape.setAsBox(rect.getWidth()/2 / BeriGame.PPM, rect.getHeight()/2/ BeriGame.PPM);
  89. fdef.shape = shape;
  90.  
  91. body.createFixture(fdef);
  92. }
  93.  
  94. bery = new Bery(world);
  95. }
  96.  
  97. public void handleInput(float dt) {
  98. if(Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
  99. if (Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT))
  100. cam.position.x += 100 * dt;
  101. }
  102. if(Gdx.input.isTouched())
  103. cam.position.x += 100 * dt;
  104. }
  105.  
  106. public void update(float dt) {
  107. handleInput(dt);
  108. world.step(1/60f, 6, 2);
  109.  
  110.  
  111. cam.update();
  112. renderer.setView(cam);
  113.  
  114.  
  115. }
  116.  
  117. @Override
  118. public void show() {
  119.  
  120. }
  121.  
  122. @Override
  123. public void render(float delta) {
  124. update(delta);
  125. Gdx.gl.glClearColor(0, 0, 0, 1);
  126. Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  127. renderer.render();
  128. b2dr.render(world, cam.combined);
  129.  
  130. game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
  131. hud.stage.draw();
  132.  
  133. }
  134.  
  135. @Override
  136. public void resize(int width, int height) {
  137. gamePort.update(width, height);
  138. }
  139.  
  140. @Override
  141. public void pause() {
  142.  
  143. }
  144.  
  145. @Override
  146. public void resume() {
  147.  
  148. }
  149.  
  150. @Override
  151. public void hide() {
  152.  
  153. }
  154.  
  155. @Override
  156. public void dispose() {
  157.  
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement