Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package br.com.expressobits.mirrorcars.screens;
- import br.com.expressobits.mirrorcars.B2DVars;
- import static br.com.expressobits.mirrorcars.B2DVars.PPM;
- import br.com.expressobits.mirrorcars.MirrorGame;
- import br.com.expressobits.mirrorcars.Player;
- import br.com.expressobits.mirrorcars.Tiles;
- import br.com.expressobits.mirrorcars.handlers.BoundedCamera;
- import br.com.expressobits.mirrorcars.handlers.GameContactListener;
- import br.com.expressobits.mirrorcars.handlers.GameInput;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Screen;
- import com.badlogic.gdx.graphics.GL20;
- import com.badlogic.gdx.graphics.OrthographicCamera;
- import com.badlogic.gdx.graphics.g2d.BitmapFont;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.math.Vector2;
- import com.badlogic.gdx.physics.box2d.Body;
- import com.badlogic.gdx.physics.box2d.BodyDef;
- import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
- import com.badlogic.gdx.physics.box2d.FixtureDef;
- import com.badlogic.gdx.physics.box2d.PolygonShape;
- import com.badlogic.gdx.physics.box2d.World;
- /**
- *
- * @author rafael
- */
- public class GameScreen implements Screen{
- protected MirrorGame game;
- private World world;
- private Box2DDebugRenderer bddr;
- private BoundedCamera b2dCam;
- private BitmapFont bitmapFont;
- private Player player;
- private Tiles tiles;
- public GameScreen(MirrorGame game) {
- this.game = game;
- this.bitmapFont = game.bitmapFont;
- // set up the box2d world and contact listener
- this.world = new World(Vector2.Zero, true);
- this.world.setContactListener(new GameContactListener());
- this.bddr = new Box2DDebugRenderer();
- //new Wheel(world,150,200);
- //create player
- tiles = new Tiles(world);
- createPlayer();
- game.cam.setBounds(0, tiles.tileMapHeight * tiles.tileSize, 0, tiles.tileMapHeight * tiles.tileSize);
- //SETUP BOX 2d
- b2dCam = new BoundedCamera();
- b2dCam.setToOrtho(false, MirrorGame.V_WIDTH / PPM, MirrorGame.V_HEIGHT / PPM);
- b2dCam.setBounds(0, (tiles.tileMapHeight * tiles.tileSize) / PPM,
- 0, (tiles.tileMapHeight * tiles.tileSize) / PPM);
- }
- public void update(float delta){
- handleInput();
- world.step(delta,6,2);
- game.cam.update();
- player.update(delta);
- }
- @Override
- public void render(float delta){
- Gdx.gl.glClearColor(0, 0, 0, 1);
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
- //Camera follow player
- game.cam.setPosition(MirrorGame.V_WIDTH / 2,player.getPosition().y * PPM + MirrorGame.V_HEIGHT / 4);
- //Draw tilemap
- tiles.render(game.cam);
- //draw player
- game.spriteBatch.setProjectionMatrix(game.cam.combined);
- player.render(game.spriteBatch);
- //HUD
- game.spriteBatch.setProjectionMatrix(game.hudCam.combined);
- //hud.render(sb);
- game.spriteBatch.begin();
- game.bitmapFont.draw(game.spriteBatch,
- (int)player.getBody().getPosition().y+" METERS",
- 10,
- 20);
- game.spriteBatch.end();
- if(MirrorGame.debug){
- bddr.render(world,game.cam.combined);
- game.cam.position.set(game.cam.position.x,
- player.getBody().getPosition().y+MirrorGame.V_HEIGHT/4/PPM,0);
- game.spriteBatch.begin();
- game.bitmapFont.draw(game.spriteBatch,"DEBUG MODE",10,40);
- game.spriteBatch.end();
- }
- update(delta);
- }
- @Override
- public void show() {
- }
- @Override
- public void resize(int width, int height) {
- }
- @Override
- public void pause() {
- }
- @Override
- public void resume() {
- }
- @Override
- public void hide() {
- }
- @Override
- public void dispose() {
- }
- private void handleInput() {
- if(GameInput.isPressed(GameInput.BUTTON_DEBUG)){
- MirrorGame.debug= !MirrorGame.debug;
- }
- if(GameInput.isDown(GameInput.BUTTON_LEFT)){
- player.getBody().getPosition().x=player.getBody().getPosition().x-10;
- }
- if(GameInput.isDown(GameInput.BUTTON_RIGHT)){
- player.getBody().getPosition().x=player.getBody().getPosition().x=10;
- }
- if(GameInput.isDown(GameInput.BUTTON_ACCELERATE)){
- player.getBody().applyForce(0f,1000f,
- player.getBody().getPosition().x,player.getBody().getPosition().y,true);
- }
- }
- public void createPlayer(){
- BodyDef playerBodyDef = new BodyDef();
- playerBodyDef.position.set(130/PPM,180/PPM);
- playerBodyDef.type = BodyDef.BodyType.DynamicBody;
- playerBodyDef.linearVelocity.set(0f,100f);
- Body body = world.createBody(playerBodyDef);
- PolygonShape shape = new PolygonShape();
- shape.setAsBox(8/PPM,16/PPM);
- FixtureDef playerFixtureDef = new FixtureDef();
- playerFixtureDef.shape = shape;
- playerFixtureDef.filter.categoryBits = B2DVars.BIT_PLAYER_CAR;
- playerFixtureDef.filter.maskBits = B2DVars.BIT_STOP |B2DVars.BIT_NPC_CAR;
- body.createFixture(playerFixtureDef).setUserData("player");
- player = new Player(body);
- }
- }
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.


