rimworld-animations/1.5/Source/GroupAnimationStages/AnimationStage_LoopRandomSe...

60 lines
2.0 KiB
C#

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];
}
}
}