Files
Syntriax.Engine/Engine.Integration/Engine.Integration.MonoGame/Behaviours/LoadContentManager.cs
Syntriax 988a6f67f2 BREAKING CHANGE: renamed original Behaviour class to BehaviourInternal, and replaced it with BehaviourBase
Original Behaviour was using old methods for detecting entering/exiting universe,
they are now all under the same hood and the original is kept for UniverseEntranceManager
because it needs to enter the universe without itself. The internal behaviour kept under
a subnamespace of "Core.Internal" for the purpose that it might come in handy for other use cases.
2025-10-22 16:50:19 +03:00

56 lines
1.8 KiB
C#

using System.Collections.Generic;
using Engine.Core;
namespace Engine.Integration.MonoGame;
public class LoadContentManager : Behaviour, IEnterUniverse, IExitUniverse, IFirstFrameUpdate
{
// 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, ILoadContent> loadContents = new(GetPriority(), SortByAscendingPriority());
private readonly List<ILoadContent> toCallLoadContents = new(32);
private MonoGameWindowContainer monoGameWindowContainer = null!;
public void FirstActiveFrame()
{
monoGameWindowContainer = Universe.FindRequiredBehaviour<MonoGameWindowContainer>();
}
public void EnterUniverse(IUniverse universe)
{
loadContents.Assign(universe);
universe.OnPreUpdate.AddListener(OnPreUpdate);
}
public void ExitUniverse(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<ILoadContent> sender, IBehaviourCollector<ILoadContent>.BehaviourCollectedArguments args)
{
toCallLoadContents.Add(args.BehaviourCollected);
}
public LoadContentManager()
{
loadContents.OnCollected.AddListener(OnFirstFrameCollected);
}
}