Skip to content

Commit

Permalink
Add AnimationManager And AnimationData
Browse files Browse the repository at this point in the history
  • Loading branch information
FirstMegaGame4 committed Apr 18, 2024
1 parent e80d1cc commit 6d09912
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.mmodding.mmodding_lib.library.client.render.entity.animation;

import net.minecraft.entity.AnimationState;
import net.minecraft.entity.Entity;

// will move that to a better package since it's not a client thing, but that will be in a next version
public class AnimationData {

public final AnimationState moving = new AnimationState();
public final AnimationState idle = new AnimationState();
public final AnimationState falling = new AnimationState();
public final AnimationState dodge = new AnimationState();

public int fallingAge;
public int fallingCount;

public AnimationData(Entity entity) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.mmodding.mmodding_lib.library.client.render.entity.animation;

import com.mmodding.mmodding_lib.library.client.utils.AnimationUtils;
import net.minecraft.client.model.ModelPart;
import net.minecraft.client.render.animation.Animation;
import net.minecraft.client.render.entity.model.SinglePartEntityModel;
import net.minecraft.entity.AnimationState;

public class AnimationManager {

private final SinglePartEntityModel<?> model;

private final Animation moving;
private final Animation idle;
private final Animation falling;
private final Animation dodge;

public AnimationManager(SinglePartEntityModel<?> model, Animation moving, Animation idle, Animation falling, Animation dodge) {
this.model = model;
this.moving = moving;
this.idle = idle;
this.falling = falling;
this.dodge = dodge;
}

private void updateFall(AnimationData data, int age, GroundChecker groundChecker) {
if (groundChecker.isInAir()) {
if (data.fallingAge != age) {
data.fallingAge = age;
data.fallingCount++;
}
}
else {
data.fallingCount = 0;
}
}

private void switchAnimation(Animation animation, AnimationState state, float animationProgress) {
AnimationUtils.updateAnimation(this.model, animation, state, animationProgress);
// data.transition = 20;
}

public void handle(AnimationData data, float animationProgress, int age, GroundChecker groundChecker, MovingChecker movingChecker) {
this.model.getPart().traverse().forEach(ModelPart::resetTransform);
if (data.dodge.isAnimating()) {
this.switchAnimation(this.dodge, data.dodge, animationProgress);
}
else {
this.updateFall(data, age, groundChecker);
if (data.fallingCount <= 3) {
if (movingChecker.isMoving()) {
this.switchAnimation(this.moving, data.moving, animationProgress);
} else {
this.switchAnimation(this.idle, data.idle, animationProgress);
}
}
}
}

@FunctionalInterface
public interface GroundChecker {

boolean isOnGround();

default boolean isInAir() {
return !this.isOnGround();
}
}

@FunctionalInterface
public interface MovingChecker {

boolean isMoving();

default boolean isStatic() {
return !this.isMoving();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ private void tick(CallbackInfo ci) {

@WrapOperation(method = "updatePostDeath", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;sendEntityStatus(Lnet/minecraft/entity/Entity;B)V"))
private void conditionallyCancelDeathAnimationFirstPart(World instance, Entity entity, byte status, Operation<Void> original) {
if (!(entity instanceof DeathAnimation animation) || animation.executeDeathAnimation() != null) {
if (!(entity instanceof DeathAnimation animation) || animation.executeDeathAnimation() == null) {
original.call(instance, entity, status);
}
}

@WrapOperation(method = "updatePostDeath", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/LivingEntity;remove(Lnet/minecraft/entity/Entity$RemovalReason;)V"))
private void conditionallyCancelDeathAnimationSecondPart(LivingEntity instance, Entity.RemovalReason removalReason, Operation<Void> original) {
if (!(instance instanceof DeathAnimation animation) || animation.executeDeathAnimation() != null) {
if (!(instance instanceof DeathAnimation animation) || animation.executeDeathAnimation() == null) {
original.call(instance, removalReason);
}
}
Expand All @@ -60,6 +60,9 @@ private void conditionallyCancelDeathAnimationSecondPart(LivingEntity instance,
private void updateDeath(CallbackInfo ci) {
LivingEntity livingEntity = (LivingEntity) (Object) this;
if (livingEntity instanceof DeathAnimation deathAnimation) {
if (deathAnimation.executeDeathAnimation() != null && this.deathTime - 1 == 0) {
deathAnimation.executeDeathAnimation().run();
}
if (this.deathTime == deathAnimation.getDeathTime() && !this.getWorld().isClient()) {
this.world.sendEntityStatus(livingEntity, EntityStatuses.ADD_DEATH_PARTICLES);
this.remove(Entity.RemovalReason.KILLED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,24 @@ public class LivingEntityRendererMixin<T extends LivingEntity> {

@ModifyExpressionValue(method = "getOverlay", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/OverlayTexture;getV(Z)I"))
private static int conditionallyCancelDeathAnimationRedOverlay(int original, LivingEntity entity, float whiteOverlayProgress) {
if (!(entity instanceof DeathAnimation animation) || animation.executeDeathAnimation() != null) {
if (!(entity instanceof DeathAnimation animation)) {
return original;
}
else {
boolean bool = entity.hurtTime > 0 || (animation.applyRedOverlayOnDeath() && entity.deathTime > 0);
boolean bool;
if (animation.applyRedOverlayOnDeath()) {
bool = entity.hurtTime > 0 || entity.deathTime > 0;
}
else {
bool = entity.hurtTime > 0;
}
return OverlayTexture.packUv(OverlayTexture.getU(whiteOverlayProgress), OverlayTexture.getV(bool));
}
}

@WrapOperation(method = "setupTransforms", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/util/math/MatrixStack;multiply(Lnet/minecraft/util/math/Quaternion;)V", ordinal = 1))
private void conditionallyCancelDeathAnimationTransform(MatrixStack instance, Quaternion quaternion, Operation<Void> original, @Local(argsOnly = true) T entity) {
if (!(entity instanceof DeathAnimation animation) || animation.executeDeathAnimation() != null) {
if (!(entity instanceof DeathAnimation animation) || animation.executeDeathAnimation() == null) {
original.call(instance, quaternion);
}
}
Expand Down

0 comments on commit 6d09912

Please sign in to comment.