﻿using HarmonyLib;
using RimWorld;
using System.Collections.Generic;
using Verse;

namespace Rimworld_Animations
{
	[HarmonyPatch(typeof(PawnRenderer), "RenderPawnAt")]
	public static class PawnRenderer_RenderPawnAt_Patch
	{
		static bool ClearCache(Pawn pawn)
		{
			return pawn.IsInvisible() || CompBodyAnimator.IsAnimating(pawn);
		}

		public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			foreach (CodeInstruction i in instructions)
			{
				if (i.Calls(AccessTools.Method(typeof(PawnUtility), nameof(PawnUtility.IsInvisible))))
				{
					yield return CodeInstruction.Call(typeof(PawnRenderer_RenderPawnAt_Patch), nameof(ClearCache));
				}
				else
				{   
					yield return i;
				}
			}
		}
	}

}
