Files
Syntriax.Engine/Engine.Core/Systems/UniverseEntranceManager.cs

41 lines
2.3 KiB
C#

using System.Collections.Generic;
namespace Engine.Core;
public class UniverseEntranceManager : Internal.BehaviourIndependent
{
// We use Ascending order because we are using reverse for loop to call them
private static Comparer<int> SortByAscendingPriority() => Comparer<int>.Create((x, y) => x.CompareTo(y));
private static System.Func<IBehaviour, int> GetPriority() => (b) => b.Priority;
private readonly ActiveBehaviourCollectorOrdered<int, IEnterUniverse> enterUniverses = new(GetPriority(), SortByAscendingPriority());
private readonly ActiveBehaviourCollectorOrdered<int, IExitUniverse> exitUniverses = new(GetPriority(), SortByAscendingPriority());
protected override void OnEnteredUniverse(IUniverse universe)
{
// FIXME: This causes an issue when the UniverseEntranceManager is already attached to a UniverseObject then registered into a Universe,
// the enter/exit universe collectors call OnUniverseObjectRegistered internally on Assign, but since the Universe calls the OnUniverseObjectRegistered
// event it tries to call OnUniverseObjectRegistered again on the same object, causing a duplicate entry error.
Debug.Assert.AssertTrue(BehaviourController.Count == 1, $"{nameof(UniverseEntranceManager)} must be in it's own {nameof(IUniverseObject)} with no other {nameof(IBehaviour)}s attached at the moment. Failing to do so might cause instantiation or serialization issues.");
enterUniverses.Assign(universe);
exitUniverses.Assign(universe);
}
protected override void OnExitedUniverse(IUniverse universe)
{
enterUniverses.Unassign();
exitUniverses.Unassign();
}
private void OnEnterUniverseCollected(IBehaviourCollector<IEnterUniverse> sender, IBehaviourCollector<IEnterUniverse>.BehaviourCollectedArguments args)
=> args.BehaviourCollected.EnterUniverse(Universe);
private void OnExitUniverseRemoved(IBehaviourCollector<IExitUniverse> sender, IBehaviourCollector<IExitUniverse>.BehaviourRemovedArguments args)
=> args.BehaviourRemoved.ExitUniverse(Universe);
public UniverseEntranceManager()
{
enterUniverses.OnCollected.AddListener(OnEnterUniverseCollected);
exitUniverses.OnRemoved.AddListener(OnExitUniverseRemoved);
}
}