Knockback on sword collision added.

This commit is contained in:
Jeasonfire 2015-05-17 02:29:54 +03:00
parent 2c4f2dbfa3
commit 71d1001bbd
5 changed files with 86 additions and 28 deletions

View File

@ -12,6 +12,7 @@ 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;
@ -35,6 +36,7 @@ public class CCombat extends Component {
/**
* Sets max health for entity and replenishes health.
*
* @param health
* @return
*/
@ -49,6 +51,11 @@ public class CCombat extends Component {
return this;
}
public CCombat setSwingForce(float force) {
this.swingForce = force;
return this;
}
public CCombat setSwingCD(float cd) {
this.swingDuration = cd;
return this;
@ -74,15 +81,19 @@ public class CCombat extends Component {
}
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;
}

View File

@ -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;
}

View File

@ -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 {

View File

@ -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);
}
}

View File

@ -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