From 71d1001bbd1ccb0b64113ac56f76f5edc0b86f90 Mon Sep 17 00:00:00 2001 From: Jeasonfire Date: Sun, 17 May 2015 02:29:54 +0300 Subject: [PATCH] Knockback on sword collision added. --- .../gladiator/components/CCombat.java | 51 +++++++++++-------- .../gladiator/components/CPhysics.java | 44 ++++++++++++++-- .../gladiator/level/premade/Round1Level.java | 1 + .../listeners/SwingHitboxListener.java | 12 +++-- .../gladiator/systems/PhysicsSystem.java | 6 +++ 5 files changed, 86 insertions(+), 28 deletions(-) diff --git a/core/src/com/saltosion/gladiator/components/CCombat.java b/core/src/com/saltosion/gladiator/components/CCombat.java index 4b160ca..ad6a0c4 100644 --- a/core/src/com/saltosion/gladiator/components/CCombat.java +++ b/core/src/com/saltosion/gladiator/components/CCombat.java @@ -8,33 +8,35 @@ import com.saltosion.gladiator.listeners.CombatListener; import com.saltosion.gladiator.util.Direction; public class CCombat extends Component { - + public int health = 0; private int maxHealth = 0; private int damage = 0; + private float swingForce = 20f; private Vector2 swingsize = new Vector2(4, 3); private CombatListener combatListener; - + private Vector2 swinging = new Vector2(); private float swingDuration = 0.4f; public float swingCdCounter = 0; public HashMap inputs = new HashMap(); - + public CCombat() { this.inputs.put(Direction.UP, false); this.inputs.put(Direction.DOWN, false); this.inputs.put(Direction.LEFT, false); this.inputs.put(Direction.RIGHT, false); } - + public CCombat setBaseDamage(int basedmg) { this.damage = basedmg; return this; } - + /** * Sets max health for entity and replenishes health. + * * @param health * @return */ @@ -43,54 +45,63 @@ public class CCombat extends Component { this.maxHealth = health; return this; } - + public CCombat setMaxHealth(int maxHealth) { this.maxHealth = maxHealth; return this; } - + + public CCombat setSwingForce(float force) { + this.swingForce = force; + return this; + } + public CCombat setSwingCD(float cd) { this.swingDuration = cd; return this; } - + public CCombat setSwinging(Vector2 swingdir) { this.swinging = swingdir; return this; } - + public CCombat setSwingSize(Vector2 swingsize) { this.swingsize = swingsize; return this; } - + public CCombat setCombatListener(CombatListener listener) { this.combatListener = listener; return this; } - + public int getMaxHealth() { return this.maxHealth; } - + public int getDamage() { - float minDmg = damage*0.9f; - float maxDmg = damage*1.1f; - int randomdamage = (int) (Math.random()*(maxDmg-minDmg)+minDmg); + float minDmg = damage * 0.9f; + float maxDmg = damage * 1.1f; + int randomdamage = (int) (Math.random() * (maxDmg - minDmg) + minDmg); if (Math.random() > 0.7) { randomdamage *= 1.5; } return randomdamage; } - + + public float getSwingForce() { + return swingForce; + } + public float getSwingDuration() { return this.swingDuration; } - + public Vector2 getSwing() { return this.swinging; } - + public Direction getSwingDirection() { if (swinging.x > 0) { return Direction.RIGHT; @@ -103,11 +114,11 @@ public class CCombat extends Component { } return null; } - + public Vector2 getSwingSize() { return this.swingsize; } - + public CombatListener getCombatListener() { return this.combatListener; } diff --git a/core/src/com/saltosion/gladiator/components/CPhysics.java b/core/src/com/saltosion/gladiator/components/CPhysics.java index 72927f0..2f3ac03 100644 --- a/core/src/com/saltosion/gladiator/components/CPhysics.java +++ b/core/src/com/saltosion/gladiator/components/CPhysics.java @@ -9,8 +9,9 @@ public class CPhysics extends Component { private final Vector2 position = new Vector2(); private final Vector2 velocity = new Vector2(); + private final Vector2 simVelocity = new Vector2(); private final Vector2 size = new Vector2(); - private float movespeed = 15f, jumpForce = 35f, gravity = 100f; + private float movespeed = 15f, jumpForce = 35f, gravity = 100f, drag = 30f; private CollisionListener collisionListener = null; private float zParallax = 1; @@ -90,8 +91,33 @@ public class CPhysics extends Component { return this; } - public CPhysics setVelocity(Vector2 pos) { - this.velocity.set(pos); + public CPhysics setVelocity(Vector2 vel) { + this.velocity.set(vel); + return this; + } + + /** + * This velocity can be set externally and will always end up being 0 after + * some time that depends on the value of the drag variable + * + * @param x The x part of the new sim velocity + * @param y The y part of the new sim velocity + * @return The host component + */ + public CPhysics setSimVelocity(float x, float y) { + this.simVelocity.set(x, y); + return this; + } + + /** + * This velocity can be set externally and will always end up being 0 after + * some time that depends on the value of the drag variable + * + * @param simVel The new sim velocity + * @return The host component + */ + public CPhysics setSimVelocity(Vector2 simVel) { + this.simVelocity.set(simVel); return this; } @@ -108,7 +134,11 @@ public class CPhysics extends Component { public CPhysics setGravity(float gravity) { this.gravity = gravity; return this; + } + public CPhysics setDrag(float drag) { + this.drag = drag; + return this; } public CPhysics setCollisionListener(CollisionListener collisionListener) { @@ -139,6 +169,10 @@ public class CPhysics extends Component { return this.velocity; } + public Vector2 getSimVelocity() { + return this.simVelocity; + } + public Vector2 getSize() { return this.size; } @@ -155,6 +189,10 @@ public class CPhysics extends Component { return this.gravity; } + public float getDrag() { + return this.drag; + } + public CollisionListener getCollisionListener() { return this.collisionListener; } diff --git a/core/src/com/saltosion/gladiator/level/premade/Round1Level.java b/core/src/com/saltosion/gladiator/level/premade/Round1Level.java index 19ba2f2..8501b35 100644 --- a/core/src/com/saltosion/gladiator/level/premade/Round1Level.java +++ b/core/src/com/saltosion/gladiator/level/premade/Round1Level.java @@ -6,6 +6,7 @@ import com.saltosion.gladiator.level.Level; import com.saltosion.gladiator.util.AppUtil; import com.saltosion.gladiator.util.Direction; import com.saltosion.gladiator.util.Global; +import com.saltosion.gladiator.util.Log; import java.util.ArrayList; public class Round1Level implements Level { diff --git a/core/src/com/saltosion/gladiator/listeners/SwingHitboxListener.java b/core/src/com/saltosion/gladiator/listeners/SwingHitboxListener.java index 24f7c8c..2d11bfa 100644 --- a/core/src/com/saltosion/gladiator/listeners/SwingHitboxListener.java +++ b/core/src/com/saltosion/gladiator/listeners/SwingHitboxListener.java @@ -39,14 +39,16 @@ public class SwingHitboxListener implements CollisionListener { CPhysics otherPhysics = pm.get(other); if (otherPhysics != null && otherPhysics.getCollisionListener() != null && otherPhysics.getCollisionListener() instanceof SwingHitboxListener) { - Log.info("Clash!"); - float x = 0; + float x = 0, y = 0; if (direction == Direction.LEFT) { - x = -1; - } - if (direction == Direction.RIGHT) { x = 1; + } else if (direction == Direction.RIGHT) { + x = -1; + } else if (direction == Direction.DOWN) { + y = 1; } + float force = cm.get(source).getSwingForce(); + pm.get(source).setSimVelocity(x * force, y * force / 8f); } } diff --git a/core/src/com/saltosion/gladiator/systems/PhysicsSystem.java b/core/src/com/saltosion/gladiator/systems/PhysicsSystem.java index 402f2af..5e1b88c 100644 --- a/core/src/com/saltosion/gladiator/systems/PhysicsSystem.java +++ b/core/src/com/saltosion/gladiator/systems/PhysicsSystem.java @@ -58,6 +58,12 @@ public class PhysicsSystem extends EntitySystem { obj.setGrounded(false); obj.getVelocity().y = obj.getJumpForce(); } + + obj.getVelocity().x += obj.getSimVelocity().x; + obj.getVelocity().y += obj.getSimVelocity().y; + + obj.getSimVelocity().x -= obj.getDrag() * deltaTime * Math.signum(obj.getSimVelocity().x); + obj.getSimVelocity().y -= obj.getDrag() * deltaTime * Math.signum(obj.getSimVelocity().y); } // Gravity