using System.Collections; using System.Collections.Generic; using UnityEngine; using Variables; public class GameManager : MonoBehaviour { [SerializeField] private PlayerList m_PlayerList; [SerializeField] private GameObject m_playerPrefab; [SerializeField] private Reference m_TimeLeft; [SerializeField] private Reference m_TotalTime; [SerializeField,NaughtyAttributes.Scene] private string m_creditScene; private bool m_gameEndCalled; private Camera m_camera; private void OnEnable() { m_PlayerList.OnPlayerJoin += OnPlayerJoin; } private void OnDisable() { m_PlayerList.OnPlayerJoin -= OnPlayerJoin; } // Start is called before the first frame update void Start() { SetUpPlayers(); m_camera = Camera.main; m_TimeLeft.Value = m_TotalTime; } private void Update() { m_TimeLeft.Value -= Time.deltaTime; if (m_TimeLeft < 0 && !m_gameEndCalled) OnGameEnd(); } private void SetUpPlayers() { foreach (PlayerData data in m_PlayerList.AllPlayers) { SpawnPlayer(data); } } private void OnGameEnd() { m_gameEndCalled = true; UnityEngine.SceneManagement.SceneManager.LoadScene(m_creditScene); } private void SpawnPlayer(PlayerData data) { GameObject newPlayer = Instantiate(m_playerPrefab); data.Input.SwitchCurrentActionMap("Gameplay"); foreach (InputBehaviour input in newPlayer.GetComponentsInChildren()) input.Initialise(data); newPlayer.GetComponentInChildren().Initalise(data); newPlayer.transform.position += Vector3.ProjectOnPlane(Random.insideUnitSphere, Vector3.up); } private void OnPlayerJoin(object sender, PlayerData data) { SpawnPlayer(data); } }