diff --git a/Assets/Prefabs/Obstacles/SoftObstacle.prefab b/Assets/Prefabs/Obstacles/SoftObstacle.prefab index e54c3dd..cac01ed 100644 --- a/Assets/Prefabs/Obstacles/SoftObstacle.prefab +++ b/Assets/Prefabs/Obstacles/SoftObstacle.prefab @@ -11,6 +11,7 @@ GameObject: - component: {fileID: 4255469056608660451} - component: {fileID: 839665940900074832} - component: {fileID: 521044286435324494} + - component: {fileID: 5683341077662129582} m_Layer: 0 m_Name: SoftObstacle m_TagString: Untagged @@ -110,3 +111,18 @@ BoxCollider2D: serializedVersion: 2 m_Size: {x: 2.1792417, y: 2.3361635} m_EdgeRadius: 0 +--- !u!114 &5683341077662129582 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 242890721790304732} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: da02c62b238bfbd48b8fc1f714a253e0, type: 3} + m_Name: + m_EditorClassIdentifier: + effectChance: 100 + mvtSlow: 2 + lifeTime: 30 diff --git a/Assets/Scenes/Tests/SampleScene.unity b/Assets/Scenes/Tests/SampleScene.unity index b53e284..fc18c60 100644 --- a/Assets/Scenes/Tests/SampleScene.unity +++ b/Assets/Scenes/Tests/SampleScene.unity @@ -32573,6 +32573,11 @@ PrefabInstance: propertyPath: nbMaxClients value: 3 objectReference: {fileID: 0} + - target: {fileID: 2157073180203202244, guid: dfb09d229e85c1446bee2e4f9357610e, + type: 3} + propertyPath: serviceTime + value: 120 + objectReference: {fileID: 0} - target: {fileID: 2157073180203202245, guid: dfb09d229e85c1446bee2e4f9357610e, type: 3} propertyPath: m_Name @@ -32641,7 +32646,12 @@ PrefabInstance: - target: {fileID: 7408334756516261553, guid: dfb09d229e85c1446bee2e4f9357610e, type: 3} propertyPath: nbMaxEvents - value: 3 + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 7408334756516261553, guid: dfb09d229e85c1446bee2e4f9357610e, + type: 3} + propertyPath: spawnChance + value: 100 objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: dfb09d229e85c1446bee2e4f9357610e, type: 3} diff --git a/Assets/Scripts/Client_controller.cs b/Assets/Scripts/Client_controller.cs index ab24930..132741c 100644 --- a/Assets/Scripts/Client_controller.cs +++ b/Assets/Scripts/Client_controller.cs @@ -22,31 +22,41 @@ public class Client_controller : MonoBehaviour, IUsable if (_availStatus.Contains(value)) _status = value; // Debug.Log(gameObject.name+" "+_status); - - //Switch Agent to obstacle if waiting - if(value=="waiting") + switch (value) { - agent.Warp(agent.destination); //Make sure agent become static at right position - agent.enabled = false; - navObstacle.enabled = true; - - if(UIWaitingTimer != null) - { - UIWaitingTimer.DisplayIcon(true); - UIWaitingTimer.SetValue(1.0f); - UIWaitingTimer.gameObject.SetActive(true); - } - } - else - { - if(UIWaitingTimer != null) - UIWaitingTimer.gameObject.SetActive(false); - if(value=="entering"||value=="leaving") - { + case "entering": navObstacle.enabled = false; agent.enabled = true; - } + if(UIWaitingTimer != null) + UIWaitingTimer.gameObject.SetActive(false); + break; + case "waiting": + //Switch Agent to obstacle if waiting + agent.Warp(agent.destination); //Make sure agent become static at right position + agent.enabled = false; + navObstacle.enabled = true; + if(UIWaitingTimer != null) + { + UIWaitingTimer.DisplayIcon(true); + UIWaitingTimer.SetValue(1.0f); + UIWaitingTimer.gameObject.SetActive(true); + } + break; + case "consuming": + EventManager.Instance.startCoroutine(gameObject); + if(UIWaitingTimer != null) + UIWaitingTimer.gameObject.SetActive(false); + break; + case "leaving": + EventManager.Instance.stopCoroutine(gameObject); + navObstacle.enabled = false; + agent.enabled = true; + if(UIWaitingTimer != null) + UIWaitingTimer.gameObject.SetActive(false); + break; + default: + break; } // navObstacle.enabled = value=="waiting"; // agent.enabled = value!="waiting"; @@ -183,7 +193,12 @@ public class Client_controller : MonoBehaviour, IUsable if(status=="leaving" && !agent.pathPending && agent.remainingDistance==0) { - ClientManager.Instance.clientLeave(gameObject); + Destroy(gameObject); } } + + void OnDestroy() + { + ClientManager.Instance.clientLeave(gameObject); + } } diff --git a/Assets/Scripts/GameSystems/ClientManager.cs b/Assets/Scripts/GameSystems/ClientManager.cs index 05262b6..645d30a 100644 --- a/Assets/Scripts/GameSystems/ClientManager.cs +++ b/Assets/Scripts/GameSystems/ClientManager.cs @@ -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 targets_dict; //Dict with target and wether they're taken by a client + private List coroutines= new List(); //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() // { diff --git a/Assets/Scripts/GameSystems/EventManager.cs b/Assets/Scripts/GameSystems/EventManager.cs index 549f7cb..58454b4 100644 --- a/Assets/Scripts/GameSystems/EventManager.cs +++ b/Assets/Scripts/GameSystems/EventManager.cs @@ -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 eventIDs = new List(); + List eventIDs = new List(); //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 coroutines= new Dictionary(); //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(); + player.mvt_speed=player.mvt_speed/mvtSlow; //Slow movement speed + + if(Random.Range(0.0f, 99.9f) hand_container; //Objects in hand + //TODO : GameObject => IGrabable + Dictionary hand_container; //Objects in hand float actionTimer; bool isInteracting; @@ -25,6 +26,7 @@ public class Tavernkeeper_controller : MonoBehaviour Rigidbody2D rigidbody2d; Animator animator; + //Grab an Object (IGrabable) w/ a specific or w/ last hand used (hand=null). public void grab(GameObject obj, string hand=null) { //Default hand @@ -52,6 +54,22 @@ public class Tavernkeeper_controller : MonoBehaviour } } + //Drop every objects in hands at tavernkeeper position. + public void emptyHands() + { + + foreach(string hand in hand_container.Keys) + { + if(hand_container[hand]!=null) + { + IGrabable grabable_obj = hand_container[hand].GetComponent(); + grabable_obj.drop(transform.position); + } + } + hand_container["left"]=null; + hand_container["right"]=null; + } + // Start is called before the first frame update void Start() {