Advertisement
Guest User

Untitled

a guest
Aug 12th, 2015
389
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.math.Vector2;
  8. import com.badlogic.gdx.physics.box2d.Body;
  9. import com.badlogic.gdx.physics.box2d.BodyDef;
  10. import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
  11. import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
  12. import com.badlogic.gdx.physics.box2d.Contact;
  13. import com.badlogic.gdx.physics.box2d.ContactImpulse;
  14. import com.badlogic.gdx.physics.box2d.ContactListener;
  15. import com.badlogic.gdx.physics.box2d.FixtureDef;
  16. import com.badlogic.gdx.physics.box2d.Manifold;
  17. import com.badlogic.gdx.physics.box2d.PolygonShape;
  18. import com.badlogic.gdx.physics.box2d.World;
  19.  
  20. public class GameLauncher extends Game {
  21.  
  22. public SpriteBatch batch;
  23. private World world;
  24. private OrthographicCamera camera;
  25. private Box2DDebugRenderer debugRenderer;
  26.  
  27. private Body player1, shadowPlayer1;
  28. private Body player2, shadowPlayer2;
  29.  
  30. public short CATEGORY_PLAYER_1 = 0x0002;
  31. public short CATEGORY_SHADOW_1 = 0x0004;
  32. public short CATEGORY_PLAYER_2 = 0x0008;
  33. public short CATEGORY_SHADOW_2 = 0x0010;
  34.  
  35. public void create() {
  36. this.batch = new SpriteBatch();
  37. this.world = new World(new Vector2(0, 0), true);
  38. this.camera = new OrthographicCamera();
  39. this.camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  40. this.camera.update();
  41. //this.mapRenderer = new OrthogonalTiledMapRenderer(this.map);
  42.  
  43. BodyDef bodyDef = new BodyDef();
  44. bodyDef.type = BodyType.DynamicBody;
  45. bodyDef.position.set(80, 20);
  46. player1 = this.world.createBody(bodyDef);
  47. PolygonShape collider = new PolygonShape();
  48. collider.setAsBox(16, 16);
  49. FixtureDef fDef = new FixtureDef();
  50. fDef.density = 0f;
  51. fDef.friction = 0f;
  52. fDef.restitution = 0f;
  53. //fDef.filter.groupIndex = -1;
  54. fDef.filter.categoryBits = CATEGORY_PLAYER_1;
  55. fDef.filter.maskBits = (short) (0xFFFF - CATEGORY_SHADOW_1 - CATEGORY_PLAYER_1 - CATEGORY_PLAYER_2);
  56. fDef.shape = collider;
  57. player1.setUserData("player");
  58. player1.createFixture(fDef);
  59.  
  60. bodyDef.type = BodyType.KinematicBody;
  61. bodyDef.position.set(20, 20);
  62. shadowPlayer1 = this.world.createBody(bodyDef);
  63. collider = new PolygonShape();
  64. collider.setAsBox(16, 16);
  65. fDef = new FixtureDef();
  66. fDef.density = 0f;
  67. fDef.friction = 0f;
  68. fDef.restitution = 0f;
  69. //fDef.filter.groupIndex = -2;
  70. fDef.filter.categoryBits = CATEGORY_SHADOW_1;
  71. fDef.filter.maskBits = (short) 0xFFFF;
  72. fDef.shape = collider;
  73. shadowPlayer1.createFixture(fDef);
  74.  
  75.  
  76. bodyDef.type = BodyType.DynamicBody;
  77. bodyDef.position.set(20, 60);
  78. player2 = 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.filter.categoryBits = CATEGORY_PLAYER_2;
  87. fDef.filter.maskBits = (short) (0xFFFF - CATEGORY_SHADOW_2 - CATEGORY_PLAYER_1 - CATEGORY_PLAYER_2);
  88. fDef.shape = collider;
  89. player2.setUserData("player");
  90. player2.createFixture(fDef);
  91.  
  92. bodyDef.type = BodyType.KinematicBody;
  93. bodyDef.position.set(80, 60);
  94. shadowPlayer2 = this.world.createBody(bodyDef);
  95. collider = new PolygonShape();
  96. collider.setAsBox(16, 16);
  97. fDef = new FixtureDef();
  98. fDef.density = 0f;
  99. fDef.friction = 0f;
  100. fDef.restitution = 0f;
  101. //fDef.filter.groupIndex = -1;
  102. fDef.filter.categoryBits = CATEGORY_SHADOW_2;
  103. fDef.filter.maskBits = (short) 0xFFFF;
  104. fDef.shape = collider;
  105. shadowPlayer2.createFixture(fDef);
  106.  
  107. collider.dispose();
  108.  
  109. this.debugRenderer = new Box2DDebugRenderer();
  110. }
  111.  
  112. public void render() {
  113. Gdx.gl.glClearColor(0, 0, 1, 1);
  114. Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
  115. Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  116. this.player1.setLinearVelocity(0, 0);
  117. this.shadowPlayer1.setLinearVelocity(0, 0);
  118. if(Gdx.input.isKeyPressed(Keys.Z)) {
  119. this.player1.setLinearVelocity(new Vector2(player1.getLinearVelocity().x, 30));
  120. }
  121. if(Gdx.input.isKeyPressed(Keys.S)) {
  122. this.player1.setLinearVelocity(new Vector2(player1.getLinearVelocity().x, -30));
  123. }
  124. if(Gdx.input.isKeyPressed(Keys.Q)) {
  125. this.player1.setLinearVelocity(new Vector2(-30, player1.getLinearVelocity().y));
  126. }
  127. if(Gdx.input.isKeyPressed(Keys.D)) {
  128. this.player1.setLinearVelocity(new Vector2(30, player1.getLinearVelocity().y));
  129. }
  130. if(Gdx.input.isKeyPressed(Keys.UP)) {
  131. this.player2.setLinearVelocity(new Vector2(player1.getLinearVelocity().x, 30));
  132. }
  133. if(Gdx.input.isKeyPressed(Keys.DOWN)) {
  134. this.player2.setLinearVelocity(new Vector2(player1.getLinearVelocity().x, -30));
  135. }
  136. if(Gdx.input.isKeyPressed(Keys.LEFT)) {
  137. this.player2.setLinearVelocity(new Vector2(-30, player1.getLinearVelocity().y));
  138. }
  139. if(Gdx.input.isKeyPressed(Keys.RIGHT)) {
  140. this.player2.setLinearVelocity(new Vector2(30, player1.getLinearVelocity().y));
  141. }
  142. this.shadowPlayer1.setTransform(this.player1.getPosition(), 0);
  143. this.shadowPlayer2.setTransform(this.player2.getPosition(), 0);
  144. this.world.step(1/60f, 6, 2);
  145. debugRenderer.render(this.world, camera.combined);
  146. this.camera.update();
  147. }
  148.  
  149. public void dispose() {
  150. batch.dispose();
  151. }
  152. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement