summaryrefslogtreecommitdiff
path: root/Client/Assets/Behavior Designer/Runtime/Conditionals/RandomProbability.cs
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-01-27 16:15:06 +0800
committerchai <chaifix@163.com>2021-01-27 16:15:06 +0800
commit97da432c35b8c7aaf9dd2c39e2aa4b1f55f36065 (patch)
treed9b1db5908a3a030c529e230386fe01062923b09 /Client/Assets/Behavior Designer/Runtime/Conditionals/RandomProbability.cs
parent1fe4ffba72f56ccc6a89d1896142425c666887d4 (diff)
+behaviour designer
Diffstat (limited to 'Client/Assets/Behavior Designer/Runtime/Conditionals/RandomProbability.cs')
-rw-r--r--Client/Assets/Behavior Designer/Runtime/Conditionals/RandomProbability.cs46
1 files changed, 46 insertions, 0 deletions
diff --git a/Client/Assets/Behavior Designer/Runtime/Conditionals/RandomProbability.cs b/Client/Assets/Behavior Designer/Runtime/Conditionals/RandomProbability.cs
new file mode 100644
index 00000000..a4c48c6f
--- /dev/null
+++ b/Client/Assets/Behavior Designer/Runtime/Conditionals/RandomProbability.cs
@@ -0,0 +1,46 @@
+using UnityEngine;
+
+namespace BehaviorDesigner.Runtime.Tasks
+{
+ [TaskDescription("The random probability task will return success when the random probability is above the succeed probability. It will otherwise return failure.")]
+ [HelpURL("http://www.opsive.com/assets/BehaviorDesigner/documentation.php?id=33")]
+ public class RandomProbability : Conditional
+ {
+ [Tooltip("The chance that the task will return success")]
+ public SharedFloat successProbability = 0.5f;
+ [Tooltip("Seed the random number generator to make things easier to debug")]
+ public SharedInt seed;
+ [Tooltip("Do we want to use the seed?")]
+ public SharedBool useSeed;
+
+ private System.Random random;
+
+ public override void OnAwake()
+ {
+ // If specified, use the seed provided.
+ if (useSeed.Value) {
+ random = new System.Random(seed.Value);
+ } else {
+ random = new System.Random();
+ }
+ }
+
+ public override TaskStatus OnUpdate()
+ {
+ // Return success if random value is less than the success probability. Otherwise return failure.
+ float randomValue = (float)random.NextDouble();
+ if (randomValue < successProbability.Value) {
+ return TaskStatus.Success;
+ }
+ return TaskStatus.Failure;
+ }
+
+ public override void OnReset()
+ {
+ // Reset the public properties back to their original values
+ successProbability = 0.5f;
+ seed = 0;
+ useSeed = false;
+ }
+ }
+} \ No newline at end of file