From 6b4beaeab5df08b3b066431abafd2084d1774f50 Mon Sep 17 00:00:00 2001 From: Jeasonfire Date: Sat, 9 May 2015 20:47:16 +0300 Subject: [PATCH] Added collision listener. --- .gitignore | 1 + .../gladiator/components/CPhysics.java | 7 +++++ .../gladiator/physics/CollisionListener.java | 22 ++++++++++++++++ .../gladiator/physics/CollisionSide.java | 11 ++++++++ .../gladiator/systems/PhysicsSystem.java | 26 ++++++++++++++++--- 5 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 core/src/com/saltosion/gladiator/physics/CollisionListener.java create mode 100644 core/src/com/saltosion/gladiator/physics/CollisionSide.java diff --git a/.gitignore b/.gitignore index 70660bf..716567b 100644 --- a/.gitignore +++ b/.gitignore @@ -53,6 +53,7 @@ dist/ nbdist/ nbactions.xml nb-configuration.xml +.nb-gradle/ ## Gradle diff --git a/core/src/com/saltosion/gladiator/components/CPhysics.java b/core/src/com/saltosion/gladiator/components/CPhysics.java index f3d3824..97b7ef9 100644 --- a/core/src/com/saltosion/gladiator/components/CPhysics.java +++ b/core/src/com/saltosion/gladiator/components/CPhysics.java @@ -2,6 +2,7 @@ package com.saltosion.gladiator.components; import com.badlogic.ashley.core.Component; import com.badlogic.gdx.math.Vector2; +import com.saltosion.gladiator.physics.CollisionListener; public class CPhysics extends Component { @@ -10,6 +11,7 @@ public class CPhysics extends Component { public Vector2 size = new Vector2(); public float movespeed = 5f, jumpForce = 0.3f, gravity = 1f; public boolean grounded = true; + public CollisionListener collisionListener = null; // Movable toggles if the entity can move by itself public boolean movable = true; @@ -48,4 +50,9 @@ public class CPhysics extends Component { return this; } + public CPhysics setCollisionListener(CollisionListener collisionListener) { + this.collisionListener = collisionListener; + return this; + } + } diff --git a/core/src/com/saltosion/gladiator/physics/CollisionListener.java b/core/src/com/saltosion/gladiator/physics/CollisionListener.java new file mode 100644 index 0000000..286d35e --- /dev/null +++ b/core/src/com/saltosion/gladiator/physics/CollisionListener.java @@ -0,0 +1,22 @@ +package com.saltosion.gladiator.physics; + +import com.badlogic.ashley.core.Entity; + +/** + * + * @author Jens "Jeasonfire" Pitkänen + */ +public interface CollisionListener { + + /** + * This method will be called when host collides with other + * + * @param side The side which host is colliding other with. Eg. if host is + * falling towards other, this argument will be CollisionSide.BOTTOM when + * the collision happens. + * @param host + * @param other + */ + public void collision(CollisionSide side, Entity host, Entity other); + +} diff --git a/core/src/com/saltosion/gladiator/physics/CollisionSide.java b/core/src/com/saltosion/gladiator/physics/CollisionSide.java new file mode 100644 index 0000000..a479307 --- /dev/null +++ b/core/src/com/saltosion/gladiator/physics/CollisionSide.java @@ -0,0 +1,11 @@ +package com.saltosion.gladiator.physics; + +/** + * + * @author Jens "Jeasonfire" Pitkänen + */ +public enum CollisionSide { + + TOP, BOTTOM, LEFT, RIGHT + +} diff --git a/core/src/com/saltosion/gladiator/systems/PhysicsSystem.java b/core/src/com/saltosion/gladiator/systems/PhysicsSystem.java index 13e6139..1659c93 100644 --- a/core/src/com/saltosion/gladiator/systems/PhysicsSystem.java +++ b/core/src/com/saltosion/gladiator/systems/PhysicsSystem.java @@ -7,6 +7,7 @@ import com.badlogic.ashley.core.EntitySystem; import com.badlogic.ashley.core.Family; import com.badlogic.ashley.utils.ImmutableArray; import com.saltosion.gladiator.components.CPhysics; +import com.saltosion.gladiator.physics.CollisionSide; /** * @@ -54,8 +55,7 @@ public class PhysicsSystem extends EntitySystem { if (i == j) { continue; } - CPhysics other = pm.get(entities.get(j)); - collision(obj, other); + collision(entities.get(i), entities.get(j)); } } @@ -64,7 +64,10 @@ public class PhysicsSystem extends EntitySystem { } } - public void collision(CPhysics cp0, CPhysics cp1) { + public void collision(Entity entity0, Entity entity1) { + CPhysics cp0 = pm.get(entity0); + CPhysics cp1 = pm.get(entity1); + float x00 = cp0.position.x - cp0.size.x / 2; float x01 = cp0.position.x + cp0.size.x / 2; float x10 = cp1.position.x - cp1.size.x / 2; @@ -86,6 +89,10 @@ public class PhysicsSystem extends EntitySystem { // cp0 is going left, stop cp0.velocity.x = 0; } + + if (cp0.collisionListener != null) { + cp0.collisionListener.collision(CollisionSide.LEFT, entity0, entity1); + } } if (x01 > x10 && Math.abs(x01 - x10) < (cp0.size.x + cp1.size.x) / 16) { // cp0's right side is colliding with cp1's left side @@ -93,6 +100,10 @@ public class PhysicsSystem extends EntitySystem { // cp0 is going right, stop cp0.velocity.x = 0; } + + if (cp0.collisionListener != null) { + cp0.collisionListener.collision(CollisionSide.RIGHT, entity0, entity1); + } } if (y00 <= y11 && Math.abs(y00 - y11) < (cp0.size.y + cp1.size.y) / 16) { // cp0's bottom side is colliding with cp1's top side @@ -101,7 +112,10 @@ public class PhysicsSystem extends EntitySystem { cp0.velocity.y = 0; } cp0.grounded = true; - //cp0.position.y -= Math.abs(y00 - y11); + + if (cp0.collisionListener != null) { + cp0.collisionListener.collision(CollisionSide.BOTTOM, entity0, entity1); + } } if (y01 > y10 && Math.abs(y01 - y10) < (cp0.size.y + cp1.size.y) / 16) { // cp0's top side is colliding with cp1's bottom side @@ -109,6 +123,10 @@ public class PhysicsSystem extends EntitySystem { // cp0 is going up, stop cp0.velocity.y = 0; } + + if (cp0.collisionListener != null) { + cp0.collisionListener.collision(CollisionSide.TOP, entity0, entity1); + } } }