| 1 | package net.minecraft.client.model.effects;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 4 | import com.mojang.math.Axis;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.model.HumanoidModel;
|
| 8 | import net.minecraft.client.model.geom.ModelPart;
|
| 9 | import net.minecraft.client.renderer.entity.state.ArmedEntityRenderState;
|
| 10 | import net.minecraft.client.renderer.entity.state.HumanoidRenderState;
|
| 11 | import net.minecraft.core.component.DataComponents;
|
| 12 | import net.minecraft.util.Ease;
|
| 13 | import net.minecraft.util.Mth;
|
| 14 | import net.minecraft.world.entity.HumanoidArm;
|
| 15 | import net.minecraft.world.item.ItemStack;
|
| 16 | import net.minecraft.world.item.component.KineticWeapon;
|
| 17 | import net.minecraft.world.item.component.KineticWeapon.Condition;
|
| 18 |
|
| 19 | @Environment(EnvType.CLIENT)
|
| 20 | public class SpearAnimations {
|
| 21 | private static float progress(final float time, final float start, final float end) {
|
| 22 | return Mth.clamp(Mth.inverseLerp(time, start, end), 0.0F, 1.0F);
|
| 23 | }
|
| 24 |
|
| 25 | public static <T extends HumanoidRenderState> void thirdPersonHandUse(
|
| 26 | final ModelPart arm, final ModelPart head, final HumanoidArm holdingArm, final ItemStack item, final T state
|
| 27 | ) {
|
| 28 | int invert = holdingArm == HumanoidArm.RIGHT ? 1 : -1;
|
| 29 | arm.yRot = -0.1F * invert + head.yRot;
|
| 30 | arm.xRot = (float) (-Math.PI / 2) + head.xRot + 0.8F;
|
| 31 | if (state.isFallFlying || state.swimAmount > 0.0F) {
|
| 32 | arm.xRot -= 0.9599311F;
|
| 33 | }
|
| 34 |
|
| 35 | arm.yRot = (float) (Math.PI / 180.0) * Math.clamp((180.0F / (float)Math.PI) * arm.yRot, -60.0F, 60.0F);
|
| 36 | arm.xRot = (float) (Math.PI / 180.0) * Math.clamp((180.0F / (float)Math.PI) * arm.xRot, -120.0F, 30.0F);
|
| 37 | if (!(state.ticksUsingItem <= 0.0F) && (!state.isUsingItem || state.useItemHand.asArm(state.mainArm) == holdingArm)) {
|
| 38 | KineticWeapon kineticWeapon = item.get(DataComponents.KINETIC_WEAPON);
|
| 39 | if (kineticWeapon != null) {
|
| 40 | SpearAnimations.UseParams params = SpearAnimations.UseParams.fromKineticWeapon(kineticWeapon, state.ticksUsingItem);
|
| 41 | arm.yRot = arm.yRot + -invert * params.swayScaleFast() * (float) (Math.PI / 180.0) * params.swayIntensity() * 1.0F;
|
| 42 | arm.zRot = arm.zRot + -invert * params.swayScaleSlow() * (float) (Math.PI / 180.0) * params.swayIntensity() * 0.5F;
|
| 43 | arm.xRot = arm.xRot
|
| 44 | + (float) (Math.PI / 180.0)
|
| 45 | * (
|
| 46 | -40.0F * params.raiseProgressStart()
|
| 47 | + 30.0F * params.raiseProgressMiddle()
|
| 48 | + -20.0F * params.raiseProgressEnd()
|
| 49 | + 20.0F * params.lowerProgress()
|
| 50 | + 10.0F * params.raiseBackProgress()
|
| 51 | + 0.6F * params.swayScaleSlow() * params.swayIntensity()
|
| 52 | );
|
| 53 | }
|
| 54 | }
|
| 55 | }
|
| 56 |
|
| 57 | public static <S extends ArmedEntityRenderState> void thirdPersonUseItem(
|
| 58 | final S state, final PoseStack poseStack, final float timeHeld, final HumanoidArm arm, final ItemStack actualItem
|
| 59 | ) {
|
| 60 | KineticWeapon kineticWeapon = actualItem.get(DataComponents.KINETIC_WEAPON);
|
| 61 | if (kineticWeapon != null && timeHeld != 0.0F) {
|
| 62 | float attack = Ease.inQuad(progress(state.swingAnimation, 0.05F, 0.2F));
|
| 63 | float retract = Ease.inOutExpo(progress(state.swingAnimation, 0.4F, 1.0F));
|
| 64 | SpearAnimations.UseParams params = SpearAnimations.UseParams.fromKineticWeapon(kineticWeapon, timeHeld);
|
| 65 | int invert = arm == HumanoidArm.RIGHT ? 1 : -1;
|
| 66 | float raiseProgressModified = 1.0F - Ease.outBack(1.0F - params.raiseProgress());
|
| 67 | float itemInHandDepth = 0.125F;
|
| 68 | float hitFeedback = hitFeedbackAmount(state.ticksSinceKineticHitFeedback);
|
| 69 | poseStack.translate(0.0, -hitFeedback * 0.4, -kineticWeapon.forwardMovement() * (raiseProgressModified - params.raiseBackProgress()) + hitFeedback);
|
| 70 | poseStack.rotateAround(
|
| 71 | Axis.XN.rotationDegrees(70.0F * (params.raiseProgress() - params.raiseBackProgress()) - 40.0F * (attack - retract)), 0.0F, -0.03125F, 0.125F
|
| 72 | );
|
| 73 | poseStack.rotateAround(Axis.YP.rotationDegrees(invert * 90 * (params.raiseProgress() - params.swayProgress() + 3.0F * retract + attack)), 0.0F, 0.0F, 0.125F);
|
| 74 | }
|
| 75 | }
|
| 76 |
|
| 77 | public static <T extends HumanoidRenderState> void thirdPersonAttackHand(final HumanoidModel<T> model, final float animation, final HumanoidArm arm) {
|
| 78 | model.rightArm.yRot = model.rightArm.yRot - model.body.yRot;
|
| 79 | model.leftArm.yRot = model.leftArm.yRot - model.body.yRot;
|
| 80 | model.leftArm.xRot = model.leftArm.xRot - model.body.yRot;
|
| 81 | float prepare = Ease.inOutSine(progress(animation, 0.0F, 0.05F));
|
| 82 | float attack = Ease.inQuad(progress(animation, 0.05F, 0.2F));
|
| 83 | float retract = Ease.inOutExpo(progress(animation, 0.4F, 1.0F));
|
| 84 | model.getArm(arm).xRot += (90.0F * prepare - 120.0F * attack + 30.0F * retract) * (float) (Math.PI / 180.0);
|
| 85 | }
|
| 86 |
|
| 87 | public static <S extends ArmedEntityRenderState> void thirdPersonAttackItem(final S state, final PoseStack poseStack) {
|
| 88 | float animation = state.swingAnimation;
|
| 89 | if (!(animation <= 0.0F)) {
|
| 90 | KineticWeapon kineticWeapon = state.getMainHandItemStack().get(DataComponents.KINETIC_WEAPON);
|
| 91 | float jetForward = kineticWeapon != null ? kineticWeapon.forwardMovement() : 0.0F;
|
| 92 | float itemInHandDepth = 0.125F;
|
| 93 | float attack = Ease.inQuad(progress(animation, 0.05F, 0.2F));
|
| 94 | float retract = Ease.inOutExpo(progress(animation, 0.4F, 1.0F));
|
| 95 | poseStack.rotateAround(Axis.XN.rotationDegrees(70.0F * (attack - retract)), 0.0F, -0.125F, 0.125F);
|
| 96 | poseStack.translate(0.0F, jetForward * (attack - retract), 0.0F);
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | private static float hitFeedbackAmount(final float ticksSinceFeedbackStart) {
|
| 101 | return 0.4F * (Ease.outQuart(progress(ticksSinceFeedbackStart, 1.0F, 3.0F)) - Ease.inOutSine(progress(ticksSinceFeedbackStart, 3.0F, 10.0F)));
|
| 102 | }
|
| 103 |
|
| 104 | public static void firstPersonUse(
|
| 105 | final float ticksSinceKineticHitFeedback, final PoseStack poseStack, final float timeHeld, final HumanoidArm arm, final ItemStack itemStack
|
| 106 | ) {
|
| 107 | KineticWeapon kineticWeapon = itemStack.get(DataComponents.KINETIC_WEAPON);
|
| 108 | if (kineticWeapon != null) {
|
| 109 | SpearAnimations.UseParams params = SpearAnimations.UseParams.fromKineticWeapon(kineticWeapon, timeHeld);
|
| 110 | int invert = arm == HumanoidArm.RIGHT ? 1 : -1;
|
| 111 | poseStack.translate(
|
| 112 | invert * (params.raiseProgress() * 0.15F + params.raiseProgressEnd() * -0.05F + params.swayProgress() * -0.1F + params.swayScaleSlow() * 0.005F),
|
| 113 | params.raiseProgress() * -0.075F + params.raiseProgressMiddle() * 0.075F + params.swayScaleFast() * 0.01F,
|
| 114 | params.raiseProgressStart() * 0.05 + params.raiseProgressEnd() * -0.05 + params.swayScaleSlow() * 0.005F
|
| 115 | );
|
| 116 | poseStack.rotateAround(
|
| 117 | Axis.XP
|
| 118 | .rotationDegrees(
|
| 119 | -65.0F * Ease.inOutBack(params.raiseProgress()) - 35.0F * params.lowerProgress() + 100.0F * params.raiseBackProgress() + -0.5F * params.swayScaleFast()
|
| 120 | ),
|
| 121 | 0.0F,
|
| 122 | 0.1F,
|
| 123 | 0.0F
|
| 124 | );
|
| 125 | poseStack.rotateAround(
|
| 126 | Axis.YN.rotationDegrees(invert * (-90.0F * progress(params.raiseProgress(), 0.5F, 0.55F) + 90.0F * params.swayProgress() + 2.0F * params.swayScaleSlow())),
|
| 127 | invert * 0.15F,
|
| 128 | 0.0F,
|
| 129 | 0.0F
|
| 130 | );
|
| 131 | poseStack.translate(0.0F, -hitFeedbackAmount(ticksSinceKineticHitFeedback), 0.0F);
|
| 132 | }
|
| 133 | }
|
| 134 |
|
| 135 | public static void firstPersonAttack(final float attack, final PoseStack poseStack, final int invert, final HumanoidArm arm) {
|
| 136 | float startingAmount = Ease.inOutSine(progress(attack, 0.0F, 0.05F));
|
| 137 | float middleAmount = Ease.outBack(progress(attack, 0.05F, 0.2F));
|
| 138 | float endingAmount = Ease.inOutExpo(progress(attack, 0.4F, 1.0F));
|
| 139 | poseStack.translate(invert * 0.1F * (startingAmount - middleAmount), -0.075F * (startingAmount - endingAmount), 0.65F * (startingAmount - middleAmount));
|
| 140 | poseStack.rotateDegrees(Axis.XP, -70.0F * (startingAmount - endingAmount));
|
| 141 | poseStack.translate(0.0, 0.0, -0.25 * (endingAmount - middleAmount));
|
| 142 | }
|
| 143 |
|
| 144 | @Environment(EnvType.CLIENT)
|
| 145 | private record UseParams(
|
| 146 | float raiseProgress,
|
| 147 | float raiseProgressStart,
|
| 148 | float raiseProgressMiddle,
|
| 149 | float raiseProgressEnd,
|
| 150 | float swayProgress,
|
| 151 | float lowerProgress,
|
| 152 | float raiseBackProgress,
|
| 153 | float swayIntensity,
|
| 154 | float swayScaleSlow,
|
| 155 | float swayScaleFast
|
| 156 | ) {
|
| 157 | public static SpearAnimations.UseParams fromKineticWeapon(final KineticWeapon kineticWeapon, final float time) {
|
| 158 | int finishRaisingTick = kineticWeapon.delayTicks();
|
| 159 | int finishSwayingTick = kineticWeapon.dismountConditions().map(Condition::maxDurationTicks).orElse(0) + finishRaisingTick;
|
| 160 | int startSwayingTick = finishSwayingTick - 20;
|
| 161 | int finishLoweringTick = kineticWeapon.knockbackConditions().map(Condition::maxDurationTicks).orElse(0) + finishRaisingTick;
|
| 162 | int startLoweringTick = finishLoweringTick - 40;
|
| 163 | int finishRaisingBackTick = kineticWeapon.damageConditions().map(Condition::maxDurationTicks).orElse(0) + finishRaisingTick;
|
| 164 | float raiseProgress = SpearAnimations.progress(time, 0.0F, finishRaisingTick);
|
| 165 | float raiseProgressStart = SpearAnimations.progress(raiseProgress, 0.0F, 0.5F);
|
| 166 | float raiseProgressMiddle = SpearAnimations.progress(raiseProgress, 0.5F, 0.8F);
|
| 167 | float raiseProgressEnd = SpearAnimations.progress(raiseProgress, 0.8F, 1.0F);
|
| 168 | float swayProgress = SpearAnimations.progress(time, startSwayingTick, finishSwayingTick);
|
| 169 | float lowerProgress = Ease.outCubic(Ease.inOutElastic(SpearAnimations.progress(time - 20.0F, startLoweringTick, finishLoweringTick)));
|
| 170 | float raiseBackProgress = SpearAnimations.progress(time, finishRaisingBackTick - 5, finishRaisingBackTick);
|
| 171 | float swayIntensity = 2.0F * Ease.outCirc(swayProgress) - 2.0F * Ease.inCirc(raiseBackProgress);
|
| 172 | float swayScaleSlow = Mth.sin(time * 19.0F * (float) (Math.PI / 180.0)) * swayIntensity;
|
| 173 | float swayScaleFast = Mth.sin(time * 30.0F * (float) (Math.PI / 180.0)) * swayIntensity;
|
| 174 | return new SpearAnimations.UseParams(
|
| 175 | raiseProgress,
|
| 176 | raiseProgressStart,
|
| 177 | raiseProgressMiddle,
|
| 178 | raiseProgressEnd,
|
| 179 | swayProgress,
|
| 180 | lowerProgress,
|
| 181 | raiseBackProgress,
|
| 182 | swayIntensity,
|
| 183 | swayScaleSlow,
|
| 184 | swayScaleFast
|
| 185 | );
|
| 186 | }
|
| 187 | }
|
| 188 | }
|
| 189 |
|