rimworld-animations/1.5/Source/Utilities/AnimationUtility.cs

98 lines
3.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RimWorld;
using rjw.Modules.Interactions.Helpers;
using rjw.Modules.Interactions.Objects;
using UnityEngine;
using Verse;
using Verse.AI;
using rjw.Modules.Interactions.Enums;
namespace Rimworld_Animations {
public static class AnimationUtility {
public static void StartAnimation(List<Pawn> participants)
{
participants[0].Drawer.renderer.SetAnimation(AnimationDefOf.TestAnimation1);
participants[1].Drawer.renderer.SetAnimation(AnimationDefOf.TestAnimation2);
}
//startgroupanimator with anchor
//don't anchor to self if anchor is self
public static void StartGroupAnimation(List<Pawn> participants, GroupAnimationDef groupAnimationDef, int reorder, Thing anchor)
{
int seed = GenTicks.TicksGame;
for (int i = 0; i < participants.Count; i++)
{
if (anchor is Pawn pawn && pawn == participants[i])
{
List<AnimationDef> allAnimationsForPawn = groupAnimationDef.GetAllAnimationsForActor(i, seed, reorder);
participants[i].TryGetComp<CompExtendedAnimator>().PlayGroupAnimation(allAnimationsForPawn);
}
else
{
//each participant gets their own unique extendedanimatoranchor, important for scribe_deep saving
List<AnimationDef> allAnimationsForPawn = groupAnimationDef.GetAllAnimationsForActor(i, seed, reorder);
BaseExtendedAnimatorAnchor animatorAnchor = new ExtendedAnimatorAnchor_Thing(anchor);
participants[i].TryGetComp<CompExtendedAnimator>().PlayGroupAnimation(allAnimationsForPawn, animatorAnchor);
}
}
}
//startgroupanimation without anchor; just play where standing
public static void StartGroupAnimation(List<Pawn> participants, GroupAnimationDef groupAnimationDef, int reorder)
{
int seed = GenTicks.TicksGame;
for (int i = 0; i < participants.Count; i++)
{
List<AnimationDef> allAnimationsForPawn = groupAnimationDef.GetAllAnimationsForActor(i, seed, reorder);
participants[i].TryGetComp<CompExtendedAnimator>().PlayGroupAnimation(allAnimationsForPawn);
}
}
public static void StopGroupAnimation(List<Pawn> participants)
{
foreach(Pawn pawn in participants)
{
pawn.TryGetComp<CompExtendedAnimator>()?.StopAnimating();
}
}
public static void StopGroupAnimation(Pawn participant)
{
participant.TryGetComp<CompExtendedAnimator>()?.StopAnimating();
}
public static GroupAnimationDef FindGroupAnimation(List<Pawn> participants, out int reorder)
{
// go through each context in each GroupAnimationDef
// if you find one where it returns canAnimationBeUsed (and reorders),
// return that animation
int reorder2 = 0;
DefDatabase<GroupAnimationDef>.AllDefsListForReading.TryRandomElement((GroupAnimationDef x) =>
x.canAnimationBeUsed(participants, out reorder2), out GroupAnimationDef result);
reorder = reorder2;
if (result != null) return result;
return null;
}
}
}