215 lines
5.9 KiB
C#
215 lines
5.9 KiB
C#
|
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<PlayersHandler>
|
||
|
{
|
||
|
// Start is called before the first frame update
|
||
|
[SerializeField] private GameObject playerPrefab;
|
||
|
|
||
|
#region PROPERTIES
|
||
|
|
||
|
public Dictionary<int, PlayerScript> Players { get; private set; } = new();
|
||
|
|
||
|
#endregion
|
||
|
#region VARIABLES
|
||
|
|
||
|
private ScoreHandler _scoreHandler;
|
||
|
private GameHandler _gameHandler;
|
||
|
|
||
|
#endregion
|
||
|
#region EVENTS
|
||
|
|
||
|
/// <summary>
|
||
|
/// Fires when a player is instanciated in the game
|
||
|
/// </summary>
|
||
|
public event EventHandler<PlayerEventArgs> OnPlayerInstanciated;
|
||
|
/// <summary>
|
||
|
/// Fires when a player have finished their attempt
|
||
|
/// </summary>
|
||
|
public event EventHandler<PlayerEventArgs> OnPlayerFinished;
|
||
|
/// <summary>
|
||
|
/// Fires when all players have finished their attempt
|
||
|
/// </summary>
|
||
|
public event EventHandler<EventArgs> OnAllPlayersFinished;
|
||
|
public class PlayerEventArgs : EventArgs
|
||
|
{
|
||
|
|
||
|
public PlayerScript player;
|
||
|
|
||
|
public PlayerEventArgs(PlayerScript player)
|
||
|
{
|
||
|
this.player = player;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
#region ENDPOINTS
|
||
|
/// <summary>
|
||
|
/// Remove a player from the game and fire a PlayerFinished event. Fire AllPlayerFinished event if needed
|
||
|
/// </summary>
|
||
|
/// <param name="playerId"></param>
|
||
|
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<T>(Vector3 position) where T : MonoBehaviour
|
||
|
{
|
||
|
|
||
|
var player = SpawnPlayer(new Vector3(position.x, position.y, -1));
|
||
|
|
||
|
PlayerScript script = player.GetComponent<PlayerScript>();
|
||
|
player.AddComponent<T>();
|
||
|
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<PlayerScript>();
|
||
|
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<PlayerScript>();
|
||
|
// 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<PlayerScript>();
|
||
|
var components = child.GetComponentsInChildren<SpriteRenderer>();
|
||
|
var jetpackFront = child.transform.Find("JetpackFront").GetComponent<SpriteRenderer>();
|
||
|
var jetpackBack = child.transform.Find("JetpackBack").GetComponent<SpriteRenderer>();
|
||
|
|
||
|
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<PlayerScript>();
|
||
|
// 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<PlayerScript>();
|
||
|
// 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<PlayerScript>();
|
||
|
// 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<PlayerScript>();
|
||
|
// script.IsHuman = true;
|
||
|
// humanPlayer.AddComponent<HumanPlayer>();
|
||
|
// DecoratePlayer(humanPlayer);
|
||
|
|
||
|
// CreateScorePanel(script);
|
||
|
|
||
|
// Players.Add(script.Id, script);
|
||
|
// OnPlayerInstanciated?.Invoke(this, new PlayerEventArgs(script));
|
||
|
//}
|