Add StockManager + Multiple minor Fix

This commit is contained in:
Antoine H 2021-01-12 16:55:43 +01:00
parent 1360a3f978
commit 4f99687ae2
10 changed files with 182 additions and 42 deletions

View file

@ -191,6 +191,11 @@ PrefabInstance:
m_Modification: m_Modification:
m_TransformParent: {fileID: 1714903347} m_TransformParent: {fileID: 1714903347}
m_Modifications: m_Modifications:
- target: {fileID: 741721532275571439, guid: 3baa223794d1dea4c986a5384a835c16,
type: 3}
propertyPath: m_IsActive
value: 1
objectReference: {fileID: 0}
- target: {fileID: 4478636634695997808, guid: 3baa223794d1dea4c986a5384a835c16, - target: {fileID: 4478636634695997808, guid: 3baa223794d1dea4c986a5384a835c16,
type: 3} type: 3}
propertyPath: m_Size.x propertyPath: m_Size.x
@ -274,7 +279,12 @@ PrefabInstance:
- target: {fileID: 4478636634695998094, guid: 3baa223794d1dea4c986a5384a835c16, - target: {fileID: 4478636634695998094, guid: 3baa223794d1dea4c986a5384a835c16,
type: 3} type: 3}
propertyPath: m_Name 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} objectReference: {fileID: 0}
m_RemovedComponents: [] m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 3baa223794d1dea4c986a5384a835c16, type: 3} m_SourcePrefab: {fileID: 100100000, guid: 3baa223794d1dea4c986a5384a835c16, type: 3}
@ -287,7 +297,6 @@ GameObject:
serializedVersion: 6 serializedVersion: 6
m_Component: m_Component:
- component: {fileID: 128722683} - component: {fileID: 128722683}
- component: {fileID: 128722684}
m_Layer: 0 m_Layer: 0
m_Name: ClientManager m_Name: ClientManager
m_TagString: Untagged m_TagString: Untagged
@ -309,18 +318,6 @@ Transform:
m_Father: {fileID: 1160225022} m_Father: {fileID: 1160225022}
m_RootOrder: 1 m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} 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 --- !u!1 &156172339
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View file

@ -139,6 +139,8 @@ public class Client_controller : MonoBehaviour, IUsable
status="waiting"; status="waiting";
waitTimer=waitingTime; waitTimer=waitingTime;
order = ClientManager.Instance.assignOrder(); order = ClientManager.Instance.assignOrder();
if(UIWaitingTimer != null) //Update UI Waiting timer Icon
UIWaitingTimer.DisplayIcon(StockManager.Instance.consumableSprite(order));
} }
if(status=="waiting") if(status=="waiting")

View file

@ -1,12 +1,11 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using System; //Exceptions
//Represents consumable : sprite and informations //Represents consumable : sprite and informations
public class Consumable //: MonoBehaviour public class Consumable //: MonoBehaviour
{ {
private HashSet<string> allowed_types = new HashSet<string>(new [] {"beer", "pression", "vodka"}); static public HashSet<string> allowed_types = new HashSet<string>(new [] {"beer", "vodka"});
private string _type; //Type from allowed_types private string _type; //Type from allowed_types
private int _value; private int _value;
private Sprite _sprite; //Display details private Sprite _sprite; //Display details
@ -35,7 +34,7 @@ public class Consumable //: MonoBehaviour
//Test if type is an allowed type //Test if type is an allowed type
if(!allowed_types.Contains(type)) if(!allowed_types.Contains(type))
{ {
throw new Exception("Invalid consumable type :"+type); Debug.LogError("Invalid consumable type :"+type);
} }
_type=type; _type=type;
_value=value; _value=value;

View file

@ -43,7 +43,7 @@ public sealed class ClientManager : MonoBehaviour
//TODO: Reputation //TODO: Reputation
public void clientReward(int money) public void clientReward(int money)
{ {
GameSystem.Instance.gold+=money; GameSystem.Instance.Gold+=money;
} }
//Destroy a client //Destroy a client
@ -79,19 +79,17 @@ public sealed class ClientManager : MonoBehaviour
} }
//Return a random order from available consummable //Return a random order from available consummable
//TODO : Check stock before assignement //TODO : Check stock before assignement ?
//TODO : Share available types w/ consummable
//TODO : Give sprite
public string assignOrder() public string assignOrder()
{ {
List<string> available_types = new List<string>(new [] {"beer", "vodka"}); List<string> available_types = new List<string>(Consumable.allowed_types);
string order_type = available_types[Random.Range(0, available_types.Count)]; string order_type = available_types[Random.Range(0, available_types.Count)];
return order_type; return order_type;
} }
// Start is called before the first frame update //Awake is called when the script instance is being loaded.
void Start() void Awake()
{ {
ClientContainer = GameObject.Find("/GameSystem/ClientManager"); ClientContainer = GameObject.Find("/GameSystem/ClientManager");
if (ClientContainer is null) if (ClientContainer is null)

View file

@ -1,7 +1,6 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using System; //Exceptions
//Define the global game system of the service. (Singleton) //Define the global game system of the service. (Singleton)
public sealed class GameSystem : MonoBehaviour public sealed class GameSystem : MonoBehaviour
@ -16,15 +15,13 @@ public sealed class GameSystem : MonoBehaviour
//TODO : Effect on gold change //TODO : Effect on gold change
//Money //Money
private int _gold; private int gold;
public int gold public int Gold
{ {
get{return _gold;} get{return gold;}
set{ set{
if(value<0) gold = Mathf.Abs(value);
value=0; Debug.Log("Gold : "+gold);
_gold = value;
Debug.Log("Gold : "+_gold);
} }
} }
@ -48,7 +45,7 @@ public sealed class GameSystem : MonoBehaviour
else //Set to specific scale else //Set to specific scale
{ {
if(newTimeScale<0.0f) 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; Time.timeScale = (float)newTimeScale;
} }

View file

@ -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<string, Sprite> consumableSprites = new Dictionary<string, Sprite>(); //Sprite associated w/ types of consumable
HashSet<string> avail_consumable = new HashSet<string>(); //Available consumable
Dictionary<string, int> global_stock; //Stocks of all the active production workshops
List<Component> workshop_register = new List<Component>();
//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<string, int>();
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<StockManager>();
}
////
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f33c46360b013aa48a333f4be9bb3c09
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -10,7 +10,15 @@ public class Production_workshop : Workshop
public string product_name; public string product_name;
public int product_value; public int product_value;
public Sprite product_sprite; 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 //Handle objects interactions w/ Workshop
//Return wether the object is taken from tavernkeeper //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 if(userObject.tag=="Grabable" && currentMug is null) //Try to stock Mug into workshop
{ {
Mug mug = userObject.GetComponent<Mug>(); Mug mug = userObject.GetComponent<Mug>();
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); Debug.Log(userObject.name+ " stocked in "+gameObject.name);
currentMug=userObject; currentMug=userObject;
@ -40,7 +48,7 @@ public class Production_workshop : Workshop
} }
else 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<prepTime && currentMug != null) //Prepare currentMug else if(userObject.tag=="Player" && prepTimer<prepTime && currentMug != null) //Prepare currentMug
@ -56,7 +64,7 @@ public class Production_workshop : Workshop
Debug.Log(gameObject.name+" give "+currentMug.name+" filled with "+product_name+" to "+userObject.name); Debug.Log(gameObject.name+" give "+currentMug.name+" filled with "+product_name+" to "+userObject.name);
//Fill mug //Fill mug
mug.fill(new Consumable(product_name,product_value,product_sprite)); mug.fill(new Consumable(product_name,product_value,product_sprite));
stock--; Stock--;
UIPrepTimer.gameObject.SetActive(false); //Turn off UI prep timer UIPrepTimer.gameObject.SetActive(false); //Turn off UI prep timer
//Give mug //Give mug
@ -70,4 +78,27 @@ public class Production_workshop : Workshop
return false; //Object not taken return false; //Object not taken
} }
// Start is called before the first frame update
protected override void Start()
{
base.Start(); //Call workshop Start
stock = initial_stock; //Using stock instead of Stock to skip set property
StockManager.Instance.registerWorkshop(this);
}
void OnEnable()
{
StockManager.Instance.registerWorkshop(this);
}
void OnDisable()
{
StockManager.Instance.removeWorkshop(this);
}
void OnDestroy()
{
StockManager.Instance.removeWorkshop(this);
}
} }

View file

@ -1,7 +1,6 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using System; //Exceptions
[RequireComponent(typeof(Rigidbody))] [RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(Animator))] [RequireComponent(typeof(Animator))]
@ -35,7 +34,7 @@ public class Tavernkeeper_controller : MonoBehaviour
//Test //Test
if(!hand_container.ContainsKey(hand)) if(!hand_container.ContainsKey(hand))
{ {
throw new Exception("Invalid key for hands :"+hand); Debug.LogError("Invalid key for hands :"+hand);
} }
IGrabable grabable_obj = obj.GetComponent<IGrabable>(); IGrabable grabable_obj = obj.GetComponent<IGrabable>();
@ -132,7 +131,7 @@ public class Tavernkeeper_controller : MonoBehaviour
//Test //Test
if(!hand_container.ContainsKey(hand)) 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 // Test collision of ray from tavernkeeper center (A verifier) at action_dist unit distance on Interactions layer

View file

@ -37,7 +37,7 @@ public abstract class Workshop : MonoBehaviour, IUsable
} }
// Start is called before the first frame update // Start is called before the first frame update
void Start() protected virtual void Start()
{ {
if(gameObject.layer != LayerMask.NameToLayer("Interactions")) if(gameObject.layer != LayerMask.NameToLayer("Interactions"))
Debug.LogWarning(gameObject.name+" layer should be set to 'Interactions' to work properly"); 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 //LateUpdate is called after classic Updates
void LateUpdate() protected virtual void LateUpdate()
{ {
if(playerInteracting) if(playerInteracting)
{ {