Advertisement
Guest User

Untitled

a guest
Aug 10th, 2015
341
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import com.badlogic.gdx.Game;
  2. import com.badlogic.gdx.Gdx;
  3. import com.badlogic.gdx.Input.Keys;
  4. import com.badlogic.gdx.graphics.GL20;
  5. import com.badlogic.gdx.graphics.OrthographicCamera;
  6. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  7. import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
  8. import com.badlogic.gdx.math.Vector2;
  9. import com.badlogic.gdx.physics.box2d.Body;
  10. import com.badlogic.gdx.physics.box2d.BodyDef;
  11. import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
  12. import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
  13. import com.badlogic.gdx.physics.box2d.FixtureDef;
  14. import com.badlogic.gdx.physics.box2d.PolygonShape;
  15. import com.badlogic.gdx.physics.box2d.World;
  16.  
  17. public class GameLauncher extends Game {
  18.  
  19. public SpriteBatch batch;
  20. private World world;
  21. private OrthographicCamera camera;
  22. private Box2DDebugRenderer debugRenderer;
  23.  
  24. private Body player1, shadowPlayer1;
  25. private Body player2, shadowPlayer2;
  26.  
  27. public void create() {
  28. this.batch = new SpriteBatch();
  29. this.world = new World(new Vector2(0, 0), true);
  30. this.camera = new OrthographicCamera();
  31. this.camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  32. this.camera.update();
  33. //this.mapRenderer = new OrthogonalTiledMapRenderer(this.map);
  34.  
  35. BodyDef bodyDef = new BodyDef();
  36. bodyDef.type = BodyType.DynamicBody;
  37. bodyDef.position.set(20, 20);
  38. player1 = this.world.createBody(bodyDef);
  39. PolygonShape collider = new PolygonShape();
  40. collider.setAsBox(16, 16);
  41. FixtureDef fDef = new FixtureDef();
  42. fDef.density = 0f;
  43. fDef.friction = 0f;
  44. fDef.restitution = 0f;
  45. fDef.filter.groupIndex = -1;
  46. fDef.shape = collider;
  47. player1.createFixture(fDef);
  48.  
  49. bodyDef.type = BodyType.KinematicBody;
  50. bodyDef.position.set(20, 20);
  51. shadowPlayer1 = this.world.createBody(bodyDef);
  52. collider = new PolygonShape();
  53. collider.setAsBox(16, 16);
  54. fDef = new FixtureDef();
  55. fDef.density = 0f;
  56. fDef.friction = 0f;
  57. fDef.restitution = 0f;
  58. fDef.filter.groupIndex = -1;
  59. fDef.shape = collider;
  60. shadowPlayer1.createFixture(fDef);
  61.  
  62.  
  63. bodyDef.type = BodyType.DynamicBody;
  64. bodyDef.position.set(60, 60);
  65. player2 = this.world.createBody(bodyDef);
  66. collider = new PolygonShape();
  67. collider.setAsBox(16, 16);
  68. fDef = new FixtureDef();
  69. fDef.density = 0f;
  70. fDef.friction = 0f;
  71. fDef.restitution = 0f;
  72. fDef.filter.groupIndex = -2;
  73. fDef.shape = collider;
  74. player2.createFixture(fDef);
  75.  
  76. bodyDef.type = BodyType.KinematicBody;
  77. bodyDef.position.set(60, 60);
  78. shadowPlayer2 = this.world.createBody(bodyDef);
  79. collider = new PolygonShape();
  80. collider.setAsBox(16, 16);
  81. fDef = new FixtureDef();
  82. fDef.density = 0f;
  83. fDef.friction = 0f;
  84. fDef.restitution = 0f;
  85. fDef.filter.groupIndex = -2;
  86. fDef.shape = collider;
  87. shadowPlayer2.createFixture(fDef);
  88.  
  89. collider.dispose();
  90.  
  91. this.debugRenderer = new Box2DDebugRenderer();
  92. }
  93.  
  94. public void render() {
  95. Gdx.gl.glClearColor(0, 0, 1, 1);
  96. Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
  97. Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  98. this.player1.setLinearVelocity(0, 0);
  99. this.shadowPlayer1.setLinearVelocity(0, 0);
  100. if(Gdx.input.isKeyPressed(Keys.Z)) {
  101. //this.player1.setLinearVelocity(new Vector2(player1.getLinearVelocity().x, 30));
  102. this.shadowPlayer1.setLinearVelocity(new Vector2(player1.getLinearVelocity().x, 30));
  103. }
  104. if(Gdx.input.isKeyPressed(Keys.S)) {
  105. //this.player1.setLinearVelocity(new Vector2(player1.getLinearVelocity().x, -30));
  106. this.shadowPlayer1.setLinearVelocity(new Vector2(player1.getLinearVelocity().x, -30));
  107. }
  108. if(Gdx.input.isKeyPressed(Keys.Q)) {
  109. //this.player1.setLinearVelocity(new Vector2(-30, player1.getLinearVelocity().y));
  110. this.shadowPlayer1.setLinearVelocity(new Vector2(-30, player1.getLinearVelocity().y));
  111. }
  112. if(Gdx.input.isKeyPressed(Keys.D)) {
  113. //this.player1.setLinearVelocity(new Vector2(30, player1.getLinearVelocity().y));
  114. this.shadowPlayer1.setLinearVelocity(new Vector2(30, player1.getLinearVelocity().y));
  115. }
  116. this.player1.setTransform(this.shadowPlayer1.getPosition(), 0);
  117. this.player2.setTransform(this.shadowPlayer2.getPosition(), 0);
  118. this.world.step(1/60f, 6, 2);
  119. debugRenderer.render(this.world, camera.combined);
  120. this.camera.update();
  121. }
  122.  
  123. public void dispose() {
  124. batch.dispose();
  125. }
  126. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement