Added ghost variable, resized player hitbox & cleaned docs.

This commit is contained in:
Jeasonfire 2015-05-10 17:30:31 +03:00
parent 1136a9b8b8
commit 543ef101e3
3 changed files with 46 additions and 19 deletions

View File

@ -84,8 +84,7 @@ public class GladiatorBrawler extends ApplicationAdapter {
renderedObject.addSequence("Idle", sequence); renderedObject.addSequence("Idle", sequence);
renderedObject.playAnimation("Idle"); renderedObject.playAnimation("Idle");
player.add(renderedObject); player.add(renderedObject);
player.add(new CPhysics().setSize(player1.getRegionWidth() * Global.SPRITE_SCALE, player.add(new CPhysics().setSize(2, 4).setPosition(0, 5));
player1.getRegionHeight() * Global.SPRITE_SCALE).setPosition(0, 5));
engine.addEntity(player); engine.addEntity(player);

View File

@ -13,33 +13,53 @@ public class CPhysics extends Component {
public boolean grounded = true; public boolean grounded = true;
public CollisionListener collisionListener = null; public CollisionListener collisionListener = null;
// Movable toggles if the entity can move by itself
public boolean movable = true; public boolean movable = true;
// GravityApplied toggles if the entity is affected by gravity
public boolean gravityApplied = true; public boolean gravityApplied = true;
// Dynamic toggles if the entity processes collisions
public boolean dynamic = true; public boolean dynamic = true;
public boolean ghost = false;
// Movement (/input) vars // Movement (/input) vars
public boolean movingLeft = false; public boolean movingLeft = false;
public boolean movingRight = false; public boolean movingRight = false;
public boolean jumping = false; public boolean jumping = false;
/**
* @param movable Toggles if the entity can move by itself
* @return Returns the instance this methdod was called from
*/
public CPhysics setMovable(boolean movable) { public CPhysics setMovable(boolean movable) {
this.movable = movable; this.movable = movable;
return this; return this;
} }
/**
* @param gravityApplied Toggles if the entity is affected by gravity
* @return Returns the instance this methdod was called from
*/
public CPhysics setGravityApplied(boolean gravityApplied) { public CPhysics setGravityApplied(boolean gravityApplied) {
this.gravityApplied = gravityApplied; this.gravityApplied = gravityApplied;
return this; return this;
} }
/**
* @param dynamic Toggles if the entity processes collisions
* @return Returns the instance this methdod was called from
*/
public CPhysics setDynamic(boolean dynamic) { public CPhysics setDynamic(boolean dynamic) {
this.dynamic = dynamic; this.dynamic = dynamic;
return this; return this;
} }
/**
* @param ghost Toggles if the entity is affected by collisions (will call
* collision listener anyway if dynamic == true)
* @return Returns the instance this methdod was called from
*/
public CPhysics setGhost(boolean ghost) {
this.ghost = ghost;
return this;
}
public CPhysics setSize(float w, float h) { public CPhysics setSize(float w, float h) {
this.size.set(w, h); this.size.set(w, h);
return this; return this;

View File

@ -89,10 +89,12 @@ public class PhysicsSystem extends EntitySystem {
if (x00 <= x11 && Math.abs(x00 - x11) < (cp0.size.x + cp1.size.x) / 16) { if (x00 <= x11 && Math.abs(x00 - x11) < (cp0.size.x + cp1.size.x) / 16) {
// cp0's left side is colliding with cp1's right side // cp0's left side is colliding with cp1's right side
if (!cp0.ghost && !cp1.ghost) {
if (cp0.velocity.x < 0) { if (cp0.velocity.x < 0) {
// cp0 is going left, stop // cp0 is going left, stop
cp0.velocity.x = 0; cp0.velocity.x = 0;
} }
}
if (cp0.collisionListener != null) { if (cp0.collisionListener != null) {
cp0.collisionListener.collision(CollisionSide.LEFT, entity0, entity1); cp0.collisionListener.collision(CollisionSide.LEFT, entity0, entity1);
@ -100,10 +102,12 @@ public class PhysicsSystem extends EntitySystem {
} }
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
if (!cp0.ghost && !cp1.ghost) {
if (cp0.velocity.x > 0) { if (cp0.velocity.x > 0) {
// cp0 is going right, stop // cp0 is going right, stop
cp0.velocity.x = 0; cp0.velocity.x = 0;
} }
}
if (cp0.collisionListener != null) { if (cp0.collisionListener != null) {
cp0.collisionListener.collision(CollisionSide.RIGHT, entity0, entity1); cp0.collisionListener.collision(CollisionSide.RIGHT, entity0, entity1);
@ -111,12 +115,14 @@ public class PhysicsSystem extends EntitySystem {
} }
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
if (!cp0.ghost && !cp1.ghost) {
if (cp0.velocity.y < 0) { if (cp0.velocity.y < 0) {
// cp0 is going down, stop // cp0 is going down, stop
cp0.velocity.y = 0; cp0.velocity.y = 0;
} }
cp0.grounded = true; cp0.grounded = true;
cp0.position.y += y11 - y00; cp0.position.y += y11 - y00;
}
if (cp0.collisionListener != null) { if (cp0.collisionListener != null) {
cp0.collisionListener.collision(CollisionSide.BOTTOM, entity0, entity1); cp0.collisionListener.collision(CollisionSide.BOTTOM, entity0, entity1);
@ -124,10 +130,12 @@ public class PhysicsSystem extends EntitySystem {
} }
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
if (!cp0.ghost && !cp1.ghost) {
if (cp0.velocity.y > 0) { if (cp0.velocity.y > 0) {
// cp0 is going up, stop // cp0 is going up, stop
cp0.velocity.y = 0; cp0.velocity.y = 0;
} }
}
if (cp0.collisionListener != null) { if (cp0.collisionListener != null) {
cp0.collisionListener.collision(CollisionSide.TOP, entity0, entity1); cp0.collisionListener.collision(CollisionSide.TOP, entity0, entity1);