Update Event system + Add basic HardObstacle
This commit is contained in:
parent
ca8790bb19
commit
fd8306645b
39 changed files with 1248 additions and 69 deletions
|
@ -24,9 +24,10 @@ public sealed class EventManager : MonoBehaviour
|
|||
[SerializeField]
|
||||
float SpawnRange = 1.0f; //Range of an event spawn from its origin (real max distance = 2*range)
|
||||
[SerializeField]
|
||||
int nbMaxEvents = 1; //Maximum active clients
|
||||
float spawnChance = 100.0f; //Probability of an event to spawn (%)
|
||||
[SerializeField]
|
||||
float spawnChance = 100.0f; //Probability of an event to spawn
|
||||
int maxSoftObs = 1, maxHardObs = 1; //Maximum active events
|
||||
|
||||
// [SerializeField]
|
||||
// float eventSpawnTimer = 0.5f; //Intial time before first spawn (pseudo-random after that)
|
||||
// [SerializeField]
|
||||
|
@ -34,46 +35,68 @@ public sealed class EventManager : MonoBehaviour
|
|||
// bool eventSpawnReady = false;
|
||||
|
||||
[SerializeField]
|
||||
string EventRessourceFolder = "Events"; //Ressource folder w/ events prefabs
|
||||
private Object[] events;
|
||||
string SoftObsRessourceFolder = "Events/SoftObstacles", HardObsRessourceFolder = "Events/HardObstacles"; //Ressource folder w/ events prefabs
|
||||
private Dictionary<string,Object[]> eventPrefabs = new Dictionary<string,Object[]>();
|
||||
GameObject EventContainer=null;
|
||||
List<int> eventIDs = new List<int>(); //List of active event ID
|
||||
|
||||
//List of active event ID
|
||||
List<GameObject> softObsList = new List<GameObject>();
|
||||
List<GameObject> hardObsList = new List<GameObject>();
|
||||
|
||||
[SerializeField]
|
||||
float coroutineRefreshRate = 1.0f; //Time (s) before refreshing a coroutine
|
||||
private Dictionary<int,IEnumerator> coroutines= new Dictionary<int,IEnumerator>(); //Dict of EventManager coroutines associated to each client ID
|
||||
|
||||
//Spawn an event near position with a probability of spawnChance%
|
||||
public void spawnEvent(Vector2 position, float spawnChance = 100.0f)
|
||||
public void spawnSoftObs(Vector2 position, float spawnChance = 100.0f)
|
||||
{
|
||||
Vector3 spawnPoint;
|
||||
if (Random.Range(0.0f, 99.9f)<spawnChance && eventIDs.Count<nbMaxEvents && RandomPoint(position, SpawnRange, out spawnPoint))
|
||||
if (Random.Range(0.0f, 99.9f)<spawnChance && softObsList.Count<maxSoftObs && RandomPoint(position, SpawnRange, out spawnPoint))
|
||||
{
|
||||
// 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
|
||||
int prefabChoice = Random.Range(0, eventPrefabs["soft"].Length);
|
||||
GameObject newEvent = Instantiate((GameObject)eventPrefabs["soft"][prefabChoice], spawnPoint, Quaternion.identity, EventContainer.transform); //Instantiate new event inside ClientManager
|
||||
softObsList.Add(newEvent); //Save event to list
|
||||
newEvent.name = newEvent.name.Split('(')[0]+newEvent.GetInstanceID(); //Rename new event
|
||||
|
||||
// eventSpawnTimer=Random.Range(1.0f, maxTimeNewEvents); //Need more random ?
|
||||
// eventSpawnReady=false;
|
||||
}
|
||||
}
|
||||
public void spawnHardObs(List<Client_controller> targetClients, Vector2 position, float spawnChance = 100.0f)
|
||||
{
|
||||
//TODO: Orienté client vers event + prefab
|
||||
Vector3 spawnPoint;
|
||||
if (Random.Range(0.0f, 99.9f)<spawnChance && hardObsList.Count<maxHardObs && RandomPoint(position, SpawnRange, out spawnPoint))
|
||||
{
|
||||
// Debug.DrawRay(spawnPoint, Vector3.up, Color.blue, 2.0f);
|
||||
int prefabChoice = Random.Range(0, eventPrefabs["hard"].Length);
|
||||
GameObject newEvent = Instantiate((GameObject)eventPrefabs["hard"][prefabChoice], spawnPoint, Quaternion.identity, EventContainer.transform); //Instantiate new event inside ClientManager
|
||||
hardObsList.Add(newEvent); //Save event to list
|
||||
newEvent.name = newEvent.name.Split('(')[0]+newEvent.GetInstanceID(); //Rename new event
|
||||
|
||||
foreach(Client_controller client in targetClients)
|
||||
client.assignToEvent(spawnPoint);
|
||||
}
|
||||
}
|
||||
|
||||
//Remove an event from the EventManager
|
||||
public void destroyEvent(GameObject eventObj)
|
||||
{
|
||||
eventIDs.Remove(eventObj.GetInstanceID());
|
||||
softObsList.Remove(eventObj);
|
||||
hardObsList.Remove(eventObj);
|
||||
}
|
||||
|
||||
//Start an event coroutine for client
|
||||
public void startCoroutine(GameObject client)
|
||||
{
|
||||
Debug.Log("EventManager: Start coroutine "+client.name);
|
||||
// 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);
|
||||
IEnumerator newCoroutine = clientCoroutine(client);
|
||||
coroutines[clientID]=newCoroutine;
|
||||
StartCoroutine(newCoroutine);
|
||||
}
|
||||
|
@ -85,22 +108,77 @@ public sealed class EventManager : MonoBehaviour
|
|||
|
||||
if(coroutines.ContainsKey(clientID))
|
||||
{
|
||||
Debug.Log("EventManager: Stop coroutine "+client.name);
|
||||
// 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)
|
||||
private IEnumerator clientCoroutine(GameObject clientObj)
|
||||
{
|
||||
Client_controller client = clientObj.GetComponent<Client_controller>();
|
||||
while(EventManager.Instance!=null){
|
||||
if(GameSystem.Instance.serviceOpen)
|
||||
EventManager.Instance.spawnEvent(position, spawnChance);
|
||||
yield return new WaitForSeconds(1.0f); //Called every second
|
||||
{
|
||||
//Try to spawn softObs or hardObs randomly
|
||||
if(Random.value<0.5 && client.status=="consuming")
|
||||
EventManager.Instance.spawnSoftObs(clientObj.transform.position, spawnChance);
|
||||
else
|
||||
{
|
||||
List<GameObject> otherClients = findNearClients(clientObj, 1.0f);
|
||||
if(otherClients.Count>0)
|
||||
{
|
||||
foreach(GameObject ocl in otherClients)
|
||||
Debug.DrawLine(clientObj.transform.position, ocl.transform.position, Color.red, coroutineRefreshRate);
|
||||
// Debug.Log("Clients near");
|
||||
|
||||
GameObject tgtClient = otherClients[Random.Range(0, otherClients.Count)];
|
||||
//TODO : Compute spawnChance w/ clients happiness
|
||||
Vector2 eventPos=(clientObj.transform.position+tgtClient.transform.position)/2; //Event pos between clients
|
||||
List<Client_controller> targetClients = new List<Client_controller>(){client, tgtClient.GetComponent<Client_controller>()};
|
||||
EventManager.Instance.spawnHardObs(targetClients, eventPos, spawnChance);
|
||||
}
|
||||
}
|
||||
}
|
||||
yield return new WaitForSeconds(coroutineRefreshRate); //Called every coroutineRefreshRate second
|
||||
}
|
||||
}
|
||||
|
||||
//Return the list of other clients in range of client. The client in event status are ignored.
|
||||
List<GameObject> findNearClients(GameObject client, float range)
|
||||
{
|
||||
List<GameObject> res = new List<GameObject>();
|
||||
List<GameObject> clientList = ClientManager.Instance.clientList;
|
||||
Vector3 originPos = client.transform.position;
|
||||
Vector3 otherPos;
|
||||
foreach(GameObject cl in clientList)
|
||||
{
|
||||
otherPos=cl.transform.position;
|
||||
if(Vector2.Distance(originPos, otherPos)<range && originPos!=otherPos)
|
||||
if(cl.GetComponent<Client_controller>().status!="event")//Ignore clients already in event status
|
||||
res.Add(cl);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
//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;
|
||||
}
|
||||
|
||||
//Awake is called when the script instance is being loaded.
|
||||
void Awake()
|
||||
{
|
||||
|
@ -116,10 +194,15 @@ public sealed class EventManager : MonoBehaviour
|
|||
if (EventContainer is null)
|
||||
throw new System.Exception("No EventManager found under GameSystem");
|
||||
|
||||
events = Resources.LoadAll(EventRessourceFolder);
|
||||
if (events.Length==0)
|
||||
eventPrefabs["soft"] = Resources.LoadAll(SoftObsRessourceFolder);
|
||||
eventPrefabs["hard"] = Resources.LoadAll(HardObsRessourceFolder);
|
||||
if (eventPrefabs["soft"].Length==0)
|
||||
{
|
||||
Debug.LogWarning("EventManager didn't find events prefab in ressource folder : "+EventRessourceFolder);
|
||||
Debug.LogWarning("EventManager didn't find events prefab in ressource folder : "+SoftObsRessourceFolder);
|
||||
}
|
||||
if (eventPrefabs["hard"].Length==0)
|
||||
{
|
||||
Debug.LogWarning("EventManager didn't find events prefab in ressource folder : "+HardObsRessourceFolder);
|
||||
}
|
||||
|
||||
ready = true;
|
||||
|
@ -146,21 +229,4 @@ public sealed class EventManager : MonoBehaviour
|
|||
{
|
||||
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++)
|
||||
{
|
||||
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