Files
LuaCsForBarotraumaEP/Barotrauma/BarotraumaShared/SharedSource/Events/EventActions/NPCWaitAction.cs
2024-04-24 18:09:05 +03:00

92 lines
3.1 KiB
C#

using System.Collections.Generic;
using System.Linq;
namespace Barotrauma
{
/// <summary>
/// Makes an NPC stop and wait.
/// </summary>
class NPCWaitAction : EventAction
{
[Serialize("", IsPropertySaveable.Yes, description: "Tag of the NPC(s) that should wait.")]
public Identifier NPCTag { get; set; }
[Serialize(true, IsPropertySaveable.Yes, description: "Should the NPC start or stop waiting?")]
public bool Wait { get; set; }
private bool isFinished = false;
public NPCWaitAction(ScriptedEvent parentEvent, ContentXElement element) : base(parentEvent, element) { }
private IEnumerable<Character> affectedNpcs;
public override void Update(float deltaTime)
{
if (isFinished) { return; }
affectedNpcs = ParentEvent.GetTargets(NPCTag).Where(c => c is Character).Select(c => c as Character);
foreach (var npc in affectedNpcs)
{
if (npc.Removed) { continue; }
if (npc.AIController is not HumanAIController humanAiController) { continue; }
if (Wait)
{
var gotoObjective = new AIObjectiveGoTo(
AIObjectiveGoTo.GetTargetHull(npc) as ISpatialEntity ?? npc, npc, humanAiController.ObjectiveManager, repeat: true)
{
FaceTargetOnCompleted = false,
OverridePriority = 100.0f,
SourceEventAction = this,
IsWaitOrder = true,
CloseEnough = 100
};
humanAiController.ObjectiveManager.AddObjective(gotoObjective);
humanAiController.ObjectiveManager.WaitTimer = 0.0f;
}
else
{
AbandonGoToObjectives(humanAiController);
}
}
isFinished = true;
}
public override bool IsFinished(ref string goTo)
{
return isFinished;
}
public override void Reset()
{
if (affectedNpcs != null)
{
foreach (var npc in affectedNpcs)
{
if (npc.Removed || npc.AIController is not HumanAIController aiController) { continue; }
AbandonGoToObjectives(aiController);
}
affectedNpcs = null;
}
isFinished = false;
}
private void AbandonGoToObjectives(HumanAIController aiController)
{
foreach (var objective in aiController.ObjectiveManager.Objectives)
{
if (objective is AIObjectiveGoTo gotoObjective && gotoObjective.SourceEventAction?.ParentEvent == ParentEvent)
{
gotoObjective.Abandon = true;
}
}
}
public override string ToDebugString()
{
return $"{ToolBox.GetDebugSymbol(isFinished)} {nameof(NPCWaitAction)} -> (NPCTag: {NPCTag.ColorizeObject()}, Wait: {Wait.ColorizeObject()})";
}
}
}