Update SIngleton definitions for easier use in Inspector

This commit is contained in:
Antoine H 2021-01-13 15:05:09 +01:00
parent 4289fae167
commit 6e865410d7
3 changed files with 99 additions and 46 deletions

View file

@ -6,13 +6,26 @@ using UnityEditor;
//Define the system managing the clients. (Singleton)
public sealed class ClientManager : MonoBehaviour
{
//Singleton
private static ClientManager _instance=null;
public static ClientManager Instance { get
{
if(_instance is null)
Debug.LogError("Missing ClientManager instance");
return _instance;
}
}
[HideInInspector]
public bool ready = false; //Wether the ClientManager is initialized
public int nbMaxClients = 3;
[SerializeField]
int nbMaxClients = 1; //Maximum active clients
[SerializeField]
float clientSpawnTimer = 0.5f; //Intial time before first spawn (pseudo-random after that)
[SerializeField]
float maxTimeNewClients = 2.0f; //Longest waiting time for new clients
bool clientSpawnReady = false;
public float clientSpawnTimer = 0.5f; //Intial time before first spawn (pseudo-random after that)
public float maxTimeNewClients = 2.0f; //Longest waiting time for new clients
string ClientRessourceFolder = "Clients";
private Object[] clients;
@ -100,6 +113,12 @@ public sealed class ClientManager : MonoBehaviour
//Awake is called when the script instance is being loaded.
void Awake()
{
//Singleton
if (_instance != null && _instance != this)
Destroy(this.gameObject);
else
_instance = this;
if(!ready)
{
ClientContainer = GameObject.Find("/GameSystem/ClientManager");
@ -174,21 +193,21 @@ public sealed class ClientManager : MonoBehaviour
}
//// Singleton Implementation (https://jlambert.developpez.com/tutoriels/dotnet/implementation-pattern-singleton-csharp/#LIII) ////
private ClientManager()
{
}
// private ClientManager()
// {
// }
public static ClientManager Instance { get { return Nested.instance; } }
// public static ClientManager Instance { get { return Nested.instance; } }
private class Nested
{
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Nested()
{
}
// private class Nested
// {
// // Explicit static constructor to tell C# compiler
// // not to mark type as beforefieldinit
// static Nested()
// {
// }
internal static readonly ClientManager instance = new GameObject("ClientManager").AddComponent<ClientManager>();
}
// internal static readonly ClientManager instance = new GameObject("ClientManager").AddComponent<ClientManager>();
// }
////
}

View file

@ -5,15 +5,27 @@ using UnityEngine;
//Define the global game system of the service. (Singleton)
public sealed class GameSystem : MonoBehaviour
{
//Singleton
private static GameSystem _instance=null;
public static GameSystem Instance { get
{
if(_instance is null)
Debug.LogError("Missing GameSystem instance");
return _instance;
}
}
[HideInInspector]
public bool ready = false; //Wether the GameSystems are initialized
//Time
bool serviceOpen = false;
public float serviceTime = 30.0f;
[SerializeField]
float serviceTime = 30.0f;
float serviceTimer = 0.0f;
UITimer UIServiceTimer;
public float slowScale = 0.5f; //Default scale for slow mode
[SerializeField]
float slowScale = 0.5f; //Default scale for slow mode
private float fixedDeltaTime;
//TODO : Effect on gold change
@ -67,6 +79,12 @@ public sealed class GameSystem : MonoBehaviour
//Awake is called when the script instance is being loaded.
void Awake()
{
//Singleton
if (_instance != null && _instance != this)
Destroy(this.gameObject);
else
_instance = this;
if(!ready)
{
// Make a copy of the fixedDeltaTime, it defaults to 0.02f, but it can be changed in the editor
@ -129,21 +147,21 @@ public sealed class GameSystem : MonoBehaviour
//public static GameSystem instance { get; private set; } //Give public access to the instance. But only set from this class
//// Singleton Implementation (https://jlambert.developpez.com/tutoriels/dotnet/implementation-pattern-singleton-csharp/#LIII) ////
private GameSystem()
{
}
// private GameSystem()
// {
// }
public static GameSystem Instance { get { return Nested.instance; } }
// public static GameSystem Instance { get { return Nested.instance; } }
private class Nested
{
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Nested()
{
}
// private class Nested
// {
// // Explicit static constructor to tell C# compiler
// // not to mark type as beforefieldinit
// static Nested()
// {
// }
internal static readonly GameSystem instance = new GameObject("GameSystem").AddComponent<GameSystem>();
}
// internal static readonly GameSystem instance = new GameObject("GameSystem").AddComponent<GameSystem>();
// }
////
}

View file

@ -4,8 +4,18 @@ using UnityEngine;
//Define the system managing the stock of goods. (Singleton)
//TODO : Update check stock of registered workshops
public class StockManager : MonoBehaviour
public sealed class StockManager : MonoBehaviour
{
//Singleton
private static StockManager _instance=null;
public static StockManager Instance { get
{
if(_instance is null)
Debug.LogError("Missing StockManager instance");
return _instance;
}
}
[HideInInspector]
public bool ready = false; //Wether the StockManager is initialized
@ -51,7 +61,7 @@ public class StockManager : MonoBehaviour
//Remove workshop
workshop_register.Remove(workshop);
Debug.Log(workshop.gameObject.name+" unregistered by StockManager");
// Debug.Log(workshop.gameObject.name+" unregistered by StockManager");
}
}
@ -74,6 +84,12 @@ public class StockManager : MonoBehaviour
//Awake is called when the script instance is being loaded.
void Awake()
{
//Singleton
if (_instance != null && _instance != this)
Destroy(this.gameObject);
else
_instance = this;
if(!ready)
{
//Initialize global stock
@ -94,21 +110,21 @@ public class StockManager : MonoBehaviour
}
//// Singleton Implementation (https://jlambert.developpez.com/tutoriels/dotnet/implementation-pattern-singleton-csharp/#LIII) ////
private StockManager()
{
}
// private StockManager()
// {
// }
public static StockManager Instance { get { return Nested.instance; } }
// public static StockManager Instance { get { return Nested.instance; } }
private class Nested
{
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Nested()
{
}
// private class Nested
// {
// // Explicit static constructor to tell C# compiler
// // not to mark type as beforefieldinit
// static Nested()
// {
// }
internal static readonly StockManager instance = new GameObject("StockManager").AddComponent<StockManager>();
}
// internal static readonly StockManager instance = new GameObject("StockManager").AddComponent<StockManager>();
// }
////
}