From 4f99687ae208f44019a44925c4fd42c52a753d9b Mon Sep 17 00:00:00 2001 From: Antoine H Date: Tue, 12 Jan 2021 16:55:43 +0100 Subject: [PATCH] Add StockManager + Multiple minor Fix --- Assets/Scenes/Tests/SampleScene.unity | 25 ++--- Assets/Scripts/Client_controller.cs | 2 + Assets/Scripts/Consumable.cs | 5 +- Assets/Scripts/GameSystems/ClientManager.cs | 12 +- Assets/Scripts/GameSystems/GameSystem.cs | 15 +-- Assets/Scripts/GameSystems/StockManager.cs | 106 ++++++++++++++++++ .../Scripts/GameSystems/StockManager.cs.meta | 11 ++ Assets/Scripts/Production_workshop.cs | 39 ++++++- Assets/Scripts/Tavernkeeper_controller.cs | 5 +- Assets/Scripts/Workshop.cs | 4 +- 10 files changed, 182 insertions(+), 42 deletions(-) create mode 100644 Assets/Scripts/GameSystems/StockManager.cs create mode 100644 Assets/Scripts/GameSystems/StockManager.cs.meta diff --git a/Assets/Scenes/Tests/SampleScene.unity b/Assets/Scenes/Tests/SampleScene.unity index 1e743bf..5ac544d 100644 --- a/Assets/Scenes/Tests/SampleScene.unity +++ b/Assets/Scenes/Tests/SampleScene.unity @@ -191,6 +191,11 @@ PrefabInstance: m_Modification: m_TransformParent: {fileID: 1714903347} m_Modifications: + - target: {fileID: 741721532275571439, guid: 3baa223794d1dea4c986a5384a835c16, + type: 3} + propertyPath: m_IsActive + value: 1 + objectReference: {fileID: 0} - target: {fileID: 4478636634695997808, guid: 3baa223794d1dea4c986a5384a835c16, type: 3} propertyPath: m_Size.x @@ -274,7 +279,12 @@ PrefabInstance: - target: {fileID: 4478636634695998094, guid: 3baa223794d1dea4c986a5384a835c16, type: 3} propertyPath: m_Name - value: Production_workshop + value: Beer barrel + objectReference: {fileID: 0} + - target: {fileID: 8573757128841810314, guid: 3baa223794d1dea4c986a5384a835c16, + type: 3} + propertyPath: m_IsActive + value: 1 objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 3baa223794d1dea4c986a5384a835c16, type: 3} @@ -287,7 +297,6 @@ GameObject: serializedVersion: 6 m_Component: - component: {fileID: 128722683} - - component: {fileID: 128722684} m_Layer: 0 m_Name: ClientManager m_TagString: Untagged @@ -309,18 +318,6 @@ 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: --- !u!1 &156172339 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Client_controller.cs b/Assets/Scripts/Client_controller.cs index 022e7ae..67e0af1 100644 --- a/Assets/Scripts/Client_controller.cs +++ b/Assets/Scripts/Client_controller.cs @@ -139,6 +139,8 @@ public class Client_controller : MonoBehaviour, IUsable status="waiting"; waitTimer=waitingTime; order = ClientManager.Instance.assignOrder(); + if(UIWaitingTimer != null) //Update UI Waiting timer Icon + UIWaitingTimer.DisplayIcon(StockManager.Instance.consumableSprite(order)); } if(status=="waiting") diff --git a/Assets/Scripts/Consumable.cs b/Assets/Scripts/Consumable.cs index ef97614..a7cd2bf 100644 --- a/Assets/Scripts/Consumable.cs +++ b/Assets/Scripts/Consumable.cs @@ -1,12 +1,11 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; -using System; //Exceptions //Represents consumable : sprite and informations public class Consumable //: MonoBehaviour { - private HashSet allowed_types = new HashSet(new [] {"beer", "pression", "vodka"}); + static public HashSet allowed_types = new HashSet(new [] {"beer", "vodka"}); private string _type; //Type from allowed_types private int _value; private Sprite _sprite; //Display details @@ -35,7 +34,7 @@ public class Consumable //: MonoBehaviour //Test if type is an allowed type if(!allowed_types.Contains(type)) { - throw new Exception("Invalid consumable type :"+type); + Debug.LogError("Invalid consumable type :"+type); } _type=type; _value=value; diff --git a/Assets/Scripts/GameSystems/ClientManager.cs b/Assets/Scripts/GameSystems/ClientManager.cs index 784b832..f367c53 100644 --- a/Assets/Scripts/GameSystems/ClientManager.cs +++ b/Assets/Scripts/GameSystems/ClientManager.cs @@ -43,7 +43,7 @@ public sealed class ClientManager : MonoBehaviour //TODO: Reputation public void clientReward(int money) { - GameSystem.Instance.gold+=money; + GameSystem.Instance.Gold+=money; } //Destroy a client @@ -79,19 +79,17 @@ public sealed class ClientManager : MonoBehaviour } //Return a random order from available consummable - //TODO : Check stock before assignement - //TODO : Share available types w/ consummable - //TODO : Give sprite + //TODO : Check stock before assignement ? public string assignOrder() { - List available_types = new List(new [] {"beer", "vodka"}); + List available_types = new List(Consumable.allowed_types); string order_type = available_types[Random.Range(0, available_types.Count)]; return order_type; } - // Start is called before the first frame update - void Start() + //Awake is called when the script instance is being loaded. + void Awake() { ClientContainer = GameObject.Find("/GameSystem/ClientManager"); if (ClientContainer is null) diff --git a/Assets/Scripts/GameSystems/GameSystem.cs b/Assets/Scripts/GameSystems/GameSystem.cs index a2cf4c3..b51453b 100644 --- a/Assets/Scripts/GameSystems/GameSystem.cs +++ b/Assets/Scripts/GameSystems/GameSystem.cs @@ -1,7 +1,6 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; -using System; //Exceptions //Define the global game system of the service. (Singleton) public sealed class GameSystem : MonoBehaviour @@ -16,15 +15,13 @@ public sealed class GameSystem : MonoBehaviour //TODO : Effect on gold change //Money - private int _gold; - public int gold + private int gold; + public int Gold { - get{return _gold;} + get{return gold;} set{ - if(value<0) - value=0; - _gold = value; - Debug.Log("Gold : "+_gold); + gold = Mathf.Abs(value); + Debug.Log("Gold : "+gold); } } @@ -48,7 +45,7 @@ public sealed class GameSystem : MonoBehaviour else //Set to specific scale { if(newTimeScale<0.0f) - throw new Exception("Trying to set time scale to negative value (rewinding time...) :"+newTimeScale); + Debug.LogError("Trying to set time scale to negative value (rewinding time...) :"+newTimeScale); Time.timeScale = (float)newTimeScale; } diff --git a/Assets/Scripts/GameSystems/StockManager.cs b/Assets/Scripts/GameSystems/StockManager.cs new file mode 100644 index 0000000..2d99748 --- /dev/null +++ b/Assets/Scripts/GameSystems/StockManager.cs @@ -0,0 +1,106 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +//Define the system managing the stock of goods. (Singleton) +//TODO : Update check stock of registered workshops +public class StockManager : MonoBehaviour +{ + //Consumable + Dictionary consumableSprites = new Dictionary(); //Sprite associated w/ types of consumable + HashSet avail_consumable = new HashSet(); //Available consumable + Dictionary global_stock; //Stocks of all the active production workshops + + List workshop_register = new List(); + + //Register a production workshop + public void registerWorkshop(Component workshop) + { + if(workshop!=null && !workshop_register.Contains(workshop)) + { + string prod_name = ((Production_workshop)workshop).product_name; + Sprite prod_sprite = ((Production_workshop)workshop).product_sprite; + int stock = ((Production_workshop)workshop).Stock; + + //Check if type is allowed for Consumables + if(!Consumable.allowed_types.Contains(prod_name)) + Debug.LogError(workshop.gameObject.name+" : "+prod_name+" isn't an allowed type of consumable."); + //Check if there's a different sprite registered for the same product + if(consumableSprites.ContainsKey(prod_name) && consumableSprites[prod_name]!=prod_sprite) + Debug.LogWarning("StockManager: Different sprites registered for "+prod_name+". Only one will be kept."); + + consumableSprites[prod_name]=prod_sprite; + updateStock(prod_name, stock); + workshop_register.Add(workshop); + Debug.Log(workshop.gameObject.name+" registered by StockManager"); + } + } + + //Remove a workshop from the register + public void removeWorkshop(Component workshop) + { + if(workshop!=null && workshop_register.Contains(workshop)) + { + //Update global stock + string prod_name = ((Production_workshop)workshop).product_name; + int stock = ((Production_workshop)workshop).Stock; + updateStock(prod_name, -stock); + + //Remove workshop + workshop_register.Remove(workshop); + Debug.Log(workshop.gameObject.name+" unregistered by StockManager"); + } + } + + public Sprite consumableSprite(string consumable) + { + if(!consumableSprites.ContainsKey(consumable)) + Debug.LogError("Stock Manager : no sprite registered for : "+consumable); + return consumableSprites[consumable]; + } + + public void updateStock(string consumable, int valueModif) + { + global_stock[consumable]= Mathf.Abs(global_stock[consumable]+valueModif); + if(global_stock[consumable]==0) + avail_consumable.Remove(consumable); + else + avail_consumable.Add(consumable); + } + + //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) + { + global_stock[cons]=0; + } + } + + // Update is called once per frame + void Update() + { + + } + + //// Singleton Implementation (https://jlambert.developpez.com/tutoriels/dotnet/implementation-pattern-singleton-csharp/#LIII) //// + private StockManager() + { + } + + 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() + { + } + + internal static readonly StockManager instance = new GameObject("StockManager").AddComponent(); + } + //// +} diff --git a/Assets/Scripts/GameSystems/StockManager.cs.meta b/Assets/Scripts/GameSystems/StockManager.cs.meta new file mode 100644 index 0000000..95b7aa6 --- /dev/null +++ b/Assets/Scripts/GameSystems/StockManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f33c46360b013aa48a333f4be9bb3c09 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Production_workshop.cs b/Assets/Scripts/Production_workshop.cs index a974414..b9e968d 100644 --- a/Assets/Scripts/Production_workshop.cs +++ b/Assets/Scripts/Production_workshop.cs @@ -10,7 +10,15 @@ public class Production_workshop : Workshop public string product_name; public int product_value; public Sprite product_sprite; - public int stock = 5; //Stock of product + public int initial_stock = 5; + private int stock; //Current stock + public int Stock{ //stock property + get{return stock;} + set{ + StockManager.Instance.updateStock(product_name, value-stock); + stock = Mathf.Abs(value); + } + } //Stock of product //Handle objects interactions w/ Workshop //Return wether the object is taken from tavernkeeper @@ -23,7 +31,7 @@ public class Production_workshop : Workshop if(userObject.tag=="Grabable" && currentMug is null) //Try to stock Mug into workshop { Mug mug = userObject.GetComponent(); - if (mug!= null && mug.content is null && !mug.dirty && stock>0) //Mug clean & empty + remaining stock in workshop + if (mug!= null && mug.content is null && !mug.dirty && Stock>0) //Mug clean & empty + remaining stock in workshop { Debug.Log(userObject.name+ " stocked in "+gameObject.name); currentMug=userObject; @@ -40,7 +48,7 @@ public class Production_workshop : Workshop } else { - Debug.Log(userObject.name+" cannot be filled with "+product_name+ " -stock:"+stock); + Debug.Log(userObject.name+" cannot be filled with "+product_name+ " -stock:"+Stock); } } else if(userObject.tag=="Player" && prepTimer(); @@ -132,7 +131,7 @@ public class Tavernkeeper_controller : MonoBehaviour //Test if(!hand_container.ContainsKey(hand)) { - throw new Exception("Invalid key for hands :"+hand); + Debug.LogError("Invalid key for hands :"+hand); } // Test collision of ray from tavernkeeper center (A verifier) at action_dist unit distance on Interactions layer diff --git a/Assets/Scripts/Workshop.cs b/Assets/Scripts/Workshop.cs index 8070b6f..e6a3419 100644 --- a/Assets/Scripts/Workshop.cs +++ b/Assets/Scripts/Workshop.cs @@ -37,7 +37,7 @@ public abstract class Workshop : MonoBehaviour, IUsable } // Start is called before the first frame update - void Start() + protected virtual void Start() { if(gameObject.layer != LayerMask.NameToLayer("Interactions")) Debug.LogWarning(gameObject.name+" layer should be set to 'Interactions' to work properly"); @@ -50,7 +50,7 @@ public abstract class Workshop : MonoBehaviour, IUsable } //LateUpdate is called after classic Updates - void LateUpdate() + protected virtual void LateUpdate() { if(playerInteracting) {