Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mygdx.game;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Input;
- import com.badlogic.gdx.Screen;
- import com.badlogic.gdx.graphics.GL20;
- import com.badlogic.gdx.graphics.OrthographicCamera;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.maps.Map;
- import com.badlogic.gdx.maps.MapObject;
- import com.badlogic.gdx.maps.objects.RectangleMapObject;
- import com.badlogic.gdx.maps.tiled.TiledMap;
- import com.badlogic.gdx.maps.tiled.TmxMapLoader;
- import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
- import com.badlogic.gdx.math.Rectangle;
- 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;
- import com.badlogic.gdx.utils.viewport.FitViewport;
- import com.badlogic.gdx.utils.viewport.Viewport;
- import com.mygdx.game.Scenes.Hud;
- import com.mygdx.game.Sprites.Bery;
- import com.sun.jndi.ldap.Ber;
- public class PlayScreen implements Screen {
- private BeriGame game;
- private OrthographicCamera cam;
- private SpriteBatch batch;
- Texture texture;
- private Viewport gamePort;
- private Hud hud;
- private Bery bery;
- private TmxMapLoader mapLoader;
- private OrthogonalTiledMapRenderer renderer;
- private TiledMap map;
- private World world;
- private Box2DDebugRenderer b2dr;
- public PlayScreen(BeriGame game) {
- this.game = game;
- texture = new Texture("badlogic.jpg");
- cam = new OrthographicCamera();
- gamePort = new FitViewport(BeriGame.V_WIDTH / BeriGame.PPM, BeriGame.V_HEIGHT / BeriGame.PPM, cam);
- hud = new Hud(game.batch);
- mapLoader = new TmxMapLoader();
- map = mapLoader.load("bery_map/beryMap.tmx");
- renderer = new OrthogonalTiledMapRenderer(map, 1 / BeriGame.PPM);
- cam.position.set(gamePort.getWorldWidth()/2, gamePort.getWorldHeight()/2, 0);
- world = new World(new Vector2(0, -10), true);
- b2dr = new Box2DDebugRenderer();
- BodyDef bdef = new BodyDef();
- PolygonShape shape = new PolygonShape();
- FixtureDef fdef = new FixtureDef();
- Body body;
- for(MapObject object : map.getLayers().get(2).getObjects().getByType(RectangleMapObject.class)) {
- Rectangle rect = ((RectangleMapObject) object).getRectangle();
- bdef.type = BodyDef.BodyType.StaticBody;
- bdef.position.set((rect.getX()+rect.getWidth()/2)/ BeriGame.PPM, (rect.getY()+rect.getHeight()/2) / BeriGame.PPM);
- body = world.createBody(bdef);
- shape.setAsBox(rect.getWidth()/2 / BeriGame.PPM, rect.getHeight()/2/ BeriGame.PPM);
- fdef.shape = shape;
- body.createFixture(fdef);
- }
- for(MapObject object : map.getLayers().get(3).getObjects().getByType(RectangleMapObject.class)) {
- Rectangle rect = ((RectangleMapObject) object).getRectangle();
- bdef.type = BodyDef.BodyType.StaticBody; // should be dynamic
- bdef.position.set((rect.getX()+rect.getWidth()/2)/ BeriGame.PPM, (rect.getY()+rect.getHeight()/2) / BeriGame.PPM);
- body = world.createBody(bdef);
- shape.setAsBox(rect.getWidth()/2 / BeriGame.PPM, rect.getHeight()/2/ BeriGame.PPM);
- fdef.shape = shape;
- body.createFixture(fdef);
- }
- bery = new Bery(world);
- }
- public void handleInput(float dt) {
- if(Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
- if (Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT))
- cam.position.x += 100 * dt;
- }
- if(Gdx.input.isTouched())
- cam.position.x += 100 * dt;
- }
- public void update(float dt) {
- handleInput(dt);
- world.step(1/60f, 6, 2);
- cam.update();
- renderer.setView(cam);
- }
- @Override
- public void show() {
- }
- @Override
- public void render(float delta) {
- update(delta);
- Gdx.gl.glClearColor(0, 0, 0, 1);
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
- renderer.render();
- b2dr.render(world, cam.combined);
- game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
- hud.stage.draw();
- }
- @Override
- public void resize(int width, int height) {
- gamePort.update(width, height);
- }
- @Override
- public void pause() {
- }
- @Override
- public void resume() {
- }
- @Override
- public void hide() {
- }
- @Override
- public void dispose() {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement