Global Game Jam 2021
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

68 lines
1.4 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
[RequireComponent(typeof(PlayerInputManager))]
public class PlayerManager : MonoBehaviour
{
private static PlayerManager Instance;
[SerializeField]
private PlayerList m_connectedPlayers;
private PlayerInputManager m_playerManager;
private void OnEnable()
{
DoSingleton();
m_playerManager = GetComponent<PlayerInputManager>();
}
public void OnPlayerJoined(UnityEngine.InputSystem.PlayerInput player)
{
GameObject newPlayer = player.gameObject;
Debug.Log($"New Player Joined");
PlayerData data = m_connectedPlayers.AddPlayer(player);
newPlayer.GetComponent<PlayerDataHolder>().Initialise(data);
newPlayer.transform.SetParent(transform, false);
}
public void AllowJoin(bool value)
{
if (value)
m_playerManager.EnableJoining();
else
m_playerManager.DisableJoining();
}
private void DoSingleton()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
public static void Clear()
{
if (Instance != null)
{
Instance.m_connectedPlayers.Players.Clear();
Destroy(Instance.gameObject);
}
}
}