Added branching and looping animationstages for groupanimations

This commit is contained in:
c0ffee 2024-04-16 20:44:51 -07:00
parent e8fd61fb4a
commit 16af5bb63a
8 changed files with 150 additions and 20 deletions

View File

@ -3,37 +3,40 @@
<GroupAnimationDef>
<defName>TestGroupAnimation1</defName>
<numActors>2</numActors>
<AnimationStages>
<li Class="Rimworld_Animations.AnimationStage_LoopForDuration">
<li Class="Rimworld_Animations.AnimationStage_TicksDuration">
<ticks>200</ticks>
<AnimationDefs>
<animationDefs>
<li>Pawn1_Stage1_TestAnimation1</li>
<li>Pawn2_Stage1_TestAnimation2</li>
</AnimationDefs>
</animationDefs>
</li>
<li Class="Rimworld_Animations.AnimationStage_LoopRandomSelectChance">
<numberOfLoops>10</numberOfLoops>
<AnimationOptions>
<loops>10</loops>
<animationOptions>
<li>
<probability>3</probability>
<li>Pawn1_Stage2_Variant1</li>
<li>Pawn2_Stage2_Variant1</li>
<animationDefs>
<li>Pawn1_Stage2_Variant1</li>
<li>Pawn2_Stage2_Variant1</li>
</animationDefs>
</li>
<li>
<probability>1</probability>
<li>Pawn1_Stage2_Variant2</li>
<li>Pawn2_Stage2_Variant2</li>
<animationDefs>
<li>Pawn1_Stage2_Variant2</li>
<li>Pawn2_Stage2_Variant2</li>
</animationDefs>
</li>
</AnimationOptions>
</animationOptions>
</li>
<li Class="Rimworld_Animations.AnimationStage_BranchPaths">
<Paths>
<li Class="Rimworld_Animations.AnimationStage_Branch">
<paths>
<li Class="Rimworld_Animations.AnimationStage_LoopRandomSelectChance">
<numberOfLoops>10</numberOfLoops>
<AnimationOptions>
<animationOptions>
<li>
<probability>3</probability>
<li>Pawn1_Stage2_Variant1</li>
@ -45,12 +48,12 @@
<li>Pawn1_Stage2_Variant2</li>
<li>Pawn2_Stage2_Variant2</li>
</li>
</AnimationOptions>
</animationOptions>
</li>
<li Class="Rimworld_Animations.AnimationStage_LoopRandomSelectChance">
<numberOfLoops>10</numberOfLoops>
<AnimationOptions>
<animationOptions>
<li>
<probability>3</probability>
<li>Pawn1_Stage2_Variant1</li>
@ -62,10 +65,10 @@
<li>Pawn1_Stage2_Variant2</li>
<li>Pawn2_Stage2_Variant2</li>
</li>
</AnimationOptions>
</animationOptions>
</li>
</Paths>
</paths>
</li>
</AnimationStages>
@ -82,6 +85,26 @@
</sexTypes>
<actorShift>1</actorShift>
</li>
<li Class="Rimworld_Animations.Actor_Custom_Races">
<sexTypes>
<li>Sex</li>
</sexTypes>
<races>
<li>Human</li>
<li>Dog</li>
</races>
<actorShift>0</actorShift>
</li>
<li Class="Rimworld_Animations.Actor_Custom_Races">
<sexTypes>
<li>Sex_Reverse</li>
</sexTypes>
<races>
<li>Dog</li>
<li>Human</li>
</races>
<actorShift>1</actorShift>
</li>
</contexts>
</GroupAnimationDef>

View File

@ -3,11 +3,15 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Verse;
namespace Rimworld_Animations
{
public abstract class AnimationStage
{
//single stage containing group of animations
//Return a list containing a tuple; int for how long the animation should play for
public abstract List<Tuple<int, AnimationDef>> GetAnimations(int actor, int seed);
}
}

View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Verse;
namespace Rimworld_Animations
{
public class AnimationStage_Branch : AnimationStage
{
List<AnimationStage> paths;
public override List<Tuple<int, AnimationDef>> GetAnimations(int actor, int seed)
{
return paths[(seed * 59) % paths.Count].GetAnimations(actor, seed);
}
}
}

View File

@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Verse;
namespace Rimworld_Animations
{
public class AnimationStage_LoopRandomSelectChance : AnimationStage
{
public int loops;
public List<AnimationLoopOption> animationOptions;
public override List<Tuple<int, AnimationDef>> GetAnimations(int actor, int seed)
{
int numberOfActors = animationOptions[0].animationDefs.Count;
List<Tuple<int, AnimationDef>> animations = new List<Tuple<int, AnimationDef>>();
for (int i = 0; i < loops; i++)
{
AnimationLoopOption option = getAnimationLoopOptionByWeight(seed + i, out int longestAnimLength);
Tuple<int, AnimationDef> animation = Tuple.Create(longestAnimLength, option.animationDefs[actor]);
animations.Append(animation);
}
return animations;
}
public class AnimationLoopOption
{
public int probability;
public List<AnimationDef> animationDefs;
}
//select random element from loop options by weight; also calculate the longest anim length
public AnimationLoopOption getAnimationLoopOptionByWeight(int seed, out int longestAnimLength)
{
int totalWeight = animationOptions.Sum(x => x.probability);
int randomNumber = (seed * 56) % totalWeight;
int cumulativeWeight = 0;
foreach(AnimationLoopOption option in animationOptions) {
cumulativeWeight += option.probability;
if (randomNumber <= cumulativeWeight)
{
longestAnimLength = option.animationDefs.Max(x => x.durationTicks);
return option;
}
}
longestAnimLength = animationOptions[0].animationDefs.Max(x => x.durationTicks);
return animationOptions[0];
}
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Verse;
namespace Rimworld_Animations
{
public class AnimationStage_TicksDuration : AnimationStage
{
int ticks;
List<AnimationDef> animationDefs;
public override List<Tuple<int, AnimationDef>> GetAnimations(int actor, int seed)
{
return new List<Tuple<int, AnimationDef>>() { Tuple.Create(ticks, animationDefs[actor]) };
}
}
}

View File

@ -12,6 +12,7 @@ namespace Rimworld_Animations
{
public Rot4 rotation;
public SoundDef sound = null;
public bool visible;
}

View File

@ -19,6 +19,7 @@ namespace Rimworld_Animations
if (__instance.AnimationWorker is AnimationWorker_KeyframesExtended extendedAnimWorker)
{
//INVIS IF ANIM CALLS FOR IT
//replace maybe?
if (!extendedAnimWorker.visibleAtTick(__instance.tree.AnimationTick))
{
__instance.requestRecache = true;
@ -27,6 +28,7 @@ namespace Rimworld_Animations
// HEAD ROTATION ADJUST FACING
Rot4 animFacing = extendedAnimWorker.facingAtTick(__instance.tree.AnimationTick);
if (parms.facing != animFacing)

View File

@ -77,6 +77,9 @@
<Compile Include="1.5\Source\Comps\CompThingAnimator.cs" />
<Compile Include="1.5\Source\Defs\AnimationDefOf.cs" />
<Compile Include="1.5\Source\GroupAnimationStages\AnimationStage.cs" />
<Compile Include="1.5\Source\GroupAnimationStages\AnimationStage_Branch.cs" />
<Compile Include="1.5\Source\GroupAnimationStages\AnimationStage_LoopRandomSelectChance.cs" />
<Compile Include="1.5\Source\GroupAnimationStages\AnimationStage_TicksDuration.cs" />
<Compile Include="1.5\Source\Keyframes\ExtendedKeyframe.cs" />
<Compile Include="1.5\Source\MainTabWindows\MainTabWindow_OffsetConfigure.cs" />
<Compile Include="1.5\Source\MainTabWindows\OffsetMainButtonDefOf.cs" />