Client_controller status + client navigation (leaving)

This commit is contained in:
Antoine H 2020-12-31 12:53:02 +01:00
parent 2db3e27a47
commit 7a7fcb6ac1
3 changed files with 63 additions and 24 deletions

View file

@ -7310,7 +7310,7 @@ Transform:
m_GameObject: {fileID: 1486551543} m_GameObject: {fileID: 1486551543}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 4.963482, y: -3.7325091, z: -3.6281848} m_LocalPosition: {x: 4.963482, y: -3.7325091, z: -3.6281848}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 0.1, y: 0.1, z: 0.1}
m_Children: [] m_Children: []
m_Father: {fileID: 1160225022} m_Father: {fileID: 1160225022}
m_RootOrder: 1 m_RootOrder: 1
@ -32161,8 +32161,8 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1678986451} m_GameObject: {fileID: 1678986451}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: -6.6, y: -2.112, z: 2.5482712} m_LocalPosition: {x: -0.349, y: -1.345, z: 2.5482712}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 0.1, y: 0.1, z: 0.1}
m_Children: [] m_Children: []
m_Father: {fileID: 1863361785} m_Father: {fileID: 1863361785}
m_RootOrder: 0 m_RootOrder: 0

View file

@ -10,11 +10,24 @@ public class Client_controller : MonoBehaviour, IUsable
{ {
public float consumeTime = 3.0f; //Time to consume currentMug public float consumeTime = 3.0f; //Time to consume currentMug
public float waitingTime = 10.0f; //Patience after ordering public float waitingTime = 10.0f; //Patience after ordering
string _status;
HashSet<string> _availStatus = new HashSet<string>(){"entering", "waiting", "consuming", "leaving"};
public string status
{
get{ return _status;}
set{
if (_availStatus.Contains(value))
_status = value;
Debug.Log(gameObject.name+" "+_status);
}
}
float consumeTimer; float consumeTimer;
float waitTimer;
GameObject currentMug = null; //Mug currently held by the client GameObject currentMug = null; //Mug currently held by the client
Transform target; //Navigation
Vector2 destination; Vector2 destination;
NavMeshAgent agent; NavMeshAgent agent;
@ -30,7 +43,8 @@ public class Client_controller : MonoBehaviour, IUsable
Mug mug = object_used.GetComponent<Mug>(); Mug mug = object_used.GetComponent<Mug>();
if (mug!= null && mug.content != null) if (mug!= null && mug.content != null)
{ {
Debug.Log(gameObject.name+" take "+object_used.name+ " of "+mug.content.Type); status = "consuming";
Debug.Log(gameObject.name+" "+status+" "+object_used.name+ " of "+mug.content.Type);
currentMug = object_used; currentMug = object_used;
consumeTimer=consumeTime; consumeTimer=consumeTime;
return true; return true;
@ -62,27 +76,43 @@ public class Client_controller : MonoBehaviour, IUsable
if(gameObject.tag != "Usable") if(gameObject.tag != "Usable")
Debug.LogWarning(gameObject.name+" tag should be set to 'Usable' to work properly"); Debug.LogWarning(gameObject.name+" tag should be set to 'Usable' to work properly");
status = "entering";
// Navigation // // Navigation //
agent = GetComponent<NavMeshAgent>(); agent = GetComponent<NavMeshAgent>();
//Prevent rotation of the ground at movement //Prevent rotation of the ground at movement
agent.updateRotation = false; agent.updateRotation = false;
agent.updateUpAxis = false; agent.updateUpAxis = false;
//Get target //Get target
agent.destination = ClientManager.Instance.assignTarget().position; agent.destination = ClientManager.Instance.assignTarget();
} }
// Update is called once per frame // Update is called once per frame
void Update() void Update()
{ {
//Navigation //Navigation
// if (Vector2.Distance(destination, target.position) > 1.0f) // Debug.Log(gameObject.name + " navigation : "+ agent.isStopped + " " + agent.remainingDistance);
// {
// destination = target.position;
// agent.destination = destination;
// }
//Timer //Reached seat
if (currentMug!= null) //Consuming mug if there's one if(status=="entering" && agent.remainingDistance==0)
{
status="waiting";
waitTimer=waitingTime;
}
if(status=="waiting")
{
waitTimer -= Time.deltaTime;
if (waitTimer < 0) //Waited too long
{
//Leave tavern
status = "leaving";
agent.destination = ClientManager.Instance.assignTarget(agent.destination); //Request next target
}
}
//Consume Timer
if (status=="consuming" && agent.remainingDistance==0) //Consuming mug if there's one and reached destination
{ {
consumeTimer -= Time.deltaTime; consumeTimer -= Time.deltaTime;
if (consumeTimer < 0) //Finished consuming mug ? if (consumeTimer < 0) //Finished consuming mug ?
@ -95,6 +125,10 @@ public class Client_controller : MonoBehaviour, IUsable
obj.drop(gameObject.transform.position+ (Vector3)Vector2.down * 0.2f); obj.drop(gameObject.transform.position+ (Vector3)Vector2.down * 0.2f);
currentMug=null; currentMug=null;
} }
//Leave tavern
status = "leaving";
agent.destination = ClientManager.Instance.assignTarget(agent.destination); //Request next target
} }
} }
} }

View file

@ -15,8 +15,8 @@ public sealed class ClientManager : MonoBehaviour
string ClientRessourceFolder = "Clients"; string ClientRessourceFolder = "Clients";
private Object[] clients; private Object[] clients;
Vector3 spawnPosition = new Vector3(0, 0, 0); //TODO : Use gameObject Vector2 spawnPoint;
Dictionary<Transform, bool> targets_dict; //Dict with target and wether they're taken by a client Dictionary<Vector2, bool> targets_dict; //Dict with target and wether they're taken by a client
//Request new client //Request new client
//Return wether a new client was created //Return wether a new client was created
@ -26,7 +26,7 @@ public sealed class ClientManager : MonoBehaviour
{ {
GameObject newClient = (GameObject)clients[Random.Range(0, clients.Length)]; GameObject newClient = (GameObject)clients[Random.Range(0, clients.Length)];
// Debug.Log("Spawning "+clientPrefab.name+" at "+spawnPosition); // Debug.Log("Spawning "+clientPrefab.name+" at "+spawnPosition);
Instantiate(newClient, spawnPosition, Quaternion.identity); Instantiate(newClient, spawnPoint, Quaternion.identity);
currentNbClient+=1; currentNbClient+=1;
clientSpawnTimer=Random.Range(1.0f, maxTimeNewClients); //Need more random ? clientSpawnTimer=Random.Range(1.0f, maxTimeNewClients); //Need more random ?
clientSpawnReady=false; clientSpawnReady=false;
@ -36,15 +36,20 @@ public sealed class ClientManager : MonoBehaviour
return false; //No new client return false; //No new client
} }
//Assign a random available target //Assign a random available target.
public Transform assignTarget() public Vector2 assignTarget(Vector2? prevTarget=null)
{ {
List<Transform> avail_tgt = new List<Transform>(); if(prevTarget != null)
foreach(KeyValuePair<Transform, bool> tgt in targets_dict) {
targets_dict[(Vector2)prevTarget]=false; //Free prevTarget
return spawnPoint;
}
List<Vector2> avail_tgt = new List<Vector2>();
foreach(KeyValuePair<Vector2, bool> tgt in targets_dict)
if(tgt.Value is false) if(tgt.Value is false)
avail_tgt.Add(tgt.Key); avail_tgt.Add(tgt.Key);
Transform target = avail_tgt[Random.Range(0, avail_tgt.Count)]; Vector2 target = avail_tgt[Random.Range(0, avail_tgt.Count)];
targets_dict[target]=true; targets_dict[target]=true;
return target; return target;
} }
@ -74,10 +79,10 @@ public sealed class ClientManager : MonoBehaviour
GameObject spawnObj = GameObject.Find("/GameSystem/ClientSpawn"); GameObject spawnObj = GameObject.Find("/GameSystem/ClientSpawn");
if (spawnObj is null) if (spawnObj is null)
throw new System.Exception("No ClientSpawn GameObject found under GameSystem"); throw new System.Exception("No ClientSpawn GameObject found under GameSystem");
spawnPosition = spawnObj.transform.position; spawnPoint = spawnObj.transform.position;
// Load Client targets // // Load Client targets //
targets_dict = new Dictionary<Transform, bool>(); targets_dict = new Dictionary<Vector2, bool>();
GameObject targetsObj = GameObject.Find("/GameSystem/Targets"); GameObject targetsObj = GameObject.Find("/GameSystem/Targets");
if (targetsObj is null) if (targetsObj is null)
throw new System.Exception("No Targets GameObject found under GameSystem"); throw new System.Exception("No Targets GameObject found under GameSystem");
@ -89,7 +94,7 @@ public sealed class ClientManager : MonoBehaviour
{ {
if(target.gameObject.name != "Targets") if(target.gameObject.name != "Targets")
{ {
targets_dict.Add(target, false); targets_dict.Add(target.position, false);
Debug.Log("Client target : "+ target.gameObject.name + target.position); Debug.Log("Client target : "+ target.gameObject.name + target.position);
} }
} }