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.

62 lines
1.3 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class GameManager : MonoBehaviour
  5. {
  6. [SerializeField]
  7. private PlayerList m_PlayerList;
  8. [SerializeField]
  9. private GameObject m_playerPrefab;
  10. private Camera m_camera;
  11. private void OnEnable()
  12. {
  13. m_PlayerList.OnPlayerJoin += OnPlayerJoin;
  14. }
  15. private void OnDisable()
  16. {
  17. m_PlayerList.OnPlayerJoin -= OnPlayerJoin;
  18. }
  19. // Start is called before the first frame update
  20. void Start()
  21. {
  22. SetUpPlayers();
  23. m_camera = Camera.main;
  24. }
  25. private void SetUpPlayers()
  26. {
  27. foreach (PlayerData data in m_PlayerList.AllPlayers)
  28. {
  29. SpawnPlayer(data);
  30. }
  31. }
  32. private void SpawnPlayer(PlayerData data)
  33. {
  34. GameObject newPlayer = Instantiate(m_playerPrefab);
  35. data.Input.SwitchCurrentActionMap("Gameplay");
  36. foreach (InputBehaviour input in newPlayer.GetComponentsInChildren<InputBehaviour>())
  37. input.Initialise(data);
  38. newPlayer.GetComponentInChildren<PlayerVisuals>().Initalise(data);
  39. newPlayer.transform.position += Vector3.ProjectOnPlane(Random.insideUnitSphere, Vector3.up);
  40. }
  41. private void OnPlayerJoin(object sender, PlayerData data)
  42. {
  43. SpawnPlayer(data);
  44. }
  45. }