Skip to content

Commit

Permalink
fix: movement speed calculate
Browse files Browse the repository at this point in the history
  • Loading branch information
IWareQ committed Jun 29, 2024
1 parent 44beeba commit 9c61e46
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

public interface EntityPlayerBaseComponent extends EntityBaseComponent, ChunkLoader, ScoreboardViewer {

double BLOCK_INTERACT_MAX_DV_DIFF = 2.0;
double BLOCK_INTERACT_MAX_DV_DIFF = 2d;

float DEFAULT_MOVEMENT_SPEED = 0.1f;

boolean isSprinting();

Expand Down Expand Up @@ -147,9 +149,7 @@ default double getMaxInteractDistance() {
return getGameType() == GameType.CREATIVE ? 13 : 7;
}

float DEFAULT_MOVEMENT_SPEED = 0.1f;
float getMovementSpeed();

void setMovementSpeed(float speed);

float getMovementSpeed();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ private EffectSlownessType() {
@Override
public void onAdd(Entity entity, EffectInstance effectInstance) {
if (!(entity instanceof EntityPlayer player)) return;
var amplifier = effectInstance.getAmplifier();
player.setMovementSpeed(EntityPlayer.DEFAULT_MOVEMENT_SPEED * (1 - 0.15f * (amplifier + 1)));
var amplifier = effectInstance.getAmplifier() + 1;
var slowness = 1 - amplifier * 0.15f;
if (slowness <= 0) slowness = 0.00001f;
player.setMovementSpeed(player.getMovementSpeed() * slowness);
}

@Override
public void onRemove(Entity entity, EffectInstance effectInstance) {
if (!(entity instanceof EntityPlayer player)) return;
player.setMovementSpeed(EntityPlayer.DEFAULT_MOVEMENT_SPEED);
var amplifier = effectInstance.getAmplifier() + 1;
var slowness = 1 - amplifier * 0.15f;
if (slowness <= 0) slowness = 0.00001f;
player.setMovementSpeed(player.getMovementSpeed() / slowness);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.allaymc.api.entity.effect.EffectInstance;
import org.allaymc.api.entity.interfaces.EntityPlayer;
import org.allaymc.api.utils.Identifier;
import org.cloudburstmc.protocol.bedrock.data.entity.EntityDataTypes;

import java.awt.*;

Expand All @@ -24,13 +23,16 @@ private EffectSpeedType() {
@Override
public void onAdd(Entity entity, EffectInstance effectInstance) {
if (!(entity instanceof EntityPlayer player)) return;
var amplifier = effectInstance.getAmplifier();
player.setMovementSpeed(EntityPlayer.DEFAULT_MOVEMENT_SPEED * (1 + 0.2f * (amplifier + 1)));
var amplifier = effectInstance.getAmplifier() + 1;
var speed = 1 + amplifier * 0.2f;
player.setMovementSpeed(player.getMovementSpeed() * speed);
}

@Override
public void onRemove(Entity entity, EffectInstance effectInstance) {
if (!(entity instanceof EntityPlayer player)) return;
player.setMovementSpeed(EntityPlayer.DEFAULT_MOVEMENT_SPEED);
var amplifier = effectInstance.getAmplifier() + 1;
var speed = 1 + amplifier * 0.2f;
player.setMovementSpeed(player.getMovementSpeed() / speed);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,14 @@ public boolean isSprinting() {

@Override
public void setSprinting(boolean sprinting) {
if (sprinting == isSprinting()) return;

var speed = getMovementSpeed();
if (sprinting) speed *= 1.3f;
else speed /= 1.3f;

setMovementSpeed(speed);

setAndSendEntityFlag(EntityFlag.SPRINTING, sprinting);
}

Expand Down

0 comments on commit 9c61e46

Please sign in to comment.