@woosh/meep-engine 2.46.25 → 2.46.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { Behavior } from "../Behavior.js";
|
|
2
|
+
import { SucceedingBehavior } from "../primitive/SucceedingBehavior.js";
|
|
3
|
+
import { assert } from "../../../../core/assert.js";
|
|
4
|
+
import { BehaviorStatus } from "../BehaviorStatus.js";
|
|
5
|
+
|
|
6
|
+
const DEFAULT = new SucceedingBehavior();
|
|
7
|
+
|
|
8
|
+
export class BranchBehavior extends Behavior {
|
|
9
|
+
#successBranch = DEFAULT
|
|
10
|
+
#failureBranch = DEFAULT
|
|
11
|
+
#condition = DEFAULT
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
*
|
|
15
|
+
* @type {Behavior|null}
|
|
16
|
+
*/
|
|
17
|
+
#current = null;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
*
|
|
21
|
+
* @param {Behavior} condition
|
|
22
|
+
* @param {Behavior} successBranch
|
|
23
|
+
* @param {Behavior} failureBranch
|
|
24
|
+
*/
|
|
25
|
+
static from(
|
|
26
|
+
condition,
|
|
27
|
+
successBranch = DEFAULT,
|
|
28
|
+
failureBranch = DEFAULT
|
|
29
|
+
) {
|
|
30
|
+
assert.defined(condition, 'condition');
|
|
31
|
+
assert.notNull(condition, 'condition');
|
|
32
|
+
assert.equal(condition.isBehavior, true, 'condition.isBehavior !== true');
|
|
33
|
+
|
|
34
|
+
assert.defined(successBranch, 'successBranch');
|
|
35
|
+
assert.notNull(successBranch, 'successBranch');
|
|
36
|
+
assert.equal(successBranch.isBehavior, true, 'successBranch.isBehavior !== true');
|
|
37
|
+
|
|
38
|
+
assert.defined(failureBranch, 'failureBranch');
|
|
39
|
+
assert.notNull(failureBranch, 'failureBranch');
|
|
40
|
+
assert.equal(failureBranch.isBehavior, true, 'failureBranch.isBehavior !== true');
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
const r = new BranchBehavior();
|
|
44
|
+
|
|
45
|
+
r.#condition = condition;
|
|
46
|
+
r.#successBranch = successBranch;
|
|
47
|
+
r.#failureBranch = failureBranch;
|
|
48
|
+
|
|
49
|
+
return r;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
initialize(context) {
|
|
54
|
+
super.initialize(context);
|
|
55
|
+
|
|
56
|
+
this.#current = this.#condition;
|
|
57
|
+
this.#current.initialize(context);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
tick(timeDelta) {
|
|
61
|
+
const s = this.#current.tick(timeDelta);
|
|
62
|
+
|
|
63
|
+
if (s !== BehaviorStatus.Succeeded && s !== BehaviorStatus.Failed) {
|
|
64
|
+
|
|
65
|
+
// current behavior was not resolved, continue
|
|
66
|
+
return s;
|
|
67
|
+
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (this.#current !== this.#condition) {
|
|
71
|
+
// underlying branch was resolved
|
|
72
|
+
return s;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// condition was resolved, move onto a branch
|
|
76
|
+
this.#current.finalize();
|
|
77
|
+
|
|
78
|
+
if (s === BehaviorStatus.Succeeded) {
|
|
79
|
+
this.#current = this.#successBranch;
|
|
80
|
+
} else {
|
|
81
|
+
this.#current = this.#failureBranch;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
this.#current.initialize(this.context);
|
|
85
|
+
|
|
86
|
+
return this.#current.tick(timeDelta);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
finalize() {
|
|
90
|
+
super.finalize();
|
|
91
|
+
|
|
92
|
+
if (this.#current !== null) {
|
|
93
|
+
this.#current.finalize()
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
}
|