Skip to content

Commit

Permalink
Redid how state machine stuff is defined (it compiles now!)
Browse files Browse the repository at this point in the history
- Moved state conditions and state poses to the state machine key builder and enums respectively
  • Loading branch information
Trainguy9512 committed Mar 21, 2024
1 parent 8a61980 commit aa184ae
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 186 deletions.
Expand Up @@ -9,6 +9,7 @@
import com.trainguy9512.animationoverhaul.util.animation.JointSkeleton;
import com.trainguy9512.animationoverhaul.animation.data.AnimationDataContainer;
import com.trainguy9512.animationoverhaul.animation.data.TimelineGroupData;
import com.trainguy9512.animationoverhaul.util.time.Easing;
import net.minecraft.client.Minecraft;
import net.minecraft.client.model.PlayerModel;
import net.minecraft.client.model.geom.PartPose;
Expand All @@ -18,6 +19,9 @@
import net.minecraft.world.item.ItemStack;
import org.joml.Vector3f;

import java.util.function.BiFunction;
import java.util.function.Function;

public class FirstPersonPlayerJointAnimator extends LivingEntityJointAnimator<LocalPlayer, PlayerModel<LocalPlayer>, FirstPersonPlayerJointAnimator.FPPlayerLocators> {

public static FirstPersonPlayerJointAnimator INSTANCE = new FirstPersonPlayerJointAnimator();
Expand Down Expand Up @@ -78,94 +82,52 @@ public enum FPPlayerLocators {
public static final AnimationVariableKey<Boolean> IS_MINING = AnimationVariableKey.of(() -> false).setIdentifier("is_mining").build();
public static final AnimationVariableKey<Boolean> IS_FALLING = AnimationVariableKey.of(() -> false).setIdentifier("is_falling").build();
public static final AnimationVariableKey<Boolean> IS_JUMPING = AnimationVariableKey.of(() -> false).setIdentifier("is_jumping").build();

public static final AnimationPoseSamplerKey<AnimationSequencePlayer> IDLE_SEQUENCE_PLAYER = AnimationPoseSamplerKey.of(() -> AnimationSequencePlayer.of(ANIMATION_FP_PLAYER_IDLE).build()).setIdentifier("idle_sequence_player").build();



enum TestStates {
IDLE,
MOVING
}






/*
enum ItemSwitchStates {
EMPTY,
EMPTY_RAISING,
BASIC_ITEM,
BASIC_ITEM_RAISING,
LOWERING
}
private static final AnimationStateMachine<ItemSwitchStates> MAINHAND_ITEMSWITCH_STATE_MACHINE = AnimationStateMachine.of("main_hand_state_machine", ItemSwitchStates.values())
.addStateTransition(ItemSwitchStates.EMPTY, ItemSwitchStates.LOWERING, 2)
.addStateTransition(ItemSwitchStates.LOWERING, ItemSwitchStates.EMPTY_RAISING, 1, 2)
.addStateTransition(ItemSwitchStates.EMPTY_RAISING, ItemSwitchStates.EMPTY, 2)
.addStateTransition(ItemSwitchStates.BASIC_ITEM, ItemSwitchStates.LOWERING, 2)
.addStateTransition(ItemSwitchStates.LOWERING, ItemSwitchStates.BASIC_ITEM_RAISING, 1, 1)
.addStateTransition(ItemSwitchStates.BASIC_ITEM_RAISING, ItemSwitchStates.BASIC_ITEM, 2);
enum JumpingStates {
JUMPING,
FALLING,
LANDING,
ON_GROUND
}
private static final AnimationStateMachine<JumpingStates> JUMP_STATE_MACHINE = AnimationStateMachine.of("jump_state_machine", JumpingStates.values())
.addStateTransition(JumpingStates.ON_GROUND, JumpingStates.JUMPING, 1, 1)
.addStateTransition(JumpingStates.ON_GROUND, JumpingStates.FALLING, 3, 2)
.addStateTransition(JumpingStates.JUMPING, JumpingStates.FALLING, 4, 1)
.addStateTransition(JumpingStates.JUMPING, JumpingStates.LANDING, 1, 2)
.addStateTransition(JumpingStates.FALLING, JumpingStates.LANDING, 1, 1)
.addStateTransition(JumpingStates.LANDING, JumpingStates.JUMPING, 1, 2)
.addStateTransition(JumpingStates.LANDING, JumpingStates.FALLING, 3, 3)
.addStateTransition(JumpingStates.LANDING, JumpingStates.ON_GROUND, 4, 1);
enum MiningStates {
IDLE,
BEGIN,
LOOPING
public static final AnimationVariableKey<Float> WALK_SPEED = AnimationVariableKey.of(() -> 0f).setIdentifier("walk_speed").build();

public static final AnimationPoseSamplerKey<AnimationSequencePlayer> IDLE_SEQUENCE_PLAYER = AnimationPoseSamplerKey.of(() -> AnimationSequencePlayer.of(ANIMATION_FP_PLAYER_IDLE)
.setPlayRate(0)
.setStartTime(0)
.build()).setIdentifier("idle_sequence_player").build();
public static final AnimationPoseSamplerKey<AnimationSequencePlayer> IDLE_SEQUENCE_PLAYER_ALT = AnimationPoseSamplerKey.of(() -> AnimationSequencePlayer.of(ANIMATION_FP_PLAYER_IDLE)
.setPlayRate(0)
.setStartTime(20)
.build()).setIdentifier("idle_sequence_player").build();

public static final AnimationPoseSamplerKey<AnimationStateMachine<TestStates>> TEST_STATE_MACHINE = AnimationPoseSamplerKey.of(() -> AnimationStateMachine.of("test_state_machine", TestStates.values())
.addStateTransition(TestStates.IDLE, TestStates.MOVING, AnimationStateMachine.StateTransition.of(
animationDataContainer -> animationDataContainer.getAnimationVariable(WALK_SPEED).get() > 0.1F)
.setTransitionTime(5)
.setEasing(Easing.CubicBezier.bezierInOutSine())
.build())
.addStateTransition(TestStates.MOVING, TestStates.IDLE, AnimationStateMachine.StateTransition.of(
animationDataContainer -> animationDataContainer.getAnimationVariable(WALK_SPEED).get() < 0.1F)
.setTransitionTime(10)
.setEasing(Easing.CubicBezier.bezierOutSine())
.build())
.build()).build();


public enum TestStates implements AnimationStateMachine.StateEnum {
IDLE {
@Override
public <L extends Enum<L>> BiFunction<AnimationDataContainer, JointSkeleton<L>, AnimationPose<L>> getStatePose() {
return (animationDataContainer, fpPlayerLocatorsJointSkeleton) -> animationDataContainer.getPoseSampler(IDLE_SEQUENCE_PLAYER).sample(fpPlayerLocatorsJointSkeleton);
}
},
MOVING {
@Override
public <L extends Enum<L>> BiFunction<AnimationDataContainer, JointSkeleton<L>, AnimationPose<L>> getStatePose() {
return (animationDataContainer, fpPlayerLocatorsJointSkeleton) -> animationDataContainer.getPoseSampler(IDLE_SEQUENCE_PLAYER_ALT).sample(fpPlayerLocatorsJointSkeleton);
}
}
}
private static final AnimationStateMachine<MiningStates> MINING_STATE_MACHINE = AnimationStateMachine.of("mining_state_machine", MiningStates.values())
.addStateTransition(MiningStates.IDLE, MiningStates.BEGIN, 2)
.addStateTransition(MiningStates.BEGIN, MiningStates.LOOPING, 1)
.addStateTransition(MiningStates.BEGIN, MiningStates.IDLE, 2)
.addStateTransition(MiningStates.LOOPING, MiningStates.IDLE, 2);

*/


//private static final AnimationMontageTrack MAIN_HAND_EMPTY_PUNCH_MONTAGE_TRACK = AnimationMontageTrack.of("main_hand_empty_punch_montage_track");
/*
private static final AnimationMontage MAIN_HAND_EMPTY_PUNCH_MONTAGE = AnimationMontage.of(ANIMATION_FP_RIGHT_EMPTY_PUNCH)
.setLength(TickTimeUtils.ticksFromMayaFrames(10F))
.setBlendInDuration(1)
.setBlendOutDuration(TickTimeUtils.ticksFromMayaFrames(8F));
private static final AnimationMontage MAIN_HAND_EMPTY_USE_ITEM_MONTAGE = AnimationMontage.of(ANIMATION_FP_RIGHT_EMPTY_USE_ITEM)
.setLength(TickTimeUtils.ticksFromMayaFrames(9F))
.setBlendInDuration(1)
.setBlendOutDuration(TickTimeUtils.ticksFromMayaFrames(5F));

*/



/*
private static final AnimationBlendSpacePlayer MAIN_HAND_EMPTY_WALK_BLENDSPACE_PLAYER = AnimationBlendSpacePlayer.of("main_hand_empty_walk_blendspace_player")
.addEntry(0, ANIMATION_FP_RIGHT_EMPTY_WALK, 0F)
.addEntry(2, ANIMATION_FP_RIGHT_EMPTY_WALK, 2.9F);
*/


public FirstPersonPlayerJointAnimator(){
super();
}
Expand Down Expand Up @@ -194,14 +156,14 @@ public AnimationPose<FPPlayerLocators> calculatePose(LocalPlayer localPlayer, An
animationDataContainer.getAnimationVariable(MAIN_HAND_ITEM).set(localPlayer.getMainHandItem().copy());
//setEntityAnimationVariable(MAIN_HAND_ITEM, this.livingEntity.getMainHandItem().copy());

AnimationPose<FPPlayerLocators> pose = sampleAnimationState(TEST_IDLE_SEQUENCE_PLAYER);
AnimationPose<FPPlayerLocators> pose = animationDataContainer.getPoseSampler(TEST_STATE_MACHINE).sample(this.getJointSkeleton());



pose = dampenArmRotation(pose);
pose = dampenArmRotation(pose, animationDataContainer);


Vector3f rotation = new Vector3f(Mth.sin(getEntityAnimationVariable(TIME_TEST) * 0.2F) * Mth.HALF_PI * 0.7f, 0, 0);
Vector3f rotation = new Vector3f(Mth.sin(animationDataContainer.getAnimationVariable(TIME_TEST).get() * 0.2F) * Mth.HALF_PI * 0.7f, 0, 0);
//Vector3f translation = new Vector3f(Mth.sin(getEntityAnimationVariable(TIME_TEST) * 1.3F) * 3F, 0, 0);
//pose.translateJoint(FPPlayerLocators.rightArm, translation, AnimationPose.TransformSpace.ENTITY, false);
//pose.rotateJoint(FPPlayerLocators.rightArm, rotation, AnimationPose.TransformSpace.ENTITY, false);
Expand All @@ -213,9 +175,9 @@ public AnimationPose<FPPlayerLocators> calculatePose(LocalPlayer localPlayer, An
/*
Get the pose with the added dampened camera rotation
*/
private AnimationPose<FPPlayerLocators> dampenArmRotation(AnimationPose<FPPlayerLocators> pose){
Vector3f cameraRotation = getEntityAnimationVariable(CAMERA_ROTATION);
Vector3f dampenedCameraRotation = getEntityAnimationVariable(DAMPENED_CAMERA_ROTATION);
private AnimationPose<FPPlayerLocators> dampenArmRotation(AnimationPose<FPPlayerLocators> pose, AnimationDataContainer animationDataContainer){
Vector3f cameraRotation = animationDataContainer.getAnimationVariable(CAMERA_ROTATION).get();
Vector3f dampenedCameraRotation = animationDataContainer.getAnimationVariable(DAMPENED_CAMERA_ROTATION).get();

Vector3f cameraDampWeight = new Vector3f(0.6F, 0.3F, 0.1F);

Expand All @@ -234,26 +196,25 @@ private AnimationPose<FPPlayerLocators> dampenArmRotation(AnimationPose<FPPlayer


@Override
public void tick(LocalPlayer localPlayer, AnimationDataContainer entityAnimationData){
public void tick(LocalPlayer localPlayer, AnimationDataContainer animationDataContainer){



animationDataContainer.getAnimationVariable(WALK_SPEED).set(this.getWalkAnimationSpeed(localPlayer));
animationDataContainer.getAnimationVariable(TIME_TEST).set(animationDataContainer.getAnimationVariable(TIME_TEST).get() + 1);

this.setEntityAnimationVariable(TIME_TEST, this.getEntityAnimationVariable(TIME_TEST) + 1);



/*
Tick the dampened camera rotation.
*/

//Tick the dampened camera rotation.
Vector3f dampenSpeed = new Vector3f(0.5F, 0.5F, 0.2F);

// First, set the target camera rotation from the living entity.
Vector3f targetRotation = new Vector3f(this.livingEntity.getXRot(), this.livingEntity.getYRot(), this.livingEntity.getYRot());
setEntityAnimationVariable(CAMERA_ROTATION, targetRotation);
Vector3f targetRotation = new Vector3f(localPlayer.getXRot(), localPlayer.getYRot(), localPlayer.getYRot());
animationDataContainer.getAnimationVariable(CAMERA_ROTATION).set(targetRotation);


Vector3f dampenedCameraRotation = getEntityAnimationVariable(DAMPENED_CAMERA_ROTATION);
Vector3f dampenedCameraRotation = animationDataContainer.getAnimationVariable(DAMPENED_CAMERA_ROTATION).get();

// If the dampened camera rotation is 0 (which is what it is upon initialization), set it to the target
if(dampenedCameraRotation.x() == 0F && dampenedCameraRotation.y() == 0F){
Expand All @@ -267,8 +228,7 @@ public void tick(LocalPlayer localPlayer, AnimationDataContainer entityAnimation
);
//dampenedCameraRotation.lerp(targetRotation, 0.5F);
}
setEntityAnimationVariable(DAMPENED_CAMERA_ROTATION, dampenedCameraRotation);

animationDataContainer.getAnimationVariable(DAMPENED_CAMERA_ROTATION).set(dampenedCameraRotation);

}

Expand All @@ -283,7 +243,7 @@ public void tickExternal(){
animationDataContainer.tickAllPoseSamplers();

if(this.localBakedPose == null){
this.localBakedPose = new BakedAnimationPose();
this.localBakedPose = new BakedAnimationPose<>();
this.localBakedPose.setPose(AnimationPose.of(this.jointSkeleton));
}
if(!this.localBakedPose.hasPose){
Expand All @@ -292,7 +252,7 @@ public void tickExternal(){
}
this.localBakedPose.pushToOld();

AnimationPose animationPose = this.calculatePose(player, animationDataContainer);
AnimationPose<FPPlayerLocators> animationPose = this.calculatePose(player, animationDataContainer);
if (animationPose == null){
animationPose = AnimationPose.of(this.jointSkeleton);
}
Expand All @@ -301,11 +261,11 @@ public void tickExternal(){



this.localBakedPose.setPose(new AnimationPose(animationPose));
this.localBakedPose.setPose(new AnimationPose<>(animationPose));
}

private boolean compareVariableItemStackWithEntityItemStack(AnimationVariableKey<ItemStack> itemStackDataKey, ItemStack entityItemStack){
ItemStack currentItemStack = getEntityAnimationVariable(itemStackDataKey);
private boolean compareVariableItemStackWithEntityItemStack(AnimationVariableKey<ItemStack> itemStackDataKey, ItemStack entityItemStack, AnimationDataContainer animationDataContainer){
ItemStack currentItemStack = animationDataContainer.getAnimationVariable(itemStackDataKey).get();
if(currentItemStack.getItem() != null && entityItemStack.getItem() == null || currentItemStack.getItem() == null && entityItemStack.getItem() != null) {
return true;
}
Expand Down
Expand Up @@ -89,7 +89,7 @@ public void tickAllPoseSamplers(){
@SuppressWarnings("unchecked")
public <P extends PoseSampler> P getPoseSampler(AnimationPoseSamplerKey<P> animationPoseSamplerKey){
if(!this.getPoseSamplerMap().containsKey(animationPoseSamplerKey)){
this.getPoseSamplerMap().put(animationPoseSamplerKey, animationPoseSamplerKey.getSuppliedDefaultValue());
this.getPoseSamplerMap().put(animationPoseSamplerKey, animationPoseSamplerKey.getSuppliedDefaultValue(this));
}
return (P) this.getPoseSamplerMap().get(animationPoseSamplerKey);
}
Expand Down
Expand Up @@ -74,13 +74,17 @@ private Supplier<P> getDefaultValueSupplier(){

/**
* Returns a new {@link PoseSampler} created from the {@link AnimationPoseSamplerKey#defaultValue}
* {@link Supplier}, and then sets its identifier to be the same as this key's identifier
* {@link Supplier}, and then sets its identifier to be the same as this key's identifier and
* sets its animation data container parent to be that of the supplied animation data container
*
* @param animationDataContainer the {@link AnimationDataContainer} parent
*
* @return a pose sample of type {@link P}
*/
public P getSuppliedDefaultValue(){
public P getSuppliedDefaultValue(AnimationDataContainer animationDataContainer){
P poseSampler = this.getDefaultValueSupplier().get();
poseSampler.setIdentifier(this.getIdentifier());
poseSampler.setAnimationDataContainer(animationDataContainer);
return poseSampler;
}

Expand Down
Expand Up @@ -13,10 +13,10 @@

public class AnimationSequencePlayer extends TimeBasedPoseSampler {

private boolean looping = true;
private boolean looping;
private ResourceLocation resourceLocation;
private float frameLength;
private float startTime = 0;
private float startTime;
private float endTime;

HashMap<String, AnimNotify> animNotifyMap = Maps.newHashMap();
Expand All @@ -28,6 +28,8 @@ private AnimationSequencePlayer(Builder<?> builder) {
this.frameLength = builder.frameLength;
this.startTime = builder.startTime;
this.endTime = builder.endTime;

this.timeElapsed = startTime;
}

public static Builder<?> of(ResourceLocation resourceLocation){
Expand All @@ -37,7 +39,7 @@ public static Builder<?> of(ResourceLocation resourceLocation){

public static class Builder<B extends Builder<B>> extends TimeBasedPoseSampler.Builder<B> {

private boolean looping = false;
private boolean looping = true;
private final ResourceLocation resourceLocation;
private final float frameLength;
private float startTime = 0f;
Expand Down

0 comments on commit aa184ae

Please sign in to comment.