From 901a2b6c1054ddbd6a6ecc3527442a28ebe6760a Mon Sep 17 00:00:00 2001 From: Antoine H Date: Wed, 13 Jan 2021 13:32:24 +0100 Subject: [PATCH] Add more interface w/ Unity Inspector and Managers --- Assets/Scenes/Tests/SampleScene.unity | 73 ++++++++++++- Assets/Scripts/GameSystems/ClientManager.cs | 110 +++++++++++--------- Assets/Scripts/GameSystems/GameSystem.cs | 36 ++++--- Assets/Scripts/GameSystems/StockManager.cs | 18 +++- 4 files changed, 168 insertions(+), 69 deletions(-) diff --git a/Assets/Scenes/Tests/SampleScene.unity b/Assets/Scenes/Tests/SampleScene.unity index 5ac544d..4c8f3ee 100644 --- a/Assets/Scenes/Tests/SampleScene.unity +++ b/Assets/Scenes/Tests/SampleScene.unity @@ -297,6 +297,7 @@ GameObject: serializedVersion: 6 m_Component: - component: {fileID: 128722683} + - component: {fileID: 128722684} m_Layer: 0 m_Name: ClientManager m_TagString: Untagged @@ -318,6 +319,22 @@ Transform: m_Father: {fileID: 1160225022} m_RootOrder: 1 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &128722684 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 128722682} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 2bc1593400bcb054db0179d45fa332e9, type: 3} + m_Name: + m_EditorClassIdentifier: + ready: 0 + nbMaxClients: 5 + clientSpawnTimer: 0.5 + maxTimeNewClients: 2 --- !u!1 &156172339 GameObject: m_ObjectHideFlags: 0 @@ -3124,7 +3141,7 @@ Transform: m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 1160225022} - m_RootOrder: 0 + m_RootOrder: 2 m_LocalEulerAnglesHint: {x: -90, y: 0, z: 0} --- !u!1 &357681231 GameObject: @@ -3574,6 +3591,50 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 6 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &722742920 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 722742921} + - component: {fileID: 722742922} + m_Layer: 0 + m_Name: StockManager + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &722742921 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 722742920} + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalPosition: {x: 0.8086959, y: -2.008171, z: 1.1246597} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 1160225022} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &722742922 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 722742920} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: f33c46360b013aa48a333f4be9bb3c09, type: 3} + m_Name: + m_EditorClassIdentifier: + ready: 0 --- !u!1001 &728581040 PrefabInstance: m_ObjectHideFlags: 0 @@ -7717,6 +7778,9 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 5f18e7df2f912f644a6a6949b25117b6, type: 3} m_Name: m_EditorClassIdentifier: + ready: 0 + serviceTime: 60 + slowScale: 0.5 --- !u!4 &1160225022 Transform: m_ObjectHideFlags: 0 @@ -7728,8 +7792,9 @@ Transform: m_LocalPosition: {x: -0.21348187, y: 4.0925093, z: -2.1510859} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: - - {fileID: 322056904} + - {fileID: 722742921} - {fileID: 128722683} + - {fileID: 322056904} - {fileID: 1863361785} - {fileID: 1486551544} m_Father: {fileID: 0} @@ -7976,7 +8041,7 @@ Transform: m_LocalScale: {x: 0.1, y: 0.1, z: 0.1} m_Children: [] m_Father: {fileID: 1160225022} - m_RootOrder: 3 + m_RootOrder: 4 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &1520733216 GameObject: @@ -32996,7 +33061,7 @@ Transform: - {fileID: 238532360} - {fileID: 1653625651} m_Father: {fileID: 1160225022} - m_RootOrder: 2 + m_RootOrder: 3 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &1905354803 GameObject: diff --git a/Assets/Scripts/GameSystems/ClientManager.cs b/Assets/Scripts/GameSystems/ClientManager.cs index f367c53..ebba4da 100644 --- a/Assets/Scripts/GameSystems/ClientManager.cs +++ b/Assets/Scripts/GameSystems/ClientManager.cs @@ -6,10 +6,13 @@ using UnityEditor; //Define the system managing the clients. (Singleton) public sealed class ClientManager : MonoBehaviour { - int nbMaxClients = 3; + [HideInInspector] + public bool ready = false; //Wether the ClientManager is initialized + + public int nbMaxClients = 3; bool clientSpawnReady = false; - float clientSpawnTimer = 0.5f; //Intial time before first spawn (pseudo-random after that) - float maxTimeNewClients = 2.0f; + 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; @@ -53,6 +56,12 @@ public sealed class ClientManager : MonoBehaviour Destroy(client); // Debug.Log(client.name+" destroyed"+clientIDs.Count); + //Prevent immediate spawn of a new client after one leaving + if(clientSpawnReady) + { + clientSpawnReady=false; + clientSpawnTimer+=0.5f; + } } //Return a random available target. Or the Exit if Exit=True. @@ -91,59 +100,64 @@ public sealed class ClientManager : MonoBehaviour //Awake is called when the script instance is being loaded. void Awake() { - ClientContainer = GameObject.Find("/GameSystem/ClientManager"); - if (ClientContainer is null) - throw new System.Exception("No ClientManager found under GameSystem"); - - // Load clients prefabs // - - // Find all assets labelled with 'usable' : - // string[] guids = AssetDatabase.FindAssets("", new string[] {"Assets/Prefabs/Characters/Clients"}); - - // foreach (string guid in guids) - // { - // Debug.Log(AssetDatabase.GUIDToAssetPath(guid)); - // Instantiate(guid, spawnPosition, Quaternion.identity); - // } - - clients = Resources.LoadAll(ClientRessourceFolder); - - // foreach (var c in clients) - // { - // Debug.Log(gameObject.name+" : "+c.name + " loaded"); - // } - if (clients.Length(); - GameObject targetsObj = GameObject.Find("/GameSystem/Targets"); - if (targetsObj is null) - throw new System.Exception("No Targets GameObject found under GameSystem"); + // Find all assets labelled with 'usable' : + // string[] guids = AssetDatabase.FindAssets("", new string[] {"Assets/Prefabs/Characters/Clients"}); - Component[] targets = targetsObj.GetComponentsInChildren(); - if(targets != null) - { - foreach(Transform target in targets) + // foreach (string guid in guids) + // { + // Debug.Log(AssetDatabase.GUIDToAssetPath(guid)); + // Instantiate(guid, spawnPosition, Quaternion.identity); + // } + + clients = Resources.LoadAll(ClientRessourceFolder); + + // foreach (var c in clients) + // { + // Debug.Log(gameObject.name+" : "+c.name + " loaded"); + // } + if (clients.Length(); + GameObject targetsObj = GameObject.Find("/GameSystem/Targets"); + if (targetsObj is null) + throw new System.Exception("No Targets GameObject found under GameSystem"); + + Component[] targets = targetsObj.GetComponentsInChildren(); + if(targets != null) + { + foreach(Transform target in targets) { - targets_dict.Add(target.position, false); - // Debug.Log("Client target : "+ target.gameObject.name + target.position); + if(target.gameObject.name != "Targets") + { + targets_dict.Add(target.position, false); + // Debug.Log("Client target : "+ target.gameObject.name + target.position); + } } } - } - if (targets_dict.Count(); + // Make a copy of the fixedDeltaTime, it defaults to 0.02f, but it can be changed in the editor + this.fixedDeltaTime = Time.fixedDeltaTime; + GameObject timerObj = GameObject.Find("/UI/Canvas/ServiceTimer"); + if(timerObj is null) + { + Debug.LogWarning("No service timer found"); + UIServiceTimer=null; + } + else + { + UIServiceTimer=timerObj.GetComponent(); + } + + if(ClientManager.Instance.ready && StockManager.Instance.ready) + { + ready=true; + Debug.Log("All GameSystems are ready"); + } } } diff --git a/Assets/Scripts/GameSystems/StockManager.cs b/Assets/Scripts/GameSystems/StockManager.cs index 2d99748..70fa751 100644 --- a/Assets/Scripts/GameSystems/StockManager.cs +++ b/Assets/Scripts/GameSystems/StockManager.cs @@ -6,6 +6,9 @@ using UnityEngine; //TODO : Update check stock of registered workshops public class StockManager : MonoBehaviour { + [HideInInspector] + public bool ready = false; //Wether the StockManager is initialized + //Consumable Dictionary consumableSprites = new Dictionary(); //Sprite associated w/ types of consumable HashSet avail_consumable = new HashSet(); //Available consumable @@ -32,7 +35,7 @@ public class StockManager : MonoBehaviour consumableSprites[prod_name]=prod_sprite; updateStock(prod_name, stock); workshop_register.Add(workshop); - Debug.Log(workshop.gameObject.name+" registered by StockManager"); + // Debug.Log(workshop.gameObject.name+" registered by StockManager"); } } @@ -71,11 +74,16 @@ public class StockManager : MonoBehaviour //Awake is called when the script instance is being loaded. void Awake() { - //Initialize global stock - global_stock= new Dictionary(); - foreach(string cons in Consumable.allowed_types) + if(!ready) { - global_stock[cons]=0; + //Initialize global stock + global_stock= new Dictionary(); + foreach(string cons in Consumable.allowed_types) + { + global_stock[cons]=0; + } + + ready = true; } }