Change client spawn logic (coroutine)

This commit is contained in:
Antoine H 2021-01-17 13:39:56 +01:00
parent c41d6af3d1
commit 63e88c3a27
4 changed files with 45 additions and 26 deletions

View file

@ -32568,6 +32568,11 @@ PrefabInstance:
m_Modification: m_Modification:
m_TransformParent: {fileID: 0} m_TransformParent: {fileID: 0}
m_Modifications: m_Modifications:
- target: {fileID: 2157073179154264261, guid: dfb09d229e85c1446bee2e4f9357610e,
type: 3}
propertyPath: nbMaxClients
value: 3
objectReference: {fileID: 0}
- target: {fileID: 2157073180203202245, guid: dfb09d229e85c1446bee2e4f9357610e, - target: {fileID: 2157073180203202245, guid: dfb09d229e85c1446bee2e4f9357610e,
type: 3} type: 3}
propertyPath: m_Name propertyPath: m_Name

View file

@ -136,8 +136,6 @@ public class Client_controller : MonoBehaviour, IUsable
if(status=="entering" && !agent.pathPending && agent.remainingDistance==0) //Reached seat ? if(status=="entering" && !agent.pathPending && agent.remainingDistance==0) //Reached seat ?
{ {
EventManager.Instance.spawnEvent(transform.position);
status="waiting"; status="waiting";
waitTimer=waitingTime; waitTimer=waitingTime;
order = ClientManager.Instance.assignOrder(); order = ClientManager.Instance.assignOrder();
@ -162,7 +160,7 @@ public class Client_controller : MonoBehaviour, IUsable
if(status=="consuming") //Consuming mug if there's one and reached destination if(status=="consuming") //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 ?
{ {
Mug obj = currentMug.GetComponent<Mug>(); Mug obj = currentMug.GetComponent<Mug>();
if(obj !=null) if(obj !=null)

View file

@ -22,10 +22,14 @@ public sealed class ClientManager : MonoBehaviour
[SerializeField] [SerializeField]
int nbMaxClients = 1; //Maximum active clients int nbMaxClients = 1; //Maximum active clients
[SerializeField] [SerializeField]
float clientSpawnTimer = 0.5f; //Intial time before first spawn (pseudo-random after that) float clientSpawnChance = 100.0f; //Chance of new client every request
[SerializeField] [SerializeField]
float maxTimeNewClients = 2.0f; //Longest waiting time for new clients float clientFrequency = 1.0f; //Time (s) between clientRequest
bool clientSpawnReady = false; // [SerializeField]
// float clientSpawnTimer = 0.5f; //Intial time before first spawn (pseudo-random after that)
// [SerializeField]
// float maxTimeNewClients = 2.0f; //Longest waiting time for new clients
// bool clientSpawnReady = false;
[SerializeField] [SerializeField]
string ClientRessourceFolder = "Clients"; string ClientRessourceFolder = "Clients";
@ -38,9 +42,9 @@ public sealed class ClientManager : MonoBehaviour
//Request new client //Request new client
//Return wether a new client was created //Return wether a new client was created
public bool clientRequest() public bool clientRequest(float SpawnChance=100.0f)
{ {
if(clientSpawnReady && clientIDs.Count<nbMaxClients && targets_dict.ContainsValue(false)) if(Random.Range(0.0f, 99.9f)<SpawnChance && clientIDs.Count<nbMaxClients && targets_dict.ContainsValue(false))
{ {
int prefabChoice = Random.Range(0, clients.Length); int prefabChoice = Random.Range(0, clients.Length);
GameObject newClient = Instantiate((GameObject)clients[prefabChoice], spawnPoint, Quaternion.identity, ClientContainer.transform); //Instantiate new client inside ClientManager GameObject newClient = Instantiate((GameObject)clients[prefabChoice], spawnPoint, Quaternion.identity, ClientContainer.transform); //Instantiate new client inside ClientManager
@ -48,8 +52,8 @@ public sealed class ClientManager : MonoBehaviour
// Debug.Log(newClient.GetInstanceID()); // Debug.Log(newClient.GetInstanceID());
newClient.name = newClient.name.Split('(')[0]+clientIDs[clientIDs.Count-1]; //Rename new client newClient.name = newClient.name.Split('(')[0]+clientIDs[clientIDs.Count-1]; //Rename new client
clientSpawnTimer=Random.Range(1.0f, maxTimeNewClients); //Need more random ? // clientSpawnTimer=Random.Range(1.0f, maxTimeNewClients); //Need more random ?
clientSpawnReady=false; // clientSpawnReady=false;
// Debug.Log("Spawning "+clientPrefab.name+" at "+spawnPosition); // Debug.Log("Spawning "+clientPrefab.name+" at "+spawnPosition);
return true; //New client instantiated return true; //New client instantiated
@ -57,6 +61,15 @@ public sealed class ClientManager : MonoBehaviour
return false; //No new client return false; //No new client
} }
private IEnumerator requestCoroutine()
{
while(ClientManager.Instance!=null){
if(GameSystem.Instance.serviceOpen)
ClientManager.Instance.clientRequest(clientSpawnChance);
yield return new WaitForSeconds(clientFrequency);
}
}
//TODO: Reputation //TODO: Reputation
public void clientReward(int money) public void clientReward(int money)
{ {
@ -71,11 +84,11 @@ public sealed class ClientManager : MonoBehaviour
// Debug.Log(client.name+" destroyed"+clientIDs.Count); // Debug.Log(client.name+" destroyed"+clientIDs.Count);
//Prevent immediate spawn of a new client after one leaving //Prevent immediate spawn of a new client after one leaving
if(clientSpawnReady) // if(clientSpawnReady)
{ // {
clientSpawnReady=false; // clientSpawnReady=false;
clientSpawnTimer+=0.5f; // clientSpawnTimer+=0.5f;
} // }
} }
//Return a random available target. Or the Exit if Exit=True. //Return a random available target. Or the Exit if Exit=True.
@ -181,15 +194,20 @@ public sealed class ClientManager : MonoBehaviour
} }
} }
void Start()
{
StartCoroutine(requestCoroutine()); //Coroutine will start in parallel
}
// Update is called once per frame // Update is called once per frame
void Update() void Update()
{ {
if(!clientSpawnReady) // if(!clientSpawnReady)
{ // {
clientSpawnTimer-= Time.deltaTime; // clientSpawnTimer-= Time.deltaTime;
if(clientSpawnTimer<=0) // if(clientSpawnTimer<=0)
clientSpawnReady=true; // clientSpawnReady=true;
} // }
// Debug.Log("Client Spawn : "+clientSpawnTimer+" / Seat available: "+targets_dict.ContainsValue(false)); // Debug.Log("Client Spawn : "+clientSpawnTimer+" / Seat available: "+targets_dict.ContainsValue(false));
} }

View file

@ -19,7 +19,8 @@ public sealed class GameSystem : MonoBehaviour
public bool ready = false; //Wether the GameSystems are initialized public bool ready = false; //Wether the GameSystems are initialized
//Time //Time
bool serviceOpen = false; [HideInInspector]
public bool serviceOpen = false;
[SerializeField] [SerializeField]
float serviceTime = 30.0f; float serviceTime = 30.0f;
float serviceTimer = 0.0f; float serviceTimer = 0.0f;
@ -75,7 +76,7 @@ public sealed class GameSystem : MonoBehaviour
else else
return false; return false;
} }
//Awake is called when the script instance is being loaded. //Awake is called when the script instance is being loaded.
void Awake() void Awake()
{ {
@ -129,9 +130,6 @@ public sealed class GameSystem : MonoBehaviour
serviceOpen = false; serviceOpen = false;
Debug.Log("Service closed"); Debug.Log("Service closed");
} }
//Request new clients
ClientManager.Instance.clientRequest();
} }
//Temporary manual slowmode toggle //Temporary manual slowmode toggle