Add 45 degree intervals to yaw change.
This commit is contained in:
@@ -159,6 +159,26 @@ public class Rotation {
|
||||
return newYaw;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the distance between a starting yaw and an offset yaw.
|
||||
* Distance can be negative if the offset yaw is left of the starting yaw.
|
||||
*
|
||||
* @param yaw The initial yaw
|
||||
* @param offsetYaw The offset yaw
|
||||
* @return The distance between the yaws
|
||||
*/
|
||||
public static float yawDistanceFromOffset(float yaw, float offsetYaw) {
|
||||
if ((yaw > 0 ^ offsetYaw > 0) && ((yaw > 90 || yaw < -90) ^ (offsetYaw > 90 || offsetYaw < -90))) {
|
||||
if (yaw < 0) {
|
||||
return 360 + (yaw - offsetYaw);
|
||||
} else {
|
||||
return 360 - (yaw - offsetYaw);
|
||||
}
|
||||
} else {
|
||||
return yaw - offsetYaw;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Yaw: " + yaw + ", Pitch: " + pitch;
|
||||
|
||||
@@ -660,12 +660,7 @@ public interface MovementHelper extends ActionCosts, Helper {
|
||||
)).setInput(Input.MOVE_FORWARD, true);
|
||||
}
|
||||
|
||||
static void moveTowardsWithoutRotation(IPlayerContext ctx, MovementState state, BlockPos dest) {
|
||||
float idealYaw = RotationUtils.calcRotationFromVec3d(
|
||||
ctx.playerHead(),
|
||||
VecUtils.getBlockPosCenter(dest),
|
||||
ctx.playerRotations()
|
||||
).getYaw();
|
||||
static void moveTowardsWithoutRotation(IPlayerContext ctx, MovementState state, float idealYaw) {
|
||||
MovementOption.getOptions(
|
||||
Mth.sin(ctx.playerRotations().getYaw() * DEG_TO_RAD_F),
|
||||
Mth.cos(ctx.playerRotations().getYaw() * DEG_TO_RAD_F),
|
||||
@@ -676,9 +671,39 @@ public interface MovementHelper extends ActionCosts, Helper {
|
||||
))).ifPresent(selection -> selection.setInputs(state));
|
||||
}
|
||||
|
||||
static void roundYaw(IPlayerContext ctx, MovementState state) {
|
||||
static void moveTowardsWithoutRotation(IPlayerContext ctx, MovementState state, BlockPos dest) {
|
||||
float idealYaw = RotationUtils.calcRotationFromVec3d(
|
||||
ctx.playerHead(),
|
||||
VecUtils.getBlockPosCenter(dest),
|
||||
ctx.playerRotations()
|
||||
).getYaw();
|
||||
moveTowardsWithoutRotation(ctx, state, idealYaw);
|
||||
}
|
||||
|
||||
static void moveTowardsWithSlightRotation(IPlayerContext ctx, MovementState state, BlockPos dest) {
|
||||
float idealYaw = RotationUtils.calcRotationFromVec3d(
|
||||
ctx.playerHead(),
|
||||
VecUtils.getBlockPosCenter(dest),
|
||||
ctx.playerRotations()
|
||||
).getYaw();
|
||||
moveTowardsWithoutRotation(ctx, state, idealYaw);
|
||||
float distance = Rotation.yawDistanceFromOffset(ctx.playerRotations().getYaw(), idealYaw) % 45f;
|
||||
float newYaw;
|
||||
if (distance > 0) {
|
||||
if (distance > 22.5f) {
|
||||
newYaw = -45f + distance;
|
||||
} else {
|
||||
newYaw = distance;
|
||||
}
|
||||
} else {
|
||||
if (distance < -22.5f) {
|
||||
newYaw = 45f + distance;
|
||||
} else {
|
||||
newYaw = distance;
|
||||
}
|
||||
}
|
||||
state.setTarget(new MovementTarget(new Rotation(
|
||||
Math.round(ctx.playerRotations().getYaw() / 45f) * 45f,
|
||||
ctx.playerRotations().getYaw() - newYaw,
|
||||
ctx.playerRotations().getPitch()
|
||||
), true));
|
||||
}
|
||||
|
||||
@@ -356,8 +356,7 @@ public class MovementTraverse extends Movement {
|
||||
}
|
||||
return state;
|
||||
}
|
||||
MovementHelper.roundYaw(ctx, state);
|
||||
MovementHelper.moveTowardsWithoutRotation(ctx, state, dest);
|
||||
MovementHelper.moveTowardsWithSlightRotation(ctx, state, dest);
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user