Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Assets/Scripts/Construction/BuildingSelectionUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ private void Start()
LoadButtons();
exitButton.onClick.AddListener(Close);
gameObject.SetActive(false);
playerCamera = mainCamera.GetComponent<PlayerCamera>();
playerConstructionHandler = player.GetComponent<PlayerConstructionHandler>();
playerCamera = GameManager.PlayerCamera;
playerConstructionHandler = GameManager.GetPlayerComponent<PlayerConstructionHandler>();
}

private void LateUpdate()
Expand Down
102 changes: 102 additions & 0 deletions Assets/Scripts/GameManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System.Collections.Generic;
using System.Linq;
using Cosmobot.Utils;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace Cosmobot
{
[DefaultExecutionOrder(ExecutionOrder.GameManager)]
public class GameManager : SingletonSystem<GameManager>
{
private Scene? playerScene;
private Transform playerTransform;
private PlayerController playerController;
private PlayerCamera playerCamera;

public static Transform PlayerTransform => SemiSafeGetPlayer(Instance.playerTransform);
public static PlayerController PlayerController => SemiSafeGetPlayer(Instance.playerController);
public static PlayerCamera PlayerCamera => SemiSafeGetPlayer(Instance.playerCamera);

public static T GetPlayerComponent<T>() where T : class
Comment thread
Acu1000 marked this conversation as resolved.
{
return SemiSafeGetPlayer(Instance.playerTransform.GetComponent<T>());
}

private static T SemiSafeGetPlayer<T>(T o) where T : class
{
#if UNITY_EDITOR
Scene? scene = Instance.playerScene;
if (!scene.HasValue || !scene.Value.isLoaded || !Instance.playerTransform)
{
Debug.LogWarning("Accessing unloaded player!");
return null;
}
#endif
return o;
}

protected override void SystemAwake()
{
SceneManager.sceneLoaded += OnSceneLoad;
SceneManager.sceneUnloaded += OnSceneUnload;
// first setup
OnSceneLoad(SceneManager.GetActiveScene(), LoadSceneMode.Single);
}

void OnSceneLoad(Scene _, LoadSceneMode _1)
{
SetupPlayer();
}

void OnSceneUnload(Scene scene)
{
if (scene.IsValid() && scene.isLoaded && scene != playerScene) return;
ClearPlayer();
}

void ClearPlayer()
{
playerScene = null;
playerTransform = null;
playerController = null;
playerCamera = null;
}

void SetupPlayer()
{
if (playerTransform) // will also check if object is valid
{
return;
}

GameObject[] playerObjects = GameObject.FindGameObjectsWithTag(Tags.Player);
List<GameObject> playerCandidates = playerObjects.Where(IsValidPlayerCandidate).ToList();
if (playerCandidates.Count == 0)
{
return;
}

GameObject selectedPlayer = playerCandidates[0];
if (playerCandidates.Count > 1)
{
string playerObjectsNames = playerObjects.Skip(1).Select(g => g.name).Aggregate((a, b) => $"{a}, '{b}'");
Debug.LogWarning($"Found multiple GameObject that appear to be an Player GameObjects" +
$"Using first one (click to select used object)." +
$"Other found objects: {playerObjectsNames}", selectedPlayer);
}

playerTransform = selectedPlayer.transform;
playerController = selectedPlayer.GetComponent<PlayerController>();
playerCamera = selectedPlayer.GetComponentInChildren<PlayerCamera>();
playerScene = selectedPlayer.scene; // use accusal GO scene
}

bool IsValidPlayerCandidate(GameObject playerCandidate)
{
return playerCandidate
&& playerCandidate.GetComponent<PlayerController>()
&& playerCandidate.GetComponentInChildren<PlayerCamera>();
}
}
}
3 changes: 3 additions & 0 deletions Assets/Scripts/GameManager.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Assets/Scripts/Utils/SingletonSystem.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using UnityEditor;
using UnityEngine;

namespace Cosmobot.Utils
Expand Down Expand Up @@ -27,6 +28,10 @@ protected virtual void OnDisable()
{
if (!ReferenceEquals(Instance, this)) return;

#if UNITY_EDITOR
// prevent log warning (and keeping enabled) when exiting playmode
if (Application.exitCancellationToken.IsCancellationRequested) return;
#endif
enabled = true;
Debug.LogWarning($"SingletonSystem {typeof(T).Name} cannot be disabled", this);
}
Expand All @@ -37,6 +42,10 @@ protected void OnDestroy()
{
SystemOnDestroy();
Instance = null;
#if UNITY_EDITOR
// prevent log warning when exiting playmode
if (Application.exitCancellationToken.IsCancellationRequested) return;
#endif
Debug.LogWarning($"SingletonSystem {typeof(T).Name} unregistered", this);
}
}
Expand Down
1 change: 1 addition & 0 deletions Assets/Scripts/Utils/Tags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public static class Tags
{
public const string Item = "Item";
public const string Enemy = "Enemy";
public const string Player = "Player";
}
}
Loading