using System;
using System.Collections.Generic;
using UnityEngine;
namespace DoomBreakers
{
public class UIPlayerManager : MonoBehaviour
{
//
//The UIPlayerManager purpose is to retrieve and supply appropriate information
//between each Player.cs class that relates to each PlayerUI.cs panel.
//
private static UIPlayerManager _UIPlayerEventManager;
private Dictionary _UIEventDictionary;
private static Dictionary _playerStatsEventDictionary;
private static Dictionary _playerEquipmentEventDictionary;
private static UIAnimationFlag[] _equipmentGainedEvent;// = new EquipmentGainedFlag[4]; //Max 4 players
public static UIPlayerManager _instance
{
get //When we access our instance from another place, we'll setup as appropriate if required.
{
if (!_UIPlayerEventManager)
{
//FindObjectOfType isn't a cheap call but we only do this once, if not at all.
_UIPlayerEventManager = FindObjectOfType(typeof(UIPlayerManager)) as UIPlayerManager;
if (!_UIPlayerEventManager)
print("\nUIPlayerManager= You need an active UIPlayerManager script attached to a GameObject within the scene!");
else
_UIPlayerEventManager.Setup();
}
return _UIPlayerEventManager;
}
}
public void FreeMemory()
{
_UIEventDictionary.Clear();
_playerStatsEventDictionary.Clear();
_playerEquipmentEventDictionary.Clear();
//for (int i = 0; i < _equipmentGainedEvent.Length; i++) _equipmentGainedEvent[i] = null;
_UIPlayerEventManager = null;
}
private void Setup()
{
if (_UIEventDictionary == null) _UIEventDictionary = new Dictionary();
if (_playerStatsEventDictionary == null) _playerStatsEventDictionary = new Dictionary();
if (_playerEquipmentEventDictionary == null) _playerEquipmentEventDictionary = new Dictionary();
_equipmentGainedEvent = new UIAnimationFlag[4]; //Max 4 players
}
public static void Subscribe(string eventName, Action listener)
{
Action thisEvent;
//out: differs from the ref keyword in that it does not require parameter variables to be
//initialized before they are passed to a method. Must be explicitly declared in the method
//definition​ as well as in the calling method.
if (_instance._UIEventDictionary.TryGetValue(eventName, out thisEvent))
{
//Add another event to the existing ones.
thisEvent += listener;
//Update the dictionary.
_instance._UIEventDictionary[eventName] = thisEvent;
}
else
{
//Add the event to the dictionary for the first time.
thisEvent += listener;
_instance._UIEventDictionary.Add(eventName, thisEvent);
}
}
public static void Unsubscribe(string eventName, Action listener)
{
if (_UIPlayerEventManager == null) //Guard Clause.
return;
Action thisEvent;
if (_instance._UIEventDictionary.TryGetValue(eventName, out thisEvent))
{
//Remove the event from the existing ones.
thisEvent -= listener;
//Now update the dictionary.
_instance._UIEventDictionary[eventName] = thisEvent;
}
}
//
//Used for communicating player statistics.
//
public static void TriggerEvent(string eventName, ref PlayerStats playerStats, int playerId)
{
if (_instance._UIEventDictionary == null) return;
if (_playerStatsEventDictionary == null) return;
Action thisEvent = null;
if (_instance._UIEventDictionary.TryGetValue(eventName, out thisEvent))
{
if(!_playerStatsEventDictionary.ContainsKey(playerId)) _playerStatsEventDictionary.Add(playerId, playerStats);
else
{
if(_playerStatsEventDictionary[playerId] != playerStats)
{
_playerStatsEventDictionary.Remove(playerId);
_playerStatsEventDictionary.Add(playerId, playerStats);
}
}
if (eventName != null) _instance._UIEventDictionary[eventName]();
//thisEvent.Invoke();
}
}
public static PlayerStats GetPlayerStats(int playerId) => _playerStatsEventDictionary[playerId];
public static void SetPlayerStats(ref PlayerStats playerStats, int playerId) => _playerStatsEventDictionary[playerId] = playerStats;
public static IPlayerEquipment GetPlayerEquipment(int playerId) => _playerEquipmentEventDictionary[playerId];
public static void SetPlayerEquipment(ref IPlayerEquipment playerEquipment, int playerId) => _playerEquipmentEventDictionary[playerId] = playerEquipment;
//
//Used for communicating player equipment.
//
public static void TriggerEvent(string eventName, UIAnimationFlag equipmentGainedFlag, int playerId)
{
Action thisEvent = null;
if (_instance._UIEventDictionary.TryGetValue(eventName, out thisEvent))
{
if (_equipmentGainedEvent[playerId] != equipmentGainedFlag)
_equipmentGainedEvent[playerId] = equipmentGainedFlag;
_instance._UIEventDictionary[eventName]();
//thisEvent.Invoke();
}
}
public static UIAnimationFlag GetEquipmentGainedFlag(int playerId)
{
UIAnimationFlag temp = _equipmentGainedEvent[playerId];
_equipmentGainedEvent[playerId] = UIAnimationFlag.None;
return temp;
}
//
//Used for communicating player kill count. Bandit.cs->UpdateStats()
//
public static void TriggerEvent(string eventName)
{
Action thisEvent = null;
if (_instance._UIEventDictionary.TryGetValue(eventName, out thisEvent))
{
//thisEvent.Invoke();
_instance._UIEventDictionary[eventName]();
}
}
}
}