using HarmonyLib; using RimWorld; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Verse; namespace Rimworld_Animations { [HarmonyPatch(typeof(PawnRenderTree), "TryGetMatrix")] public class HarmonyPatch_PawnRenderTree { public static void Prefix(PawnRenderTree __instance, Dictionary ___nodesByTag, PawnRenderNode node, ref PawnDrawParms parms) { //find lowest parent that is animating, or nothing if not animating PawnRenderNode animatingNode = node; while (animatingNode != null && !(animatingNode.AnimationWorker is AnimationWorker_KeyframesExtended)) { animatingNode = animatingNode.parent; } //if animating parent node found, if (animatingNode?.AnimationWorker is AnimationWorker_KeyframesExtended animatingNodeAnimationWorker) { //change parm to facing to animate correctly parms.facing = animatingNodeAnimationWorker.facingAtTick(__instance.AnimationTick); } } } //recaching //done here because changing parms causes recaching anyway, so might as well do it here [HarmonyPatch(typeof(PawnRenderTree), "AdjustParms")] public class HarmonyPatch_PawnRenderTree2 { public static void Prefix(PawnRenderTree __instance, ref PawnDrawParms parms) { int animationTick = __instance.AnimationTick; if (__instance.rootNode.AnimationWorker is AnimationWorker_KeyframesExtended rootAnimWorkerExtended) { //recache during facing turn if (rootAnimWorkerExtended.shouldRecache(animationTick)) { __instance.rootNode.requestRecache = true; return; } } foreach (PawnRenderNode node in __instance.rootNode.children) { if (node.AnimationWorker is AnimationWorker_KeyframesExtended animWorkerExtended) { //recache during flicker on/off if (animWorkerExtended.shouldRecache(animationTick)) { node.requestRecache = true; return; } } } } } }