Search in sources :

Example 1 with EntityDamageEvent

use of io.xol.chunkstories.api.events.entity.EntityDamageEvent in project chunkstories-core by Hugobros3.

the class EntityLivingImplementation method damage.

@Override
public float damage(DamageCause cause, HitBox osef, float damage) {
    if (damageCooldown > System.currentTimeMillis())
        return 0f;
    EntityDamageEvent event = new EntityDamageEvent(this, cause, damage);
    this.getWorld().getGameLogic().getPluginsManager().fireEvent(event);
    if (!event.isCancelled()) {
        entityHealthComponent.damage(event.getDamageDealt());
        lastDamageCause = cause;
        damageCooldown = System.currentTimeMillis() + cause.getCooldownInMs();
        float damageDealt = event.getDamageDealt();
        // Applies knockback
        if (cause instanceof Entity) {
            Entity attacker = (Entity) cause;
            Vector3d attackKnockback = this.getLocation().sub(attacker.getLocation().add(0d, 0d, 0d));
            attackKnockback.y = (0d);
            attackKnockback.normalize();
            float knockback = (float) Math.max(1f, Math.pow(damageDealt, 0.5f));
            attackKnockback.mul(knockback / 50d);
            attackKnockback.y = (knockback / 50d);
            /*
				attackKnockback.scale(damageDealt / 500d);
				attackKnockback.scale(1.0 / (1.0 + 5 * this.getVelocityComponent().getVelocity().length()));*/
            // .scale(1/60d).scale(damageDealt / 10f);
            this.getVelocityComponent().addVelocity(attackKnockback);
        }
        return damageDealt;
    }
    return 0f;
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) Vector3d(org.joml.Vector3d) EntityDamageEvent(io.xol.chunkstories.api.events.entity.EntityDamageEvent)

Aggregations

Entity (io.xol.chunkstories.api.entity.Entity)1 EntityDamageEvent (io.xol.chunkstories.api.events.entity.EntityDamageEvent)1 Vector3d (org.joml.Vector3d)1