rimworld-animations/1.6/Source/Animations/GroupAnimations/GroupAnimationDef.cs

192 lines
6.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Verse;
namespace Rimworld_Animations
{
public class GroupAnimationDef : Def
{
public int numActors;
public List<AnimationStage> animationStages;
public List<GroupAnimationContext> contexts;
public List<AnimationOffsetDef> offsetDefs;
/*
public bool CanAnimationBeUsed(List<Pawn> actors, out int highestPriority, out int contextNum)
{
contextNum = 0;
bool animationFound = false;
highestPriority = -999999999;
if (actors.Count != numActors) return DebugLogThenReturn(false, actors, highestPriority, contextNum);
if (!contexts.NullOrEmpty())
{
int contextIndex = 0;
foreach (GroupAnimationContext context in contexts)
{
if (context.CanAnimationBeUsed(actors))
{
animationFound = true;
if (context.AnimationPriority() > highestPriority)
{
contextNum = contextIndex;
//get highest priority context for fitting animation
highestPriority = context.AnimationPriority();
}
}
contextIndex++;
}
}
return DebugLogThenReturn(animationFound, actors, highestPriority, contextNum);
}*/
public bool CanAnimationBeUsed(List<Pawn> actors, out List<Pawn> bestPermutation, out int highestPriority)
{
bool workingContextFound = false;
highestPriority = -99999999;
int contextNum = -1;
bestPermutation = null;
if (actors.Count != numActors) return false;
if (contexts.NullOrEmpty()) return false;
for (int i = 0; i < contexts.Count; i++)
{
List<Pawn> workingPermutation = contexts[i].FindAnyWorkingSet(actors);
if (workingPermutation != null && contexts[i].priority > highestPriority)
{
contextNum = i;
workingContextFound = true;
bestPermutation = workingPermutation;
highestPriority = contexts[i].priority;
}
}
DebugLog(workingContextFound, actors, bestPermutation, highestPriority, contextNum);
return workingContextFound;
}
private void DebugLog(bool workingContextFound, List<Pawn> actors, List<Pawn> actorsOrdered, int priority, int contextNum)
{
if (RJWAnimationSettings.debugMode)
{
if (workingContextFound)
{
string debugMessage = this.defName;
debugMessage += " (priority: " + priority + ", context: " + contextNum + ") succeeded with pawn permutation: ";
bool first = true;
foreach (Pawn pawn in actorsOrdered)
{
debugMessage = debugMessage + (first ? "" : ", ") + pawn.Name + " (" + pawn.def.defName + ")";
first = false;
}
Log.Message(debugMessage);
}
else
{
string debugMessage = this.defName + " failed for pawns: ";
bool first = true;
foreach (Pawn pawn in actors)
{
debugMessage = debugMessage + (first ? "" : ", ") + pawn.Name + " (" + pawn.def.defName + ")";
first = false;
}
Log.Message(debugMessage);
}
}
}
/*
public int Reorder(List<Pawn> actors)
{
int priority = -999999999;
int reorder = 0;
foreach (GroupAnimationContext context in contexts)
{
if (context.CanAnimationBeUsed(actors, numActors))
{
if (context.AnimationPriority() > priority)
{
//get the reorder for highest priority context for fitting animation
priority = context.AnimationPriority();
}
}
}
return reorder;
}*/
public List<AnimationDef> GetAllAnimationsForActor(int actor, int seed, List<Pawn> actors)
{
List<AnimationDef> animations = new List<AnimationDef>();
foreach (AnimationStage stage in animationStages)
{
//add all new animations to list of animations
animations.AddRange(stage.GetAnimations(actor, seed, actors));
}
return animations;
}
public void GetOffset(int actor, Pawn pawn, out Vector3? position, out int? rotation)
{
position = null;
rotation = null;
string bodyTypeDef = (pawn.story?.bodyType != null) ? pawn.story.bodyType.ToString() : "";
if (offsetDefs[actor].FindOffset(pawn, out BaseAnimationOffset animationOffset))
{
position = animationOffset.getOffset(pawn);
rotation = animationOffset.getRotation(pawn);
}
if (RJWAnimationSettings.offsets == null) RJWAnimationSettings.offsets = new Dictionary<string, PawnScaledOffsets>();
if (RJWAnimationSettings.offsets.ContainsKey(AnimationUtility.OffsetLookupKey(pawn)))
{
if (RJWAnimationSettings.debugMode)
{
Log.Message("Using saved offset " + AnimationUtility.OffsetLookupKey(pawn));
}
RJWAnimationSettings.offsets[AnimationUtility.OffsetLookupKey(pawn)].GetOffsets(pawn, out Vector3 offset, out int rot);
position = offset;
rotation = rot;
}
}
}
}