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-14 11:40:09 -05:00
|
|
|
[SerializeField] private Vector2 spawnPosition;
|
2025-01-17 09:40:48 -05:00
|
|
|
[SerializeField] private InputActionAsset playerActions;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Header("Debug")]
|
|
|
|
|
[SerializeField] private InputActionAsset player2Actions;
|
2025-01-14 11:40:09 -05:00
|
|
|
|
2025-01-15 15:05:04 -05:00
|
|
|
private PlayerCameraMovement playerCamera;
|
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;
|
|
|
|
|
|
2025-01-15 15:05:04 -05:00
|
|
|
playerCamera = FindFirstObjectByType<PlayerCameraMovement>();
|
2025-01-14 11:40:09 -05:00
|
|
|
}
|
|
|
|
|
|
2025-01-17 09:40:48 -05:00
|
|
|
private void Update()
|
|
|
|
|
{
|
|
|
|
|
if (InputSystem.actions.FindAction("Join Player").WasPressedThisFrame())
|
|
|
|
|
{
|
|
|
|
|
JoinPlayer(playerActions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Keyboard.current.periodKey.wasPressedThisFrame)
|
|
|
|
|
{
|
|
|
|
|
JoinPlayer(player2Actions);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-14 11:40:09 -05:00
|
|
|
private void OnPlayerJoined(PlayerInput playerInput)
|
|
|
|
|
{
|
|
|
|
|
playerInput.transform.position = spawnPosition;
|
2025-01-15 13:34:36 -05:00
|
|
|
players.Add(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 09:40:48 -05:00
|
|
|
|
|
|
|
|
private void JoinPlayer(InputActionAsset actions)
|
|
|
|
|
{
|
|
|
|
|
PlayerInput player = GetComponent<PlayerInputManager>().JoinPlayer(-1, -1, null, Keyboard.current);
|
|
|
|
|
print(player == null);
|
|
|
|
|
player.actions = actions;
|
|
|
|
|
}
|
2025-01-14 11:40:09 -05:00
|
|
|
}
|