Skip to content

Commit

Permalink
frame tag basics
Browse files Browse the repository at this point in the history
  • Loading branch information
Arathain committed Jul 27, 2024
1 parent 7e45f34 commit 527cc9f
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/main/java/arathain/vigorem/Vigorem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) -> {
Expand Down
24 changes: 21 additions & 3 deletions src/main/java/arathain/vigorem/api/anim/Animation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, List<Keyframe>> keyframes;
public final List<FrameTagData> frameTagData = new LinkedList<>();
private final int length;
public int frame = 0;
protected final EntityQuery entityQuery = new EntityQuery();
Expand All @@ -30,6 +32,11 @@ public Animation(int length, Map<String, List<Keyframe>> keyframes) {
this.keyframes = keyframes;
}

public <T extends Animation> T putFrameTags(Collection<FrameTagData> frametags) {
frameTagData.addAll(frametags);
return (T) this;
}

public boolean shouldTransformHead() {
return true;
};
Expand Down Expand Up @@ -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";
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/arathain/vigorem/api/frametag/FrameTag.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
27 changes: 27 additions & 0 deletions src/main/java/arathain/vigorem/api/frametag/FrameTagData.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
6 changes: 4 additions & 2 deletions src/main/java/arathain/vigorem/api/init/Animations.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<FrameTagData> 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));
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/arathain/vigorem/api/init/FrameTags.java
Original file line number Diff line number Diff line change
@@ -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<String, FrameTag> FRAME_TAGS = new HashMap<>();

FrameTag INVULNERABILITY = put(FrameTag.regularTag("vigorem", "iframe"));

static <T extends FrameTag> T put(T tag) {
FRAME_TAGS.put(tag.getId(), tag);
return tag;
}
}
5 changes: 4 additions & 1 deletion src/main/java/arathain/vigorem/mixin/PlayerEntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,12 +24,14 @@ protected PlayerEntityMixin(EntityType<? extends LivingEntity> entityType, World
@Inject(method = "damage", at = @At("HEAD"), cancellable = true)
public void vigorem$damage(DamageSource sauce, float dmg, CallbackInfoReturnable<Boolean> 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) {
Expand Down

0 comments on commit 527cc9f

Please sign in to comment.