thw

Play File

thw
Jul 5th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.48 KB | None | 0 0
  1. package com.otg.irongun.states;
  2.  
  3. import net.dermetfan.gdx.graphics.g2d.AnimatedSprite;
  4.  
  5. import com.badlogic.gdx.Gdx;
  6. import com.badlogic.gdx.graphics.GL30;
  7. import com.badlogic.gdx.graphics.g2d.Animation;
  8. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  9. import com.badlogic.gdx.math.Vector2;
  10. import com.badlogic.gdx.math.Vector3;
  11. import com.badlogic.gdx.physics.box2d.Body;
  12. import com.badlogic.gdx.physics.box2d.BodyDef;
  13. import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
  14. import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
  15. import com.badlogic.gdx.physics.box2d.FixtureDef;
  16. import com.badlogic.gdx.physics.box2d.MassData;
  17. import com.badlogic.gdx.physics.box2d.PolygonShape;
  18. import com.badlogic.gdx.physics.box2d.World;
  19. import com.otg.irongun.IronGun;
  20. import com.otg.irongun.entities.HUD;
  21. import com.otg.irongun.entities.Player;
  22. import com.otg.irongun.handlers.InputProcessManager;
  23. import com.otg.irongun.handlers.IronGunContactListener;
  24. import com.otg.irongun.handlers.ProcessManager;
  25.  
  26. public class Play extends GameState {
  27.  
  28.     private World world, ui;
  29.     private Box2DDebugRenderer debugger;
  30.     private IronGunContactListener igCL;
  31.     private Player player;
  32.     private HUD hud;
  33.     private int tick;
  34.    
  35.     public Play(ProcessManager pm) {
  36.         super(pm);
  37.         tick = 0;
  38.         create();
  39.     }
  40.  
  41.     @Override
  42.     public void create() {
  43.         Vector2 fg = new Vector2(0, 0);
  44.         igCL = new IronGunContactListener();
  45.         this.world = new World(fg, true);
  46.         this.world.setContactListener(igCL);
  47.         this.ui = new World(new Vector2(0,0), true);
  48.  
  49.         createUI();
  50.         createPlayer(true);
  51.        
  52.         hud = new HUD(player);
  53.        
  54.         Gdx.input.setInputProcessor(new InputProcessManager(processManager, hudCam));
  55.        
  56.         if(IronGun.DEBUG)
  57.             debugger = new Box2DDebugRenderer();
  58.     }
  59.    
  60.     public void createPlayer(boolean isNew) {
  61.         if (isNew) {
  62.             BodyDef bdef = new BodyDef();
  63.             bdef.type = BodyType.DynamicBody;
  64.             bdef.position.set(100/IronGun.PPM,100/IronGun.PPM);
  65.             bdef.fixedRotation = true;
  66.            
  67.             Body body = world.createBody(bdef);
  68.            
  69.             PolygonShape shape = new PolygonShape();
  70.             shape.setAsBox(9.5f / IronGun.PPM, 16f / IronGun.PPM);
  71.            
  72.             FixtureDef fdef = new FixtureDef();
  73.             fdef.shape = shape;
  74.             fdef.density = 29;
  75.             fdef.friction = 0;
  76.            
  77.             body.createFixture(fdef);
  78.             shape.dispose();
  79.            
  80.             MassData md = body.getMassData();
  81.             md.mass = 93;
  82.             body.setMassData(md);
  83.            
  84.             TextureRegion texRegs[] = new TextureRegion[2];
  85.             texRegs[0] = new TextureRegion(atlas.findRegion("Robot"));
  86.             texRegs[1] = new TextureRegion(atlas.findRegion("Robot2"));
  87.             Animation animation = new Animation(1 / 3f, texRegs);
  88.             animation.setPlayMode(Animation.PlayMode.LOOP);
  89.             AnimatedSprite animatedSprite = new AnimatedSprite(animation);
  90.            
  91.             player = new Player(animatedSprite, body);
  92.             body.setUserData(player);
  93.            
  94.         } else {
  95.            
  96.         }
  97.        
  98.     }
  99.    
  100.     private void createUI() {
  101.         BodyDef bdef = new BodyDef();
  102.         bdef.type = BodyType.StaticBody;
  103.         bdef.position.set(32, 32);
  104.         bdef.fixedRotation = true;
  105.        
  106.         Body body = ui.createBody(bdef);
  107.         PolygonShape shape = new PolygonShape();
  108.         shape.setAsBox(16, 16);
  109.        
  110.         FixtureDef fdef = new FixtureDef();
  111.         fdef.shape = shape;
  112.         fdef.density = 0;
  113.         fdef.friction = 0;
  114.        
  115.         body.createFixture(fdef);
  116.         shape.dispose();
  117.        
  118.         body.setUserData("Button");
  119.     }
  120.    
  121.     @Override
  122.     public void update(float Δ) {     
  123.         world.step(IronGun.STEP, 1, 1);
  124.         ui.step(IronGun.STEP, 1, 1);
  125.        
  126.         player.update(Δ);
  127.        
  128.         if(tick == 60) {
  129.             Gdx.app.log("Player @: ", player.getBody().getPosition().x + ", " + player.getBody().getPosition().y);
  130.             tick = 0;
  131.         }
  132.         tick++;
  133.        
  134.     }
  135.  
  136.     @Override
  137.     public void render(float Δ) {
  138.         Gdx.gl.glClearColor(0, 0, 0, 1);
  139.         Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
  140.        
  141.         cam.position.set(new Vector3(player.getBody().getPosition().x * IronGun.PPM, player.getBody().getPosition().y * IronGun.PPM, 0));
  142.         cam.update();
  143.  
  144.         batch.setProjectionMatrix(cam.combined);
  145.        
  146.         batch.begin();
  147.         player.draw(batch, player.getBody());
  148.         batch.end();
  149.        
  150.         batch.setProjectionMatrix(hudCam.combined);
  151.         hud.render(batch);
  152.        
  153.         if(IronGun.DEBUG) {
  154.             debugCam.position.set(new Vector3(player.getBody().getPosition().x * IronGun.PPM, player.getBody().getPosition().y * IronGun.PPM, 0));
  155.             debugCam.update();
  156.             debugger.render(world, debugCam.combined);
  157.             debugger.render(ui, hudCam.combined);
  158.         }
  159.     }
  160.  
  161.     @Override
  162.     public void dispose() {
  163.         // TODO Auto-generated method stub
  164.        
  165.     }
  166.    
  167.     public Player getPlayer() {
  168.         return player;
  169.     }
  170.  
  171. }
Advertisement
Add Comment
Please, Sign In to add comment