SHARE
TWEET

no render

a guest Oct 27th, 2015 113 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.dungeon.architect.screens;
  2.  
  3. import com.badlogic.gdx.Gdx;
  4. import com.badlogic.gdx.Screen;
  5. import com.badlogic.gdx.graphics.GL20;
  6. import com.badlogic.gdx.graphics.OrthographicCamera;
  7. import com.badlogic.gdx.graphics.g2d.TextureAtlas;
  8. import com.badlogic.gdx.maps.MapObject;
  9. import com.badlogic.gdx.maps.objects.RectangleMapObject;
  10. import com.badlogic.gdx.maps.tiled.TiledMap;
  11. import com.badlogic.gdx.maps.tiled.TmxMapLoader;
  12. import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
  13. import com.badlogic.gdx.math.Rectangle;
  14. import com.badlogic.gdx.math.Vector2;
  15. import com.badlogic.gdx.physics.box2d.Body;
  16. import com.badlogic.gdx.physics.box2d.BodyDef;
  17. import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
  18. import com.badlogic.gdx.physics.box2d.FixtureDef;
  19. import com.badlogic.gdx.physics.box2d.PolygonShape;
  20. import com.badlogic.gdx.physics.box2d.World;
  21. import com.badlogic.gdx.utils.viewport.FitViewport;
  22.  
  23. import com.badlogic.gdx.utils.viewport.Viewport;
  24. import com.dungeon.architect.GameBase;
  25. import com.dungeon.architect.engine.Physics;
  26. import com.dungeon.architect.entities.Hero;
  27. import com.dungeon.architect.scenes.HUD;
  28.  
  29. /**
  30.  * Created by Adam on 25/10/2015.
  31.  */
  32. public class GameScreen implements Screen{
  33.  
  34.  
  35.     //base game variables
  36.     private GameBase game;
  37.     private OrthographicCamera gamecam;
  38.     private Viewport viewport;
  39.     private HUD hud;
  40.     private Hero player;
  41.  
  42.  
  43.     //map variables
  44.     private TmxMapLoader maploader;
  45.     private TiledMap map;
  46.     private OrthogonalTiledMapRenderer renderer;
  47.  
  48.     //box2d physics
  49.     private World world;
  50.     private Box2DDebugRenderer b2dr;
  51.  
  52.     //sprites
  53.     private TextureAtlas atlas;
  54.  
  55.  
  56.     public GameScreen(GameBase game)
  57.     {
  58.         atlas = new TextureAtlas("atlases/test.pack");
  59.  
  60.         this.game = game;
  61.  
  62.         gamecam = new OrthographicCamera();
  63.         viewport = new FitViewport(GameBase.WIDTH / GameBase.PPM,GameBase.HEIGHT / GameBase.PPM ,gamecam);
  64.         hud = new HUD(game.batch);
  65.  
  66.  
  67.  
  68.  
  69.         //create tile map and renderer
  70.         maploader = new TmxMapLoader();
  71.         map = maploader.load("maps/test.tmx");
  72.         renderer = new OrthogonalTiledMapRenderer(map, 1 / GameBase.PPM);
  73.  
  74.         //center the camera to the middle of the world
  75.         gamecam.position.set(viewport.getWorldWidth() / 2, viewport.getWorldHeight() / 2,0);
  76.  
  77.         world = new World(new Vector2(0,0), true);
  78.         b2dr = new Box2DDebugRenderer();
  79.  
  80.         //initialize world physics
  81.         new Physics(world,map);
  82.  
  83.         //create the Hero
  84.          player = new Hero(world,this);
  85.  
  86.     }
  87.  
  88.     public TextureAtlas getAtlas(){
  89.         return atlas;
  90.     }
  91.  
  92.     @Override
  93.     public void show() {
  94.  
  95.     }
  96.  
  97.     public void handleInput(float dt){
  98.  
  99.  
  100.     }
  101.  
  102.  
  103.     public void update(float dt){
  104.         handleInput(dt);
  105.  
  106.         world.step(1 / 60f, 6, 2);
  107.  
  108.         //update player
  109.         player.update(dt);
  110.  
  111.         gamecam.position.x = player.b2body.getPosition().x;
  112.         gamecam.position.y = player.b2body.getPosition().y;
  113.  
  114.         gamecam.update();
  115.         renderer.setView(gamecam);
  116.  
  117.     }
  118.  
  119.     @Override
  120.     public void render(float delta) {
  121.         update(delta);
  122.  
  123.         Gdx.gl.glClearColor(0, 0, 0, 1);
  124.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  125.  
  126.         //render the tilemap
  127.         renderer.render();
  128.  
  129.         //render physics
  130.         b2dr.render(world, gamecam.combined);
  131.  
  132.  
  133.  
  134.         game.batch.setProjectionMatrix(gamecam.combined);
  135.         game.batch.begin();
  136.  
  137.         //draw here
  138.         player.draw(game.batch);
  139.  
  140.  
  141.         game.batch.end();
  142.  
  143.         //draw HUD
  144.         game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
  145.         hud.stage.draw();
  146.     }
  147.  
  148.     @Override
  149.     public void resize(int width, int height) {
  150.  
  151.         viewport.update(width, height);
  152.  
  153.     }
  154.  
  155.     @Override
  156.     public void pause() {
  157.  
  158.     }
  159.  
  160.     @Override
  161.     public void resume() {
  162.  
  163.     }
  164.  
  165.     @Override
  166.     public void hide() {
  167.  
  168.     }
  169.  
  170.     @Override
  171.     public void dispose() {
  172.         map.dispose();
  173.         renderer.dispose();
  174.         world.dispose();
  175.         b2dr.dispose();
  176.         hud.dispose();
  177.     }
  178. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top