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.

61 lines
1.2 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.InputSystem;
  5. [RequireComponent(typeof(PlayerInputManager))]
  6. public class PlayerManager : MonoBehaviour
  7. {
  8. private static PlayerManager Instance;
  9. [SerializeField]
  10. private PlayerList m_connectedPlayers;
  11. private PlayerInputManager m_playerManager;
  12. private void OnEnable()
  13. {
  14. DoSingleton();
  15. m_playerManager = GetComponent<PlayerInputManager>();
  16. }
  17. public void OnPlayerJoined(UnityEngine.InputSystem.PlayerInput player)
  18. {
  19. GameObject newPlayer = player.gameObject;
  20. Debug.Log($"New Player Joined");
  21. PlayerData data = m_connectedPlayers.AddPlayer(player);
  22. newPlayer.GetComponent<PlayerDataHolder>().Initialise(data);
  23. newPlayer.transform.SetParent(transform, false);
  24. }
  25. public void AllowJoin(bool value)
  26. {
  27. if (value)
  28. m_playerManager.EnableJoining();
  29. else
  30. m_playerManager.DisableJoining();
  31. }
  32. private void DoSingleton()
  33. {
  34. if (Instance == null)
  35. {
  36. Instance = this;
  37. DontDestroyOnLoad(gameObject);
  38. }
  39. else
  40. {
  41. Destroy(gameObject);
  42. }
  43. }
  44. }