From 97da432c35b8c7aaf9dd2c39e2aa4b1f55f36065 Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 27 Jan 2021 16:15:06 +0800 Subject: +behaviour designer --- .../Runtime/Conditionals/HasReceivedEvent.cs | 102 +++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 Client/Assets/Behavior Designer/Runtime/Conditionals/HasReceivedEvent.cs (limited to 'Client/Assets/Behavior Designer/Runtime/Conditionals/HasReceivedEvent.cs') diff --git a/Client/Assets/Behavior Designer/Runtime/Conditionals/HasReceivedEvent.cs b/Client/Assets/Behavior Designer/Runtime/Conditionals/HasReceivedEvent.cs new file mode 100644 index 00000000..b9c16252 --- /dev/null +++ b/Client/Assets/Behavior Designer/Runtime/Conditionals/HasReceivedEvent.cs @@ -0,0 +1,102 @@ +using UnityEngine; + +namespace BehaviorDesigner.Runtime.Tasks +{ + [TaskDescription("Returns success as soon as the event specified by eventName has been received.")] + [HelpURL("http://www.opsive.com/assets/BehaviorDesigner/documentation.php?id=123")] + [TaskIcon("{SkinColor}HasReceivedEventIcon.png")] + public class HasReceivedEvent : Conditional + { + [Tooltip("The name of the event to receive")] + public SharedString eventName = ""; + [Tooltip("Optionally store the first sent argument")] + [SharedRequired] + public SharedVariable storedValue1; + [Tooltip("Optionally store the second sent argument")] + [SharedRequired] + public SharedVariable storedValue2; + [Tooltip("Optionally store the third sent argument")] + [SharedRequired] + public SharedVariable storedValue3; + + private bool eventReceived = false; + + public override void OnAwake() + { + // Let the behavior tree know that we are interested in receiving the event specified + Owner.RegisterEvent(eventName.Value, ReceivedEvent); + Owner.RegisterEvent(eventName.Value, ReceivedEvent); + Owner.RegisterEvent(eventName.Value, ReceivedEvent); + Owner.RegisterEvent(eventName.Value, ReceivedEvent); + } + + public override TaskStatus OnUpdate() + { + return eventReceived ? TaskStatus.Success : TaskStatus.Failure; + } + + public override void OnEnd() + { + eventReceived = false; + } + + private void ReceivedEvent() + { + eventReceived = true; + } + + private void ReceivedEvent(object arg1) + { + ReceivedEvent(); + + if (storedValue1 != null && !storedValue1.IsNone) { + storedValue1.SetValue(arg1); + } + } + + private void ReceivedEvent(object arg1, object arg2) + { + ReceivedEvent(); + + if (storedValue1 != null && !storedValue1.IsNone) { + storedValue1.SetValue(arg1); + } + + if (storedValue2 != null && !storedValue2.IsNone) { + storedValue2.SetValue(arg2); + } + } + + private void ReceivedEvent(object arg1, object arg2, object arg3) + { + ReceivedEvent(); + + if (storedValue1 != null && !storedValue1.IsNone) { + storedValue1.SetValue(arg1); + } + + if (storedValue2 != null && !storedValue2.IsNone) { + storedValue2.SetValue(arg2); + } + + if (storedValue3 != null && !storedValue3.IsNone) { + storedValue3.SetValue(arg3); + } + } + + public override void OnBehaviorComplete() + { + // Stop receiving the event when the behavior tree is complete + Owner.UnregisterEvent(eventName.Value, ReceivedEvent); + Owner.UnregisterEvent(eventName.Value, ReceivedEvent); + Owner.UnregisterEvent(eventName.Value, ReceivedEvent); + Owner.UnregisterEvent(eventName.Value, ReceivedEvent); + } + + public override void OnReset() + { + // Reset the properties back to their original values + eventName = ""; + } + } +} \ No newline at end of file -- cgit v1.1-26-g67d0