|
| 1 | +package mc.duzo.animation.generic; |
| 2 | + |
| 3 | +import net.minecraft.client.render.entity.animation.Animation; |
| 4 | +import net.minecraft.client.render.entity.model.EntityModel; |
| 5 | +import net.minecraft.entity.AnimationState; |
| 6 | +import net.minecraft.entity.LivingEntity; |
| 7 | +import net.minecraft.util.Identifier; |
| 8 | + |
| 9 | +import mc.duzo.animation.player.PlayerAnimationHelper; |
| 10 | +import mc.duzo.animation.registry.Identifiable; |
| 11 | + |
| 12 | +public abstract class AnimationHolder implements Identifiable { |
| 13 | + private final Identifier id; |
| 14 | + protected final AnimationState state; |
| 15 | + protected final Animation animation; |
| 16 | + protected final AnimationInfo info; |
| 17 | + |
| 18 | + protected AnimationHolder(Identifier id, Animation anim, AnimationInfo info) { |
| 19 | + this.id = id; |
| 20 | + this.state = new AnimationState(); |
| 21 | + this.animation = anim; |
| 22 | + this.info = info; |
| 23 | + } |
| 24 | + protected AnimationHolder(Identifier id, Animation anim) { |
| 25 | + this(id, anim, new AnimationInfo(VisibilityList.all(), AnimationInfo.Perspective.THIRD_PERSON_FRONT, AnimationInfo.Movement.DISABLE, AnimationInfo.Transform.ALL)); |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public Identifier id() { |
| 30 | + return this.id; |
| 31 | + } |
| 32 | + |
| 33 | + public void update(EntityModel<?> model, float progress, LivingEntity player) { |
| 34 | + // overwritten update method goes here |
| 35 | + |
| 36 | + if (this.isFinished(player)) { |
| 37 | + this.state.stop(); |
| 38 | + this.onFinished(player); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + if (!this.state.isRunning()) { |
| 43 | + this.onStart(player); |
| 44 | + } |
| 45 | + |
| 46 | + this.state.startIfNotRunning(player.age); |
| 47 | + } |
| 48 | + |
| 49 | + public boolean isFinished(LivingEntity entity) { |
| 50 | + if (this.animation.looping()) return false; // Looping animations should extend this class so they properly finish |
| 51 | + |
| 52 | + return this.getRunningSeconds() >= this.animation.lengthInSeconds(); |
| 53 | + } |
| 54 | + |
| 55 | + protected void onFinished(LivingEntity player) { |
| 56 | + |
| 57 | + } |
| 58 | + protected void onStart(LivingEntity player) { |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + protected float getRunningSeconds() { |
| 63 | + return PlayerAnimationHelper.getRunningSeconds(this.animation, this.state.getTimeRunning()); |
| 64 | + } |
| 65 | + public Animation getAnimation() { |
| 66 | + return animation; |
| 67 | + } |
| 68 | + |
| 69 | + public AnimationInfo getInfo() { |
| 70 | + return this.info; |
| 71 | + } |
| 72 | +} |
0 commit comments