Skip to content

Commit

Permalink
Animation Data key rework
Browse files Browse the repository at this point in the history
- Condensed driver key and pose sampler key into one class since they do the same thing.
  • Loading branch information
Trainguy9512 committed Feb 7, 2025
1 parent 375429c commit 49dcb85
Show file tree
Hide file tree
Showing 18 changed files with 94 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public enum TestStates {
MOVING
}

public static final PoseSamplerKey<AnimationStateMachine<TestStates>> TEST_STATE_MACHINE = PoseSamplerKey.builder(() -> AnimationStateMachine.of(TestStates.values())
public static final PoseSamplerKey<AnimationStateMachine<TestStates>> TEST_STATE_MACHINE = PoseSamplerKey.builder(() -> AnimationStateMachine.builder(TestStates.values())
.addStateTransition(TestStates.IDLE, TestStates.MOVING, AnimationStateMachine.StateTransition.builder(
animationDataContainer -> animationDataContainer.get(WALK_SPEED) > 0.1F)
.setTransitionDuration(5)
Expand All @@ -90,12 +90,12 @@ public enum TestStates {
.build()).build();

public static final PoseSamplerKey<AnimationSequencePlayer> IDLE_SEQUENCE_PLAYER = PoseSamplerKey.builder(
() -> AnimationSequencePlayer.of(ANIMATION_FP_PLAYER_IDLE)
() -> AnimationSequencePlayer.builder(ANIMATION_FP_PLAYER_IDLE)
.setPlayRate(0)
.setStartTime(0)
.build()).setIdentifier("idle_sequence_player").build();
public static final PoseSamplerKey<AnimationSequencePlayer> IDLE_SEQUENCE_PLAYER_ALT = PoseSamplerKey.builder(
() -> AnimationSequencePlayer.of(ANIMATION_FP_PLAYER_IDLE)
() -> AnimationSequencePlayer.builder(ANIMATION_FP_PLAYER_IDLE)
.setPlayRate(1)
.setStartTime(20)
.addProgressTimeOnActiveStates(TEST_STATE_MACHINE, TestStates.MOVING)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.trainguy9512.animationoverhaul.animation.data;

import com.trainguy9512.animationoverhaul.animation.pose.sample.PoseSampler;

import java.util.function.Supplier;

/**
* Represents a key for associating with data within pose samplers and animation driver containers
* <p>
* These keys are used for storing the default value of a type of data, and then accessing it
* whenever an instance is needed.
* <p>
*
* @param <D> The type of data associated with the key
* @param identifier String identifier used for debugging
* @param dataSupplier The default value of the key
*
* @see PoseSampler
* @see AnimationDriverContainer
*/
public record AnimationDataKey<D>(String identifier, Supplier<D> dataSupplier) {

public AnimationDataKey<D> of(String identifier, Supplier<D> dataSupplier){
return new AnimationDataKey<>(identifier, dataSupplier);
}

public D createInstance(){
return this.dataSupplier.get();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.trainguy9512.animationoverhaul.animation.data;

import com.google.common.collect.Maps;
import com.trainguy9512.animationoverhaul.animation.pose.sample.*;

import java.util.*;
import java.util.function.Supplier;
Expand All @@ -12,13 +11,10 @@
* <li>Animation Variables</li>
* <li>Pose Samplers</li>
* </ul>
*
* @see AnimationDriverKey
* @see PoseSamplerKey
*/
public class AnimationDriverContainer {

private final HashMap<AnimationDriverKey<?>, AnimationDriver<?>> animationDrivers;
private final HashMap<AnimationDataKey<?>, AnimationDriver<?>> animationDrivers;

public AnimationDriverContainer(){
this.animationDrivers = Maps.newHashMap();
Expand All @@ -27,11 +23,11 @@ public AnimationDriverContainer(){
/**
* Retrieves value of animation driver from the given key.
*
* @param dataKey the {@link AnimationDriverKey} attached to the desired {@link AnimationDriver}
* @param dataKey the {@link AnimationDataKey} attached to the desired {@link AnimationDriver}
*
* @return an {@link AnimationDriver} object reference
*/
public <D> D get(AnimationDriverKey<D> dataKey) {
public <D> D get(AnimationDataKey<D> dataKey) {
return this.getDriver(dataKey).get();
}

Expand All @@ -41,12 +37,12 @@ public <D> D get(AnimationDriverKey<D> dataKey) {
* @param dataKey Data key value of param
* @param value New value to set
*/
public <D> void set(AnimationDriverKey<D> dataKey, D value){
public <D> void set(AnimationDataKey<D> dataKey, D value){
this.getDriver(dataKey).set(value);
}

@SuppressWarnings("unchecked")
private <D> AnimationDriver<D> getDriver(AnimationDriverKey<D> dataKey) {
private <D> AnimationDriver<D> getDriver(AnimationDataKey<D> dataKey) {
return (AnimationDriver<D>) this.animationDrivers.computeIfAbsent(dataKey, AnimationDriver::new);
}

Expand All @@ -55,9 +51,9 @@ private static class AnimationDriver<D>{
private D value;
private final Supplier<D> defaultValue;

private AnimationDriver(AnimationDriverKey<D> key){
this.value = key.defaultValue().get();
this.defaultValue = key.defaultValue();
private AnimationDriver(AnimationDataKey<D> key){
this.value = key.dataSupplier().get();
this.defaultValue = key.dataSupplier();
}

public D get(){
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

public class PoseSamplerStateContainer {

private final HashMap<PoseSamplerKey<?>, PoseSampler> poseSamplers;
private final HashMap<AnimationDataKey<? extends PoseSampler>, PoseSampler> poseSamplers;
private final JointSkeleton jointSkeleton;

public PoseSamplerStateContainer(JointSkeleton jointSkeleton) {
Expand Down Expand Up @@ -44,20 +44,20 @@ private void tickUpdateOrderGroup(AnimationDriverContainer animationDriverContai
* this pose sampler state container, then a new one is created from the key's default
* value and loaded into this animation data container and returned.
*
* @param poseSamplerKey the {@link PoseSamplerKey} attached to the desired {@link PoseSampler}
* @param poseSamplerKey the {@link AnimationDataKey<PoseSampler>} attached to the desired {@link PoseSampler}
*
* @return a {@link PoseSampler} object reference
*/
@SuppressWarnings("unchecked")
public <P extends PoseSampler> P getPoseSampler(PoseSamplerKey<P> poseSamplerKey){
public <P extends PoseSampler> P getPoseSampler(AnimationDataKey<P> poseSamplerKey){
return (P) this.poseSamplers.computeIfAbsent(poseSamplerKey, PoseSamplerKey::constructPoseSampler);
}

public AnimationPose sample(PoseSamplerKey<? extends Sampleable> poseSamplerKey, AnimationDriverContainer animationDriverContainer){
public AnimationPose sample(AnimationDataKey<? extends Sampleable> poseSamplerKey, AnimationDriverContainer animationDriverContainer){
return this.getPoseSampler(poseSamplerKey).sample(animationDriverContainer, this, this.jointSkeleton);
}

public AnimationPose sample(PoseSamplerKey<? extends SampleableFromInput> poseSamplerKey, AnimationDriverContainer animationDriverContainer, AnimationPose animationPose){
public AnimationPose sample(AnimationDataKey<? extends SampleableFromInput> poseSamplerKey, AnimationDriverContainer animationDriverContainer, AnimationPose animationPose){
return this.getPoseSampler(poseSamplerKey).sample(animationDriverContainer, this, this.jointSkeleton, animationPose);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public void blend(AnimationPose animationPose, float alpha, Easing easing){
}

public void blendLinear(AnimationPose animationPose, float alpha){
this.blend(animationPose, alpha, Easing.Linear.of());
this.blend(animationPose, alpha, Easing.LINEAR);
}

public AnimationPose getBlended(AnimationPose animationPose, float alpha, Easing easing){
Expand All @@ -188,7 +188,7 @@ public AnimationPose getBlended(AnimationPose animationPose, float alpha, Easing
}

public AnimationPose getBlendedLinear(AnimationPose animationPose, float alpha){
return this.getBlended(animationPose, alpha, Easing.Linear.of());
return this.getBlended(animationPose, alpha, Easing.LINEAR);
}

public void blendByJoints(AnimationPose animationPose, @NotNull List<String> joints, float alpha, Easing easing){
Expand All @@ -206,7 +206,7 @@ public AnimationPose getBlendedByJoints(AnimationPose animationPose, @NotNull Li
}

public AnimationPose getBlendedByJointsLinear(AnimationPose animationPose, List<String> joints, float alpha){
return this.getBlendedByJoints(animationPose, joints, alpha, Easing.Linear.of());
return this.getBlendedByJoints(animationPose, joints, alpha, Easing.LINEAR);
}

public AnimationPose getSelectedByJoints(AnimationPose animationPose, List<String> joints){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static JointTransform of(Vector3f translation, Quaternionf rotation, Vect
public static JointTransform getJointTransformFromAnimationSequence(ResourceLocation resourceLocation, String joint, float time){
if(AnimationSequenceData.INSTANCE.isValid(resourceLocation)){
Timeline<JointTransform> timeline = AnimationSequenceData.INSTANCE.get(resourceLocation).getJointTimeline(joint);
return new JointTransform(timeline.getValueAt(time));
return new JointTransform(timeline.getValueAtFractional(time));
} else {
return JointTransform.ZERO;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private AnimationBlendSpacePlayer(Builder<?> builder) {
this.playRateMultiplier = builder.playRateMultiplier;
}

public static Builder<?> of(){
public static Builder<?> builder(){
return new Builder<>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class AnimationMontage {

private AnimationMontage(ResourceLocation resourceLocation){
this.resourceLocation = resourceLocation;
this.length = AnimationSequenceData.INSTANCE.get(resourceLocation).getFrameLength();
this.length = AnimationSequenceData.INSTANCE.get(resourceLocation).frameLength();
}

public static AnimationMontage of(ResourceLocation resourceLocation){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.trainguy9512.animationoverhaul.animation.data.PoseSamplerStateContainer;
import com.trainguy9512.animationoverhaul.animation.pose.AnimationPose;
import com.trainguy9512.animationoverhaul.animation.pose.JointSkeleton;
import com.trainguy9512.animationoverhaul.util.time.Easing;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import java.util.ArrayList;

Expand All @@ -15,7 +17,7 @@ protected AnimationMontageTrack(Builder<?> builder) {
super(builder);
}

public static Builder<?> of(){
public static Builder<?> builder(){
return new Builder<>();
}

Expand Down Expand Up @@ -103,4 +105,25 @@ public void playMontage(AnimationMontage animationMontage){
activeMontages.get(0).forceInactive(activeMontages.get(1).getBlendDuration(true));
}
}


private final ResourceLocation resourceLocation;

private float length;
private float startOffset;
private float playRate = 1;
private float blendInDuration = 1;
private float blendOutDuration = 1;
private Easing blendInEasing = Easing.LINEAR;
private Easing blendOutEasing = Easing.LINEAR;

public record MontageConfiguration(ResourceLocation animationSequence, float length, float startOffset, float playRate, float blendInDuration, float blendOutDuration, Easing blendInEaring, Easing blendOutEasing){

public static Builder builder(ResourceLocation animationSequence)

public static class Builder {

}

}
}
Loading

0 comments on commit 49dcb85

Please sign in to comment.