Update Event system + Add basic HardObstacle

This commit is contained in:
Antoine H 2021-01-22 17:34:44 +01:00
parent ca8790bb19
commit fd8306645b
39 changed files with 1248 additions and 69 deletions

View file

@ -0,0 +1,82 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Define the behavior of a cleaning workshop
public class Cleaning_workshop : Workshop
{
List<GameObject> stock = new List<GameObject>(); //List of mug in workshop
//Handle objects interactions w/ Workshop
//Return wether the object is taken from tavernkeeper
public override bool use(GameObject userObject)
{
if(userObject != null)
{
//TODO : Gérer grabable autre que Mug
if(userObject.tag=="Grabable")
{
//Stock and empty mug
Mug mug = userObject.GetComponent<Mug>();
if (mug!= null)
{
Debug.Log(userObject.name+ " stocked in "+gameObject.name);
mug.take();
stock.Add(userObject);
return true; //Object taken
}
}
else if(userObject.tag=="Player" && prepTimer<prepTime && currentMug != null) //Prepare currentMug
{
continueUse(userObject);
}
else if(userObject.tag=="Player" && prepTimer>=prepTime) //Give tavernkeeper currentMug if cleaned
{
Tavernkeeper_controller player = userObject.GetComponent<Tavernkeeper_controller>();
Mug mug = currentMug.GetComponent<Mug>();
if(player!=null && mug !=null)
{
Debug.Log(gameObject.name+" give "+currentMug.name+" to "+userObject.name);
//Clean mug
mug.dirty=false;
//Give mug
player.grab(currentMug);
currentMug=null;
UIPrepTimer.gameObject.SetActive(false); //Turn off UI prep timer
}
}
}
return false;
}
// Update is called once per frame
void Update()
{
if(currentMug!=null)
Debug.Log(stock.Count+ " - CurrentMug: "+currentMug.name+" "+currentMug.GetComponent<Mug>().dirty);
//Set current mug if there's stock
if(currentMug is null && stock.Count>0)
{
currentMug=stock[0];
stock.RemoveAt(0);
Mug mug = currentMug.GetComponent<Mug>();
if(mug.content != null)//Empty mug
{
mug.consume();
prepTimer=0.0f;
}
else if(!mug.dirty)//Mug already clean
prepTimer=prepTime;
if(UIPrepTimer != null) //Display UI prep timer
{
UIPrepTimer.SetValue(prepTimer/prepTime);
// UIPrepTimer.DisplayIcon(false);
UIPrepTimer.gameObject.SetActive(true);
}
}
}
}

View file

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

View file

@ -0,0 +1,105 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Define the behavior of a production workshop
public class Production_workshop : Workshop
{
// public float prepTime = 2.0f; //Time for preparation of product
public string product_name;
public int product_value;
public Sprite product_sprite;
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
public override bool use(GameObject userObject)
{
if(userObject != null)
{
// Debug.Log(userObject.tag);
//TODO : Gérer Grabable qui ne sont pas des mugs ?
if(userObject.tag=="Grabable" && currentMug is null) //Try to stock Mug into workshop
{
Mug mug = userObject.GetComponent<Mug>();
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);
mug.take();
currentMug=userObject;
if(UIPrepTimer != null) //Display UI prep timer
{
prepTimer=0.0f;
UIPrepTimer.SetValue(prepTimer/prepTime);
UIPrepTimer.DisplayIcon(product_sprite);
UIPrepTimer.gameObject.SetActive(true);
}
return true; //Object taken
}
else
{
Debug.Log(userObject.name+" cannot be filled with "+product_name+ " -stock:"+Stock);
}
}
else if(userObject.tag=="Player" && prepTimer<prepTime && currentMug != null) //Prepare currentMug
{
continueUse(userObject);
}
else if(userObject.tag=="Player" && prepTimer>=prepTime) //Give tavernkeeper currentMug if finished preparation
{
Tavernkeeper_controller player = userObject.GetComponent<Tavernkeeper_controller>();
Mug mug = currentMug.GetComponent<Mug>();
if(player!=null && mug !=null)
{
Debug.Log(gameObject.name+" give "+currentMug.name+" filled with "+product_name+" to "+userObject.name);
//Fill mug
mug.fill(new Consumable(product_name,product_value,product_sprite));
Stock--;
UIPrepTimer.gameObject.SetActive(false); //Turn off UI prep timer
//Give mug
player.grab(currentMug);
currentMug=null;
}
}
}
else
Debug.LogWarning(gameObject.name+" doesn't handle : "+userObject);
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

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

View file

@ -0,0 +1,72 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Define the behavior of a workshop
//TODO : Only stock Component instead of GameObject in currentMug ?
[RequireComponent(typeof(Collider2D))]
public abstract class Workshop : MonoBehaviour, IUsable
{
protected GameObject currentMug = null; //Mug currently stocked in workshop
public float prepTime = 2.0f; //Time for preparation of product
protected float prepTimer= 0.0f;
public UITimer UIPrepTimer = null; //Script of the UI display
protected bool playerInteracting = false; //Wether the player is interacting w/ the workshop
protected float interactionCd = 0.0f; //Time to consider the interaction stopped
public float interactionSmoothing = 0.0f; //% of action_cd added to the interaction CD for smooth continued interaction
protected float cdTimer = 0.0f;
//Handle objects interactions w/ Workshop
//Return wether the object is taken from tavernkeeper
public abstract bool use(GameObject userObject);
//Handle continuous interaction w/ Workshop
protected void continueUse(GameObject userObject)
{
Debug.Log(gameObject.name+" still used by "+userObject.name);
if(interactionCd<0.01f) //No interaction CD => Try to set it up
{
Tavernkeeper_controller player = userObject.GetComponent<Tavernkeeper_controller>();
if(player != null)
interactionCd=player.action_cd*(1.0f+interactionSmoothing); //=action_cd+ interactionSmoothing(%) action_cd (for smooth continued interaction)
else
Debug.LogWarning(userObject.name+" cannot have a continuous interaction on "+gameObject.name);
}
playerInteracting = true; //Set interaction indicator
cdTimer = interactionCd; //Reset Interaction CD
}
// Start is called before the first frame update
protected virtual void Start()
{
if(gameObject.layer != LayerMask.NameToLayer("Interactions"))
Debug.LogWarning(gameObject.name+" layer should be set to 'Interactions' to work properly");
if(gameObject.tag != "Usable")
Debug.LogWarning(gameObject.name+" tag should be set to 'Usable' to work properly");
if(UIPrepTimer is null)
Debug.LogWarning(gameObject.name+" doesn't have a UIPrepTimer set");
else
UIPrepTimer.gameObject.SetActive(false);
}
//LateUpdate is called after classic Updates
protected virtual void LateUpdate()
{
if(playerInteracting)
{
//Continue Preparation
if(prepTimer<prepTime) //Update UI Prep timer
{
prepTimer+=Time.deltaTime;
if(UIPrepTimer != null)
UIPrepTimer.SetValue(prepTimer/prepTime);
}
//Update interaction CD
cdTimer-=Time.deltaTime;
if (cdTimer<0)
playerInteracting=false; //Reset interaction indicator
}
}
}

View file

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