Skip to content

Commit

Permalink
Refactoring push
Browse files Browse the repository at this point in the history
  • Loading branch information
Trainguy9512 committed Jan 25, 2025
1 parent b5de40f commit 65c8bfd
Show file tree
Hide file tree
Showing 16 changed files with 233 additions and 232 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import com.google.common.collect.Maps;
import com.trainguy9512.animationoverhaul.AnimationOverhaulMain;
import com.trainguy9512.animationoverhaul.animation.animator.entity.EntityJointAnimator;
import com.trainguy9512.animationoverhaul.animation.data.AnimationData;
import com.trainguy9512.animationoverhaul.animation.data.PoseSamplerStateContainer;
import com.trainguy9512.animationoverhaul.animation.pose.AnimationPose;
import com.trainguy9512.animationoverhaul.animation.pose.BakedAnimationPose;
import com.trainguy9512.animationoverhaul.animation.data.AnimationDataContainer;
import com.trainguy9512.animationoverhaul.animation.pose.JointSkeleton;
import net.minecraft.world.entity.Entity;

Expand All @@ -16,7 +16,7 @@
public class EntityJointAnimatorDispatcher {
public static final EntityJointAnimatorDispatcher INSTANCE = new EntityJointAnimatorDispatcher();

private final HashMap<UUID, AnimationDataContainer> entityAnimationDataContainerStorage;
private final HashMap<UUID, AnimationData> entityAnimationDataContainerStorage;
private final HashMap<UUID, BakedAnimationPose> entityBakedAnimationPoseStorage;
private final HashMap<UUID, PoseSamplerStateContainer> entityPoseSamplerStateContainerStorage;

Expand All @@ -34,24 +34,26 @@ public <T extends Entity> void tick(T entity){
JointSkeleton jointSkeleton = entityJointAnimator.getJointSkeleton();

BakedAnimationPose bakedPose = this.getEntityBakedAnimationPose(entityUUID, jointSkeleton);
AnimationDataContainer animationDataContainer = this.getEntityAnimationDataContainer(entityUUID);
PoseSamplerStateContainer poseSamplerStateContainer = this.getEntityPoseSamplerStateContainer(entityUUID);
AnimationData animationData = this.getEntityAnimationDataContainer(entityUUID);
PoseSamplerStateContainer poseSamplerStateContainer = this.getEntityPoseSamplerStateContainer(entityUUID, jointSkeleton);

// Step 1: Extract animation driver data
entityJointAnimator.extractAnimationData(entity, animationDataContainer);
entityJointAnimator.extractAnimationData(entity, animationData);

// Step 2: Update pose samplers using animation driver data
poseSamplerStateContainer.tick(animationDataContainer);
poseSamplerStateContainer.tick(animationData);

// Step 3: Calculate pose
AnimationPose calculatedAnimationPose = entityJointAnimator.calculatePose(animationDataContainer, poseSamplerStateContainer);
AnimationPose calculatedAnimationPose = entityJointAnimator.calculatePose(animationData, poseSamplerStateContainer);
if (calculatedAnimationPose == null){
calculatedAnimationPose = AnimationPose.of(jointSkeleton);
}

// Step 4: Push the local space pose to the baked pose, and save the baked pose.
bakedPose.pushPose(calculatedAnimationPose.getConvertedToLocalSpace());
this.saveBakedPose(entityUUID, bakedPose);


}

public <L extends Enum<L>> void saveBakedPose(UUID uuid, BakedAnimationPose bakedPose){
Expand All @@ -63,8 +65,8 @@ public <L extends Enum<L>> void saveBakedPose(UUID uuid, BakedAnimationPose bake
* @param uuid Entity UUID
* @return Animation data container
*/
public PoseSamplerStateContainer getEntityPoseSamplerStateContainer(UUID uuid){
return this.entityPoseSamplerStateContainerStorage.getOrDefault(uuid, new PoseSamplerStateContainer());
public PoseSamplerStateContainer getEntityPoseSamplerStateContainer(UUID uuid, JointSkeleton jointSkeleton){
return this.entityPoseSamplerStateContainerStorage.computeIfAbsent(uuid, (uuid1 -> new PoseSamplerStateContainer(jointSkeleton)));
}

/**
Expand All @@ -81,8 +83,8 @@ public <L extends Enum<L>> BakedAnimationPose getEntityBakedAnimationPose(UUID u
* @param uuid Entity UUID
* @return Animation data container
*/
private AnimationDataContainer getEntityAnimationDataContainer(UUID uuid){
return this.entityAnimationDataContainerStorage.getOrDefault(uuid, new AnimationDataContainer());
private AnimationData getEntityAnimationDataContainer(UUID uuid){
return this.entityAnimationDataContainerStorage.getOrDefault(uuid, new AnimationData());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.trainguy9512.animationoverhaul.animation.animator;

import com.trainguy9512.animationoverhaul.animation.animator.entity.LivingEntityJointAnimator;
import com.trainguy9512.animationoverhaul.animation.data.AnimationData;
import com.trainguy9512.animationoverhaul.animation.data.PoseSamplerKey;
import com.trainguy9512.animationoverhaul.animation.data.AnimationVariableKey;
import com.trainguy9512.animationoverhaul.animation.pose.AnimationPose;
import com.trainguy9512.animationoverhaul.animation.pose.BakedAnimationPose;
import com.trainguy9512.animationoverhaul.animation.pose.sample.*;
import com.trainguy9512.animationoverhaul.animation.pose.JointSkeleton;
import com.trainguy9512.animationoverhaul.animation.data.AnimationDataContainer;
import com.trainguy9512.animationoverhaul.animation.data.AnimationSequenceData;
import com.trainguy9512.animationoverhaul.util.time.Easing;
import net.minecraft.client.Minecraft;
Expand All @@ -27,7 +27,7 @@ public class FirstPersonPlayerJointAnimator extends LivingEntityJointAnimator<Lo

public static FirstPersonPlayerJointAnimator INSTANCE = new FirstPersonPlayerJointAnimator();

public AnimationDataContainer localAnimationDataContainer = new AnimationDataContainer();
public AnimationData localAnimationData = new AnimationData();
public BakedAnimationPose<FPPlayerJoints> localBakedPose;


Expand Down Expand Up @@ -112,13 +112,13 @@ public class FirstPersonPlayerJointAnimator extends LivingEntityJointAnimator<Lo
public enum TestStates implements AnimationStateMachine.StateEnum {
IDLE {
@Override
public <L extends Enum<L>> BiFunction<AnimationDataContainer, JointSkeleton<L>, AnimationPose<L>> getStatePose() {
public <L extends Enum<L>> BiFunction<AnimationData, 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() {
public <L extends Enum<L>> BiFunction<AnimationData, JointSkeleton<L>, AnimationPose<L>> getStatePose() {
return (animationDataContainer, fpPlayerLocatorsJointSkeleton) -> animationDataContainer.getPoseSampler(IDLE_SEQUENCE_PLAYER_ALT).sample(fpPlayerLocatorsJointSkeleton);
}
}
Expand Down Expand Up @@ -153,21 +153,21 @@ protected JointSkeleton.Builder buildSkeleton() {
}

@Override
public AnimationPose<FPPlayerJoints> calculatePose(AnimationDataContainer animationDataContainer) {
public AnimationPose<FPPlayerJoints> calculatePose(AnimationData animationData) {
// Update main hand item based on the anim notify
//animationDataContainer.getAnimationVariable(MAIN_HAND_ITEM).set(localPlayer.getMainHandItem().copy());


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

AnimationPose<FPPlayerJoints> pose = animationDataContainer.getPoseSampler(TEST_STATE_MACHINE).sample(this.getJointSkeleton());
AnimationPose<FPPlayerJoints> pose = animationData.getPoseSampler(TEST_STATE_MACHINE).sample(this.getJointSkeleton());



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


Vector3f rotation = new Vector3f(Mth.sin(animationDataContainer.getAnimationVariable(TIME_TEST).get() * 0.2F) * Mth.HALF_PI * 0.7f, 0, 0);
Vector3f rotation = new Vector3f(Mth.sin(animationData.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 @@ -179,9 +179,9 @@ public AnimationPose<FPPlayerJoints> calculatePose(AnimationDataContainer animat
/*
Get the pose with the added dampened camera rotation
*/
private AnimationPose<FPPlayerJoints> dampenArmRotation(AnimationPose<FPPlayerJoints> pose, AnimationDataContainer animationDataContainer){
Vector3f cameraRotation = animationDataContainer.getAnimationVariable(CAMERA_ROTATION).get();
Vector3f dampenedCameraRotation = animationDataContainer.getAnimationVariable(DAMPENED_CAMERA_ROTATION).get();
private AnimationPose<FPPlayerJoints> dampenArmRotation(AnimationPose<FPPlayerJoints> pose, AnimationData animationData){
Vector3f cameraRotation = animationData.getAnimationVariable(CAMERA_ROTATION).get();
Vector3f dampenedCameraRotation = animationData.getAnimationVariable(DAMPENED_CAMERA_ROTATION).get();

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

Expand All @@ -200,12 +200,12 @@ private AnimationPose<FPPlayerJoints> dampenArmRotation(AnimationPose<FPPlayerJo


@Override
public AnimationDataContainer extractAnimationData(LocalPlayer dataReference, AnimationDataContainer animationDataContainer){
public AnimationData extractAnimationData(LocalPlayer dataReference, AnimationData animationData){



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



Expand All @@ -215,10 +215,10 @@ public AnimationDataContainer extractAnimationData(LocalPlayer dataReference, An

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


Vector3f dampenedCameraRotation = animationDataContainer.getAnimationVariable(DAMPENED_CAMERA_ROTATION).get();
Vector3f dampenedCameraRotation = animationData.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 @@ -232,7 +232,7 @@ public AnimationDataContainer extractAnimationData(LocalPlayer dataReference, An
);
//dampenedCameraRotation.lerp(targetRotation, 0.5F);
}
animationDataContainer.getAnimationVariable(DAMPENED_CAMERA_ROTATION).set(dampenedCameraRotation);
animationData.getAnimationVariable(DAMPENED_CAMERA_ROTATION).set(dampenedCameraRotation);

}

Expand All @@ -241,16 +241,16 @@ public AnimationDataContainer extractAnimationData(LocalPlayer dataReference, An

public void tickExternal(){
LocalPlayer player = Minecraft.getInstance().player;
AnimationDataContainer animationDataContainer = this.localAnimationDataContainer;
AnimationData animationData = this.localAnimationData;

this.extractAnimationData(player, animationDataContainer);
animationDataContainer.tickAllPoseSamplers();
this.extractAnimationData(player, animationData);
animationData.tickAllPoseSamplers();

if(this.localBakedPose == null){
this.localBakedPose = new BakedAnimationPose<>(this.jointSkeleton);
}

AnimationPose<FPPlayerJoints> animationPose = this.calculatePose(animationDataContainer);
AnimationPose<FPPlayerJoints> animationPose = this.calculatePose(animationData);
if (animationPose == null){
animationPose = AnimationPose.of(this.jointSkeleton);
}
Expand All @@ -259,8 +259,8 @@ public void tickExternal(){
this.localBakedPose.pushPose(animationPose);
}

private boolean compareVariableItemStackWithEntityItemStack(AnimationVariableKey<ItemStack> itemStackDataKey, ItemStack entityItemStack, AnimationDataContainer animationDataContainer){
ItemStack currentItemStack = animationDataContainer.getAnimationVariable(itemStackDataKey).get();
private boolean compareVariableItemStackWithEntityItemStack(AnimationVariableKey<ItemStack> itemStackDataKey, ItemStack entityItemStack, AnimationData animationData){
ItemStack currentItemStack = animationData.getAnimationVariable(itemStackDataKey).get();
if(currentItemStack.getItem() != null && entityItemStack.getItem() == null || currentItemStack.getItem() == null && entityItemStack.getItem() != null) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.trainguy9512.animationoverhaul.animation.animator;

import com.trainguy9512.animationoverhaul.animation.data.AnimationDataContainer;
import com.trainguy9512.animationoverhaul.animation.data.AnimationData;
import com.trainguy9512.animationoverhaul.animation.data.PoseSamplerStateContainer;
import com.trainguy9512.animationoverhaul.animation.pose.AnimationPose;
import com.trainguy9512.animationoverhaul.animation.pose.JointSkeleton;
Expand Down Expand Up @@ -34,16 +34,16 @@ public JointSkeleton getJointSkeleton(){
/**
* Uses an object for data reference and updates the animation data container. Called once per tick, prior to pose samplers updating and pose calculation.
* @param dataReference Object used as reference for updating the animation data container
* @param animationDataContainer Data container from the previous tick
* @param animationData Data container from the previous tick
* @return Resulting data container
*/
public abstract AnimationDataContainer extractAnimationData(T dataReference, AnimationDataContainer animationDataContainer);
public abstract AnimationData extractAnimationData(T dataReference, AnimationData animationData);

/**
* Calculates and returns an animation pose once per tick, after pose sampler update and animation data extraction
* @param animationDataContainer Data container containing extracted animation variable data.
* @param animationData Data container containing extracted animation variable data.
* @param poseSamplerStateContainer Data container containing pose sampler states, used for sampling poses.
* @return Calculated animation pose to be passed off to the baked animation pose
*/
public abstract AnimationPose calculatePose(AnimationDataContainer animationDataContainer, PoseSamplerStateContainer poseSamplerStateContainer);
public abstract AnimationPose calculatePose(AnimationData animationData, PoseSamplerStateContainer poseSamplerStateContainer);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.trainguy9512.animationoverhaul.animation.animator.entity;

import com.trainguy9512.animationoverhaul.animation.data.AnimationData;
import com.trainguy9512.animationoverhaul.animation.pose.AnimationPose;
import com.trainguy9512.animationoverhaul.animation.pose.JointSkeleton;
import com.trainguy9512.animationoverhaul.animation.data.AnimationDataContainer;
import net.minecraft.client.model.PlayerModel;
import net.minecraft.client.model.geom.ModelPart;
import net.minecraft.client.model.geom.PartPose;
Expand Down Expand Up @@ -86,15 +86,15 @@ protected JointSkeleton<ModelPartLocators> buildSkeleton() {

// Ticking every sampleable animation state, in this case updating the state machine conditions
@Override
public AnimationDataContainer extractAnimationData(Player dataReference, AnimationDataContainer animationDataContainer) {
return animationDataContainer;
public AnimationData extractAnimationData(Player dataReference, AnimationData animationData) {
return animationData;


}

// This is the function for getting the final pose every tick
@Override
public AnimationPose<ModelPartLocators> calculatePose(AnimationDataContainer animationDataContainer) {
public AnimationPose<ModelPartLocators> calculatePose(AnimationData animationData) {
return AnimationPose.of(this.jointSkeleton);
}

Expand Down
Loading

0 comments on commit 65c8bfd

Please sign in to comment.