mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-22 15:22:56 +01:00
Implement knockback
This commit is contained in:
parent
17aa606037
commit
6c60c4d0fc
@ -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,
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user