Working Event system & SoftObstacle

This commit is contained in:
Antoine H 2021-01-17 17:56:17 +01:00
parent 08f623a1b1
commit a940cb64ba
8 changed files with 279 additions and 68 deletions

View file

@ -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

View file

@ -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}

View file

@ -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);
}
}

View file

@ -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()
// {

View file

@ -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;
}
}

View file

@ -0,0 +1,64 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Define the behavior of a soft obstacle
//TODO : Effect on clients ? (Lower Tips/Repu ?)
[RequireComponent(typeof(Collider2D))]
public class SoftObstacle : MonoBehaviour
{
[SerializeField]
float effectChance = 100.0f; //Probabily of fall on entering.
[SerializeField]
float mvtSlow = 1.0f; //Scale of movement slowdown (1.0f = no change).
[SerializeField]
float lifeTime = 3.0f; //Time to consume currentMug
float lifeTimer;
Tavernkeeper_controller player;
// Start is called before the first frame update
void Start()
{
lifeTimer=lifeTime;
}
// Update is called once per frame
void Update()
{
lifeTimer -= Time.deltaTime;
if(lifeTimer<0)
{
Destroy(gameObject);
}
}
//TODO : Trigger falling animation on tavernkeeper
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag=="Player")
{
player=other.GetComponent<Tavernkeeper_controller>();
player.mvt_speed=player.mvt_speed/mvtSlow; //Slow movement speed
if(Random.Range(0.0f, 99.9f)<effectChance)
{
Debug.Log("Woops");
player.emptyHands();
}
}
}
void OnTriggerExit2D(Collider2D other)
{
if(other.tag=="Player")
{
player.mvt_speed=player.mvt_speed*mvtSlow; //Restore movement speed
}
}
void OnDestroy()
{
EventManager.Instance.destroyEvent(gameObject);
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: da02c62b238bfbd48b8fc1f714a253e0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -10,7 +10,8 @@ public class Tavernkeeper_controller : MonoBehaviour
public float action_dist = 1.5f; //Action distance
public float action_cd = 0.5f; //Action cooldown
IDictionary<string, GameObject> hand_container; //Objects in hand
//TODO : GameObject => IGrabable
Dictionary<string, GameObject> 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<IGrabable>();
grabable_obj.drop(transform.position);
}
}
hand_container["left"]=null;
hand_container["right"]=null;
}
// Start is called before the first frame update
void Start()
{