using Assets.Scripts; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.UIElements; using static PlayersHandler; public class PlayersHandler : SingletonMB { // Start is called before the first frame update [SerializeField] private GameObject playerPrefab; #region PROPERTIES public Dictionary Players { get; private set; } = new(); #endregion #region VARIABLES private ScoreHandler _scoreHandler; private GameHandler _gameHandler; #endregion #region EVENTS /// /// Fires when a player is instanciated in the game /// public event EventHandler OnPlayerInstanciated; /// /// Fires when a player have finished their attempt /// public event EventHandler OnPlayerFinished; /// /// Fires when all players have finished their attempt /// public event EventHandler OnAllPlayersFinished; public class PlayerEventArgs : EventArgs { public PlayerScript player; public PlayerEventArgs(PlayerScript player) { this.player = player; } } #endregion #region ENDPOINTS /// /// Remove a player from the game and fire a PlayerFinished event. Fire AllPlayerFinished event if needed /// /// public void RemovePlayer(int playerId) { if (Players.TryGetValue(playerId, out PlayerScript player)) { Players.Remove(playerId); if(!_gameHandler.AttemptEnding) OnPlayerFinished.Invoke(this, new PlayerEventArgs(player)); player.Dispose(); } if(Players.Count == 0 && !_gameHandler.AttemptEnding) { OnAllPlayersFinished.Invoke(this, new EventArgs()); } } public bool InstanciatePlayer(Vector3 position) where T : MonoBehaviour { var player = SpawnPlayer(new Vector3(position.x, position.y, -1)); PlayerScript script = player.GetComponent(); player.AddComponent(); Players.Add(script.Id, script); DecoratePlayer(player); CreateScorePanel(script); Debug.Log($"# Player \"{script.Name}\" instanciated !"); OnPlayerInstanciated?.Invoke(this, new PlayerEventArgs(script)); return true; } #endregion #region METHODS private GameObject SpawnPlayer(Vector3 position) { var player = Instantiate(playerPrefab, position, Quaternion.identity); var script = player.GetComponent(); script.SetId(Players.Count); return player; } private void CreateScorePanel(PlayerScript script) { _scoreHandler.AddScorePanel(script.Name, script.Id, script.Color); } //private GameObject InstantiatePlayer(string playerName, Vector3 position) //{ // incrementialId++; // var player = Instantiate(playerPrefab, position, Quaternion.identity); // var script = player.GetComponent(); // script.SetName(playerName); // script.SetId(incrementialId); // _scoreHandler.AddScorePanel(script.Name, script.Id, script.Color); // Players.Add(script); // return player; //} private void DecoratePlayer(GameObject player) { Transform child = player.transform.Find("sprite"); PlayerScript script = player.GetComponent(); var components = child.GetComponentsInChildren(); var jetpackFront = child.transform.Find("JetpackFront").GetComponent(); var jetpackBack = child.transform.Find("JetpackBack").GetComponent(); float alpha = _gameHandler.HaveHumanPlayer && !script.IsHuman ? 0.4f : 1.0f; var scriptColor = script.Color!=null ? script.Color : new Color(255, 255, 255); scriptColor.a = alpha; foreach ( var component in components ) { component.color = new Color(255, 255, 255, alpha); } jetpackFront.color = scriptColor; jetpackBack.color = scriptColor; } #endregion #region LIFECYCLE protected override void Awake() { base.Awake(); _scoreHandler = ScoreHandler.Instance; _gameHandler = GameHandler.Instance; } #endregion } //public void InstanciateOmnicient(Vector3 position) //{ // var bot = SpawnPlayer(position); // PlayerScript script = bot.GetComponent(); // Players.Add(script.Id, script); // DecoratePlayer(bot); // CreateScorePanel(script); // OnPlayerInstanciated?.Invoke(this, new PlayerEventArgs(script)); //} //public void InstanciateObserver(Vector3 position) //{ // var bot = SpawnPlayer(position); // PlayerScript script = bot.GetComponent(); // Players.Add(script.Id, script); // DecoratePlayer(bot); // CreateScorePanel(script); // OnPlayerInstanciated?.Invoke(this, new PlayerEventArgs(script)); //} //public void InstanciateIterator(Vector3 position) //{ // var bot = SpawnPlayer(position); // PlayerScript script = bot.GetComponent(); // Players.Add(script.Id, script); // DecoratePlayer(bot); // CreateScorePanel(script); // OnPlayerInstanciated?.Invoke(this, new PlayerEventArgs(script)); //} //public void InstanciateHumanPlayer(Vector3 position) //{ // Debug.Log("Instanciate Human"); // var humanPlayer = SpawnPlayer(new Vector3(position.x, position.y, -1)); // PlayerScript script = humanPlayer.GetComponent(); // script.IsHuman = true; // humanPlayer.AddComponent(); // DecoratePlayer(humanPlayer); // CreateScorePanel(script); // Players.Add(script.Id, script); // OnPlayerInstanciated?.Invoke(this, new PlayerEventArgs(script)); //}