Update Event system + Add basic HardObstacle
This commit is contained in:
parent
ca8790bb19
commit
fd8306645b
39 changed files with 1248 additions and 69 deletions
|
@ -4,6 +4,8 @@ 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))]
|
||||
|
@ -13,14 +15,18 @@ public class Client_controller : MonoBehaviour, IUsable
|
|||
public float waitingTime = 10.0f; //Patience after reaching seat
|
||||
public UITimer UIWaitingTimer = null;
|
||||
|
||||
Animator animator;
|
||||
string _status;
|
||||
HashSet<string> _availStatus = new HashSet<string>(){"entering", "waiting", "consuming", "leaving"};
|
||||
string _prevStatus;
|
||||
HashSet<string> _availStatus = new HashSet<string>(){"entering", "waiting", "consuming", "leaving", "event"};
|
||||
public string status
|
||||
{
|
||||
get{ return _status;}
|
||||
set{
|
||||
if (_availStatus.Contains(value))
|
||||
_prevStatus=_status;
|
||||
_status = value;
|
||||
animator.SetTrigger(_status); //Update status in animator
|
||||
// Debug.Log(gameObject.name+" "+_status);
|
||||
switch (value)
|
||||
{
|
||||
|
@ -31,8 +37,9 @@ public class Client_controller : MonoBehaviour, IUsable
|
|||
UIWaitingTimer.gameObject.SetActive(false);
|
||||
break;
|
||||
case "waiting":
|
||||
EventManager.Instance.startCoroutine(gameObject);
|
||||
//Switch Agent to obstacle if waiting
|
||||
agent.Warp(agent.destination); //Make sure agent become static at right position
|
||||
agent.Warp(assigedPos); //Make sure agent become static at right position
|
||||
agent.enabled = false;
|
||||
navObstacle.enabled = true;
|
||||
|
||||
|
@ -48,6 +55,7 @@ public class Client_controller : MonoBehaviour, IUsable
|
|||
if(UIWaitingTimer != null)
|
||||
UIWaitingTimer.gameObject.SetActive(false);
|
||||
break;
|
||||
case "event":
|
||||
case "leaving":
|
||||
EventManager.Instance.stopCoroutine(gameObject);
|
||||
navObstacle.enabled = false;
|
||||
|
@ -69,7 +77,7 @@ public class Client_controller : MonoBehaviour, IUsable
|
|||
GameObject currentMug = null; //Mug currently held by the client
|
||||
|
||||
//Navigation
|
||||
Vector2 destination;
|
||||
Vector2 assigedPos; //Chair to sit or destination to stay (leave)
|
||||
NavMeshAgent agent;
|
||||
NavMeshObstacle navObstacle; //Obstacle for other agents
|
||||
|
||||
|
@ -111,6 +119,27 @@ public class Client_controller : MonoBehaviour, IUsable
|
|||
}
|
||||
}
|
||||
|
||||
//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;
|
||||
// NavMeshHit hit;
|
||||
// NavMesh.SamplePosition(gameObject.transform.position, out hit, agent.height*2, NavMesh.AllAreas);
|
||||
// agent.Warp(hit.position);
|
||||
if(agent.enabled)
|
||||
agent.SetDestination(assigedPos);
|
||||
else
|
||||
gameObject.transform.position=assigedPos;
|
||||
}
|
||||
else
|
||||
{
|
||||
status="event";
|
||||
agent.SetDestination((Vector2)destination);
|
||||
}
|
||||
}
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
|
@ -124,6 +153,8 @@ public class Client_controller : MonoBehaviour, IUsable
|
|||
else
|
||||
UIWaitingTimer.gameObject.SetActive(false);
|
||||
|
||||
animator = GetComponent<Animator>();
|
||||
|
||||
// Navigation //
|
||||
agent = GetComponent<NavMeshAgent>();
|
||||
navObstacle = GetComponent<NavMeshObstacle>();
|
||||
|
@ -132,7 +163,8 @@ public class Client_controller : MonoBehaviour, IUsable
|
|||
agent.updateRotation = false;
|
||||
agent.updateUpAxis = false;
|
||||
//Get target
|
||||
agent.SetDestination(ClientManager.Instance.assignTarget());
|
||||
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);
|
||||
|
||||
|
@ -154,21 +186,22 @@ public class Client_controller : MonoBehaviour, IUsable
|
|||
UIWaitingTimer.DisplayIcon(StockManager.Instance.consumableSprite(order));
|
||||
}
|
||||
|
||||
if(status=="waiting")
|
||||
else if(status=="waiting")
|
||||
{
|
||||
waitTimer -= Time.deltaTime;
|
||||
if (waitTimer < 0) //Waited too long
|
||||
{
|
||||
//Leave tavern
|
||||
status = "leaving";
|
||||
agent.SetDestination(ClientManager.Instance.assignTarget(agent.destination, true)); //Request next target
|
||||
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
|
||||
if(status=="consuming") //Consuming mug if there's one and reached destination
|
||||
else if(status=="consuming") //Consuming mug if there's one and reached destination
|
||||
{
|
||||
consumeTimer -= Time.deltaTime;
|
||||
if(consumeTimer < 0) //Finished consuming mug ?
|
||||
|
@ -190,11 +223,12 @@ public class Client_controller : MonoBehaviour, IUsable
|
|||
|
||||
//Leave tavern
|
||||
status = "leaving";
|
||||
agent.SetDestination(ClientManager.Instance.assignTarget(agent.destination, true)); //Request next target
|
||||
assigedPos = ClientManager.Instance.assignTarget(assigedPos, true); //Request leaving target
|
||||
agent.SetDestination(assigedPos);
|
||||
}
|
||||
}
|
||||
|
||||
if(status=="leaving" && !agent.pathPending && agent.remainingDistance==0)
|
||||
else if(status=="leaving" && !agent.pathPending && agent.remainingDistance==0)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
|
|
8
Assets/Scripts/Events.meta
Normal file
8
Assets/Scripts/Events.meta
Normal file
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 026890d1556d5ae40adefad79d1eb3c4
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
92
Assets/Scripts/Events/HardObstacle.cs
Normal file
92
Assets/Scripts/Events/HardObstacle.cs
Normal file
|
@ -0,0 +1,92 @@
|
|||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
|
||||
//Define the behavior of a hard obstacle (Block movement)
|
||||
[RequireComponent(typeof(Animator))]
|
||||
[RequireComponent(typeof(Collider2D))]
|
||||
[RequireComponent(typeof(NavMeshObstacle))]
|
||||
public class HardObstacle : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
float lifeTime = -1.0f; //Time before self-destruct (Negative value to prevent self-destruct)
|
||||
float lifeTimer;
|
||||
|
||||
List<Client_controller> angryClients = new List<Client_controller>(); //Clients in the fight
|
||||
|
||||
Collider2D ObsCollider;
|
||||
NavMeshObstacle Obstacle;
|
||||
// SpriteRenderer ObsRenderer;
|
||||
Animator animator;
|
||||
|
||||
bool gameRunning = true;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
lifeTimer=lifeTime;
|
||||
|
||||
ObsCollider = GetComponent<Collider2D>();
|
||||
|
||||
Obstacle = GetComponent<NavMeshObstacle>();
|
||||
Obstacle.enabled=false; //Don't block movement before client entounter
|
||||
|
||||
animator = GetComponent<Animator>();
|
||||
animator.SetBool("active fight", false); //Disable animation before client encounter
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
if(animator.GetBool("active fight") && lifeTimer>0)
|
||||
{
|
||||
lifeTimer -= Time.deltaTime;
|
||||
if(lifeTimer<0)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OnTriggerEnter2D(Collider2D other)
|
||||
{
|
||||
Client_controller newClient = other.GetComponent<Client_controller>();
|
||||
if(newClient!=null && !angryClients.Contains(newClient))
|
||||
{
|
||||
if(newClient.status!="event") //Make sure to set right status
|
||||
newClient.assignToEvent(gameObject.transform.position);
|
||||
angryClients.Add(newClient);
|
||||
// Debug.Log("New fighting client. Current nb : "+angryClients.Count);
|
||||
|
||||
if(angryClients.Count>1)//Start fight
|
||||
{
|
||||
foreach(Client_controller client in angryClients) //Turn off client (to merge for a fight)
|
||||
client.gameObject.SetActive(false);
|
||||
|
||||
animator.SetBool("active fight", true);
|
||||
|
||||
//Block movement
|
||||
Obstacle.enabled=true;
|
||||
ObsCollider.isTrigger=false; //Trigger becoming solid
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
if(gameRunning) //Only apply if game is still running
|
||||
foreach(Client_controller client in angryClients) //Clients return to their previous behavior
|
||||
{
|
||||
client.gameObject.SetActive(true);
|
||||
client.assignToEvent(); //Restore previous behavior
|
||||
}
|
||||
|
||||
EventManager.Instance.destroyEvent(gameObject);
|
||||
}
|
||||
|
||||
void OnApplicationQuit()
|
||||
{
|
||||
gameRunning=false;
|
||||
}
|
||||
}
|
11
Assets/Scripts/Events/HardObstacle.cs.meta
Normal file
11
Assets/Scripts/Events/HardObstacle.cs.meta
Normal file
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6bcb4ca5242cefc4bb06adff707d9c5f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -2,7 +2,7 @@
|
|||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
//Define the behavior of a soft obstacle
|
||||
//Define the behavior of a soft obstacle (Slow movement and chance of falling)
|
||||
//TODO : Effect on clients ? (Lower Tips/Repu ?)
|
||||
[RequireComponent(typeof(Collider2D))]
|
||||
public class SoftObstacle : MonoBehaviour
|
||||
|
@ -13,7 +13,7 @@ public class SoftObstacle : MonoBehaviour
|
|||
float mvtSlow = 1.0f; //Scale of movement slowdown (1.0f = no change).
|
||||
|
||||
[SerializeField]
|
||||
float lifeTime = 3.0f; //Time to consume currentMug
|
||||
float lifeTime = -1.0f; //Time before self-destruct (Negative value to prevent self-destruct)
|
||||
float lifeTimer;
|
||||
|
||||
Tavernkeeper_controller player;
|
||||
|
@ -27,10 +27,13 @@ public class SoftObstacle : MonoBehaviour
|
|||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
lifeTimer -= Time.deltaTime;
|
||||
if(lifeTimer<0)
|
||||
if(lifeTimer>0)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
lifeTimer -= Time.deltaTime;
|
||||
if(lifeTimer<0)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
//TODO : Trigger falling animation on tavernkeeper
|
|
@ -36,9 +36,15 @@ public sealed class ClientManager : MonoBehaviour
|
|||
|
||||
[SerializeField]
|
||||
string ClientRessourceFolder = "Clients";
|
||||
private Object[] clients;
|
||||
private Object[] clientsPrefab;
|
||||
GameObject ClientContainer = null;
|
||||
List<int> clientIDs = new List<int>();
|
||||
|
||||
private List<GameObject> _clientList = new List<GameObject>();
|
||||
public List<GameObject> clientList
|
||||
{
|
||||
get{return _clientList;}
|
||||
private set{_clientList=value;}
|
||||
}
|
||||
|
||||
Vector2 spawnPoint;
|
||||
Dictionary<Vector2, bool> targets_dict; //Dict with target and wether they're taken by a client
|
||||
|
@ -49,13 +55,14 @@ public sealed class ClientManager : MonoBehaviour
|
|||
//Return wether a new client was created
|
||||
public bool clientRequest(float SpawnChance=100.0f)
|
||||
{
|
||||
if(Random.Range(0.0f, 99.9f)<SpawnChance && clientIDs.Count<nbMaxClients && targets_dict.ContainsValue(false))
|
||||
if(Random.Range(0.0f, 99.9f)<SpawnChance && clientList.Count<nbMaxClients && targets_dict.ContainsValue(false))
|
||||
{
|
||||
int prefabChoice = Random.Range(0, clients.Length);
|
||||
GameObject newClient = Instantiate((GameObject)clients[prefabChoice], spawnPoint, Quaternion.identity, ClientContainer.transform); //Instantiate new client inside ClientManager
|
||||
clientIDs.Add(newClient.GetInstanceID()); //Save ID
|
||||
int prefabChoice = Random.Range(0, clientsPrefab.Length);
|
||||
GameObject newClient = Instantiate((GameObject)clientsPrefab[prefabChoice], spawnPoint, Quaternion.identity, ClientContainer.transform); //Instantiate new client inside ClientManager
|
||||
// Debug.Log(newClient.GetInstanceID());
|
||||
newClient.name = newClient.name.Split('(')[0]+clientIDs[clientIDs.Count-1]; //Rename new client
|
||||
newClient.name = newClient.name.Split('(')[0]+newClient.GetInstanceID(); //Rename new client
|
||||
|
||||
clientList.Add(newClient); //Save client ref
|
||||
|
||||
// clientSpawnTimer=Random.Range(1.0f, maxTimeNewClients); //Need more random ?
|
||||
// clientSpawnReady=false;
|
||||
|
@ -75,7 +82,7 @@ public sealed class ClientManager : MonoBehaviour
|
|||
//Destroy a client
|
||||
public void clientLeave(GameObject client)
|
||||
{
|
||||
clientIDs.Remove(client.GetInstanceID());
|
||||
clientList.Remove(client);
|
||||
// Debug.Log(client.name+" destroyed"+clientIDs.Count);
|
||||
|
||||
//Prevent immediate spawn of a new client after one leaving
|
||||
|
@ -156,15 +163,15 @@ public sealed class ClientManager : MonoBehaviour
|
|||
// Instantiate(guid, spawnPosition, Quaternion.identity);
|
||||
// }
|
||||
|
||||
clients = Resources.LoadAll(ClientRessourceFolder);
|
||||
clientsPrefab = Resources.LoadAll(ClientRessourceFolder);
|
||||
|
||||
// foreach (var c in clients)
|
||||
// {
|
||||
// Debug.Log(gameObject.name+" : "+c.name + " loaded");
|
||||
// }
|
||||
if (clients.Length<nbMaxClients)
|
||||
if (clientsPrefab.Length<nbMaxClients)
|
||||
{
|
||||
Debug.LogWarning("ClientManager doesn't have enough client prefab to manage unique MaxClients : "+clients.Length+"/"+nbMaxClients);
|
||||
Debug.LogWarning("ClientManager doesn't have enough client prefab to manage unique MaxClients : "+clientsPrefab.Length+"/"+nbMaxClients);
|
||||
}
|
||||
|
||||
// Load Client spawn point //
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
[RequireComponent(typeof(Rigidbody2D))]
|
||||
[RequireComponent(typeof(Animator))]
|
||||
public class Tavernkeeper_controller : MonoBehaviour
|
||||
{
|
||||
|
|
8
Assets/Scripts/Workshops.meta
Normal file
8
Assets/Scripts/Workshops.meta
Normal file
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 18d1386fcf79908469f9c3aa3ab835b6
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Add table
Add a link
Reference in a new issue