using System.Collections.Generic;
using D2D.Utilities;
using D2D.Tools;
using UnityEngine;
namespace D2D.Core
{
///
/// Activates and deactivates pausables according to
/// current game state IsGameActiveDuringState option
///
public class PausablesHub : MonoBehaviour, ILazy
{
///
/// All existed pausable objects in game
///
private static List _pausables = new List();
private GameStateMachine _gameStateMachine;
private void OnEnable()
{
_gameStateMachine = this.FindLazy();
// Adjust switching pausable activity to any state
_gameStateMachine.On(UpdatePausablesActivityAccordingToState, gameObject);
}
private void UpdatePausablesActivityAccordingToState()
{
for (int i = 0; i < _pausables.Count; i++)
{
// Remove null or already destroyed pausables
if (_pausables[i] == null || _pausables[i].gameObject == null)
{
_pausables.RemoveAt(i);
continue;
}
// If all is ok, update pausable activity according to current game state
// _pausables[i].gameObject.SetActive(_gameStateMachine.Last.IsGameActiveDuringState);
}
}
///
/// Register pausable. So now, new object will be paused or not according to game state
///
public void AddPausable(PausablesMember newPausable)
{
_pausables.Add(newPausable);
// if (_gameStateMachine.IsEmpty || !_gameStateMachine.Last.IsGameActiveDuringState)
// newPausable.gameObject.SetActive(false);
}
}
}