Skip to content

Commit

Permalink
Smooth motion due to airflow and wind. Should fix any bumps and sudde…
Browse files Browse the repository at this point in the history
…n changes in direction/altitude when flying and overall make controlling whilst flying more manageable
  • Loading branch information
Sollace committed Feb 13, 2024
1 parent 2754d41 commit 92fd226
Showing 1 changed file with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ public class PlayerPhysics extends EntityPhysics<PlayerEntity> implements Tickab

private final Pony pony;

private Lerp updraft = new Lerp(0);
private Lerp windStrength = new Lerp(0);

public PlayerPhysics(Pony pony) {
super(pony.asEntity(), Creature.GRAVITY);
this.pony = pony;
Expand Down Expand Up @@ -364,6 +367,8 @@ public void tick() {
soundPlaying = false;
descentRate = 0;
ticksDiving = 0;
updraft.update(0, 100);
windStrength.update(0, 100);

if (Abilities.RAINBOOM.canUse(pony.getCompositeRace()) && entity.isOnGround()) {
pony.getMagicalReserves().getCharge().set(0);
Expand All @@ -376,6 +381,8 @@ public void tick() {
} else {
descentRate = 0;
soundPlaying = false;
updraft.update(0, 100);
windStrength.update(0, 100);
}

if (!entity.isOnGround()) {
Expand Down Expand Up @@ -623,12 +630,13 @@ private void moveFlying(MutableVector velocity) {
velocity.x += - forward * MathHelper.sin(entity.getYaw() * 0.017453292F);
velocity.z += forward * MathHelper.cos(entity.getYaw() * 0.017453292F);


if (entity.getWorld().hasRain(entity.getBlockPos())) {
applyTurbulance(velocity);
} else {
double updraft = WeatherConditions.getUpdraft(new BlockPos.Mutable().set(entity.getBlockPos()), entity.getWorld()) / 3F;
updraft *= 1 + motion;
float targetUpdraft = (float)WeatherConditions.getUpdraft(new BlockPos.Mutable().set(entity.getBlockPos()), entity.getWorld()) / 3F;
targetUpdraft *= 1 + motion;
this.updraft.update(targetUpdraft, targetUpdraft > this.updraft.getTarget() ? 30_000 : 3000);
double updraft = this.updraft.getValue();
velocity.y += updraft;
descentRate -= updraft;
}
Expand Down Expand Up @@ -702,19 +710,25 @@ private void applyTurbulance(MutableVector velocity) {
.multiply(globalEffectStrength / 100D)
.multiply(1 / (1 + Math.floor(pony.getLevel().get() / 10F)));





if (effectStrength * gust.getX() >= 1) {
SoundEmitter.playSoundAt(entity, USounds.AMBIENT_WIND_GUST, SoundCategory.AMBIENT, 3, 1);
}

float weight = 1 + (EnchantmentHelper.getEquipmentLevel(UEnchantments.HEAVY, entity) * 0.8F) + (pony.getCompositeRace().canUseEarth() ? 1 : 0);

velocity.add(WeatherConditions.getAirflow(entity.getBlockPos(), entity.getWorld()), 0.04F * effectStrength);
velocity.add(Vec3d.fromPolar(
(entity.getPitch() + (float)gust.getY()) * MathHelper.RADIANS_PER_DEGREE,
(entity.getYaw() + (float)gust.getZ()) * MathHelper.RADIANS_PER_DEGREE
),
effectStrength * (float)gust.getX() / weight
);
Vec3d airflow = WeatherConditions.getAirflow(entity.getBlockPos(), entity.getWorld())
.multiply(0.04F * effectStrength)
.add(Vec3d.fromPolar(
(entity.getPitch() + (float)gust.getY()) * MathHelper.RADIANS_PER_DEGREE,
(entity.getYaw() + (float)gust.getZ()) * MathHelper.RADIANS_PER_DEGREE
).multiply(effectStrength * (float)gust.getX() / weight));

windStrength.update((float)airflow.length(), airflow.length() > windStrength.getValue() ? 1000 : 500);
velocity.add(airflow.normalize(), windStrength.getValue());

if (!entity.getWorld().isClient && effectStrength > 0.9F && entity.getWorld().isThundering() && entity.getWorld().random.nextInt(9000) == 0) {
LightningEntity lightning = EntityType.LIGHTNING_BOLT.create(entity.getWorld());
Expand Down Expand Up @@ -794,6 +808,7 @@ public void toNBT(NbtCompound compound) {
compound.putBoolean("isFlyingEither", isFlyingEither);
compound.putInt("ticksInAir", ticksInAir);
compound.putFloat("descentRate", descentRate);
compound.putFloat("updraft", updraft.getValue());
}

@Override
Expand All @@ -804,6 +819,7 @@ public void fromNBT(NbtCompound compound) {
isFlyingEither = compound.getBoolean("isFlyingEither");
ticksInAir = compound.getInt("ticksInAir");
descentRate = compound.getFloat("descentRate");
updraft.update(compound.getFloat("updraft"), 0);

entity.calculateDimensions();
}
Expand Down

0 comments on commit 92fd226

Please sign in to comment.