diff --git a/frontend/core/src/com/game/tankwars/CollisionDetection.java b/frontend/core/src/com/game/tankwars/CollisionDetection.java new file mode 100644 index 0000000000000000000000000000000000000000..8fed826d8f0e63f34a8fd617647b00069e2605e5 --- /dev/null +++ b/frontend/core/src/com/game/tankwars/CollisionDetection.java @@ -0,0 +1,60 @@ +package com.game.tankwars; + +import com.badlogic.gdx.physics.box2d.Body; +import com.badlogic.gdx.physics.box2d.BodyDef; +import com.badlogic.gdx.physics.box2d.Contact; +import com.badlogic.gdx.physics.box2d.ContactImpulse; +import com.badlogic.gdx.physics.box2d.ContactListener; +import com.badlogic.gdx.physics.box2d.Fixture; +import com.badlogic.gdx.physics.box2d.Manifold; +import com.badlogic.gdx.physics.box2d.World; +import com.game.tankwars.model.Box2dWorld; +import com.game.tankwars.model.FixtureData; + +public class CollisionDetection implements ContactListener { + + World world; + + public CollisionDetection() { + this.world = Box2dWorld.getWorld(); + } + + @Override + public void beginContact(Contact contact) { + Fixture fixA = contact.getFixtureA(); + Fixture fixB = contact.getFixtureB(); + FixtureData dataA = (FixtureData) fixA.getUserData(); + FixtureData dataB = (FixtureData) fixB.getUserData(); + + if (dataA.getId().equals("userTank") && dataB.getId().equals("bullet")) { + System.out.println("The bullet has hit the player's tank"); + dataB.hasCollided(); + } + + else if (dataA.getId().equals("opponentTank") && dataB.getId().equals("bullet")) { + System.out.println("The bullet has hit the opponent's tank"); + dataB.hasCollided(); + } + + else if (dataA.getId().equals("terrain") && dataB.getId().equals("bullet")) { + System.out.println("The bullet has hit the ground!"); + dataB.hasCollided(); + + } + } + + @Override + public void endContact(Contact contact) { + + } + + @Override + public void preSolve(Contact contact, Manifold oldManifold) { + + } + + @Override + public void postSolve(Contact contact, ContactImpulse impulse) { + + } +} diff --git a/frontend/core/src/com/game/tankwars/model/Box2dWorld.java b/frontend/core/src/com/game/tankwars/model/Box2dWorld.java index b012dc18b9ea6ae6852873b37c6a8e7ee1b7a887..2676877e51c4515547685f038e5eb713f5badad8 100644 --- a/frontend/core/src/com/game/tankwars/model/Box2dWorld.java +++ b/frontend/core/src/com/game/tankwars/model/Box2dWorld.java @@ -1,10 +1,14 @@ package com.game.tankwars.model; import com.badlogic.gdx.math.Vector2; +import com.badlogic.gdx.physics.box2d.Body; import com.badlogic.gdx.physics.box2d.World; +import com.badlogic.gdx.utils.Array; public class Box2dWorld { + private Array<Body> deadBodies = new Array<>(); + private static final World world = new World(new Vector2(0, -10), true); public void logicStep(float dt) { @@ -15,5 +19,18 @@ public class Box2dWorld { return world; } + public void addDeadBody(Body body) { + deadBodies.add(body); + } + + public void destroyDeadBodies() { + if (deadBodies.size > 0) { + for (Body b : deadBodies) { + world.destroyBody(b); + } + deadBodies.clear(); + } + } + } diff --git a/frontend/core/src/com/game/tankwars/model/Bullet.java b/frontend/core/src/com/game/tankwars/model/Bullet.java index 23dd7c38a2a004c34c0c892f65864430807365ae..13556789910cca9e0cba65db263286eb5b0cb3ba 100644 --- a/frontend/core/src/com/game/tankwars/model/Bullet.java +++ b/frontend/core/src/com/game/tankwars/model/Bullet.java @@ -24,6 +24,7 @@ public class Bullet { private Sprite bulletSprite; private float angle; + private boolean collision = false; public Bullet(Tank tank) { this.tank = tank; @@ -40,6 +41,7 @@ public class Bullet { fixtureDef.restitution = 0.0f; fixtureDef.friction = 1f; body.createFixture(fixtureDef); + body.getFixtureList().get(0).setUserData(new FixtureData("bullet")); shape.dispose(); @@ -82,6 +84,12 @@ public class Bullet { public Body getBody() { return body; } + public boolean collision() { + return collision; + } + public void setCollision() { + collision = true; + } } diff --git a/frontend/core/src/com/game/tankwars/model/FixtureData.java b/frontend/core/src/com/game/tankwars/model/FixtureData.java new file mode 100644 index 0000000000000000000000000000000000000000..d829bd0a4d9f2653afb8564e3e1d2b1d2e2f766e --- /dev/null +++ b/frontend/core/src/com/game/tankwars/model/FixtureData.java @@ -0,0 +1,26 @@ +package com.game.tankwars.model; + +public class FixtureData { + + String id; + + Boolean collision; + + public FixtureData(String id) { + this.id = id; + this.collision = false; + } + + public void hasCollided() { + this.collision = true; + } + + public boolean isHit() { + return collision ? true : false; + } + + public String getId() { + return id; + } + +} diff --git a/frontend/core/src/com/game/tankwars/model/Tank.java b/frontend/core/src/com/game/tankwars/model/Tank.java index 6ed7c6d5990ac24705ebf81e4dd4cedaeb24016a..0076914f1b98aaf1c75baf465176e19c1ce1cf0a 100644 --- a/frontend/core/src/com/game/tankwars/model/Tank.java +++ b/frontend/core/src/com/game/tankwars/model/Tank.java @@ -45,7 +45,7 @@ public class Tank { int fuel = 150; int health = 100; - public Tank(int posInVertArr, Texture chassisTexture, Texture cannonTexture, Terrain terrain, TankWarsGame tankWarsGame, boolean directionLeft, float cannonAngle) { + public Tank(int posInVertArr, Texture chassisTexture, Texture cannonTexture, Terrain terrain, TankWarsGame tankWarsGame, boolean directionLeft, float cannonAngle, String id) { VIEWPORT_HEIGHT = tankWarsGame.getViewportHeight(); VIEWPORT_WIDTH = tankWarsGame.getViewportWidth(); @@ -80,6 +80,7 @@ public class Tank { chassis = world.createBody(bodyDef); chassis.setUserData(chassisSprite); chassis.createFixture(fixtureDef); + chassis.getFixtureList().get(0).setUserData(new FixtureData(id)); chassisSprite.scale(0.2f); //Cannon diff --git a/frontend/core/src/com/game/tankwars/model/Terrain.java b/frontend/core/src/com/game/tankwars/model/Terrain.java index 90f1d7736a22422d65d9f39d2cccced337163bf9..e5fb1cdab9cacda580d6405014ad53b6d68a9d0f 100644 --- a/frontend/core/src/com/game/tankwars/model/Terrain.java +++ b/frontend/core/src/com/game/tankwars/model/Terrain.java @@ -43,6 +43,7 @@ public class Terrain { body = world.createBody(bodyDef); body.createFixture(fixtureDef); + body.getFixtureList().get(0).setUserData(new FixtureData("terrain")); chainShape.dispose(); } diff --git a/frontend/core/src/com/game/tankwars/view/GameScreen.java b/frontend/core/src/com/game/tankwars/view/GameScreen.java index fa81a9ebb9f1154883e4a368c7131f0f709ade48..69007c65d4e5d94a96e76299486ef1931284182f 100644 --- a/frontend/core/src/com/game/tankwars/view/GameScreen.java +++ b/frontend/core/src/com/game/tankwars/view/GameScreen.java @@ -19,10 +19,12 @@ import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer; import com.badlogic.gdx.physics.box2d.World; import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.viewport.FitViewport; +import com.game.tankwars.CollisionDetection; import com.game.tankwars.TankWarsGame; import com.game.tankwars.controller.GameController; import com.game.tankwars.model.Box2dWorld; import com.game.tankwars.model.Bullet; +import com.game.tankwars.model.FixtureData; import com.game.tankwars.model.Tank; import com.game.tankwars.model.Terrain; @@ -46,8 +48,10 @@ public class GameScreen implements Screen { OrthographicCamera hudCam; Box2DDebugRenderer debugRenderer; GameController controller; + CollisionDetection collisionDetection; private Bullet bullet; + private boolean bulletToDestroy = false; public GameScreen(final TankWarsGame tankWarsGame){ this.tankWarsGame = tankWarsGame; @@ -61,6 +65,9 @@ public class GameScreen implements Screen { model = new Box2dWorld(); world = Box2dWorld.getWorld(); + collisionDetection = new CollisionDetection(); + world.setContactListener(collisionDetection); + worldCam = new OrthographicCamera(scale(TankWarsGame.GAMEPORT_WIDTH), scale(TankWarsGame.GAMEPORT_HEIGHT)); worldCam.position.set(scale(TankWarsGame.GAMEPORT_WIDTH)/2, scale(TankWarsGame.GAMEPORT_HEIGHT)/2, 0); @@ -82,14 +89,16 @@ public class GameScreen implements Screen { new Texture("camo-tank-barrel.png"), terrain, tankWarsGame, true, - 120); + 120, + "userTank"); opponentTank = new Tank(opponentPos, new Texture("camo-tank-1.png"), new Texture("camo-tank-barrel.png"), terrain, tankWarsGame, false, - 225); + 225, + "opponentTank"); horizontalScaling = Gdx.graphics.getWidth() / TankWarsGame.GAMEPORT_WIDTH; verticalScaling = Gdx.graphics.getHeight() / TankWarsGame.GAMEPORT_HEIGHT; @@ -104,6 +113,7 @@ public class GameScreen implements Screen { @Override public void render(float delta) { model.logicStep(Gdx.graphics.getDeltaTime()); + model.destroyDeadBodies(); Gdx.gl.glClearColor(0, 0, 100, 100); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); debugRenderer.render(world, worldCam.combined); @@ -146,10 +156,17 @@ public class GameScreen implements Screen { terrain.draw(shapeRender); shapeRender.end(); updateBullet(); + if (checkBulletCollision()) { + Body body = this.bullet.getBody(); + bullet.getBulletSprite().getTexture().dispose(); + this.bullet = null; + model.addDeadBody(body); + System.out.println("Destroy body!"); + } batch.begin(); - if (bullet != (null)) { - bullet.getBulletSprite().draw(batch); + if (this.bullet != (null)) { + this.bullet.getBulletSprite().draw(batch); } myTank.getChassisSprite().draw(batch); myTank.getCannonSprite().draw(batch); @@ -204,4 +221,17 @@ public class GameScreen implements Screen { this.bullet = bullet; } } + + private boolean checkBulletCollision() { + if (this.bullet == null) { + return false; + } + try { + FixtureData data = (FixtureData) bullet.getBody().getFixtureList().get(0).getUserData(); + return data.isHit(); + } catch (IndexOutOfBoundsException e){ + return false; + } + + } }