Skip to content

Commit

Permalink
More WIP on restructuring
Browse files Browse the repository at this point in the history
- Implementing builders into pose samplers
  • Loading branch information
Trainguy9512 committed Mar 16, 2024
1 parent 83725fb commit ab9504d
Show file tree
Hide file tree
Showing 13 changed files with 302 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ enum MiningStates {
*/


private static final AnimationMontageTrack MAIN_HAND_EMPTY_PUNCH_MONTAGE_TRACK = AnimationMontageTrack.of("main_hand_empty_punch_montage_track");
//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))
Expand Down Expand Up @@ -186,11 +186,14 @@ protected JointSkeleton<FPPlayerLocators> buildRig() {
@Override
public AnimationPose<FPPlayerLocators> calculatePose(LocalPlayer localPlayer, AnimationDataContainer animationDataContainer) {
// Update main hand item based on the anim notify

setEntityAnimationVariable(MAIN_HAND_ITEM, this.livingEntity.getMainHandItem().copy());

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);



pose = dampenArmRotation(pose);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public AnimationPose<?> getCachedPose(String identifier, JointSkeleton<?> jointS
}
}

public <D> AnimationVariable<D> get(AnimationVariableKey<D> dataKey){
public <D> AnimationVariable<D> getAnimationVariable(AnimationVariableKey<D> dataKey){
if(!animationVariables.containsKey(dataKey)){
animationVariables.put(dataKey, new AnimationVariable<>(dataKey));
}
Expand All @@ -107,11 +107,11 @@ public TreeMap<String, AnimationVariable<?>> getDebugData(){
}

public <D> void setValue(AnimationVariableKey<D> dataKey, D value){
this.get(dataKey).set(value);
this.getAnimationVariable(dataKey).set(value);
}

public <D> D getValue(AnimationVariableKey<D> dataKey){
return this.get(dataKey).get();
return this.getAnimationVariable(dataKey).get();
}


Expand All @@ -126,7 +126,7 @@ public <D> D getValue(AnimationVariableKey<D> dataKey){
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.get(dataKey);
AnimationVariable<Float> data = this.getAnimationVariable(dataKey);
data.set(Mth.clamp((data.get()) + (condition ? 1/ticksToIncrement : -1/ticksToDecrement), 0, 1));
}

Expand All @@ -138,7 +138,7 @@ public void incrementInTicksFromCondition(AnimationVariableKey<Float> dataKey, b
* @param ticksToIncrement Time in ticks to increment from 0 to 1
*/
public void incrementInTicksOrResetFromCondition(AnimationVariableKey<Float> dataKey, boolean condition, float ticksToIncrement){
AnimationVariable<Float> data = this.get(dataKey);
AnimationVariable<Float> data = this.getAnimationVariable(dataKey);
if(condition){
data.set(0F);
data.set(0F);
Expand All @@ -158,8 +158,8 @@ public void incrementInTicksOrResetFromCondition(AnimationVariableKey<Float> dat
* @param random Java random object used to pick a random index within numberOfAnimations
*/
public void incrementInTicksOrResetRandomFromCondition(AnimationVariableKey<Float> dataKeyMain, AnimationVariableKey<Integer> dataKeyIndex, int numberOfAnimations, boolean condition, float ticksToIncrement, Random random){
AnimationVariable<Float> dataMain = this.get(dataKeyMain);
AnimationVariable<Integer> dataIndex = this.get(dataKeyIndex);
AnimationVariable<Float> dataMain = this.getAnimationVariable(dataKeyMain);
AnimationVariable<Integer> dataIndex = this.getAnimationVariable(dataKeyIndex);
if(condition){
dataMain.set(0F);
dataMain.set(0F);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ public class AnimationVariableKey<D> {
private final Supplier<D> defaultValue;
private final String debugIdentifier;

private AnimationVariableKey(Supplier<D> defaultValue, String debugIdentifier) {
this.defaultValue = defaultValue;
this.debugIdentifier = debugIdentifier;
private AnimationVariableKey(Builder<D> builder) {
this.defaultValue = builder.defaultValue;
this.debugIdentifier = builder.debugIdentifier;
}

/**
Expand Down Expand Up @@ -50,7 +50,7 @@ public Builder<D> setDebugIdentifier(String debugIdentifier){
}

public AnimationVariableKey<D> build(){
return new AnimationVariableKey<>(this.defaultValue, this.debugIdentifier);
return new AnimationVariableKey<>(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.trainguy9512.animationoverhaul.animation.data;

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

import java.util.function.Supplier;

public class PoseSamplerKey<P extends PoseSampler> {

private final Supplier<P> defaultValue;
private final String debugIdentifier;

private PoseSamplerKey(Builder<P> builder) {
this.defaultValue = builder.defaultValue;
this.debugIdentifier = builder.debugIdentifier;
}

/**
* Creates a builder for a key
*
* @param defaultValue The default value of the animation variable
*/
public static <P extends PoseSampler> Builder<P> of(Supplier<P> defaultValue){
return new Builder<>(defaultValue);
}

public String getDebugIdentifier() {
return debugIdentifier;
}

public Supplier<P> getDefaultValue(){
return this.defaultValue;
}

public static class Builder<P extends PoseSampler> {

private final Supplier<P> defaultValue;
private String debugIdentifier = "null";


private Builder(Supplier<P> defaultValue) {
this.defaultValue = defaultValue;
}

/**
* Sets the debug identifier for the key builder. This is used in in-game value debugging for identifying pose samplers and printing them to the screen.
*
* @param debugIdentifier The string name used in the identifier
*/
public Builder<P> setDebugIdentifier(String debugIdentifier){
this.debugIdentifier = debugIdentifier;
return this;
}

public PoseSamplerKey<P> build(){
return new PoseSamplerKey<>(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public TimelineGroup get(ResourceLocation resourceLocation){
}
}

public boolean isValid(ResourceLocation resourceLocation){
return animationEntries.containsKey(resourceLocation);
}

public static String FIRST_PERSON_PLAYER_KEY = "player/first_person/";

public static ResourceLocation getNativeResourceLocation(String path){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,54 @@

public class AnimationBlendSpacePlayer extends TimeBasedPoseSampler {

private final TreeMap<Float, BlendSpaceEntry> blendSpaceEntryTreeMap = new TreeMap<Float, BlendSpaceEntry>();
private final TreeMap<Float, BlendSpaceEntry> blendSpaceEntryTreeMap;
private float currentValue = 0;
private float playRateMultiplier = 1;

private AnimationBlendSpacePlayer(String identifier) {
super(identifier);
private AnimationBlendSpacePlayer(Builder<?> builder) {
super(builder);
this.blendSpaceEntryTreeMap = builder.blendSpaceEntryTreeMap;
this.currentValue = builder.currentValue;
this.playRateMultiplier = builder.playRateMultiplier;
}

public static AnimationBlendSpacePlayer of(String identifier){
return new AnimationBlendSpacePlayer(identifier);
public static Builder<?> of(String identifier){
return new Builder<>(identifier);
}


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

private final TreeMap<Float, BlendSpaceEntry> blendSpaceEntryTreeMap = new TreeMap<>();
private float currentValue = 0f;
private float playRateMultiplier = 1f;

protected Builder(String identifier) {
super(identifier);
}

@SuppressWarnings("unchecked")
public B addBlendSpaceEntry(float position, ResourceLocation resourceLocation, float playRate){
this.blendSpaceEntryTreeMap.put(position, new BlendSpaceEntry(resourceLocation, playRate));
return (B) this;
}

@SuppressWarnings("unchecked")
public B setDefaultValue(float currentValue){
this.currentValue = currentValue;
return (B) this;
}

@SuppressWarnings("unchecked")
public B setPlayRateMultiplier(float playRateMultiplier){
this.playRateMultiplier = playRateMultiplier;
return (B) this;
}

@Override
public AnimationBlendSpacePlayer build() {
return new AnimationBlendSpacePlayer(this);
}
}

public AnimationBlendSpacePlayer addEntry(float position, ResourceLocation resourceLocation, float playRate){
Expand All @@ -38,7 +76,7 @@ public void setValue(float value){
}

private float getPlayRateBlended(){
if(this.blendSpaceEntryTreeMap.entrySet().size() == 0){
if(this.blendSpaceEntryTreeMap.entrySet().isEmpty()){
return 0;
}

Expand All @@ -61,7 +99,7 @@ private float getPlayRateBlended(){

@Override
public <L extends Enum<L>> AnimationPose<L> sample(JointSkeleton<L> jointSkeleton, AnimationDataContainer.CachedPoseContainer cachedPoseContainer) {
if(this.blendSpaceEntryTreeMap.entrySet().size() == 0){
if(this.blendSpaceEntryTreeMap.entrySet().isEmpty()){
return AnimationPose.of(jointSkeleton);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,31 @@ public class AnimationMontageTrack extends PoseSampler {

private final ArrayList<AnimationMontage> activeMontages = new ArrayList<AnimationMontage>();

public AnimationMontageTrack(String identifier) {
super(identifier);
protected AnimationMontageTrack(Builder<?> builder) {
super(builder);
}

public static AnimationMontageTrack of(String identifier){
return new AnimationMontageTrack(identifier);
public static Builder<?> of(String identifier){
return new Builder<>(identifier);
}


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

protected Builder(String identifier) {
super(identifier);
}

@Override
public AnimationMontageTrack build() {
return new AnimationMontageTrack(this);
}
}

@Override
public void tick(){
// Only run if there's actually montages currently loaded
if(activeMontages.size() > 0){
if(this.isActive()){
ArrayList<AnimationMontage> montagesToRemove = new ArrayList<>();
for(AnimationMontage animationMontage : activeMontages){
animationMontage.tick();
Expand All @@ -48,15 +61,15 @@ public <L extends Enum<L>> AnimationPose<L> sampleFromInputPose(AnimationPose<L>
}

public boolean isActive(){
return this.activeMontages.size() > 0;
return !this.activeMontages.isEmpty();
}

private <L extends Enum<L>> AnimationPose<L> getBlendedPose(AnimationPose<L> inputPose, JointSkeleton<L> jointSkeleton){
// Initialize the animation pose
AnimationPose<L> animationPose = AnimationPose.of(jointSkeleton);

// Only do this stuff if there's any loaded animation montages
if(this.activeMontages.size() > 0){
if(this.isActive()){
// Iterate over each montage and get the blended animation between top and bottom layers
for(int i = 0; i < this.activeMontages.size(); i++){
AnimationMontage animationMontage = this.activeMontages.get(i);
Expand Down
Loading

0 comments on commit ab9504d

Please sign in to comment.