Skip to content

Commit

Permalink
Improve power up movement in water
Browse files Browse the repository at this point in the history
  • Loading branch information
WenXin20 committed Dec 22, 2024
1 parent 5b77848 commit b149f37
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,29 @@
import com.wenxin2.marioverse.init.ParticleRegistry;
import com.wenxin2.marioverse.init.TagRegistry;
import java.util.List;
import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.tags.FluidTags;
import net.minecraft.util.Mth;
import net.minecraft.util.ParticleUtils;
import net.minecraft.util.valueproviders.UniformInt;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MoverType;
import net.minecraft.world.entity.PathfinderMob;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.animal.FlyingAnimal;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import net.neoforged.neoforge.common.NeoForgeMod;
import net.neoforged.neoforge.fluids.FluidType;
import software.bernie.geckolib.animatable.GeoAnimatable;
import software.bernie.geckolib.animatable.GeoEntity;
import software.bernie.geckolib.animatable.instance.AnimatableInstanceCache;
Expand Down Expand Up @@ -44,12 +57,76 @@ protected <E extends GeoAnimatable> PlayState walkAnimController(final Animation
return PlayState.STOP;
}

private SoundEvent getFallDamageSound(int height) {
return height > 4 ? this.getFallSounds().big() : this.getFallSounds().small();
}

@Override
public void tick() {
super.tick();
this.checkForCollisions();
}

@Override
public void jumpInFluid(FluidType type) {
this.jumpInLiquidInternal(() -> super.jumpInFluid(type));
}

private void jumpInLiquidInternal(Runnable onSuper) {
if (this.getNavigation().canFloat()) {
onSuper.run();
} else this.setDeltaMovement(this.getDeltaMovement()
.add(0.0, this.getAttributeValue(Attributes.JUMP_STRENGTH), 0.0));
}

@Override
public void travel(Vec3 vec3) {
if (this.isControlledByLocalInstance()) {
double d9 = this.getY();
double d0 = this.getGravity();
boolean flag = this.getDeltaMovement().y <= 0.0;
FluidState fluidstate = this.level().getFluidState(this.blockPosition());
if (flag && this.hasEffect(MobEffects.SLOW_FALLING))
d0 = Math.min(d0, 0.01);

if ((this.isInWaterOrBubble() || (this.isInFluidType(fluidstate)
&& fluidstate.getFluidType() != NeoForgeMod.LAVA_TYPE.value()))
&& this.isAffectedByFluids() && !this.canStandOnFluid(fluidstate)) {
if (this.isInWaterOrBubble() || (this.isInFluidType(fluidstate)
&& !this.moveInFluid(fluidstate, vec3, d0))) {
float f4 = this.isSprinting() ? 0.9F : this.getWaterSlowDown();
float f5 = 0.02F;
float f6 = (float) this.getAttributeValue(Attributes.WATER_MOVEMENT_EFFICIENCY);

if (!this.onGround())
f6 *= 1.0F;

if (f6 > 0.0F) {
f4 += (0.54600006F - f4) * f6;
f5 += (this.getSpeed() - f5) * f6;
}

if (this.hasEffect(MobEffects.DOLPHINS_GRACE))
f4 = 0.96F;

f5 *= (float) this.getAttributeValue(NeoForgeMod.SWIM_SPEED);
this.moveRelative(f5, vec3);
this.move(MoverType.SELF, this.getDeltaMovement());
Vec3 vec36 = this.getDeltaMovement();
if (this.horizontalCollision && this.onClimbable())
vec36 = new Vec3(vec36.x, 0.5, vec36.z);

this.setDeltaMovement(vec36.multiply(f4, 0.7F, f4));
Vec3 vec32 = this.getFluidFallingAdjustedMovement(d0, flag, this.getDeltaMovement());
this.setDeltaMovement(vec32);
if (this.horizontalCollision
&& this.isFree(vec32.x, vec32.y + 0.6F - this.getY() + d9, vec32.z))
this.setDeltaMovement(vec32.x, 0.5F, vec32.z);
}
} else super.travel(vec3);
}
}

@Override
public boolean hurt(DamageSource source, float amount) {
// Poof particle effect
Expand All @@ -64,6 +141,11 @@ public boolean hurt(DamageSource source, float amount) {
return true;
}

@Override
public boolean isColliding(BlockPos p_20040_, BlockState p_20041_) {
return super.isColliding(p_20040_, p_20041_);
}

public void checkForCollisions() {
AABB boundingBox = this.getBoundingBox().inflate(0.1);
List<Entity> entities = this.level().getEntities(this, boundingBox, entity -> entity != this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,26 @@

import com.wenxin2.marioverse.init.TagRegistry;
import java.util.List;
import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.tags.FluidTags;
import net.minecraft.util.Mth;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.MoverType;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.animal.FlyingAnimal;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import net.neoforged.neoforge.common.NeoForgeMod;
import net.neoforged.neoforge.fluids.FluidType;
import software.bernie.geckolib.animatable.GeoEntity;
import software.bernie.geckolib.animatable.instance.AnimatableInstanceCache;
import software.bernie.geckolib.animation.AnimatableManager;
Expand All @@ -30,12 +42,76 @@ public AnimatableInstanceCache getAnimatableInstanceCache() {
return this.cache;
}

private SoundEvent getFallDamageSound(int height) {
return height > 4 ? this.getFallSounds().big() : this.getFallSounds().small();
}

@Override
public void tick() {
super.tick();
this.checkForCollisions();
}

@Override
public void jumpInFluid(FluidType type) {
this.jumpInLiquidInternal(() -> super.jumpInFluid(type));
}

private void jumpInLiquidInternal(Runnable onSuper) {
if (this.getNavigation().canFloat()) {
onSuper.run();
} else this.setDeltaMovement(this.getDeltaMovement()
.add(0.0, 0.0, 0.0));
}

@Override
public void travel(Vec3 vec3) {
if (this.isControlledByLocalInstance()) {
double d9 = this.getY();
double d0 = this.getGravity();
boolean flag = this.getDeltaMovement().y <= 0.0;
FluidState fluidstate = this.level().getFluidState(this.blockPosition());
if (flag && this.hasEffect(MobEffects.SLOW_FALLING))
d0 = Math.min(d0, 0.01);

if ((this.isInWaterOrBubble() || (this.isInFluidType(fluidstate)
&& fluidstate.getFluidType() != NeoForgeMod.LAVA_TYPE.value()))
&& this.isAffectedByFluids() && !this.canStandOnFluid(fluidstate)) {
if (this.isInWaterOrBubble() || (this.isInFluidType(fluidstate)
&& !this.moveInFluid(fluidstate, vec3, d0))) {
float f4 = this.isSprinting() ? 0.9F : this.getWaterSlowDown();
float f5 = 0.02F;
float f6 = (float) this.getAttributeValue(Attributes.WATER_MOVEMENT_EFFICIENCY);

if (!this.onGround())
f6 *= 1.0F;

if (f6 > 0.0F) {
f4 += (0.54600006F - f4) * f6;
f5 += (this.getSpeed() - f5) * f6;
}

if (this.hasEffect(MobEffects.DOLPHINS_GRACE))
f4 = 0.96F;

f5 *= (float) this.getAttributeValue(NeoForgeMod.SWIM_SPEED);
this.moveRelative(f5, vec3);
this.move(MoverType.SELF, this.getDeltaMovement());
Vec3 vec36 = this.getDeltaMovement();
if (this.horizontalCollision && this.onClimbable())
vec36 = new Vec3(vec36.x, 0.5, vec36.z);

this.setDeltaMovement(vec36.multiply(f4, 0.7F, f4));
Vec3 vec32 = this.getFluidFallingAdjustedMovement(d0, flag, this.getDeltaMovement());
this.setDeltaMovement(vec32);
if (this.horizontalCollision
&& this.isFree(vec32.x, vec32.y + 0.6F - this.getY() + d9, vec32.z))
this.setDeltaMovement(vec32.x, 0.5F, vec32.z);
}
} else super.travel(vec3);
}
}

@Override
public boolean hurt(DamageSource source, float amount) {
// Poof particle effect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.goal.FloatGoal;
import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
Expand All @@ -38,7 +39,7 @@ public FireFlowerEntity(EntityType<? extends FireFlowerEntity> entityType, Level

@Override
protected void registerGoals() {
this.goalSelector.addGoal(1, new FloatGoal(this));
this.goalSelector.addGoal(1, new LookAtPlayerGoal(this, Player.class, 8.0F));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ public MushroomEntity(EntityType<? extends MushroomEntity> entityType, Level wor

@Override
protected void registerGoals() {
this.goalSelector.addGoal(1, new FloatGoal(this));
this.goalSelector.addGoal(2, new ContinuousStrollGoal(this, 0.7D));
this.goalSelector.addGoal(3, new LookAtPlayerGoal(this, Player.class, 8.0F));
this.goalSelector.addGoal(1, new ContinuousStrollGoal(this, 0.7D));
this.goalSelector.addGoal(2, new LookAtPlayerGoal(this, Player.class, 8.0F));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.ai.goal.FloatGoal;
import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.neoforged.neoforge.fluids.FluidType;
import software.bernie.geckolib.animatable.GeoAnimatable;
import software.bernie.geckolib.animatable.GeoEntity;
import software.bernie.geckolib.animatable.instance.AnimatableInstanceCache;
Expand All @@ -37,8 +40,8 @@ public SuperStarEntity(EntityType<? extends SuperStarEntity> entityType, Level w

@Override
protected void registerGoals() {
this.goalSelector.addGoal(1, new ContinuousJumpGoal(this));
this.goalSelector.addGoal(2, new FloatGoal(this));
this.goalSelector.addGoal(1, new LookAtPlayerGoal(this, Player.class, 8.0F));
this.goalSelector.addGoal(2, new ContinuousJumpGoal(this));
}

@Override
Expand Down Expand Up @@ -69,6 +72,13 @@ public void tick() {
this.level().broadcastEntityEvent(this, (byte) 114);
}

@Override
public void jumpInFluid(FluidType type) {
if (this.onGround())
this.setDeltaMovement(this.getDeltaMovement()
.add(0.0, this.getAttributeValue(Attributes.JUMP_STRENGTH), 0.0));
}

@Override
public void handleCollision(Entity entity) {
if (!this.level().isClientSide) {
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/wenxin2/marioverse/init/EntityRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.minecraft.world.level.levelgen.Heightmap;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.common.NeoForgeMod;
import net.neoforged.neoforge.event.entity.EntityAttributeCreationEvent;
import net.neoforged.neoforge.event.entity.RegisterSpawnPlacementsEvent;
import net.neoforged.neoforge.registries.DeferredHolder;
Expand Down Expand Up @@ -76,14 +77,17 @@ public static void registerEntityAttributes(EntityAttributeCreationEvent event)
AttributeSupplier.Builder genericMushroomAttribs = PathfinderMob.createMobAttributes()
.add(Attributes.MAX_HEALTH, 1)
.add(Attributes.MOVEMENT_SPEED, 0.4F)
.add(Attributes.SAFE_FALL_DISTANCE, 10.0F);
.add(Attributes.SAFE_FALL_DISTANCE, 10.0F)
.add(Attributes.WATER_MOVEMENT_EFFICIENCY, 0.3F);
AttributeSupplier.Builder genericStarAttribs = PathfinderMob.createMobAttributes()
.add(Attributes.JUMP_STRENGTH, 0.5F)
.add(Attributes.MAX_HEALTH, 1)
.add(Attributes.MOVEMENT_SPEED, 0.8F)
.add(Attributes.SAFE_FALL_DISTANCE, 10.0F);
.add(Attributes.SAFE_FALL_DISTANCE, 10.0F)
.add(Attributes.WATER_MOVEMENT_EFFICIENCY, 0.3F);
AttributeSupplier.Builder genericPowerUpAttribs = PathfinderMob.createMobAttributes()
.add(Attributes.MAX_HEALTH, 1);
.add(Attributes.MAX_HEALTH, 1)
.add(Attributes.WATER_MOVEMENT_EFFICIENCY, 0.3F);

event.put(EntityRegistry.FIRE_FLOWER.get(), genericPowerUpAttribs.build());
event.put(EntityRegistry.MUSHROOM.get(), genericMushroomAttribs.build());
Expand Down

0 comments on commit b149f37

Please sign in to comment.