2025-03-07 23:32:42 -05:00
|
|
|
using System.Collections;
|
2025-01-15 13:34:36 -05:00
|
|
|
using System.Collections.Generic;
|
2025-04-16 19:57:54 -04:00
|
|
|
using UnityEngine; using Game; using Music; using Player;
|
2025-01-14 11:40:09 -05:00
|
|
|
using UnityEngine.InputSystem;
|
2025-04-16 19:57:54 -04:00
|
|
|
namespace Player
|
|
|
|
|
{
|
2025-01-14 11:40:09 -05:00
|
|
|
|
|
|
|
|
public class PlayerManager : MonoBehaviour
|
|
|
|
|
{
|
2025-01-15 13:34:36 -05:00
|
|
|
public static PlayerManager Instance;
|
2025-02-16 16:47:45 -05:00
|
|
|
public List<PlayerJoinCard> cards;
|
2025-01-17 09:40:48 -05:00
|
|
|
[SerializeField] private InputActionAsset playerActions;
|
2025-02-08 18:54:21 -05:00
|
|
|
public List<Color> playerColors;
|
2025-02-16 16:47:45 -05:00
|
|
|
public GameObject playerSelect;
|
2025-02-28 13:49:02 -05:00
|
|
|
private bool gameStarted = false;
|
|
|
|
|
|
2025-01-15 13:34:36 -05:00
|
|
|
private void Awake()
|
|
|
|
|
{
|
|
|
|
|
Init();
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-14 11:40:09 -05:00
|
|
|
private void Start()
|
|
|
|
|
{
|
|
|
|
|
GetComponent<PlayerInputManager>().onPlayerJoined += OnPlayerJoined;
|
|
|
|
|
GetComponent<PlayerInputManager>().onPlayerLeft += OnPlayerLeft;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-28 17:39:07 -04:00
|
|
|
private void OnPlayerJoined(PlayerInput playerInput) // Adds a player when they join
|
2025-01-14 11:40:09 -05:00
|
|
|
{
|
2025-02-28 13:49:02 -05:00
|
|
|
if (gameStarted)
|
|
|
|
|
{
|
|
|
|
|
Destroy(playerInput.gameObject);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-04-18 02:50:50 -04:00
|
|
|
print("Player joined");
|
2025-02-19 20:11:57 -05:00
|
|
|
DontDestroyOnLoad(playerInput.gameObject);
|
2025-04-18 02:50:50 -04:00
|
|
|
if (PlayerCardCreator.Instance == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-02-19 20:11:57 -05:00
|
|
|
PlayerJoinCard card = PlayerCardCreator.Instance.CreateCard();
|
2025-04-18 02:50:50 -04:00
|
|
|
if (card == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-02-19 20:11:57 -05:00
|
|
|
card.playerNumber = GameManager.players.Count + 1;
|
2025-02-16 16:47:45 -05:00
|
|
|
cards.Add(card);
|
2025-04-18 02:50:50 -04:00
|
|
|
if (GameManager.players == null)
|
|
|
|
|
{
|
|
|
|
|
GameManager.players = new List<GameObject>();
|
|
|
|
|
}
|
2025-02-19 20:11:57 -05:00
|
|
|
GameManager.players.Add(playerInput.gameObject);
|
|
|
|
|
Colorize(GameManager.players.Count - 1);
|
2025-01-14 11:40:09 -05:00
|
|
|
}
|
|
|
|
|
|
2025-03-07 11:56:19 -05:00
|
|
|
|
2025-03-28 17:39:07 -04:00
|
|
|
private void OnPlayerLeft(PlayerInput playerInput) // Removes the player if they leave
|
2025-01-14 11:40:09 -05:00
|
|
|
{
|
|
|
|
|
Destroy(playerInput.gameObject);
|
2025-02-19 20:11:57 -05:00
|
|
|
GameManager.players.Remove(playerInput.gameObject);
|
2025-04-18 02:50:50 -04:00
|
|
|
print("Player left");
|
2025-01-14 11:40:09 -05:00
|
|
|
}
|
2025-01-15 13:34:36 -05:00
|
|
|
|
|
|
|
|
private void Init()
|
|
|
|
|
{
|
|
|
|
|
if (Instance == null)
|
|
|
|
|
{
|
|
|
|
|
Instance = this;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-01-17 12:05:54 -05:00
|
|
|
Destroy(this.gameObject);
|
2025-01-15 13:34:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
2025-02-08 18:54:21 -05:00
|
|
|
|
2025-03-28 17:39:07 -04:00
|
|
|
public void StartGame() // Allows game to start after a player has joined
|
2025-02-08 18:54:21 -05:00
|
|
|
{
|
2025-03-06 01:27:42 -05:00
|
|
|
if (GameManager.players.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-03-07 11:56:19 -05:00
|
|
|
gameStarted = true;
|
|
|
|
|
HubManager.Instance.LoadScene(GameManager.map);
|
2025-02-16 16:47:45 -05:00
|
|
|
}
|
|
|
|
|
|
2025-03-28 17:39:07 -04:00
|
|
|
private void Colorize(int index) // Pairs each player with a unique color
|
2025-02-16 16:47:45 -05:00
|
|
|
{
|
2025-02-19 20:11:57 -05:00
|
|
|
GameObject player = GameManager.players[index];
|
|
|
|
|
Color color = playerColors[(GameManager.players.Count - 1) % playerColors.Count];
|
|
|
|
|
float tint = Mathf.Floor((GameManager.players.Count - 1) / playerColors.Count);
|
2025-02-08 18:54:21 -05:00
|
|
|
color = (color + color + Color.white * tint) / (tint + 2);
|
2025-03-07 16:25:59 -05:00
|
|
|
GameManager.playerColors.Add(color);
|
2025-02-16 16:47:45 -05:00
|
|
|
ApplyColor(player, color);
|
2025-02-19 20:11:57 -05:00
|
|
|
ApplyColor(cards[GameManager.players.IndexOf(player)].playerPreview, color);
|
2025-02-16 16:47:45 -05:00
|
|
|
}
|
|
|
|
|
|
2025-03-28 17:39:07 -04:00
|
|
|
private void ApplyColor(GameObject obj, Color color) // Applies a color to each player
|
2025-02-28 13:49:02 -05:00
|
|
|
{
|
2025-02-16 16:47:45 -05:00
|
|
|
if (obj.TryGetComponent<SpriteRenderer>(out _))
|
2025-02-08 18:54:21 -05:00
|
|
|
{
|
2025-02-16 16:47:45 -05:00
|
|
|
obj.GetComponent<SpriteRenderer>().color = color;
|
2025-02-08 18:54:21 -05:00
|
|
|
}
|
2025-02-16 16:47:45 -05:00
|
|
|
foreach (Transform child in obj.transform)
|
2025-02-08 18:54:21 -05:00
|
|
|
{
|
|
|
|
|
if (child.TryGetComponent<SpriteRenderer>(out _))
|
|
|
|
|
{
|
2025-02-16 16:47:45 -05:00
|
|
|
ApplyColor(child.gameObject, color);
|
2025-02-08 18:54:21 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-16 19:57:54 -04:00
|
|
|
}}
|