2025-01-15 13:34:36 -05:00
|
|
|
using System.Collections.Generic;
|
2025-01-17 09:40:48 -05:00
|
|
|
using NUnit.Framework.Constraints;
|
2025-01-14 11:40:09 -05:00
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.InputSystem;
|
|
|
|
|
|
|
|
|
|
public class PlayerManager : MonoBehaviour
|
|
|
|
|
{
|
2025-01-15 13:34:36 -05:00
|
|
|
public static PlayerManager Instance;
|
|
|
|
|
|
|
|
|
|
public List<GameObject> players;
|
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-01-17 12:05:54 -05:00
|
|
|
private Vector2 spawnPosition;
|
2025-01-14 11:40:09 -05:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnPlayerJoined(PlayerInput playerInput)
|
|
|
|
|
{
|
|
|
|
|
playerInput.transform.position = spawnPosition;
|
2025-01-15 13:34:36 -05:00
|
|
|
players.Add(playerInput.gameObject);
|
2025-02-08 18:54:21 -05:00
|
|
|
Colorize(playerInput.gameObject);
|
2025-01-14 11:40:09 -05:00
|
|
|
print("Player joined");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnPlayerLeft(PlayerInput playerInput)
|
|
|
|
|
{
|
|
|
|
|
Destroy(playerInput.gameObject);
|
2025-01-15 13:34:36 -05:00
|
|
|
players.Remove(playerInput.gameObject);
|
2025-01-14 11:40:09 -05:00
|
|
|
print("Player left");
|
|
|
|
|
}
|
2025-01-15 13:34:36 -05:00
|
|
|
|
|
|
|
|
private void Init()
|
|
|
|
|
{
|
|
|
|
|
if (Instance == null)
|
|
|
|
|
{
|
|
|
|
|
Instance = this;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
print("A PlayerManager already exists.");
|
2025-01-17 12:05:54 -05:00
|
|
|
Destroy(this.gameObject);
|
2025-01-15 13:34:36 -05:00
|
|
|
}
|
2025-01-17 12:05:54 -05:00
|
|
|
|
|
|
|
|
spawnPosition = transform.position;
|
2025-01-15 13:34:36 -05:00
|
|
|
}
|
2025-02-08 18:54:21 -05:00
|
|
|
|
|
|
|
|
private void Colorize(GameObject player)
|
|
|
|
|
{
|
|
|
|
|
Color color = playerColors[(players.Count - 1) % playerColors.Count];
|
|
|
|
|
float tint = Mathf.Floor((players.Count - 1) / playerColors.Count);
|
|
|
|
|
color = (color + color + Color.white * tint) / (tint + 2);
|
|
|
|
|
|
|
|
|
|
if (player.TryGetComponent<SpriteRenderer>(out _))
|
|
|
|
|
{
|
|
|
|
|
player.GetComponent<SpriteRenderer>().color = color;
|
|
|
|
|
}
|
|
|
|
|
foreach (Transform child in player.transform)
|
|
|
|
|
{
|
|
|
|
|
if (child.TryGetComponent<SpriteRenderer>(out _))
|
|
|
|
|
{
|
|
|
|
|
Colorize(child.gameObject);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-14 11:40:09 -05:00
|
|
|
}
|