2024-01-30 19:21:45 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
using Syntriax.Engine.Core.Abstract;
|
|
|
|
|
|
|
|
namespace Syntriax.Engine.Core;
|
|
|
|
|
2024-02-09 09:43:15 +03:00
|
|
|
public class BehaviourCollector<T> : IAssignableGameManager, IEnumerable<T>
|
2024-01-30 19:21:45 +03:00
|
|
|
{
|
2024-07-15 01:13:39 +03:00
|
|
|
public event IAssignable.OnUnassignedDelegate? OnUnassigned = null;
|
|
|
|
public event IAssignableGameManager.OnGameManagerAssignedDelegate? OnGameManagerAssigned = null;
|
2024-01-30 19:21:45 +03:00
|
|
|
|
2024-07-15 01:13:39 +03:00
|
|
|
public event OnCollectedDelegate? OnCollected = null;
|
|
|
|
public event OnRemovedDelegate? OnRemoved = null;
|
2024-01-30 19:21:45 +03:00
|
|
|
|
|
|
|
private readonly List<T> _behaviours = new(32);
|
|
|
|
|
|
|
|
public IReadOnlyList<T> Behaviours => _behaviours;
|
|
|
|
public IGameManager GameManager { get; private set; } = null!;
|
|
|
|
|
|
|
|
public T this[Index index] => _behaviours[index];
|
|
|
|
|
2024-02-09 09:43:15 +03:00
|
|
|
public BehaviourCollector() { }
|
|
|
|
public BehaviourCollector(IGameManager gameManager) => Assign(gameManager);
|
2024-01-30 19:21:45 +03:00
|
|
|
|
|
|
|
private void OnGameObjectRegistered(IGameManager manager, IGameObject gameObject)
|
|
|
|
{
|
|
|
|
gameObject.BehaviourController.OnBehaviourAdded += OnBehaviourAdded;
|
|
|
|
gameObject.BehaviourController.OnBehaviourRemoved += OnBehaviourRemoved;
|
2024-10-22 21:46:26 +03:00
|
|
|
|
|
|
|
foreach (IBehaviour item in gameObject.BehaviourController)
|
|
|
|
OnBehaviourAdded(gameObject.BehaviourController, item);
|
2024-01-30 19:21:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private void OnGameObjectUnregistered(IGameManager manager, IGameObject gameObject)
|
|
|
|
{
|
|
|
|
gameObject.BehaviourController.OnBehaviourAdded -= OnBehaviourAdded;
|
|
|
|
gameObject.BehaviourController.OnBehaviourRemoved -= OnBehaviourRemoved;
|
2024-10-05 23:08:44 +03:00
|
|
|
|
|
|
|
foreach (IBehaviour item in gameObject.BehaviourController)
|
|
|
|
OnBehaviourRemoved(gameObject.BehaviourController, item);
|
2024-01-30 19:21:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private void OnBehaviourAdded(IBehaviourController controller, IBehaviour behaviour)
|
|
|
|
{
|
|
|
|
if (behaviour is not T tBehaviour)
|
|
|
|
return;
|
|
|
|
|
|
|
|
_behaviours.Add(tBehaviour);
|
2024-02-09 09:43:15 +03:00
|
|
|
OnCollected?.Invoke(this, tBehaviour);
|
2024-01-30 19:21:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private void OnBehaviourRemoved(IBehaviourController controller, IBehaviour behaviour)
|
|
|
|
{
|
|
|
|
if (behaviour is not T tBehaviour)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!_behaviours.Remove(tBehaviour))
|
|
|
|
return;
|
|
|
|
|
2024-02-09 09:43:15 +03:00
|
|
|
OnRemoved?.Invoke(this, tBehaviour);
|
2024-01-30 19:21:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool Assign(IGameManager gameManager)
|
|
|
|
{
|
|
|
|
if (GameManager is not null)
|
|
|
|
return false;
|
|
|
|
|
2024-11-16 22:44:45 +03:00
|
|
|
foreach (IGameObject gameObject in gameManager.GameObjects)
|
2024-01-30 19:21:45 +03:00
|
|
|
OnGameObjectRegistered(gameManager, gameObject);
|
|
|
|
|
|
|
|
gameManager.OnGameObjectRegistered += OnGameObjectRegistered;
|
|
|
|
gameManager.OnGameObjectUnRegistered += OnGameObjectUnregistered;
|
|
|
|
|
|
|
|
GameManager = gameManager;
|
|
|
|
OnGameManagerAssigned?.Invoke(this);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Unassign()
|
|
|
|
{
|
|
|
|
if (GameManager is null)
|
|
|
|
return false;
|
|
|
|
|
2024-11-16 22:44:45 +03:00
|
|
|
foreach (IGameObject gameObject in GameManager.GameObjects)
|
2024-01-30 19:21:45 +03:00
|
|
|
OnGameObjectUnregistered(GameManager, gameObject);
|
|
|
|
|
|
|
|
GameManager.OnGameObjectRegistered -= OnGameObjectRegistered;
|
|
|
|
GameManager.OnGameObjectUnRegistered -= OnGameObjectUnregistered;
|
|
|
|
|
|
|
|
GameManager = null!;
|
|
|
|
OnUnassigned?.Invoke(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerator<T> GetEnumerator() => _behaviours.GetEnumerator();
|
|
|
|
IEnumerator IEnumerable.GetEnumerator() => _behaviours.GetEnumerator();
|
2024-07-15 01:13:39 +03:00
|
|
|
|
|
|
|
|
|
|
|
public delegate void OnCollectedDelegate(BehaviourCollector<T> sender, T behaviourCollected);
|
|
|
|
public delegate void OnRemovedDelegate(BehaviourCollector<T> sender, T behaviourRemoved);
|
2024-01-30 19:21:45 +03:00
|
|
|
}
|