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.

87 lines
1.9 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Variables;
  5. public class GameManager : MonoBehaviour
  6. {
  7. [SerializeField]
  8. private PlayerList m_PlayerList;
  9. [SerializeField]
  10. private GameObject m_playerPrefab;
  11. [SerializeField]
  12. private Reference<float> m_TimeLeft;
  13. [SerializeField]
  14. private Reference<float> m_TotalTime;
  15. [SerializeField,NaughtyAttributes.Scene]
  16. private string m_creditScene;
  17. private bool m_gameEndCalled;
  18. private Camera m_camera;
  19. private void OnEnable()
  20. {
  21. m_PlayerList.OnPlayerJoin += OnPlayerJoin;
  22. }
  23. private void OnDisable()
  24. {
  25. m_PlayerList.OnPlayerJoin -= OnPlayerJoin;
  26. }
  27. // Start is called before the first frame update
  28. void Start()
  29. {
  30. SetUpPlayers();
  31. m_camera = Camera.main;
  32. m_TimeLeft.Value = m_TotalTime;
  33. }
  34. private void Update()
  35. {
  36. m_TimeLeft.Value -= Time.deltaTime;
  37. if (m_TimeLeft < 0 && !m_gameEndCalled)
  38. OnGameEnd();
  39. }
  40. private void SetUpPlayers()
  41. {
  42. foreach (PlayerData data in m_PlayerList.AllPlayers)
  43. {
  44. SpawnPlayer(data);
  45. }
  46. }
  47. private void OnGameEnd()
  48. {
  49. m_gameEndCalled = true;
  50. UnityEngine.SceneManagement.SceneManager.LoadScene(m_creditScene);
  51. }
  52. private void SpawnPlayer(PlayerData data)
  53. {
  54. GameObject newPlayer = Instantiate(m_playerPrefab);
  55. data.Input.SwitchCurrentActionMap("Gameplay");
  56. foreach (InputBehaviour input in newPlayer.GetComponentsInChildren<InputBehaviour>())
  57. input.Initialise(data);
  58. newPlayer.GetComponentInChildren<PlayerVisuals>().Initalise(data);
  59. newPlayer.transform.position += Vector3.ProjectOnPlane(Random.insideUnitSphere, Vector3.up);
  60. }
  61. private void OnPlayerJoin(object sender, PlayerData data)
  62. {
  63. SpawnPlayer(data);
  64. }
  65. }