using System.Collections.Generic; using Syntriax.Engine.Core; namespace Syntriax.Engine.Integration.MonoGame; public class LoadContentManager : Behaviour, IFirstFrameUpdate { // We use Ascending order because we are using reverse for loop to call them private static Comparer SortByAscendingPriority() => Comparer.Create((x, y) => x.Priority.CompareTo(y.Priority)); private readonly ActiveBehaviourCollectorSorted loadContents = new() { SortBy = SortByAscendingPriority() }; private readonly List toCallLoadContents = new(32); private MonoGameWindowContainer monoGameWindowContainer = null!; public void FirstActiveFrame() { monoGameWindowContainer = Universe.FindRequiredBehaviour(); } protected override void OnEnteredUniverse(IUniverse universe) { loadContents.Assign(universe); universe.OnPreUpdate.AddListener(OnPreUpdate); } protected override void OnExitedUniverse(IUniverse universe) { loadContents.Unassign(); universe.OnPreUpdate.RemoveListener(OnPreUpdate); } private void OnPreUpdate(IUniverse sender, IUniverse.UpdateArguments args) { for (int i = toCallLoadContents.Count - 1; i >= 0; i--) { toCallLoadContents[i].LoadContent(monoGameWindowContainer.Window.Content); toCallLoadContents.RemoveAt(i); } } private void OnFirstFrameCollected(IBehaviourCollector sender, IBehaviourCollector.BehaviourCollectedArguments args) { toCallLoadContents.Add(args.BehaviourCollected); } public LoadContentManager() { loadContents.OnCollected.AddListener(OnFirstFrameCollected); } }