Skip to content

Commit

Permalink
Cleaned up AnimationDataContainer.java
Browse files Browse the repository at this point in the history
- Removed cached posed containers, they weren't used anywhere and not sure if the problem they were created to solve really is a problem anymore
- Documented most stuff in AnimationDataContainer.java
  • Loading branch information
Trainguy9512 committed Mar 20, 2024
1 parent c77de69 commit 8a61980
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 216 deletions.
Expand Up @@ -38,7 +38,7 @@ public <T extends Entity, L extends Enum<L>> void tickEntity(T entity, EntityJoi
// First tick the entity part animator
entityJointAnimator.tick(entity, animationDataContainer);

animationDataContainer.tickAnimationStates();
animationDataContainer.tickAllPoseSamplers();


/*
Expand Down
Expand Up @@ -280,7 +280,7 @@ public void tickExternal(){
AnimationDataContainer animationDataContainer = this.localAnimationDataContainer;

this.tick(player, animationDataContainer);
animationDataContainer.tickAnimationStates();
animationDataContainer.tickAllPoseSamplers();

if(this.localBakedPose == null){
this.localBakedPose = new BakedAnimationPose();
Expand Down
Expand Up @@ -24,182 +24,93 @@ public class AnimationDataContainer {
private final HashMap<AnimationVariableKey<?>, AnimationVariable<?>> animationVariableMap;
//private final HashMap<String, PoseSampler> entitySampleableAnimationStates;
private final HashMap<AnimationPoseSamplerKey<?>, PoseSampler> poseSamplerMap;
private final CachedPoseContainer cachedPoseContainer = new CachedPoseContainer();

public AnimationDataContainer(){
this.animationVariableMap = Maps.newHashMap();
this.poseSamplerMap = Maps.newHashMap();
}

public Set<PoseSampler> getPoseSampler

public void tickAnimationStates(){
for(PoseSampler poseSampler : entitySampleableAnimationStates.values()){
poseSampler.tick();
}
}

public <D extends PoseSampler> D getAnimationState(D sampleableAnimationState){
for(String identifier : this.entitySampleableAnimationStates.keySet()){
if (Objects.equals(sampleableAnimationState.getIdentifier(), identifier)){
return (D) this.entitySampleableAnimationStates.get(identifier);
}
}
this.entitySampleableAnimationStates.put(sampleableAnimationState.getIdentifier(), sampleableAnimationState);
return sampleableAnimationState;
}

public <L extends Enum<L>> AnimationPose<L> sampleAnimationState(JointSkeleton<L> jointSkeleton, PoseSampler poseSampler){
for(String identifier : this.entitySampleableAnimationStates.keySet()){
if (Objects.equals(poseSampler.getIdentifier(), identifier)){
return this.entitySampleableAnimationStates.get(identifier).sample(jointSkeleton, cachedPoseContainer);
}
}
this.entitySampleableAnimationStates.put(poseSampler.getIdentifier(), poseSampler);
return (this.entitySampleableAnimationStates.get(poseSampler.getIdentifier())).sample(jointSkeleton, cachedPoseContainer);
}

public <L extends Enum<L>> AnimationPose<L> sampleAnimationStateFromInputPose(AnimationPose<L> inputPose, JointSkeleton<L> jointSkeleton, PoseSampler poseSampler){
for(String identifier : this.entitySampleableAnimationStates.keySet()){
if (Objects.equals(poseSampler.getIdentifier(), identifier)){
return this.entitySampleableAnimationStates.get(identifier).sampleFromInputPose(inputPose, jointSkeleton, cachedPoseContainer);
}
}
this.entitySampleableAnimationStates.put(poseSampler.getIdentifier(), poseSampler);
return (this.entitySampleableAnimationStates.get(poseSampler.getIdentifier())).sampleFromInputPose(inputPose, jointSkeleton, cachedPoseContainer);
}

public <L extends Enum<L>> void saveCachedPose(String identifier, AnimationPose<L> animationPose){
this.cachedPoseContainer.saveCachedPose(identifier, animationPose);
}

public <L extends Enum<L>> AnimationPose<?> getCachedPose(String identifier, JointSkeleton<L> jointSkeleton){
return this.cachedPoseContainer.getCachedPose(identifier, jointSkeleton);
}

public class CachedPoseContainer {
private final HashMap<String, AnimationPose<?>> poses = Maps.newHashMap();

public CachedPoseContainer(){
}

public void saveCachedPose(String identifier, AnimationPose<?> animationPose){
this.poses.put(identifier, animationPose);
}

public <L extends Enum<L>> AnimationPose<L> getCachedPose(String identifier, JointSkeleton<L> jointSkeleton){
if(this.poses.containsKey(identifier)){
return (AnimationPose<L>) this.poses.get(identifier);
}
return AnimationPose.of(jointSkeleton);
}
}

public <D> AnimationVariable<D> getAnimationVariable(AnimationVariableKey<D> dataKey){
if(!animationVariableMap.containsKey(dataKey)){
animationVariableMap.put(dataKey, new AnimationVariable<>(dataKey));
}
return (AnimationVariable<D>) animationVariableMap.get(dataKey);
}

public TreeMap<String, AnimationVariable<?>> getDebugData(){
TreeMap<String, AnimationVariable<?>> finalList = Maps.newTreeMap();
for(AnimationVariableKey<?> dataKey : this.animationVariableMap.keySet()){
AnimationVariable<?> data = animationVariableMap.get(dataKey);

String[] typeSplitted = data.get().getClass().toString().split("\\.");
String type = typeSplitted[typeSplitted.length - 1];

typeSplitted = type.split("\\$");
type = typeSplitted[typeSplitted.length - 1];

String debugIdentifier = dataKey.getIdentifier() + " (" + type + "):";
finalList.put(debugIdentifier, data);
}
return finalList;
/**
* Returns this animation data container's hash map of pose sampler keys to pose samplers currently loaded.
*
* @return {@link HashMap} of {@link AnimationPoseSamplerKey} keys to {@link PoseSampler} values.
*/
public HashMap<AnimationPoseSamplerKey<?>, PoseSampler> getPoseSamplerMap(){
return this.poseSamplerMap;
}

public <D> void setValue(AnimationVariableKey<D> dataKey, D value){
this.getAnimationVariable(dataKey).set(value);
/**
* Returns a collection of every pose sampler currently loaded into this animation data container.
*
* @return {@link Collection} of {@link PoseSampler} values.
*/
public Collection<PoseSampler> getPoseSamplers(){
return this.getPoseSamplerMap().values();
}

public <D> D getValue(AnimationVariableKey<D> dataKey){
return this.getAnimationVariable(dataKey).get();
/**
* Returns this animation data container's hash map of animation variable keys to animation variables currently loaded.
*
* @return {@link HashMap} of {@link AnimationVariableKey} keys to {@link AnimationVariable} values.
*/
public HashMap<AnimationVariableKey<?>, AnimationVariable<?>> getAnimationVariableMap(){
return this.animationVariableMap;
}


/**
* Increments a float value based on a condition, incremented in ticks
* Returns a collection of every animation variable currently loaded into this animation data container.
*
* @param dataKey Float data key
* @param condition Boolean condition to decide whether the value should increment or decrement
* @param ticksToIncrement Time in ticks to increment from 0 to 1
* @param ticksToDecrement Time in ticks to decrement from 1 to 0
* @return {@link Collection} of {@link PoseSampler} values.
*/
public void incrementInTicksFromCondition(AnimationVariableKey<Float> dataKey, boolean condition, float ticksToIncrement, float ticksToDecrement){
ticksToIncrement = Math.max(1, ticksToIncrement);
ticksToDecrement = Math.max(1, ticksToDecrement);
AnimationVariable<Float> data = this.getAnimationVariable(dataKey);
data.set(Mth.clamp((data.get()) + (condition ? 1/ticksToIncrement : -1/ticksToDecrement), 0, 1));
public Collection<AnimationVariable<?>> getAnimationVariables(){
return this.getAnimationVariableMap().values();
}

/**
* Increments or resets a value based on a condition, incremented in ticks
* Iterates over every currently loaded pose sampler and executes the {@link PoseSampler#tick()} method
*
* @param dataKey Float data key
* @param condition Boolean condition to decide whether the value should reset to 0 or increment
* @param ticksToIncrement Time in ticks to increment from 0 to 1
* @implNote Only do this once per game tick! For entities, this is handled in the entity joint animator dispatcher.
*/
public void incrementInTicksOrResetFromCondition(AnimationVariableKey<Float> dataKey, boolean condition, float ticksToIncrement){
AnimationVariable<Float> data = this.getAnimationVariable(dataKey);
if(condition){
data.set(0F);
data.set(0F);
} else {
this.incrementInTicksFromCondition(dataKey, true, ticksToIncrement, ticksToIncrement);
public void tickAllPoseSamplers(){
for(PoseSampler poseSampler : this.getPoseSamplers()){
poseSampler.tick();
}
}

/**
* Increments or resets a value based on a condition, incremented in ticks
* Returns a pose sampler from the given key. If one is not currently loaded into
* this animation data container, then a new one is created from the key's default
* value and loaded into this animation data container and returned.
*
* @param animationPoseSamplerKey the {@link AnimationPoseSamplerKey} attached to the desired {@link PoseSampler}
*
* @param dataKeyMain Main float data key
* @param dataKeyIndex Index data key
* @param numberOfAnimations Number of animations to pick from
* @param condition Boolean condition to decide whether the value should reset to 0 or increment
* @param ticksToIncrement Time in ticks to increment from 0 to 1
* @param random Java random object used to pick a random index within numberOfAnimations
* @return a {@link PoseSampler} object reference
*/
public void incrementInTicksOrResetRandomFromCondition(AnimationVariableKey<Float> dataKeyMain, AnimationVariableKey<Integer> dataKeyIndex, int numberOfAnimations, boolean condition, float ticksToIncrement, Random random){
AnimationVariable<Float> dataMain = this.getAnimationVariable(dataKeyMain);
AnimationVariable<Integer> dataIndex = this.getAnimationVariable(dataKeyIndex);
if(condition){
dataMain.set(0F);
dataMain.set(0F);
dataIndex.set(random.nextInt(numberOfAnimations));
} else {
this.incrementInTicksFromCondition(dataKeyMain, true, ticksToIncrement, ticksToIncrement);
@SuppressWarnings("unchecked")
public <P extends PoseSampler> P getPoseSampler(AnimationPoseSamplerKey<P> animationPoseSamplerKey){
if(!this.getPoseSamplerMap().containsKey(animationPoseSamplerKey)){
this.getPoseSamplerMap().put(animationPoseSamplerKey, animationPoseSamplerKey.getSuppliedDefaultValue());
}
return (P) this.getPoseSamplerMap().get(animationPoseSamplerKey);
}

/*
public static class DataKey<D>{
private final String identifier;
private final D defaultValue;
public DataKey(String identifier, D defaultValue){
this.identifier = identifier;
this.defaultValue = defaultValue;
}
public String getIdentifier(){
return this.identifier;
/**
* Returns an animation variable from the given key. If one is not currently loaded into
* this animation data container, then a new one is created from the key's default
* value and loaded into this animation data container and returned.
*
* @param dataKey the {@link AnimationVariableKey} attached to the desired {@link AnimationVariable}
*
* @return an {@link AnimationVariable} object reference
*/
@SuppressWarnings("unchecked")
public <D> AnimationVariable<D> getAnimationVariable(AnimationVariableKey<D> dataKey){
if(!this.getAnimationVariableMap().containsKey(dataKey)){
this.getAnimationVariableMap().put(dataKey, new AnimationVariable<>(dataKey));
}
return (AnimationVariable<D>) this.getAnimationVariableMap().get(dataKey);
}

*/


public static class AnimationVariable<D>{

Expand All @@ -213,14 +124,30 @@ private AnimationVariable(AnimationVariableKey<D> dataKey){
this.valueOld = dataKey.getDefaultValueSupplier().get();
}

/**
* Returns the value of this animation variable.
*
* @return - value of the {@link AnimationVariable} instance's type
*/
public D get(){
return this.value;
}

/**
* Returns the value of this animation variable prior to the last time it was set.
*
* @return - value of the {@link AnimationVariable} instance's type
*/
public D getOld(){
return this.valueOld;
}

/**
* Sets the value of this animation variable. Also updates the old value, setting it
* to what it was prior to this method call.
*
* @param value new value of the {@link AnimationVariable} instance's type
*/
public void set(D value){
this.valueOld = this.value;
if(value != null){
Expand All @@ -230,10 +157,18 @@ public void set(D value){
}
}

/**
* Sets this animation variable's value to the default value, supplied from the
* default value supplier.
*/
public void setDefaultValue(){
this.set(this.defaultValue.get());
}

/**
* Returns whether the animation variable's type is that of a float or not.
* @return {@link Boolean} true if the type is float.
*/
public boolean isFloat(){
return this.value instanceof Float;
}
Expand Down

This file was deleted.

Expand Up @@ -98,7 +98,7 @@ private float getPlayRateBlended(){
}

@Override
public <L extends Enum<L>> AnimationPose<L> sample(JointSkeleton<L> jointSkeleton, AnimationDataContainer.CachedPoseContainer cachedPoseContainer) {
public <L extends Enum<L>> AnimationPose<L> sample(JointSkeleton<L> jointSkeleton) {
if(this.blendSpaceEntryTreeMap.entrySet().isEmpty()){
return AnimationPose.of(jointSkeleton);
}
Expand Down
Expand Up @@ -51,12 +51,7 @@ public void tick(){
}

@Override
public <L extends Enum<L>> AnimationPose<L> sample(JointSkeleton<L> jointSkeleton, AnimationDataContainer.CachedPoseContainer cachedPoseContainer){
return AnimationPose.of(jointSkeleton);
}

@Override
public <L extends Enum<L>> AnimationPose<L> sampleFromInputPose(AnimationPose<L> inputPose, JointSkeleton<L> jointSkeleton, AnimationDataContainer.CachedPoseContainer cachedPoseContainer) {
public <L extends Enum<L>> AnimationPose<L> sampleFromInputPose(AnimationPose<L> inputPose, JointSkeleton<L> jointSkeleton) {
return getBlendedPose(inputPose, jointSkeleton);
}

Expand Down
Expand Up @@ -118,7 +118,7 @@ public boolean isAnimNotityActive(String identifier){
}

@Override
public <L extends Enum<L>> AnimationPose<L> sample(JointSkeleton<L> jointSkeleton, AnimationDataContainer.CachedPoseContainer cachedPoseContainer){
public <L extends Enum<L>> AnimationPose<L> sample(JointSkeleton<L> jointSkeleton){
return AnimationPose.fromChannelTimeline(jointSkeleton, this.resourceLocation, this.getTimeFromTicks());
//return super.sample(locatorSkeleton);
}
Expand Down

0 comments on commit 8a61980

Please sign in to comment.