ai_behavior/behavior.rs
1use input::Button;
2
3/// Describes a behavior.
4///
5/// This is used for more complex event logic.
6/// Can also be used for game AI.
7#[derive(Clone, Deserialize, Serialize, PartialEq)]
8pub enum Behavior<A> {
9 /// Wait for a button to be pressed.
10 ///
11 /// Returns `Success` when the button is pressed,
12 /// otherwise it returns `Running`.
13 WaitForPressed(Button),
14 /// Wait for a button to be released.
15 ///
16 /// Returns `Success` when the button is released,
17 /// otherwise it returns `Running`.
18 WaitForReleased(Button),
19 /// Waits an amount of time before continuing.
20 ///
21 /// f64: Time in seconds
22 Wait(f64),
23 /// Wait forever.
24 WaitForever,
25 /// A high level description of an action.
26 Action(A),
27 /// Converts `Success` into `Failure` and vice versa.
28 Fail(Box<Behavior<A>>),
29 /// Ignores failures and returns `Success`.
30 AlwaysSucceed(Box<Behavior<A>>),
31 /// Runs behaviors one by one until a behavior succeeds.
32 ///
33 /// If a behavior fails it will try the next one.
34 /// Fails if the last behavior fails.
35 /// Can be thought of as a short-circuited logical OR gate.
36 Select(Vec<Behavior<A>>),
37 /// `If(condition, success, failure)`
38 If(Box<Behavior<A>>, Box<Behavior<A>>, Box<Behavior<A>>),
39 /// Runs behaviors one by one until all succeeded.
40 ///
41 /// The sequence fails if a behavior fails.
42 /// The sequence succeeds if all the behavior succeeds.
43 /// Can be thought of as a short-circuited logical AND gate.
44 Sequence(Vec<Behavior<A>>),
45 /// Loops while conditional behavior is running.
46 ///
47 /// Succeeds if the conditional behavior succeeds.
48 /// Fails if the conditional behavior fails,
49 /// or if any behavior in the loop body fails.
50 While(Box<Behavior<A>>, Vec<Behavior<A>>),
51 /// Runs all behaviors in parallel until all succeeded.
52 ///
53 /// Succeeds if all behaviors succeed.
54 /// Fails is any behavior fails.
55 WhenAll(Vec<Behavior<A>>),
56 /// Runs all behaviors in parallel until one succeeds.
57 ///
58 /// Succeeds if one behavior succeeds.
59 /// Fails if all behaviors failed.
60 WhenAny(Vec<Behavior<A>>),
61 /// Runs all behaviors in parallel until all succeeds in sequence.
62 ///
63 /// Succeeds if all behaviors succeed, but only if succeeding in sequence.
64 /// Fails if one behavior fails.
65 After(Vec<Behavior<A>>),
66}