From 527cc9f56c0e76db6937cc05fdbfd1a1480e02c6 Mon Sep 17 00:00:00 2001 From: Arathain <76701473+Arathain@users.noreply.github.com> Date: Sat, 27 Jul 2024 20:10:18 +0200 Subject: [PATCH] frame tag basics --- src/main/java/arathain/vigorem/Vigorem.java | 3 +++ .../arathain/vigorem/api/anim/Animation.java | 24 ++++++++++++++--- .../vigorem/api/frametag/FrameTag.java | 22 +++++++++++++++ .../vigorem/api/frametag/FrameTagData.java | 27 +++++++++++++++++++ .../arathain/vigorem/api/init/Animations.java | 6 +++-- .../arathain/vigorem/api/init/FrameTags.java | 20 ++++++++++++++ .../vigorem/mixin/PlayerEntityMixin.java | 5 +++- 7 files changed, 101 insertions(+), 6 deletions(-) create mode 100644 src/main/java/arathain/vigorem/api/frametag/FrameTag.java create mode 100644 src/main/java/arathain/vigorem/api/frametag/FrameTagData.java create mode 100644 src/main/java/arathain/vigorem/api/init/FrameTags.java diff --git a/src/main/java/arathain/vigorem/Vigorem.java b/src/main/java/arathain/vigorem/Vigorem.java index 383e304..eff031d 100644 --- a/src/main/java/arathain/vigorem/Vigorem.java +++ b/src/main/java/arathain/vigorem/Vigorem.java @@ -3,7 +3,9 @@ import arathain.vigorem.anim.EntityAnimationSyncPacket; import arathain.vigorem.api.AnimationPacket; import arathain.vigorem.api.init.Animations; +import arathain.vigorem.api.init.FrameTags; import arathain.vigorem.test.BrickShithouseHammerItem; +import com.google.common.reflect.Reflection; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.event.player.UseItemCallback; import net.fabricmc.fabric.api.item.v1.FabricItem; @@ -36,6 +38,7 @@ public class Vigorem implements ModInitializer { @Override public void onInitialize() { + Reflection.initialize(FrameTags.class); Animations.init(); if(FabricLoader.getInstance().isDevelopmentEnvironment()) { UseItemCallback.EVENT.register((player, world, hand) -> { diff --git a/src/main/java/arathain/vigorem/api/anim/Animation.java b/src/main/java/arathain/vigorem/api/anim/Animation.java index 3f3fc85..671c78f 100644 --- a/src/main/java/arathain/vigorem/api/anim/Animation.java +++ b/src/main/java/arathain/vigorem/api/anim/Animation.java @@ -4,6 +4,8 @@ import arathain.vigorem.anim.EntityQuery; import arathain.vigorem.anim.OffsetModelPart; import arathain.vigorem.api.Keyframe; +import arathain.vigorem.api.frametag.FrameTag; +import arathain.vigorem.api.frametag.FrameTagData; import net.minecraft.client.model.ModelPart; import net.minecraft.client.network.AbstractClientPlayerEntity; import net.minecraft.client.render.entity.model.PlayerEntityModel; @@ -16,11 +18,11 @@ import net.minecraft.util.math.Vec3d; import org.joml.Vector3f; -import java.util.List; -import java.util.Map; +import java.util.*; public abstract class Animation { public final Map> keyframes; + public final List frameTagData = new LinkedList<>(); private final int length; public int frame = 0; protected final EntityQuery entityQuery = new EntityQuery(); @@ -30,6 +32,11 @@ public Animation(int length, Map> keyframes) { this.keyframes = keyframes; } + public T putFrameTags(Collection frametags) { + frameTagData.addAll(frametags); + return (T) this; + } + public boolean shouldTransformHead() { return true; }; @@ -79,7 +86,18 @@ public boolean lockHeldItem(){ return false; } - public boolean isIFrame() {return false;} + public boolean hasFrameTag(FrameTag tag, float delta) { + for(FrameTagData data : frameTagData) { + if(data.getFrameTag().equals(tag)) { + return data.isActive(frame+delta); + } + } + return false; + } + + public boolean hasFrameTag(FrameTag tag) { + return hasFrameTag(tag, 0); + } public void rotateGlobal(MatrixStack matrices, float tickDelta) { String part = "global"; diff --git a/src/main/java/arathain/vigorem/api/frametag/FrameTag.java b/src/main/java/arathain/vigorem/api/frametag/FrameTag.java new file mode 100644 index 0000000..eda40d6 --- /dev/null +++ b/src/main/java/arathain/vigorem/api/frametag/FrameTag.java @@ -0,0 +1,22 @@ +package arathain.vigorem.api.frametag; + +public class FrameTag { + protected final String id; + + public FrameTag(String type, String namespace, String name, String subname) { + String add = subname != null && !subname.isEmpty() ? "$" + subname : ""; + this.id = type + "/" + namespace + ":" + name + add; + } + + public static FrameTag regularTag(String namespace, String name) { + return new FrameTag("stat", namespace, name, null); + } + public static FrameTag customTag(String type, String namespace, String name) { + return new FrameTag(type, namespace, name, null); + } + + + public String getId() { + return id; + } +} diff --git a/src/main/java/arathain/vigorem/api/frametag/FrameTagData.java b/src/main/java/arathain/vigorem/api/frametag/FrameTagData.java new file mode 100644 index 0000000..8235e59 --- /dev/null +++ b/src/main/java/arathain/vigorem/api/frametag/FrameTagData.java @@ -0,0 +1,27 @@ +package arathain.vigorem.api.frametag; + +public class FrameTagData { + protected final FrameTag frameTag; + protected final float[] frametimes; + + public FrameTagData(FrameTag frameTag, float[] frametimes) { + this.frameTag = frameTag; + this.frametimes = frametimes; + } + + public boolean isActive(float frame) { + int i = 1; + for(float f : frametimes) { + if(f < frame ) { + i++; + } else { + break; + } + } + return i % 2 == 0; + } + + public FrameTag getFrameTag() { + return frameTag; + } +} diff --git a/src/main/java/arathain/vigorem/api/init/Animations.java b/src/main/java/arathain/vigorem/api/init/Animations.java index 5ddddb0..be259e9 100644 --- a/src/main/java/arathain/vigorem/api/init/Animations.java +++ b/src/main/java/arathain/vigorem/api/init/Animations.java @@ -6,6 +6,7 @@ import arathain.vigorem.api.anim.Animation; import arathain.vigorem.api.Easing; import arathain.vigorem.api.Keyframe; +import arathain.vigorem.api.frametag.FrameTagData; import arathain.vigorem.test.SmashAnimation; import arathain.vigorem.test.TPoseAnimation; import net.minecraft.util.Identifier; @@ -123,9 +124,10 @@ public static void init() { cache.add(new Keyframe(Easing.QUAD_IN_OUT, Vigorem.ZERO, Vigorem.ZERO, Vigorem.ZERO, Vigorem.ZERO, 33)); SMASH_R.put("left_leg", new ArrayList<>(cache)); cache.clear(); + List f = List.of(new FrameTagData(FrameTags.INVULNERABILITY, new float[]{15f})); - put(Vigorem.id("smash_left"), () -> new SmashAnimation(33, SMASH_R, true)); - put(Vigorem.id("smash_right"), () -> new SmashAnimation(33, SMASH_R, false)); + put(Vigorem.id("smash_left"), () -> new SmashAnimation(33, SMASH_R, true).putFrameTags(f)); + put(Vigorem.id("smash_right"), () -> new SmashAnimation(33, SMASH_R, false).putFrameTags(f)); put(Vigorem.id("t_pose"), () -> new TPoseAnimation(30, TPOSE_START, 20, TPOSE_END)); cache.add(new Keyframe(Easing.QUAD_IN, new Vector3f(0f, 2f, 4f), deg(40f, 0f, 0f), Vigorem.ZERO, new Vector3f(0, -12, 0), 0.0f)); diff --git a/src/main/java/arathain/vigorem/api/init/FrameTags.java b/src/main/java/arathain/vigorem/api/init/FrameTags.java new file mode 100644 index 0000000..4303fff --- /dev/null +++ b/src/main/java/arathain/vigorem/api/init/FrameTags.java @@ -0,0 +1,20 @@ +package arathain.vigorem.api.init; + +import arathain.vigorem.api.anim.Animation; +import arathain.vigorem.api.frametag.FrameTag; +import net.minecraft.util.Identifier; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.Supplier; + +public interface FrameTags { + Map FRAME_TAGS = new HashMap<>(); + + FrameTag INVULNERABILITY = put(FrameTag.regularTag("vigorem", "iframe")); + + static T put(T tag) { + FRAME_TAGS.put(tag.getId(), tag); + return tag; + } +} diff --git a/src/main/java/arathain/vigorem/mixin/PlayerEntityMixin.java b/src/main/java/arathain/vigorem/mixin/PlayerEntityMixin.java index 09dbde8..6486c31 100644 --- a/src/main/java/arathain/vigorem/mixin/PlayerEntityMixin.java +++ b/src/main/java/arathain/vigorem/mixin/PlayerEntityMixin.java @@ -3,6 +3,7 @@ import arathain.vigorem.VigoremComponents; import arathain.vigorem.anim.AnimationComponent; import arathain.vigorem.api.anim.Animation; +import arathain.vigorem.api.init.FrameTags; import net.minecraft.entity.EntityType; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.damage.DamageSource; @@ -23,12 +24,14 @@ protected PlayerEntityMixin(EntityType entityType, World @Inject(method = "damage", at = @At("HEAD"), cancellable = true) public void vigorem$damage(DamageSource sauce, float dmg, CallbackInfoReturnable info) { Animation c = this.getComponent(VigoremComponents.ANIMATION).current; - if (c != null && c.isIFrame()) { + if (c != null && c.hasFrameTag(FrameTags.INVULNERABILITY)) { info.setReturnValue(false); } } + + @Inject(method = "tick", at = @At("TAIL")) public void vigorem$tick(CallbackInfo info) { if(!getWorld().isClient) {