Added collision listener.

This commit is contained in:
Jeasonfire 2015-05-09 20:47:16 +03:00
parent 9c70a7326b
commit 6b4beaeab5
5 changed files with 63 additions and 4 deletions

1
.gitignore vendored
View File

@ -53,6 +53,7 @@ dist/
nbdist/ nbdist/
nbactions.xml nbactions.xml
nb-configuration.xml nb-configuration.xml
.nb-gradle/
## Gradle ## Gradle

View File

@ -2,6 +2,7 @@ package com.saltosion.gladiator.components;
import com.badlogic.ashley.core.Component; import com.badlogic.ashley.core.Component;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.saltosion.gladiator.physics.CollisionListener;
public class CPhysics extends Component { public class CPhysics extends Component {
@ -10,6 +11,7 @@ public class CPhysics extends Component {
public Vector2 size = new Vector2(); public Vector2 size = new Vector2();
public float movespeed = 5f, jumpForce = 0.3f, gravity = 1f; public float movespeed = 5f, jumpForce = 0.3f, gravity = 1f;
public boolean grounded = true; public boolean grounded = true;
public CollisionListener collisionListener = null;
// Movable toggles if the entity can move by itself // Movable toggles if the entity can move by itself
public boolean movable = true; public boolean movable = true;
@ -48,4 +50,9 @@ public class CPhysics extends Component {
return this; return this;
} }
public CPhysics setCollisionListener(CollisionListener collisionListener) {
this.collisionListener = collisionListener;
return this;
}
} }

View File

@ -0,0 +1,22 @@
package com.saltosion.gladiator.physics;
import com.badlogic.ashley.core.Entity;
/**
*
* @author Jens "Jeasonfire" Pitkänen <jeasonfire@gmail.com>
*/
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);
}

View File

@ -0,0 +1,11 @@
package com.saltosion.gladiator.physics;
/**
*
* @author Jens "Jeasonfire" Pitkänen <jeasonfire@gmail.com>
*/
public enum CollisionSide {
TOP, BOTTOM, LEFT, RIGHT
}

View File

@ -7,6 +7,7 @@ import com.badlogic.ashley.core.EntitySystem;
import com.badlogic.ashley.core.Family; import com.badlogic.ashley.core.Family;
import com.badlogic.ashley.utils.ImmutableArray; import com.badlogic.ashley.utils.ImmutableArray;
import com.saltosion.gladiator.components.CPhysics; import com.saltosion.gladiator.components.CPhysics;
import com.saltosion.gladiator.physics.CollisionSide;
/** /**
* *
@ -54,8 +55,7 @@ public class PhysicsSystem extends EntitySystem {
if (i == j) { if (i == j) {
continue; continue;
} }
CPhysics other = pm.get(entities.get(j)); collision(entities.get(i), entities.get(j));
collision(obj, other);
} }
} }
@ -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 x00 = cp0.position.x - cp0.size.x / 2;
float x01 = 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; float x10 = cp1.position.x - cp1.size.x / 2;
@ -86,6 +89,10 @@ public class PhysicsSystem extends EntitySystem {
// cp0 is going left, stop // cp0 is going left, stop
cp0.velocity.x = 0; 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) { 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 // 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 is going right, stop
cp0.velocity.x = 0; 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) { 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 // 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.velocity.y = 0;
} }
cp0.grounded = true; 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) { 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 // 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 is going up, stop
cp0.velocity.y = 0; cp0.velocity.y = 0;
} }
if (cp0.collisionListener != null) {
cp0.collisionListener.collision(CollisionSide.TOP, entity0, entity1);
}
} }
} }