From d319f05a2e182a04b6280a3ab991dea63148e2a8 Mon Sep 17 00:00:00 2001 From: austin Date: Fri, 2 Aug 2024 14:32:33 -0400 Subject: [PATCH] interrupting animations --- .../entity/btree/StateTransitionUtil.java | 15 +++++++-- .../client/firstPerson/FirstPersonTree.java | 27 +++++++++++++++ .../entity/state/life/ClientLifeTree.java | 1 - .../electrosphere/renderer/actor/Actor.java | 33 +++++++++++++++++++ .../server/poseactor/PoseActor.java | 33 +++++++++++++++++++ 5 files changed, 106 insertions(+), 3 deletions(-) diff --git a/src/main/java/electrosphere/entity/btree/StateTransitionUtil.java b/src/main/java/electrosphere/entity/btree/StateTransitionUtil.java index 32a6e2a4..3f12e10c 100644 --- a/src/main/java/electrosphere/entity/btree/StateTransitionUtil.java +++ b/src/main/java/electrosphere/entity/btree/StateTransitionUtil.java @@ -109,7 +109,10 @@ public class StateTransitionUtil { animation = state.getAnimation.get(); } - //Main simulation + + // + //Play main animation + // if(!actor.isPlayingAnimation() && state.onComplete != null && state.startedAnimation == true){ //state transition if this isn't set to loop state.onComplete.run(); @@ -138,6 +141,10 @@ public class StateTransitionUtil { state.onComplete.run(); state.startedAnimation = false; } + + // + //Play animation in first person + // if(animation != null){ FirstPersonTree.conditionallyPlayAnimation(Globals.firstPersonEntity, animation); } @@ -160,6 +167,10 @@ public class StateTransitionUtil { } + + // + //Play main animation + // if(animation == null){ state.startedAnimation = true; if(state.onComplete != null){ @@ -193,7 +204,7 @@ public class StateTransitionUtil { //Gets the first person animation Supplier getAnimation; - + //The audio data TreeDataAudio audioData; diff --git a/src/main/java/electrosphere/entity/state/client/firstPerson/FirstPersonTree.java b/src/main/java/electrosphere/entity/state/client/firstPerson/FirstPersonTree.java index 607784d2..548abfa4 100644 --- a/src/main/java/electrosphere/entity/state/client/firstPerson/FirstPersonTree.java +++ b/src/main/java/electrosphere/entity/state/client/firstPerson/FirstPersonTree.java @@ -122,6 +122,22 @@ public class FirstPersonTree implements BehaviorTree { } } + /** + * Plays an animation if it exists + * @param animationName the name of the animation + */ + public void interruptAnimation(TreeDataAnimation animation){ + if(Globals.firstPersonEntity != null){ + Actor actor = EntityUtils.getActor(Globals.firstPersonEntity); + if( + (actor.isPlayingAnimation() || actor.isPlayingAnimation(animation.getNameFirstPerson())) && + (Globals.assetManager.fetchModel(actor.getModelPath()) != null && Globals.assetManager.fetchModel(actor.getModelPath()).getAnimation(animation.getNameFirstPerson()) != null) + ){ + actor.interruptAnimation(animation, false); + } + } + } + /** * If the entity has a first person tree, plays the provided animation * @param entity The entity @@ -145,4 +161,15 @@ public class FirstPersonTree implements BehaviorTree { } } + /** + * If the entity has a first person tree, interrupts the provided animation + * @param entity The entity + * @param animation The animation + */ + public static void conditionallyInterruptAnimation(Entity entity, TreeDataAnimation animation){ + if(entity != null && FirstPersonTree.hasTree(entity)){ + FirstPersonTree.getTree(entity).interruptAnimation(animation); + } + } + } diff --git a/src/main/java/electrosphere/entity/state/life/ClientLifeTree.java b/src/main/java/electrosphere/entity/state/life/ClientLifeTree.java index 3dba02bd..c216e98f 100644 --- a/src/main/java/electrosphere/entity/state/life/ClientLifeTree.java +++ b/src/main/java/electrosphere/entity/state/life/ClientLifeTree.java @@ -1,7 +1,6 @@ package electrosphere.entity.state.life; -import electrosphere.net.parser.net.message.SynchronizationMessage; import electrosphere.net.synchronization.BehaviorTreeIdEnums; import electrosphere.entity.btree.BehaviorTree; import electrosphere.entity.btree.StateTransitionUtil; diff --git a/src/main/java/electrosphere/renderer/actor/Actor.java b/src/main/java/electrosphere/renderer/actor/Actor.java index f155c280..e4229182 100644 --- a/src/main/java/electrosphere/renderer/actor/Actor.java +++ b/src/main/java/electrosphere/renderer/actor/Actor.java @@ -279,6 +279,39 @@ public class Actor { } } + /** + * Interrupts an animation, thereby causing it to stop playing + * @param animation The animation to interrupt + */ + public void interruptAnimation(TreeDataAnimation animation, boolean isThirdPerson){ + //Get the animation's name + String animationName = ""; + if(isThirdPerson){ + animationName = animation.getNameThirdPerson(); + } else { + animationName = animation.getNameFirstPerson(); + } + + //Get the animation's priority + int priority = AnimationPriorities.getValue(AnimationPriorities.DEFAULT); + if(animation.getPriority() != null){ + priority = animation.getPriority(); + } + if(animation.getPriorityCategory() != null){ + priority = AnimationPriorities.getValue(animation.getPriorityCategory()); + } + + toRemoveMasks.clear(); + for(ActorAnimationMask mask : this.animationQueue){ + if(mask.animationName == animationName && mask.priority == priority){ + toRemoveMasks.add(mask); + } + } + for(ActorAnimationMask currentMask : toRemoveMasks){ + animationQueue.remove(currentMask); + } + } + public void playAnimationWithMask(String animationName, int priority, List boneMask){ Model model = Globals.assetManager.fetchModel(modelPath); if(model != null){ diff --git a/src/main/java/electrosphere/server/poseactor/PoseActor.java b/src/main/java/electrosphere/server/poseactor/PoseActor.java index 8fcd6181..956cf2f9 100644 --- a/src/main/java/electrosphere/server/poseactor/PoseActor.java +++ b/src/main/java/electrosphere/server/poseactor/PoseActor.java @@ -255,6 +255,39 @@ public class PoseActor { } } + /** + * Interrupts an animation, thereby causing it to stop playing + * @param animation The animation to interrupt + */ + public void interruptAnimation(TreeDataAnimation animation, boolean isThirdPerson){ + //Get the animation's name + String animationName = ""; + if(isThirdPerson){ + animationName = animation.getNameThirdPerson(); + } else { + animationName = animation.getNameFirstPerson(); + } + + //Get the animation's priority + int priority = AnimationPriorities.getValue(AnimationPriorities.DEFAULT); + if(animation.getPriority() != null){ + priority = animation.getPriority(); + } + if(animation.getPriorityCategory() != null){ + priority = AnimationPriorities.getValue(animation.getPriorityCategory()); + } + + toRemoveMasks.clear(); + for(ActorAnimationMask mask : this.animationQueue){ + if(mask.getAnimationName() == animationName && mask.getPriority() == priority){ + toRemoveMasks.add(mask); + } + } + for(ActorAnimationMask currentMask : toRemoveMasks){ + animationQueue.remove(currentMask); + } + } + /** * Play an animation with a mask that makes the animation only apply to specific bones * @param animationName The name of the animation