52 lines
1.7 KiB
C#
52 lines
1.7 KiB
C#
using System.Collections.Generic;
|
|
using State;
|
|
using State.GameStateMachine;
|
|
using State.PawnStateMachine;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
using UnityEngine.InputSystem.UI;
|
|
|
|
namespace Managers
|
|
{
|
|
public class PlayerJoiner : MonoBehaviour
|
|
{
|
|
[SerializeField] private GameStateMachine stateMachine;
|
|
[SerializeField] private PlayerInputManager playerInputManager;
|
|
[SerializeField] private GameObject[] characterPrefabs = new GameObject[2];
|
|
|
|
|
|
private readonly List<PlayerInput> _players = new();
|
|
private PlayerInput _currentUIOwner;
|
|
|
|
private void OnEnable()
|
|
{
|
|
playerInputManager.onPlayerJoined += HandlePlayerJoined;
|
|
playerInputManager.onPlayerLeft += HandlePlayerLeft;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
playerInputManager.onPlayerJoined -= HandlePlayerJoined;
|
|
playerInputManager.onPlayerLeft -= HandlePlayerLeft;
|
|
}
|
|
|
|
private void HandlePlayerJoined(PlayerInput playerInput)
|
|
{
|
|
Debug.Log("Player joined!");
|
|
_players.Add(playerInput);
|
|
|
|
// TODO: Move all of this to a player spawner
|
|
GameStateMachine.Instance.ChangeState(new PlayLevel());
|
|
var character = Instantiate(characterPrefabs[_players.Count - 1]);
|
|
var playerStateMachine = playerInput.gameObject.GetComponent<PawnStateMachine>();
|
|
playerStateMachine.controlledPawn = character.GetComponent<Pawn>();
|
|
playerInput.actions.Enable();
|
|
}
|
|
|
|
private void HandlePlayerLeft(PlayerInput playerInput)
|
|
{
|
|
_players.Remove(playerInput);
|
|
if(_currentUIOwner == playerInput) _currentUIOwner = null;
|
|
}
|
|
}
|
|
} |