Implement knockback

This commit is contained in:
Németh Noel 2021-06-27 23:05:54 +02:00
parent 17aa606037
commit 6c60c4d0fc
2 changed files with 30 additions and 0 deletions

View File

@ -1739,6 +1739,22 @@ public class Entity implements Viewable, Tickable, EventHandler<EntityEvent>, Da
}
}
/**
* Applies knockback to the entity
*
* @param strength the strength of the knockback, 0.4 is the vanilla value for a bare hand hit
* @param x knockback on x axle, for default knockback use the following formula <pre>sin(attacker.yaw * (pi/180))</pre>
* @param z knockback on z axle, for default knockback use the following formula <pre>-cos(attacker.yaw * (pi/180))</pre>
*/
public void takeKnockback(final float strength, final double x, final double z) {
if (strength > 0) {
final Vector velocityModifier = new Vector(x, 0d, z).normalize().multiply(strength);
this.velocity.setX(velocity.getX() / 2d - velocityModifier.getX());
this.velocity.setY(onGround ? Math.min(.4d, velocity.getY() / 2d + strength) : velocity.getY());
this.velocity.setZ(velocity.getZ() / 2d - velocityModifier.getZ());
}
}
public enum Pose {
STANDING,
FALL_FLYING,

View File

@ -779,4 +779,18 @@ public class LivingEntity extends Entity implements EquipmentHandler {
return null;
}
/**
* Applies knockback
* <p>
* Note: The strength is reduced based on knockback resistance
*
* @param strength the strength of the knockback, 0.4 is the vanilla value for a bare hand hit
* @param x knockback on x axle, for default knockback use the following formula <pre>sin(attacker.yaw * (pi/180))</pre>
* @param z knockback on z axle, for default knockback use the following formula <pre>-cos(attacker.yaw * (pi/180))</pre>
*/
@Override
public void takeKnockback(float strength, final double x, final double z) {
strength *= 1 - getAttributeValue(Attribute.KNOCKBACK_RESISTANCE);
super.takeKnockback(strength, x, z);
}
}