SHARE
TWEET

ERROR_GAME

RafaelMCorrea Nov 6th, 2016 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package br.com.expressobits.mirrorcars.screens;
  7.  
  8. import br.com.expressobits.mirrorcars.B2DVars;
  9. import static br.com.expressobits.mirrorcars.B2DVars.PPM;
  10. import br.com.expressobits.mirrorcars.MirrorGame;
  11. import br.com.expressobits.mirrorcars.Player;
  12. import br.com.expressobits.mirrorcars.Tiles;
  13. import br.com.expressobits.mirrorcars.handlers.BoundedCamera;
  14. import br.com.expressobits.mirrorcars.handlers.GameContactListener;
  15. import br.com.expressobits.mirrorcars.handlers.GameInput;
  16. import com.badlogic.gdx.Gdx;
  17. import com.badlogic.gdx.Screen;
  18. import com.badlogic.gdx.graphics.GL20;
  19. import com.badlogic.gdx.graphics.OrthographicCamera;
  20. import com.badlogic.gdx.graphics.g2d.BitmapFont;
  21. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  22. import com.badlogic.gdx.math.Vector2;
  23. import com.badlogic.gdx.physics.box2d.Body;
  24. import com.badlogic.gdx.physics.box2d.BodyDef;
  25. import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
  26. import com.badlogic.gdx.physics.box2d.FixtureDef;
  27. import com.badlogic.gdx.physics.box2d.PolygonShape;
  28. import com.badlogic.gdx.physics.box2d.World;
  29.  
  30. /**
  31.  *
  32.  * @author rafael
  33.  */
  34. public class GameScreen implements Screen{
  35.    
  36.     protected MirrorGame game;
  37.     private World world;
  38.     private Box2DDebugRenderer bddr;
  39.     private BoundedCamera b2dCam;
  40.     private BitmapFont bitmapFont;
  41.    
  42.     private Player player;
  43.     private Tiles tiles;
  44.    
  45.    
  46.    
  47.     public GameScreen(MirrorGame game) {
  48.         this.game = game;
  49.         this.bitmapFont = game.bitmapFont;
  50.        
  51.        
  52.         // set up the box2d world and contact listener
  53.         this.world = new World(Vector2.Zero, true);
  54.         this.world.setContactListener(new GameContactListener());
  55.         this.bddr = new Box2DDebugRenderer();
  56.         //new Wheel(world,150,200);
  57.        
  58.         //create player
  59.         tiles = new Tiles(world);
  60.         createPlayer();
  61.        
  62.         game.cam.setBounds(0, tiles.tileMapHeight * tiles.tileSize, 0, tiles.tileMapHeight * tiles.tileSize);
  63.        
  64.        
  65.         //SETUP BOX 2d
  66.         b2dCam = new BoundedCamera();
  67.         b2dCam.setToOrtho(false, MirrorGame.V_WIDTH / PPM, MirrorGame.V_HEIGHT / PPM);
  68.         b2dCam.setBounds(0, (tiles.tileMapHeight * tiles.tileSize) / PPM,
  69.                 0, (tiles.tileMapHeight * tiles.tileSize) / PPM);
  70.        
  71.     }
  72.    
  73.     public void update(float delta){
  74.         handleInput();
  75.         world.step(delta,6,2);
  76.        
  77.         game.cam.update();
  78.         player.update(delta);
  79.     }
  80.    
  81.     @Override
  82.     public void render(float delta){
  83.         Gdx.gl.glClearColor(0, 0, 0, 1);
  84.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  85.        
  86.         //Camera follow player
  87.         game.cam.setPosition(MirrorGame.V_WIDTH / 2,player.getPosition().y * PPM + MirrorGame.V_HEIGHT / 4);
  88.        
  89.         //Draw tilemap
  90.         tiles.render(game.cam);
  91.        
  92.         //draw player
  93.         game.spriteBatch.setProjectionMatrix(game.cam.combined);
  94.         player.render(game.spriteBatch);
  95.        
  96.        
  97.         //HUD
  98.         game.spriteBatch.setProjectionMatrix(game.hudCam.combined);
  99.         //hud.render(sb);
  100.         game.spriteBatch.begin();
  101.         game.bitmapFont.draw(game.spriteBatch,
  102.                 (int)player.getBody().getPosition().y+" METERS",
  103.                 10,
  104.                 20);
  105.         game.spriteBatch.end();
  106.        
  107.        
  108.         if(MirrorGame.debug){
  109.             bddr.render(world,game.cam.combined);
  110.             game.cam.position.set(game.cam.position.x,
  111.                 player.getBody().getPosition().y+MirrorGame.V_HEIGHT/4/PPM,0);
  112.            
  113.             game.spriteBatch.begin();
  114.         game.bitmapFont.draw(game.spriteBatch,"DEBUG MODE",10,40);
  115.         game.spriteBatch.end();
  116.         }
  117.         update(delta);
  118.        
  119.        
  120.        
  121.     }
  122.    
  123.     @Override
  124.     public void show() {
  125.     }
  126.  
  127.    
  128.  
  129.     @Override
  130.     public void resize(int width, int height) {
  131.     }
  132.  
  133.     @Override
  134.     public void pause() {
  135.     }
  136.  
  137.     @Override
  138.     public void resume() {
  139.     }
  140.  
  141.     @Override
  142.     public void hide() {
  143.     }
  144.  
  145.     @Override
  146.     public void dispose() {
  147.        
  148.     }
  149.  
  150.     private void handleInput() {
  151.        
  152.         if(GameInput.isPressed(GameInput.BUTTON_DEBUG)){
  153.             MirrorGame.debug= !MirrorGame.debug;
  154.         }
  155.        
  156.         if(GameInput.isDown(GameInput.BUTTON_LEFT)){
  157.             player.getBody().getPosition().x=player.getBody().getPosition().x-10;
  158.         }
  159.        
  160.         if(GameInput.isDown(GameInput.BUTTON_RIGHT)){
  161.             player.getBody().getPosition().x=player.getBody().getPosition().x=10;
  162.         }
  163.        
  164.         if(GameInput.isDown(GameInput.BUTTON_ACCELERATE)){
  165.             player.getBody().applyForce(0f,1000f,
  166.                     player.getBody().getPosition().x,player.getBody().getPosition().y,true);
  167.         }
  168.        
  169.     }
  170.    
  171.     public void createPlayer(){
  172.         BodyDef playerBodyDef = new BodyDef();
  173.         playerBodyDef.position.set(130/PPM,180/PPM);
  174.         playerBodyDef.type = BodyDef.BodyType.DynamicBody;
  175.         playerBodyDef.linearVelocity.set(0f,100f);
  176.        
  177.         Body body = world.createBody(playerBodyDef);
  178.        
  179.         PolygonShape shape = new PolygonShape();
  180.         shape.setAsBox(8/PPM,16/PPM);
  181.         FixtureDef playerFixtureDef = new FixtureDef();
  182.         playerFixtureDef.shape = shape;
  183.         playerFixtureDef.filter.categoryBits = B2DVars.BIT_PLAYER_CAR;
  184.         playerFixtureDef.filter.maskBits = B2DVars.BIT_STOP |B2DVars.BIT_NPC_CAR;
  185.         body.createFixture(playerFixtureDef).setUserData("player");
  186.         player = new Player(body);
  187.     }
  188.    
  189. }
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