Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.92 KB | None | 0 0
  1. if ((fixtureB == obstacleFixture) && (fixtureA == shootFixture)) {
  2. // destroy();
  3. obstacleBody.destroyFixture(obstacleFixture);
  4.  
  5. }
  6.  
  7. File: /var/lib/jenkins/workspace/libgdx/extensions/gdx-box2d/gdx-box2d/jni/Box2D/Dynamics/b2Body.cpp, Line 216
  8.  
  9. Expression: m_world->IsLocked() == false
  10.  
  11. public class Box2DScreen extends MyScreenAdapter {
  12. private World world;
  13. private Box2DDebugRenderer renderer;
  14. private OrthographicCamera camara;
  15.  
  16. private Body playerBody;
  17. private Fixture playerFixture;
  18.  
  19. private Body shootBody;
  20. private Fixture shootFixture;
  21. private Body floorBody;
  22. private Fixture floorFixture;
  23. private Body obstacleBody;
  24. private Fixture obstacleFixture;
  25.  
  26. private boolean mustJump = false;
  27. private boolean isShooting = false;
  28. private boolean jumping = false;
  29. private boolean shooting = false;
  30. private boolean playerLive = true;
  31.  
  32. public Box2DScreen(MainGame game) {
  33. super(game);
  34. }
  35.  
  36. @Override
  37. public void show() {
  38. world = new World(new Vector2(0, -10), true);
  39. renderer = new Box2DDebugRenderer();
  40. camara = new OrthographicCamera(16.00f, 9.00f);
  41. camara.translate(0, 1);
  42. world.setContactListener(new ContactListener() {
  43. @Override
  44. public void beginContact(Contact contact) {
  45. Fixture fixtureA = contact.getFixtureA();
  46. Fixture fixtureB = contact.getFixtureB();
  47.  
  48. if ((fixtureA.getUserData().equals("player")) && (fixtureB.getUserData().equals("floor"))) {
  49. if (Gdx.input.isTouched()) {
  50. mustJump = true;
  51. }
  52. jumping = false;
  53. }
  54.  
  55. if ((fixtureA.getUserData().equals("floor")) && (fixtureB.getUserData().equals("player"))) {
  56. if (Gdx.input.isTouched()) {
  57. mustJump = true;
  58. }
  59. jumping = false;
  60. }
  61. if ((fixtureA.getUserData().equals("player")) && (fixtureB.getUserData().equals("obstacle"))) {
  62. if (Gdx.input.isTouched()) {
  63. playerLive = false;
  64. }
  65. }
  66. if ((fixtureA.getUserData().equals("obstacle")) && (fixtureB.getUserData().equals("player"))) {
  67. if (Gdx.input.isTouched()) {
  68. playerLive = false;
  69. }
  70. }
  71.  
  72. if ((fixtureA.getUserData().equals("obstacle")) && (fixtureB.getUserData().equals("shoot"))) {
  73. //destroy();
  74. obstacleBody.destroyFixture(obstacleFixture);
  75.  
  76. }
  77. }
  78.  
  79. @Override
  80. public void endContact(Contact contact) {
  81. Fixture fixtureA = contact.getFixtureA();
  82. Fixture fixtureB = contact.getFixtureB();
  83.  
  84. if ((fixtureA == playerFixture) && (fixtureB == floorFixture)) {
  85. jumping = true;
  86. }
  87. if ((fixtureB == playerFixture) && (fixtureA == floorFixture)) {
  88. jumping = true;
  89. }
  90.  
  91. if ((fixtureA == shootFixture) && (fixtureB == obstacleFixture)) {
  92. // destroy();
  93.  
  94. }
  95. if ((fixtureB == obstacleFixture) && (fixtureA == shootFixture)) {
  96. // destroy();
  97.  
  98. }
  99.  
  100. }
  101.  
  102. @Override
  103. public void preSolve(Contact contact, Manifold oldManifold) {
  104.  
  105. }
  106.  
  107. @Override
  108. public void postSolve(Contact contact, ContactImpulse impulse) {
  109.  
  110. }
  111. });
  112. BodyDef playerBodyDef;
  113. playerBodyDef = createPlayerBodyDef();
  114. playerBody = world.createBody(playerBodyDef);
  115. PolygonShape playerShape = new PolygonShape();
  116. playerShape.setAsBox(0.5f, 0.5f);
  117. playerFixture = playerBody.createFixture(playerShape, 1);
  118. playerShape.dispose();
  119.  
  120. BodyDef floorBodyDef;
  121. floorBodyDef = createFloorBodyDef();
  122. floorBody = world.createBody(floorBodyDef);
  123. PolygonShape floorShape = new PolygonShape();
  124. floorShape.setAsBox(500.00f, 1.00f);
  125. floorFixture = floorBody.createFixture(floorShape, 1);
  126. floorShape.dispose();
  127.  
  128. BodyDef obstacleBodyDef;
  129. obstacleBodyDef = createObstacleBodyDef(6f);
  130. obstacleBody = world.createBody(obstacleBodyDef);
  131. obstacleFixture = createObstacleFixture(obstacleBody);
  132.  
  133.  
  134. playerFixture.setUserData("player");
  135. floorFixture.setUserData("floor");
  136. obstacleFixture.setUserData("obstacle");
  137. }
  138.  
  139.  
  140. private BodyDef createShootBodyDef() {
  141. BodyDef def = new BodyDef();
  142. def.position.set(playerBody.getPosition().x + 1f, playerBody.getPosition().y);
  143. def.type = BodyDef.BodyType.DynamicBody;
  144. def.gravityScale = 0;
  145. return def;
  146. }
  147.  
  148. private Fixture createObstacleFixture(Body obstacle) {
  149. Vector2[] vertices = new Vector2[3];
  150. vertices[0] = new Vector2(-0.5f, -0.5f);
  151. vertices[1] = new Vector2(0.5f, -0.5f);
  152. vertices[2] = new Vector2(0, 0.5f);
  153.  
  154. PolygonShape shape = new PolygonShape();
  155. shape.set(vertices);
  156.  
  157. Fixture fix = obstacle.createFixture(shape, 1);
  158. shape.dispose();
  159. return fix;
  160. }
  161.  
  162. private BodyDef createObstacleBodyDef(float x) {
  163. BodyDef def = new BodyDef();
  164. def.position.set(x, 0.5f);
  165. def.type = BodyDef.BodyType.StaticBody;
  166.  
  167. return def;
  168. }
  169.  
  170. private BodyDef createPlayerBodyDef() {
  171. BodyDef def = new BodyDef();
  172. def.position.set(0, 0.5f);
  173. def.type = BodyDef.BodyType.DynamicBody;
  174. return def;
  175. }
  176.  
  177. private BodyDef createFloorBodyDef() {
  178. BodyDef def = new BodyDef();
  179. def.position.set(0, -1);
  180. def.type = BodyDef.BodyType.StaticBody;
  181. return def;
  182. }
  183.  
  184. @Override
  185. public void dispose() {
  186. playerBody.destroyFixture(playerFixture);
  187. floorBody.destroyFixture(floorFixture);
  188. obstacleBody.destroyFixture(obstacleFixture);
  189. shootBody.destroyFixture(shootFixture);
  190.  
  191. world.destroyBody(playerBody);
  192. world.destroyBody(floorBody);
  193. world.destroyBody(obstacleBody);
  194. world.destroyBody(shootBody);
  195.  
  196. world.dispose();
  197. renderer.dispose();
  198.  
  199. }
  200.  
  201. @Override
  202. public void render(float delta) {
  203. Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  204.  
  205. if (mustJump) {
  206. mustJump = false;
  207. saltar();
  208. }
  209. if (Gdx.input.justTouched() && !jumping) {
  210. mustJump = true;
  211. }
  212. if (Gdx.input.justTouched() && shooting) {
  213. destroy();
  214. }
  215.  
  216. if (Gdx.input.justTouched() && !shooting) {
  217. BodyDef shootBodyDef;
  218. shootBodyDef = createShootBodyDef();
  219. shootBody = world.createBody(shootBodyDef);
  220. PolygonShape shootShape = new PolygonShape();
  221. shootShape.setAsBox(0.3f, 0.09f);
  222. shootFixture = shootBody.createFixture(shootShape, 1);
  223. shootShape.dispose();
  224. shootFixture.setUserData("shoot");
  225. shooting = true;
  226. }
  227.  
  228.  
  229. if (shooting) {
  230. float velocidadY = shootBody.getLinearVelocity().y;
  231. shootBody.setLinearVelocity(6, velocidadY);
  232. }
  233. if (playerLive) {
  234. float velocidadY = playerBody.getLinearVelocity().y;
  235. playerBody.setLinearVelocity((float) 0.7, velocidadY);
  236. }
  237.  
  238.  
  239. world.step(delta, 6, 2);
  240. camara.update();
  241. renderer.render(world, camara.combined);
  242. }
  243.  
  244. private void saltar() {
  245. Vector2 position = playerBody.getPosition();
  246. playerBody.applyLinearImpulse(0, 6, position.x, position.y, true);
  247. }
  248. private void destroy(){
  249. shooting = false;
  250. shootBody.destroyFixture(shootFixture);
  251. world.destroyBody(shootBody);
  252. }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement