Skip to content

Commit

Permalink
Tweak slime bounce parameters
Browse files Browse the repository at this point in the history
Instead of preserving 95% of speed, preserves 97.5%
Instead of the min fall height being 2, its now a min fall velocity if less than 3 (meaning you can bounce from a 1 block jump)
Might help with #4871
  • Loading branch information
KnightMiner committed May 21, 2022
1 parent 74ec777 commit ed7034c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraftforge.event.server.ServerStoppingEvent;
import slimeknights.tconstruct.common.Sounds;

import javax.annotation.Nullable;
import java.util.IdentityHashMap;

/** Logic for entities bouncing */
Expand All @@ -28,15 +29,15 @@ public static void init() {
* @param entity Entity to bounce
*/
public static void addBounceHandler(LivingEntity entity) {
addBounceHandler(entity, 0d);
addBounceHandler(entity, null);
}

/**
* Causes the entity to bounce, needed because the fall event will reset motion afterwards
* @param entity Entity to bounce
* @param bounce Bounce amount
*/
public static void addBounceHandler(LivingEntity entity, double bounce) {
public static void addBounceHandler(LivingEntity entity, @Nullable Vec3 bounce) {
// no fake players PlayerTick event
if (entity instanceof FakePlayer) {
return;
Expand All @@ -45,7 +46,7 @@ public static void addBounceHandler(LivingEntity entity, double bounce) {
BounceInfo info = BOUNCING_ENTITIES.get(entity);
if (info == null) {
BOUNCING_ENTITIES.put(entity, new BounceInfo(entity, bounce));
} else if (bounce != 0) {
} else if (bounce != null) {
// updated bounce if needed
info.bounce = bounce;
// add one to the tick as there is a 1 tick delay between falling and ticking for many entities
Expand All @@ -71,8 +72,10 @@ private static void onLivingTick(LivingUpdateEvent event) {

// if its the bounce tick, time to bounce. This is to circumvent the logic that resets y motion after landing
if (entity.tickCount == info.bounceTick) {
Vec3 motion = entity.getDeltaMovement();
entity.setDeltaMovement(motion.x, info.bounce, motion.z);
if (info.bounce != null) {
entity.setDeltaMovement(info.bounce);
info.bounce = null;
}
info.bounceTick = 0;
}

Expand All @@ -93,11 +96,11 @@ private static void onLivingTick(LivingUpdateEvent event) {
} else if (motionSq < info.lastMagSq) {
info.stopMagTick = 0;
// preserve 95% of former speed
double boost = Math.sqrt(info.lastMagSq / motionSq) * 0.95f;
double boost = Math.sqrt(info.lastMagSq / motionSq) * 0.975f;
if (boost > 1) {
entity.setDeltaMovement(motion.x * boost, motion.y, motion.z * boost);
entity.hasImpulse = true;
info.lastMagSq = info.lastMagSq * 0.95f * 0.95f;
info.lastMagSq = info.lastMagSq * 0.975f * 0.975f;
// play sound if we had a big angle change
double newAngle = Mth.atan2(motion.z, motion.x);
if (Math.abs(newAngle - info.lastAngle) > 1) {
Expand Down Expand Up @@ -132,8 +135,9 @@ private static void serverStopping(ServerStoppingEvent event) {

/** Data class to keep track of bouncing info for an entity */
private static class BounceInfo {
/** Velocity the entity should have, unused if 0 */
private double bounce;
/** Velocity the entity should have, unused if null */
@Nullable
private Vec3 bounce;
/** Time to update the entities velocity */
private int bounceTick;
/** Tick to stop entity magnitude changes */
Expand All @@ -147,9 +151,9 @@ private static class BounceInfo {
/** Last angle of motion, used for sound effects */
private double lastAngle;

public BounceInfo(LivingEntity entity, double bounce) {
public BounceInfo(LivingEntity entity, @Nullable Vec3 bounce) {
this.bounce = bounce;
if (bounce != 0) {
if (bounce != null) {
// add one to the tick as there is a 1 tick delay between falling and ticking for many entities
this.bounceTick = entity.tickCount + 1;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public BouncyModifier() {
private static void onFall(LivingFallEvent event) {
LivingEntity living = event.getEntityLiving();
// using fall distance as the event distance could be reduced by jump boost
if (living == null || living.fallDistance <= 2f) {
if (living == null || (living.getDeltaMovement().y > -0.3 && living.fallDistance < 3)) {
return;
}
// can the entity bounce?
Expand All @@ -47,16 +47,17 @@ private static void onFall(LivingFallEvent event) {
double gravity = living.getAttributeValue(ForgeMod.ENTITY_GRAVITY.get());
double time = Math.sqrt(living.fallDistance / gravity);
double velocity = gravity * time;
living.setDeltaMovement(motion.x / 0.95f, velocity, motion.z / 0.95f);
living.setDeltaMovement(motion.x / 0.975f, velocity, motion.z / 0.975f);
living.hurtMarked = true;

// preserve momentum
SlimeBounceHandler.addBounceHandler(living);
} else {
// for non-players, need to defer the bounce
// only slow down half as much when bouncing
living.setDeltaMovement(motion.x / 0.95f, motion.y * -0.9, motion.z / 0.95f);
SlimeBounceHandler.addBounceHandler(living, living.getDeltaMovement().y);
float factor = living.fallDistance < 2 ? -0.7f : -0.9f;
living.setDeltaMovement(motion.x / 0.975f, motion.y * factor, motion.z / 0.975f);
SlimeBounceHandler.addBounceHandler(living, living.getDeltaMovement());
}
// update airborn status
event.setDistance(0.0F);
Expand Down

0 comments on commit ed7034c

Please sign in to comment.