Working Event system & SoftObstacle
This commit is contained in:
parent
08f623a1b1
commit
a940cb64ba
8 changed files with 279 additions and 68 deletions
|
@ -4,6 +4,7 @@ using UnityEngine;
|
|||
using UnityEditor;
|
||||
|
||||
//Define the system managing the clients. (Singleton)
|
||||
//TODO: Switch to a registering approach for clients
|
||||
public sealed class ClientManager : MonoBehaviour
|
||||
{
|
||||
//Singleton
|
||||
|
@ -40,6 +41,8 @@ public sealed class ClientManager : MonoBehaviour
|
|||
Vector2 spawnPoint;
|
||||
Dictionary<Vector2, bool> targets_dict; //Dict with target and wether they're taken by a client
|
||||
|
||||
private List<IEnumerator> coroutines= new List<IEnumerator>(); //List of ClientManager coroutines
|
||||
|
||||
//Request new client
|
||||
//Return wether a new client was created
|
||||
public bool clientRequest(float SpawnChance=100.0f)
|
||||
|
@ -61,15 +64,6 @@ public sealed class ClientManager : MonoBehaviour
|
|||
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
|
||||
public void clientReward(int money)
|
||||
{
|
||||
|
@ -79,8 +73,7 @@ public sealed class ClientManager : MonoBehaviour
|
|||
//Destroy a client
|
||||
public void clientLeave(GameObject client)
|
||||
{
|
||||
clientIDs.Remove(-int.Parse(client.name.Split('-')[1]));
|
||||
Destroy(client);
|
||||
clientIDs.Remove(client.GetInstanceID());
|
||||
// Debug.Log(client.name+" destroyed"+clientIDs.Count);
|
||||
|
||||
//Prevent immediate spawn of a new client after one leaving
|
||||
|
@ -124,6 +117,17 @@ public sealed class ClientManager : MonoBehaviour
|
|||
return order_type;
|
||||
}
|
||||
|
||||
//InvokeRepeating() is another option
|
||||
//Coroutine to be started in a parallel process. It'll repeatidly request new client.
|
||||
private IEnumerator requestCoroutine()
|
||||
{
|
||||
while(ClientManager.Instance!=null){
|
||||
if(GameSystem.Instance.serviceOpen)
|
||||
ClientManager.Instance.clientRequest(clientSpawnChance);
|
||||
yield return new WaitForSeconds(clientFrequency);
|
||||
}
|
||||
}
|
||||
|
||||
//Awake is called when the script instance is being loaded.
|
||||
void Awake()
|
||||
{
|
||||
|
@ -196,7 +200,10 @@ public sealed class ClientManager : MonoBehaviour
|
|||
|
||||
void Start()
|
||||
{
|
||||
StartCoroutine(requestCoroutine()); //Coroutine will start in parallel
|
||||
//Start coroutines in parallel
|
||||
coroutines.Add(requestCoroutine());
|
||||
foreach(IEnumerator c in coroutines)
|
||||
StartCoroutine(c);
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
|
@ -211,6 +218,16 @@ public sealed class ClientManager : MonoBehaviour
|
|||
// Debug.Log("Client Spawn : "+clientSpawnTimer+" / Seat available: "+targets_dict.ContainsValue(false));
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
StopAllCoroutines();
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
StopAllCoroutines();
|
||||
}
|
||||
|
||||
//// Singleton Implementation (https://jlambert.developpez.com/tutoriels/dotnet/implementation-pattern-singleton-csharp/#LIII) ////
|
||||
// private ClientManager()
|
||||
// {
|
||||
|
|
|
@ -4,6 +4,7 @@ using UnityEngine;
|
|||
using UnityEngine.AI;
|
||||
|
||||
//Define the system managing the events. (Singleton)
|
||||
//TODO: Switch to a registering approach for events
|
||||
public sealed class EventManager : MonoBehaviour
|
||||
{
|
||||
//Singleton
|
||||
|
@ -20,53 +21,85 @@ public sealed class EventManager : MonoBehaviour
|
|||
public bool ready = false; //Wether the ClientManager is initialized
|
||||
|
||||
[SerializeField]
|
||||
float SpawnRange = 1.0f;
|
||||
float SpawnRange = 1.0f; //Range of an event spawn from its origin (real max distance = 2*range)
|
||||
[SerializeField]
|
||||
int nbMaxEvents = 1; //Maximum active clients
|
||||
[SerializeField]
|
||||
float eventSpawnTimer = 0.5f; //Intial time before first spawn (pseudo-random after that)
|
||||
[SerializeField]
|
||||
float maxTimeNewEvents = 2.0f; //Longest waiting time for new clients
|
||||
bool eventSpawnReady = false;
|
||||
float spawnChance = 100.0f; //Probability of an event to spawn
|
||||
// [SerializeField]
|
||||
// float eventSpawnTimer = 0.5f; //Intial time before first spawn (pseudo-random after that)
|
||||
// [SerializeField]
|
||||
// float maxTimeNewEvents = 2.0f; //Longest waiting time for new clients
|
||||
// bool eventSpawnReady = false;
|
||||
|
||||
[SerializeField]
|
||||
string EventRessourceFolder = "Events";
|
||||
string EventRessourceFolder = "Events"; //Ressource folder w/ events prefabs
|
||||
private Object[] events;
|
||||
GameObject EventContainer=null;
|
||||
List<int> eventIDs = new List<int>();
|
||||
List<int> eventIDs = new List<int>(); //List of active event ID
|
||||
|
||||
//Try to find a random point on NavMesh inside a range circle. A result would at a maximum distance of 2*range.
|
||||
bool RandomPoint(Vector3 center, float range, out Vector3 result)
|
||||
{
|
||||
for (int i = 0; i < 50; i++)
|
||||
{
|
||||
Vector3 randomPoint = center + (Vector3)Random.insideUnitCircle * range;
|
||||
NavMeshHit hit;
|
||||
if (NavMesh.SamplePosition(randomPoint, out hit, range, NavMesh.AllAreas))
|
||||
{
|
||||
result = hit.position;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
result = Vector3.zero;
|
||||
return false;
|
||||
}
|
||||
private Dictionary<int,IEnumerator> coroutines= new Dictionary<int,IEnumerator>(); //Dict of EventManager coroutines associated to each client ID
|
||||
|
||||
public void spawnEvent(Vector3 position)
|
||||
//Spawn an event near position with a probability of spawnChance%
|
||||
public void spawnEvent(Vector2 position, float spawnChance = 100.0f)
|
||||
{
|
||||
Vector3 spawnPoint;
|
||||
if (eventSpawnReady && eventIDs.Count<nbMaxEvents && RandomPoint(position, SpawnRange, out spawnPoint))
|
||||
if (Random.Range(0.0f, 99.9f)<spawnChance && eventIDs.Count<nbMaxEvents && RandomPoint(position, SpawnRange, out spawnPoint))
|
||||
{
|
||||
Debug.DrawRay(spawnPoint, Vector3.up, Color.blue, 2.0f);
|
||||
// Debug.DrawRay(spawnPoint, Vector3.up, Color.blue, 2.0f);
|
||||
int prefabChoice = Random.Range(0, events.Length);
|
||||
GameObject newEvent = Instantiate((GameObject)events[prefabChoice], spawnPoint, Quaternion.identity, EventContainer.transform); //Instantiate new event inside ClientManager
|
||||
eventIDs.Add(newEvent.GetInstanceID()); //Save ID
|
||||
newEvent.name = newEvent.name.Split('(')[0]+eventIDs[eventIDs.Count-1]; //Rename new client
|
||||
|
||||
eventSpawnTimer=Random.Range(1.0f, maxTimeNewEvents); //Need more random ?
|
||||
eventSpawnReady=false;
|
||||
// eventSpawnTimer=Random.Range(1.0f, maxTimeNewEvents); //Need more random ?
|
||||
// eventSpawnReady=false;
|
||||
}
|
||||
}
|
||||
|
||||
//Remove an event from the EventManager
|
||||
public void destroyEvent(GameObject eventObj)
|
||||
{
|
||||
eventIDs.Remove(eventObj.GetInstanceID());
|
||||
}
|
||||
|
||||
//Start an event coroutine for client
|
||||
public void startCoroutine(GameObject client)
|
||||
{
|
||||
Debug.Log("EventManager: Start coroutine "+client.name);
|
||||
int clientID = client.GetInstanceID();
|
||||
|
||||
if(coroutines.ContainsKey(clientID)) //Stop previous coroutine of the client
|
||||
StopCoroutine(coroutines[clientID]);
|
||||
|
||||
IEnumerator newCoroutine = clientCoroutine(client.transform.position);
|
||||
coroutines[clientID]=newCoroutine;
|
||||
StartCoroutine(newCoroutine);
|
||||
}
|
||||
|
||||
//Stop the event coroutine for client
|
||||
public void stopCoroutine(GameObject client)
|
||||
{
|
||||
int clientID = client.GetInstanceID();
|
||||
|
||||
if(coroutines.ContainsKey(clientID))
|
||||
{
|
||||
Debug.Log("EventManager: Stop coroutine "+client.name);
|
||||
StopCoroutine(coroutines[clientID]);
|
||||
}
|
||||
}
|
||||
|
||||
//InvokeRepeating() is another option
|
||||
//Coroutine to be started in a parallel process.
|
||||
private IEnumerator clientCoroutine(Vector2 position)
|
||||
{
|
||||
while(EventManager.Instance!=null){
|
||||
if(GameSystem.Instance.serviceOpen)
|
||||
EventManager.Instance.spawnEvent(position, spawnChance);
|
||||
yield return new WaitForSeconds(1.0f); //Called every second
|
||||
}
|
||||
}
|
||||
|
||||
//Awake is called when the script instance is being loaded.
|
||||
void Awake()
|
||||
{
|
||||
|
@ -95,11 +128,38 @@ public sealed class EventManager : MonoBehaviour
|
|||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if(!eventSpawnReady)
|
||||
// if(!eventSpawnReady)
|
||||
// {
|
||||
// eventSpawnTimer-= Time.deltaTime;
|
||||
// if(eventSpawnTimer<=0)
|
||||
// eventSpawnReady=true;
|
||||
// }
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
StopAllCoroutines();
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
StopAllCoroutines();
|
||||
}
|
||||
|
||||
//Try to find a random point on NavMesh inside a range circle. A result would at a maximum distance of 2*range.
|
||||
bool RandomPoint(Vector3 center, float range, out Vector3 result)
|
||||
{
|
||||
for (int i = 0; i < 50; i++)
|
||||
{
|
||||
eventSpawnTimer-= Time.deltaTime;
|
||||
if(eventSpawnTimer<=0)
|
||||
eventSpawnReady=true;
|
||||
Vector3 randomPoint = center + (Vector3)Random.insideUnitCircle * range;
|
||||
NavMeshHit hit;
|
||||
if (NavMesh.SamplePosition(randomPoint, out hit, range, NavMesh.AllAreas))
|
||||
{
|
||||
result = hit.position;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
result = Vector3.zero;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue