Refactoring of status system to prevent bug w/ events
This commit is contained in:
parent
fd8306645b
commit
e89483d612
13 changed files with 487 additions and 113 deletions
278
Assets/Scripts/Characters/Client_controller.cs
Normal file
278
Assets/Scripts/Characters/Client_controller.cs
Normal file
|
@ -0,0 +1,278 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
|
||||
//Define the behavior of a client
|
||||
[RequireComponent(typeof(Animator))]
|
||||
[RequireComponent(typeof(Rigidbody2D))]
|
||||
[RequireComponent(typeof(Collider2D))]
|
||||
[RequireComponent(typeof(NavMeshAgent))]
|
||||
[RequireComponent(typeof(NavMeshObstacle))]
|
||||
public class Client_controller : MonoBehaviour, IUsable
|
||||
{
|
||||
public float consumeTime = 3.0f; //Time to consume currentMug
|
||||
public float waitingTime = 10.0f; //Patience after reaching seat
|
||||
public UITimer UIWaitingTimer = null;
|
||||
|
||||
Animator animator;
|
||||
string _status;
|
||||
string _prevStatus;
|
||||
string _lastStatusRequest=null;
|
||||
// private readonly object balanceLock = new object();
|
||||
// bool updatingStatus=false;
|
||||
HashSet<string> _availStatus = new HashSet<string>(){"entering", "waiting", "consuming", "leaving", "event"};
|
||||
public string status
|
||||
{
|
||||
get{ return _status;}
|
||||
//BEWARE : Set is only a request. The status is only really set in update.
|
||||
set{
|
||||
if (_availStatus.Contains(value))
|
||||
{
|
||||
if(value==_status)
|
||||
Debug.LogWarning(gameObject.name+" status is set twice to:"+value);
|
||||
else //Request change of status
|
||||
{
|
||||
// if(_lastStatusRequest!=null)
|
||||
// Debug.LogWarning(gameObject.name+" status request("+_lastStatusRequest+") is overriden by : "+value);
|
||||
_lastStatusRequest = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
string order = "none"; //Order requested by the client
|
||||
float consumeTimer;
|
||||
float waitTimer;
|
||||
GameObject currentMug = null; //Mug currently held by the client
|
||||
|
||||
//Navigation
|
||||
Vector2 assigedPos; //Chair to sit or destination to stay (leave)
|
||||
Vector2 currentObjective; //Current destination to reach
|
||||
NavMeshAgent agent;
|
||||
NavMeshObstacle navObstacle; //Obstacle for other agents
|
||||
|
||||
//Handle objects interactions w/ Workshop
|
||||
//Return wether the object is taken from tavernkeeper
|
||||
public bool use(GameObject object_used)
|
||||
{
|
||||
if(status == "waiting" && currentMug is null) //No mug in hand
|
||||
{
|
||||
//TODO : Gérer Grabale qui ne sont pas des Mugs ?
|
||||
if(object_used != null && object_used.tag=="Grabable")
|
||||
{
|
||||
Mug mug = object_used.GetComponent<Mug>();
|
||||
if (mug!= null && mug.content != null && mug.content.Type==order)
|
||||
{
|
||||
status = "consuming";
|
||||
Debug.Log(gameObject.name+" "+status+" "+object_used.name+ " of "+mug.content.Type);
|
||||
currentMug = object_used;
|
||||
consumeTimer=consumeTime;
|
||||
mug.take();
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log(gameObject.name+" doesn't want that "+object_used.name+" - Request : "+order);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log(gameObject.name+" doesn't want that "+object_used.name+" - Request : "+order);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log(gameObject.name+" already consumming "+currentMug.name);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//Assign client to an event/destination. Restore its previous status if no event/destination is given.
|
||||
public void assignToEvent(Vector2? destination=null)
|
||||
{
|
||||
if(destination is null)
|
||||
{
|
||||
status=_prevStatus;
|
||||
currentObjective=assigedPos;
|
||||
}
|
||||
else
|
||||
{
|
||||
status="event";
|
||||
currentObjective=(Vector2)destination;
|
||||
}
|
||||
}
|
||||
|
||||
//Update client attributes in fonction of the newStatus. Should only be called once by Update.
|
||||
protected void updateStatus(string newStatus)
|
||||
{
|
||||
switch (newStatus)
|
||||
{
|
||||
case "entering":
|
||||
navObstacle.enabled = false;
|
||||
agent.enabled = true;
|
||||
if(UIWaitingTimer != null)
|
||||
UIWaitingTimer.gameObject.SetActive(false);
|
||||
break;
|
||||
case "waiting":
|
||||
EventManager.Instance.startCoroutine(gameObject);
|
||||
//Switch Agent to obstacle if waiting
|
||||
// agent.Warp(assigedPos); //Make sure agent become static at right position
|
||||
agent.enabled = false;
|
||||
navObstacle.enabled = true;
|
||||
|
||||
if(UIWaitingTimer != null)
|
||||
{
|
||||
UIWaitingTimer.DisplayIcon(true);
|
||||
UIWaitingTimer.gameObject.SetActive(true);
|
||||
}
|
||||
break;
|
||||
case "consuming":
|
||||
EventManager.Instance.startCoroutine(gameObject);
|
||||
if(UIWaitingTimer != null)
|
||||
UIWaitingTimer.gameObject.SetActive(false);
|
||||
break;
|
||||
case "event":
|
||||
case "leaving":
|
||||
EventManager.Instance.stopCoroutine(gameObject);
|
||||
navObstacle.enabled = false;
|
||||
agent.enabled = true;
|
||||
if(UIWaitingTimer != null)
|
||||
UIWaitingTimer.gameObject.SetActive(false);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
//Navigation
|
||||
if(agent.enabled) //Assign destination
|
||||
agent.SetDestination(currentObjective);
|
||||
else //Warp to destination
|
||||
gameObject.transform.position=currentObjective;
|
||||
|
||||
if(status=="event"&&!agent.enabled)
|
||||
Debug.LogWarning("Wrong status update : "+ gameObject.name + _prevStatus + status +" "+ _lastStatusRequest);
|
||||
}
|
||||
|
||||
// Start is called before the first frame update
|
||||
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(UIWaitingTimer is null)
|
||||
Debug.LogWarning(gameObject.name+" doesn't have a UIWaitingTimer set");
|
||||
else
|
||||
UIWaitingTimer.gameObject.SetActive(false);
|
||||
|
||||
animator = GetComponent<Animator>();
|
||||
|
||||
// Navigation //
|
||||
agent = GetComponent<NavMeshAgent>();
|
||||
navObstacle = GetComponent<NavMeshObstacle>();
|
||||
|
||||
//Prevent rotation of the ground at movement
|
||||
agent.updateRotation = false;
|
||||
agent.updateUpAxis = false;
|
||||
//Get target
|
||||
currentObjective = assigedPos = ClientManager.Instance.assignTarget(); //Chair to go
|
||||
// agent.SetDestination(assigedPos);
|
||||
//Assign Random priority to prevent two agent blocking each other
|
||||
agent.avoidancePriority=Random.Range(0, 99);
|
||||
|
||||
status = "entering";
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
//Update status if it was requested
|
||||
if(_lastStatusRequest !=null)
|
||||
{
|
||||
_prevStatus=_status;
|
||||
_status = _lastStatusRequest;
|
||||
|
||||
animator.SetTrigger(_status); //Update status in animator
|
||||
updateStatus(_status);
|
||||
|
||||
_lastStatusRequest = null;
|
||||
|
||||
// Debug.Log(gameObject.name+" "+_status);
|
||||
}
|
||||
|
||||
//Navigation
|
||||
// Debug.Log(gameObject.name + " navigation : "+ agent.isStopped + " " + agent.remainingDistance);
|
||||
Debug.DrawLine(gameObject.transform.position, agent.destination, Color.blue, 0.0f);
|
||||
|
||||
|
||||
if(status=="entering" && !agent.pathPending && agent.remainingDistance==0) //Reached seat ?
|
||||
{
|
||||
status="waiting";
|
||||
waitTimer=waitingTime;
|
||||
order = ClientManager.Instance.assignOrder();
|
||||
if(UIWaitingTimer != null) //Update UI Waiting timer Icon
|
||||
UIWaitingTimer.DisplayIcon(StockManager.Instance.consumableSprite(order));
|
||||
}
|
||||
|
||||
else if(status=="waiting")
|
||||
{
|
||||
waitTimer -= Time.deltaTime;
|
||||
if (waitTimer < 0) //Waited too long
|
||||
{
|
||||
//Leave tavern
|
||||
status = "leaving";
|
||||
currentObjective = assigedPos = ClientManager.Instance.assignTarget(assigedPos, true); //Request leaving target
|
||||
// agent.SetDestination(assigedPos);
|
||||
}
|
||||
else if(UIWaitingTimer != null) //Update UI Waiting timer
|
||||
UIWaitingTimer.SetValue(waitTimer/waitingTime);
|
||||
}
|
||||
|
||||
//Consume Timer
|
||||
else if(status=="consuming") //Consuming mug if there's one and reached destination
|
||||
{
|
||||
consumeTimer -= Time.deltaTime;
|
||||
if(consumeTimer < 0) //Finished consuming mug ?
|
||||
{
|
||||
Mug obj = currentMug.GetComponent<Mug>();
|
||||
if(obj !=null)
|
||||
{
|
||||
//Reward
|
||||
Consumable content = obj.consume();
|
||||
int money = (int)(content.Value*(1.0f+waitTimer/waitingTime)); //Reward = value order + Tips (value * waitTime)
|
||||
ClientManager.Instance.clientReward(money);
|
||||
|
||||
//Drop mug
|
||||
Transform dropPos = gameObject.transform;
|
||||
dropPos.position += (Vector3)Vector2.down * 0.2f;
|
||||
obj.drop(dropPos);
|
||||
currentMug=null;
|
||||
}
|
||||
|
||||
//Leave tavern
|
||||
status = "leaving";
|
||||
assigedPos = ClientManager.Instance.assignTarget(assigedPos, true); //Request leaving target
|
||||
agent.SetDestination(assigedPos);
|
||||
}
|
||||
}
|
||||
|
||||
else if(status=="leaving" && !agent.pathPending && agent.remainingDistance<0.5) //Reached exit ?
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
|
||||
else if(status=="event" && !agent.pathPending && agent.remainingDistance==0) //Reached event ?
|
||||
{
|
||||
assignToEvent(); //In case events already finished, come back to normal
|
||||
}
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
ClientManager.Instance.clientLeave(gameObject);
|
||||
}
|
||||
}
|
11
Assets/Scripts/Characters/Client_controller.cs.meta
Normal file
11
Assets/Scripts/Characters/Client_controller.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2dfbc8fb0a162ab4da4ff5cb76650e03
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
218
Assets/Scripts/Characters/Tavernkeeper_controller.cs
Normal file
218
Assets/Scripts/Characters/Tavernkeeper_controller.cs
Normal file
|
@ -0,0 +1,218 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(Rigidbody2D))]
|
||||
[RequireComponent(typeof(Animator))]
|
||||
public class Tavernkeeper_controller : MonoBehaviour
|
||||
{
|
||||
public float mvt_speed = 5.0f; //Movement speed
|
||||
public float action_dist = 1.5f; //Action distance
|
||||
public float action_cd = 0.5f; //Action cooldown
|
||||
|
||||
Dictionary<string, GameObject> hand_container; //Objects (IGrabable) in hand
|
||||
|
||||
float actionTimer;
|
||||
bool isInteracting;
|
||||
|
||||
// Last user inputs
|
||||
float horizontal;
|
||||
float vertical;
|
||||
float hands;
|
||||
string currentHand;
|
||||
|
||||
Vector2 lookDirection = new Vector2(1,0);
|
||||
Rigidbody2D rigidbody2d;
|
||||
Animator animator;
|
||||
|
||||
//Grab an Object (IGrabable) w/ a specific or w/ last hand used (hand=null).
|
||||
public void grab(GameObject obj, string hand=null)
|
||||
{
|
||||
//Default hand
|
||||
if(hand is null)
|
||||
hand = currentHand;
|
||||
|
||||
//Test
|
||||
if(!hand_container.ContainsKey(hand))
|
||||
{
|
||||
Debug.LogError("Invalid key for hands :"+hand);
|
||||
}
|
||||
|
||||
IGrabable grabable_obj = obj.GetComponent<IGrabable>();
|
||||
if(grabable_obj!=null && hand_container[hand] is null && grabable_obj.size==1) //Empty hand
|
||||
{
|
||||
// hit_object.transform.SetParent(transform);
|
||||
// hit_object.transform.localPosition = new Vector2(-0.2f,0.2f);
|
||||
|
||||
Transform mugSlot = gameObject.transform.Find(hand+"MugSlot");
|
||||
grabable_obj.take(mugSlot);
|
||||
hand_container[hand]=obj;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log(gameObject.name+" cannot grab (hand full): " + obj);
|
||||
}
|
||||
}
|
||||
|
||||
//Drop every objects in hands at tavernkeeper position.
|
||||
public void emptyHands()
|
||||
{
|
||||
|
||||
foreach(string hand in hand_container.Keys)
|
||||
{
|
||||
if(hand_container[hand]!=null)
|
||||
{
|
||||
IGrabable grabable_obj = hand_container[hand].GetComponent<IGrabable>();
|
||||
grabable_obj.drop(transform);
|
||||
}
|
||||
}
|
||||
hand_container["left"]=null;
|
||||
hand_container["right"]=null;
|
||||
}
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
if(gameObject.tag != "Player")
|
||||
Debug.LogWarning(gameObject.name+" tag should be set to 'Player' to work properly");
|
||||
|
||||
rigidbody2d = GetComponent<Rigidbody2D>();
|
||||
animator = GetComponent<Animator>();
|
||||
|
||||
hand_container = new Dictionary<string, GameObject>(){
|
||||
{"left", null},
|
||||
{"right", null}
|
||||
};
|
||||
|
||||
// GameSystem.Instance.startService();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
//Read inputs
|
||||
horizontal = Input.GetAxis("Horizontal"); //See Edit/Project setting / Input Manager
|
||||
vertical = Input.GetAxis("Vertical");
|
||||
hands = Input.GetAxis("Hand");
|
||||
|
||||
//Movement action
|
||||
movement(new Vector2(horizontal, vertical));
|
||||
|
||||
//Actions delay
|
||||
if(isInteracting)
|
||||
{
|
||||
actionTimer -= Time.deltaTime;
|
||||
if (actionTimer < 0)
|
||||
isInteracting = false;
|
||||
}
|
||||
//Hands actions
|
||||
if(!isInteracting && !Mathf.Approximately(hands, 0.0f))
|
||||
{
|
||||
actionTimer= action_cd;
|
||||
isInteracting=true;
|
||||
if(hands>0)
|
||||
currentHand="left";
|
||||
else
|
||||
currentHand="right";
|
||||
handAction(currentHand);
|
||||
}
|
||||
}
|
||||
|
||||
// Update used by the Physics engine
|
||||
void FixedUpdate()
|
||||
{
|
||||
//Movement of a physic object
|
||||
Vector2 position = rigidbody2d.position;
|
||||
position.x = position.x + mvt_speed * horizontal * Time.deltaTime;
|
||||
position.y = position.y + mvt_speed * vertical * Time.deltaTime;
|
||||
|
||||
rigidbody2d.MovePosition(position); //Movement processed by the phyisc engine for Collision, etc.
|
||||
}
|
||||
|
||||
//Movement
|
||||
void movement(Vector2 move)
|
||||
{
|
||||
//Update animation direction
|
||||
if(move != Vector2.zero)//Movement requested ? //!Mathf.Approximately(move.x, 0.0f) || !Mathf.Approximately(move.y, 0.0f)
|
||||
{
|
||||
lookDirection.Set(move.x, move.y); //== lookDirection=move
|
||||
lookDirection.Normalize();
|
||||
}
|
||||
animator.SetFloat("Look X", lookDirection.x);
|
||||
animator.SetFloat("Look Y", lookDirection.y);
|
||||
animator.SetFloat("Speed", move.magnitude);
|
||||
}
|
||||
|
||||
//Handle action with hands ("left" or "right")
|
||||
void handAction(string hand)
|
||||
{
|
||||
//Test
|
||||
if(!hand_container.ContainsKey(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
|
||||
RaycastHit2D hit = Physics2D.Raycast(rigidbody2d.position + Vector2.up * 0.2f, lookDirection, action_dist, LayerMask.GetMask("Interactions"));
|
||||
if (hit.collider != null)
|
||||
{
|
||||
GameObject hit_object = hit.collider.gameObject;
|
||||
// Debug.Log("Raycast has hit the object " + hit_object.name+ hit_object.tag);
|
||||
if (hit_object != null)
|
||||
{
|
||||
//Handle objects interactions by tags
|
||||
//TODO : Factoriser actions des Clients/Workshop
|
||||
//TODO : Gérer cas Grabable & Workshop
|
||||
if(hit_object.tag == "Grabable")
|
||||
{
|
||||
grab(hit_object, hand);
|
||||
}
|
||||
else if(hit_object.tag == "Usable")
|
||||
{
|
||||
IUsable usable = hit_object.GetComponent<IUsable>();
|
||||
if(usable!=null)
|
||||
{
|
||||
if(hand_container[hand] is null) //No object in hands
|
||||
{
|
||||
// Debug.Log(gameObject.name+" use "+hit_object.name);
|
||||
usable.use(gameObject); //Tavernkeeper interacting directly w/ usable
|
||||
}
|
||||
else if(usable.use(hand_container[hand])) //Interactions w/ object in hands
|
||||
{
|
||||
//Object taken by usable
|
||||
// Debug.Log("Give "+ hand_container[hand].name+" to "+hit_object.name);
|
||||
hand_container[hand]=null;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log(hit_object.tag+" tag not handled by "+gameObject.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (hand_container[hand] != null) //Hand full and no hits : use object
|
||||
{
|
||||
IGrabable obj = hand_container[hand].GetComponent<IGrabable>();
|
||||
if(obj !=null)
|
||||
{
|
||||
if(obj.use(gameObject)) //Use object in hand
|
||||
hand_container[hand]=null; //True = object dropped
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Returns set of free hands (keys)
|
||||
// ISet<string> freeHands()
|
||||
// {
|
||||
// HashSet<string> res = new HashSet<string>();
|
||||
// foreach ( KeyValuePair<string, GameObject> kvp in hand_container)
|
||||
// {
|
||||
// if(kvp.Value is null)
|
||||
// {
|
||||
// res.Add(kvp.Key);
|
||||
// }
|
||||
// }
|
||||
// return res;
|
||||
// }
|
||||
}
|
11
Assets/Scripts/Characters/Tavernkeeper_controller.cs.meta
Normal file
11
Assets/Scripts/Characters/Tavernkeeper_controller.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 5222b6d867fe67f4a973336825e0ee8c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Add table
Add a link
Reference in a new issue