Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.otg.irongun.states;
- import net.dermetfan.gdx.graphics.g2d.AnimatedSprite;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.graphics.GL30;
- import com.badlogic.gdx.graphics.g2d.Animation;
- import com.badlogic.gdx.graphics.g2d.TextureRegion;
- import com.badlogic.gdx.math.Vector2;
- import com.badlogic.gdx.math.Vector3;
- import com.badlogic.gdx.physics.box2d.Body;
- import com.badlogic.gdx.physics.box2d.BodyDef;
- import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
- import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
- import com.badlogic.gdx.physics.box2d.FixtureDef;
- import com.badlogic.gdx.physics.box2d.MassData;
- import com.badlogic.gdx.physics.box2d.PolygonShape;
- import com.badlogic.gdx.physics.box2d.World;
- import com.otg.irongun.IronGun;
- import com.otg.irongun.entities.HUD;
- import com.otg.irongun.entities.Player;
- import com.otg.irongun.handlers.InputProcessManager;
- import com.otg.irongun.handlers.IronGunContactListener;
- import com.otg.irongun.handlers.ProcessManager;
- public class Play extends GameState {
- private World world, ui;
- private Box2DDebugRenderer debugger;
- private IronGunContactListener igCL;
- private Player player;
- private HUD hud;
- private int tick;
- public Play(ProcessManager pm) {
- super(pm);
- tick = 0;
- create();
- }
- @Override
- public void create() {
- Vector2 fg = new Vector2(0, 0);
- igCL = new IronGunContactListener();
- this.world = new World(fg, true);
- this.world.setContactListener(igCL);
- this.ui = new World(new Vector2(0,0), true);
- createUI();
- createPlayer(true);
- hud = new HUD(player);
- Gdx.input.setInputProcessor(new InputProcessManager(processManager, hudCam));
- if(IronGun.DEBUG)
- debugger = new Box2DDebugRenderer();
- }
- public void createPlayer(boolean isNew) {
- if (isNew) {
- BodyDef bdef = new BodyDef();
- bdef.type = BodyType.DynamicBody;
- bdef.position.set(100/IronGun.PPM,100/IronGun.PPM);
- bdef.fixedRotation = true;
- Body body = world.createBody(bdef);
- PolygonShape shape = new PolygonShape();
- shape.setAsBox(9.5f / IronGun.PPM, 16f / IronGun.PPM);
- FixtureDef fdef = new FixtureDef();
- fdef.shape = shape;
- fdef.density = 29;
- fdef.friction = 0;
- body.createFixture(fdef);
- shape.dispose();
- MassData md = body.getMassData();
- md.mass = 93;
- body.setMassData(md);
- TextureRegion texRegs[] = new TextureRegion[2];
- texRegs[0] = new TextureRegion(atlas.findRegion("Robot"));
- texRegs[1] = new TextureRegion(atlas.findRegion("Robot2"));
- Animation animation = new Animation(1 / 3f, texRegs);
- animation.setPlayMode(Animation.PlayMode.LOOP);
- AnimatedSprite animatedSprite = new AnimatedSprite(animation);
- player = new Player(animatedSprite, body);
- body.setUserData(player);
- } else {
- }
- }
- private void createUI() {
- BodyDef bdef = new BodyDef();
- bdef.type = BodyType.StaticBody;
- bdef.position.set(32, 32);
- bdef.fixedRotation = true;
- Body body = ui.createBody(bdef);
- PolygonShape shape = new PolygonShape();
- shape.setAsBox(16, 16);
- FixtureDef fdef = new FixtureDef();
- fdef.shape = shape;
- fdef.density = 0;
- fdef.friction = 0;
- body.createFixture(fdef);
- shape.dispose();
- body.setUserData("Button");
- }
- @Override
- public void update(float Δ) {
- world.step(IronGun.STEP, 1, 1);
- ui.step(IronGun.STEP, 1, 1);
- player.update(Δ);
- if(tick == 60) {
- Gdx.app.log("Player @: ", player.getBody().getPosition().x + ", " + player.getBody().getPosition().y);
- tick = 0;
- }
- tick++;
- }
- @Override
- public void render(float Δ) {
- Gdx.gl.glClearColor(0, 0, 0, 1);
- Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
- cam.position.set(new Vector3(player.getBody().getPosition().x * IronGun.PPM, player.getBody().getPosition().y * IronGun.PPM, 0));
- cam.update();
- batch.setProjectionMatrix(cam.combined);
- batch.begin();
- player.draw(batch, player.getBody());
- batch.end();
- batch.setProjectionMatrix(hudCam.combined);
- hud.render(batch);
- if(IronGun.DEBUG) {
- debugCam.position.set(new Vector3(player.getBody().getPosition().x * IronGun.PPM, player.getBody().getPosition().y * IronGun.PPM, 0));
- debugCam.update();
- debugger.render(world, debugCam.combined);
- debugger.render(ui, hudCam.combined);
- }
- }
- @Override
- public void dispose() {
- // TODO Auto-generated method stub
- }
- public Player getPlayer() {
- return player;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment