@woosh/meep-engine 2.46.22 → 2.46.24

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/build/meep.cjs CHANGED
@@ -125808,7 +125808,765 @@ EngineHarness.getSingleton = function () {
125808
125808
  return singleton;
125809
125809
  };
125810
125810
 
125811
+ /**
125812
+ * Will try every child behaviour in order until one succeeds or if all fail - the selector behavior will fail too
125813
+ */
125814
+ class SelectorBehavior extends CompositeBehavior {
125815
+ constructor() {
125816
+ super();
125817
+
125818
+ /**
125819
+ *
125820
+ * @type {Behavior}
125821
+ * @private
125822
+ */
125823
+ this.__currentBehaviour = null;
125824
+
125825
+ /**
125826
+ *
125827
+ * @type {number}
125828
+ * @private
125829
+ */
125830
+ this.__currentBehaviourIndex = -1;
125831
+ }
125832
+
125833
+ /**
125834
+ *
125835
+ * @param {Behavior[]} children
125836
+ * @returns {SelectorBehavior}
125837
+ */
125838
+ static from(children) {
125839
+ assert.isArray(children, 'children');
125840
+
125841
+ const r = new SelectorBehavior();
125842
+
125843
+ children.forEach(r.addChild, r);
125844
+
125845
+ return r;
125846
+ }
125847
+
125848
+ tick(timeDelta) {
125849
+ if (this.__children.length < 1) {
125850
+ // no options, nothing chosen
125851
+ return BehaviorStatus.Failed;
125852
+ }
125853
+
125854
+ //keep going until a child behavior says it's running
125855
+ for (; ;) {
125856
+ const s = this.__currentBehaviour.tick(timeDelta);
125857
+
125858
+ //if child succeeds or keeps running, do the same
125859
+ if (s !== BehaviorStatus.Failed) {
125860
+ return s;
125861
+ }
125862
+
125863
+ this.__currentBehaviour.finalize();
125864
+
125865
+ //Continue search for a fallback until the last child
125866
+ const children = this.__children;
125867
+
125868
+ this.__currentBehaviourIndex++;
125869
+
125870
+ if (this.__currentBehaviourIndex >= children.length) {
125871
+ this.__currentBehaviour = null;
125872
+
125873
+ return BehaviorStatus.Failed;
125874
+ }
125875
+
125876
+ this.__currentBehaviour = children[this.__currentBehaviourIndex];
125877
+
125878
+ this.__currentBehaviour.initialize(this.context);
125879
+ }
125880
+ }
125881
+
125882
+ finalize() {
125883
+ if (this.__currentBehaviour !== null) {
125884
+ this.__currentBehaviour.finalize();
125885
+ this.__currentBehaviour = null;
125886
+ }
125887
+ }
125888
+
125889
+ initialize(context) {
125890
+ super.initialize(context);
125891
+
125892
+ const children = this.__children;
125893
+
125894
+ if (children.length > 0) {
125895
+
125896
+ this.__currentBehaviourIndex = 0;
125897
+ this.__currentBehaviour = children[0];
125898
+
125899
+ this.__currentBehaviour.initialize();
125900
+
125901
+ } else {
125902
+ // no children
125903
+
125904
+ this.__currentBehaviourIndex = -1;
125905
+ this.__currentBehaviour = null;
125906
+
125907
+ }
125908
+ }
125909
+ }
125910
+
125911
+ /**
125912
+ *
125913
+ * @enum {number}
125914
+ */
125915
+ const ParallelBehaviorPolicy = {
125916
+ RequireOne: 0,
125917
+ RequireAll: 1
125918
+ };
125919
+
125920
+ class ParallelBehavior extends CompositeBehavior {
125921
+ /**
125922
+ *
125923
+ * @param {ParallelBehaviorPolicy} successPolicy
125924
+ * @param {ParallelBehaviorPolicy} failurePolicy
125925
+ */
125926
+ constructor(successPolicy, failurePolicy) {
125927
+ super();
125928
+
125929
+ /**
125930
+ * @private
125931
+ * @type {ParallelBehaviorPolicy}
125932
+ */
125933
+ this.successPolicy = successPolicy;
125934
+
125935
+ /**
125936
+ * @private
125937
+ * @type {ParallelBehaviorPolicy}
125938
+ */
125939
+ this.failurePolicy = failurePolicy;
125940
+
125941
+ /**
125942
+ * @private
125943
+ * @type {BitSet}
125944
+ */
125945
+ this.activeSet = new BitSet();
125946
+
125947
+ /**
125948
+ * @private
125949
+ * @type {number}
125950
+ */
125951
+ this.successCount = 0;
125952
+ /**
125953
+ * @private
125954
+ * @type {number}
125955
+ */
125956
+ this.failureCount = 0;
125957
+ }
125958
+
125959
+ /**
125960
+ *
125961
+ * @return {ParallelBehaviorPolicy}
125962
+ */
125963
+ getSuccessPolicy() {
125964
+ return this.successPolicy;
125965
+ }
125966
+
125967
+ /**
125968
+ *
125969
+ * @param {ParallelBehaviorPolicy|number} v
125970
+ */
125971
+ setSuccessPolicy(v) {
125972
+ this.successPolicy = v;
125973
+ }
125974
+
125975
+ /**
125976
+ *
125977
+ * @return {ParallelBehaviorPolicy}
125978
+ */
125979
+ getFailurePolicy() {
125980
+ return this.failurePolicy;
125981
+ }
125982
+
125983
+ /**
125984
+ *
125985
+ * @param {ParallelBehaviorPolicy|number} v
125986
+ */
125987
+ setFailurePolicy(v) {
125988
+ this.failurePolicy = v;
125989
+ }
125990
+
125991
+ /**
125992
+ *
125993
+ * @param {number} timeDelta
125994
+ * @returns {BehaviorStatus|number}
125995
+ */
125996
+ tick(timeDelta) {
125997
+
125998
+ const activeSet = this.activeSet;
125999
+
126000
+ /**
126001
+ *
126002
+ * @type {Behavior[]}
126003
+ */
126004
+ const children = this.__children;
126005
+
126006
+ const numChildren = children.length;
126007
+
126008
+ let i;
126009
+
126010
+ for (i = 0; i < numChildren; i++) {
126011
+ if (!activeSet.get(i)) {
126012
+ continue;
126013
+ }
126014
+
126015
+ const child = children[i];
126016
+
126017
+ const status = child.tick(timeDelta);
126018
+
126019
+ if (status === BehaviorStatus.Succeeded) {
126020
+ activeSet.set(i, false);
126021
+
126022
+ this.successCount++;
126023
+
126024
+ child.finalize();
126025
+
126026
+ if (this.successPolicy === ParallelBehaviorPolicy.RequireOne) {
126027
+
126028
+ this.__finalizeActiveChildren();
126029
+
126030
+ return BehaviorStatus.Succeeded;
126031
+
126032
+ }
126033
+
126034
+ } else if (status === BehaviorStatus.Failed) {
126035
+ activeSet.set(i, false);
126036
+
126037
+ this.failureCount++;
126038
+
126039
+ child.finalize();
126040
+
126041
+ if (this.failurePolicy === ParallelBehaviorPolicy.RequireOne) {
126042
+
126043
+ this.__finalizeActiveChildren();
126044
+
126045
+ return BehaviorStatus.Failed;
126046
+
126047
+ } else if (this.successPolicy === ParallelBehaviorPolicy.RequireAll) {
126048
+
126049
+ this.__finalizeActiveChildren();
126050
+
126051
+ return BehaviorStatus.Failed;
126052
+
126053
+ }
126054
+
126055
+ }
126056
+ }
126057
+
126058
+ if (this.successCount === numChildren && this.successPolicy === ParallelBehaviorPolicy.RequireAll) {
126059
+
126060
+ return BehaviorStatus.Succeeded;
126061
+
126062
+ } else if (this.failureCount === numChildren && this.failurePolicy === ParallelBehaviorPolicy.RequireAll) {
126063
+
126064
+ return BehaviorStatus.Failed;
126065
+
126066
+ } else if ((this.failureCount + this.successCount) === numChildren) {
126067
+
126068
+ return BehaviorStatus.Failed;
126069
+
126070
+ } else {
126071
+
126072
+ return BehaviorStatus.Running;
126073
+
126074
+ }
126075
+ }
126076
+
126077
+ initialize(context) {
126078
+ this.successCount = 0;
126079
+ this.failureCount = 0;
126080
+
126081
+ const children = this.__children;
126082
+ const numChildren = children.length;
126083
+
126084
+ for (let i = 0; i < numChildren; i++) {
126085
+ const behavior = children[i];
126086
+
126087
+ behavior.initialize(context);
126088
+
126089
+ this.activeSet.set(i, true);
126090
+ }
126091
+
126092
+ super.initialize(context);
126093
+ }
126094
+
126095
+ /**
126096
+ *
126097
+ * @private
126098
+ */
126099
+ __finalizeActiveChildren() {
126100
+ const children = this.__children;
126101
+
126102
+ const activeSet = this.activeSet;
126103
+
126104
+ for (let i = activeSet.nextSetBit(0); i !== -1; i = activeSet.nextSetBit(i + 1)) {
126105
+ const behavior = children[i];
126106
+
126107
+ behavior.finalize();
126108
+ }
126109
+
126110
+ }
126111
+
126112
+
126113
+ finalize() {
126114
+ //finalize remaining active behaviours
126115
+
126116
+ this.__finalizeActiveChildren();
126117
+ }
126118
+
126119
+ /**
126120
+ *
126121
+ * @param {Behavior[]} elements
126122
+ * @param {ParallelBehaviorPolicy} success
126123
+ * @param {ParallelBehaviorPolicy} failure
126124
+ * @returns {ParallelBehavior}
126125
+ */
126126
+ static from(elements, success = ParallelBehaviorPolicy.RequireAll, failure = ParallelBehaviorPolicy.RequireOne) {
126127
+ const r = new ParallelBehavior(success, failure);
126128
+
126129
+ elements.forEach(e => r.addChild(e));
126130
+
126131
+ return r;
126132
+ }
126133
+ }
126134
+
126135
+ /**
126136
+ * @readonly
126137
+ * @type {boolean}
126138
+ */
126139
+ ParallelBehavior.prototype.isParallelBehavior = true;
126140
+
126141
+ /**
126142
+ * @readonly
126143
+ * @type {string}
126144
+ */
126145
+ ParallelBehavior.typeName = "ParallelBehavior";
126146
+
126147
+ /**
126148
+ *
126149
+ * @param {DataType} type
126150
+ * @returns {Vector1|ObservedBoolean}
126151
+ */
126152
+ function createValueByType(type) {
126153
+
126154
+ let value;
126155
+
126156
+ switch (type) {
126157
+ case DataType.Number:
126158
+ value = new Vector1(0);
126159
+ break;
126160
+ case DataType.Boolean:
126161
+ value = new ObservedBoolean(false);
126162
+ break;
126163
+ case DataType.String:
126164
+ value = new ObservedString("");
126165
+ break;
126166
+ default:
126167
+ throw new TypeError(`Unsupported data type '${type}'`);
126168
+ }
126169
+
126170
+ return value;
126171
+ }
126172
+
126173
+ class BlackboardValue {
126174
+ /**
126175
+ *
126176
+ * @param {DataType} type
126177
+ */
126178
+ constructor(type) {
126179
+ /**
126180
+ *
126181
+ * @type {number}
126182
+ */
126183
+ this.referenceCount = 0;
126184
+ /**
126185
+ *
126186
+ * @type {DataType}
126187
+ */
126188
+ this.type = type;
126189
+ this.value = createValueByType(type);
126190
+ }
126191
+ }
126192
+
126193
+ class AbstractBlackboard {
126194
+
126195
+ /**
126196
+ * Checks is the blackboard contains a certain value or not by name/type combination
126197
+ * @param {string} name
126198
+ * @param {DataType} type
126199
+ * @returns {boolean}
126200
+ */
126201
+ contains(name, type) {
126202
+ throw new Error('Not implemented');
126203
+ }
126204
+
126205
+ /**
126206
+ * Produces a list of all held keys
126207
+ * @returns {string[]}
126208
+ */
126209
+ getKeys() {
126210
+ throw new Error('Not implemented');
126211
+ }
126212
+
126213
+ /**
126214
+ * @template T
126215
+ * @param {string} name
126216
+ * @param {DataType} type
126217
+ * @param {string|number|boolean} [initialValue]
126218
+ * @returns {T}
126219
+ */
126220
+ acquire(name, type, initialValue) {
126221
+ throw new Error('Not implemented');
126222
+ }
126223
+
126224
+ /**
126225
+ * Release reference to a value
126226
+ * @param {string} name
126227
+ * @returns {void}
126228
+ */
126229
+ release(name) {
126230
+ throw new Error('Not implemented');
126231
+ }
126232
+
126233
+ /**
126234
+ *
126235
+ * @param {string} name
126236
+ * @param {boolean} [initialValue=false]
126237
+ * @returns {ObservedBoolean}
126238
+ */
126239
+ acquireBoolean(name, initialValue = false) {
126240
+ assert.typeOf(initialValue, 'boolean', 'initialValue');
126241
+
126242
+ return this.acquire(name, DataType.Boolean, initialValue);
126243
+ }
126244
+
126245
+ /**
126246
+ *
126247
+ * @param {string} name
126248
+ * @param {number} [initialValue=0]
126249
+ * @returns {Vector1}
126250
+ */
126251
+ acquireNumber(name, initialValue = 0) {
126252
+ assert.isNumber(initialValue, 'initialValue');
126253
+
126254
+ return this.acquire(name, DataType.Number, initialValue);
126255
+ }
126256
+
126257
+ /**
126258
+ *
126259
+ * @param {string} name
126260
+ * @param {number} [value=1]
126261
+ * @returns {void}
126262
+ */
126263
+ incrementNumber(name, value = 1) {
126264
+ const vector1 = this.acquireNumber(name);
126265
+
126266
+ vector1._add(value);
126267
+
126268
+ this.release(name);
126269
+ }
126270
+
126271
+ /**
126272
+ *
126273
+ * @param {string} name
126274
+ * @param {string} [initialValue]
126275
+ * @returns {ObservedString}
126276
+ */
126277
+ acquireString(name, initialValue = '') {
126278
+ assert.typeOf(initialValue, 'string', 'initialValue');
126279
+
126280
+ return this.acquire(name, DataType.String, initialValue);
126281
+ }
126282
+ }
126283
+
126284
+ /**
126285
+ * @template T
126286
+ * @param {T} value
126287
+ * @return {DataType}
126288
+ */
126289
+ function computeDataTypeFromValue(value) {
126290
+ const t = typeof value;
126291
+
126292
+ switch (t) {
126293
+ case "number":
126294
+ return DataType.Number;
126295
+ case "string":
126296
+ return DataType.String;
126297
+ case "boolean":
126298
+ return DataType.Boolean;
126299
+ default:
126300
+ if (Array.isArray(value)) {
126301
+ return DataType.Array;
126302
+ }
126303
+
126304
+ throw new Error(`Unsupported value type for value '${value}'`);
126305
+ }
126306
+ }
126307
+
126308
+ /**
126309
+ * Given a blackboard, returns a Proxy instance, exposing blackboard attributes as plain JSON
126310
+ * Useful for connecting blackboard to systems that are not explicitly designed to work with a blackboard
126311
+ * @param {AbstractBlackboard} blackboard
126312
+ * @returns {Proxy}
126313
+ */
126314
+ function make_blackboard_proxy(blackboard) {
126315
+ return new Proxy(blackboard, {
126316
+ /**
126317
+ *
126318
+ * @param target
126319
+ * @param {string} p
126320
+ * @param receiver
126321
+ * @returns {*}
126322
+ */
126323
+ get(target, p, receiver) {
126324
+
126325
+ if (!target.contains(p, DataType.Any)) {
126326
+ // property not found
126327
+ return undefined;
126328
+ }
126329
+
126330
+ const value_container = target.acquire(p, DataType.Any);
126331
+
126332
+ return value_container.getValue();
126333
+ },
126334
+ set(target, p, value, receiver) {
126335
+
126336
+ const dataType = computeDataTypeFromValue(value);
126337
+
126338
+ const value_container = target.acquire(p, dataType, value);
126339
+
126340
+ value_container.set(value);
126341
+
126342
+ // succeeded
126343
+ return true;
126344
+ },
126345
+ ownKeys(target) {
126346
+
126347
+ return target.getKeys();
126348
+
126349
+ }
126350
+ })
126351
+ }
126352
+
126353
+ class Blackboard extends AbstractBlackboard {
126354
+ constructor() {
126355
+ super();
126356
+
126357
+ this.on = {
126358
+ /**
126359
+ * Property added
126360
+ * @type {Signal}
126361
+ */
126362
+ added: new Signal()
126363
+ };
126364
+
126365
+ /**
126366
+ *
126367
+ * @type {Object<BlackboardValue>}
126368
+ */
126369
+ this.data = {};
126370
+
126371
+ /**
126372
+ * @private
126373
+ */
126374
+ this.proxy = make_blackboard_proxy(this);
126375
+ }
126376
+
126377
+ getKeys() {
126378
+ return Reflect.ownKeys(this.data);
126379
+ }
126380
+
126381
+ /**
126382
+ *
126383
+ * @returns {Object}
126384
+ */
126385
+ getValueProxy() {
126386
+ return this.proxy;
126387
+ }
126388
+
126389
+ /**
126390
+ *
126391
+ * @param {string} name
126392
+ * @param {DataType} type
126393
+ * @return {boolean} true only if entry exists and matches type, or if supplied parameter type is set to "Any"
126394
+ */
126395
+ contains(name, type) {
126396
+ assert.isString(name, 'name');
126397
+ assert.enum(type, DataType, 'type');
126398
+
126399
+ const datum = this.data[name];
126400
+
126401
+ if (datum === undefined) {
126402
+ return false;
126403
+ }
126404
+
126405
+ if (type !== DataType.Any && datum.type !== type) {
126406
+ return false;
126407
+ }
126408
+
126409
+ return true;
126410
+ }
126411
+
126412
+ /**
126413
+ *
126414
+ * @param {function(name:string, value:*, type: DataType)} visitor
126415
+ * @param {*} [thisArg]
126416
+ */
126417
+ traverse(visitor, thisArg) {
126418
+ assert.typeOf(visitor, 'function', 'visitor');
126419
+
126420
+ for (let name in this.data) {
126421
+ if (!this.data.hasOwnProperty(name)) {
126422
+ continue;
126423
+ }
126424
+
126425
+ const blackboardValue = this.data[name];
126426
+
126427
+ visitor.call(thisArg, name, blackboardValue.value, blackboardValue.type);
126428
+ }
126429
+ }
126430
+
126431
+ /**
126432
+ *
126433
+ * @param {function(name:string, value:*, type: DataType)} visitor
126434
+ * @param {RegExp} pattern
126435
+ */
126436
+ traverseWithPattern(pattern, visitor) {
126437
+ assert.defined(pattern, 'pattern');
126438
+ assert.ok(pattern instanceof RegExp, 'pattern is not a RegExp');
126439
+
126440
+ this.traverse(function (name, value, type) {
126441
+ if (pattern.test(name)) {
126442
+ visitor(name, value, type);
126443
+ }
126444
+ });
126445
+ }
126446
+
126447
+ /**
126448
+ * @template T
126449
+ * @param {string} name
126450
+ * @param {DataType} type
126451
+ * @param {number|boolean} [initialValue]
126452
+ * @returns {T}
126453
+ */
126454
+ acquire(name, type, initialValue) {
126455
+ assert.isString(name, 'name');
126456
+
126457
+ let datum;
126458
+
126459
+ if (this.data.hasOwnProperty(name)) {
126460
+ // property exists
126461
+ datum = this.data[name];
126462
+
126463
+ if (type !== DataType.Any && datum.type !== type) {
126464
+ throw new TypeError(`Value '${name}' exists, but is type(='${datum.type}'), expected type '${type}'`);
126465
+ }
126466
+
126467
+ } else {
126468
+ //doesn't exist - create it
126469
+ datum = new BlackboardValue(type);
126470
+
126471
+ const concrete_value = datum.value;
126472
+
126473
+ if (initialValue !== undefined) {
126474
+ concrete_value.set(initialValue);
126475
+ }
126476
+
126477
+ this.data[name] = datum;
126478
+
126479
+ this.on.added.send4(name, concrete_value.getValue(), type, this);
126480
+ }
126481
+
126482
+ datum.referenceCount++;
126483
+
126484
+ return datum.value;
126485
+ }
126486
+
126487
+ /**
126488
+ *
126489
+ * @param {string} name
126490
+ */
126491
+ release(name) {
126492
+ assert.typeOf(name, 'string', 'name');
126493
+ assert.ok(this.data.hasOwnProperty(name), `Attempting to release a value '${name}' that doesn't exist`);
126494
+
126495
+ const datum = this.data[name];
126496
+
126497
+ datum.referenceCount--;
126498
+
126499
+ //todo cleanup value from blackboard if no one is using it
126500
+ }
126501
+
126502
+ /**
126503
+ * Drop all values
126504
+ */
126505
+ reset() {
126506
+ this.data = {};
126507
+ }
126508
+
126509
+ /**
126510
+ *
126511
+ * @param {Blackboard} other
126512
+ */
126513
+ copy(other) {
126514
+ this.reset();
126515
+
126516
+ other.traverse((name, value, type) => {
126517
+ this.acquire(name, type, value.getValue());
126518
+ });
126519
+ }
126520
+
126521
+ /**
126522
+ *
126523
+ * @returns {Blackboard}
126524
+ */
126525
+ clone() {
126526
+ const r = new Blackboard();
126527
+
126528
+ r.copy(this);
126529
+
126530
+ return r;
126531
+ }
126532
+
126533
+
126534
+ toJSON() {
126535
+ const result = {};
126536
+
126537
+ this.traverse((name, value, type) => {
126538
+ result[name] = value.toJSON();
126539
+ });
126540
+
126541
+ return result;
126542
+ }
126543
+
126544
+ fromJSON(json) {
126545
+ this.reset();
126546
+
126547
+ for (let propName in json) {
126548
+ const value = json[propName];
126549
+
126550
+ const value_type = typeof value;
126551
+
126552
+ if (value_type === 'number') {
126553
+ this.acquireNumber(propName, value).set(value);
126554
+ } else if (value_type === 'boolean') {
126555
+ this.acquireBoolean(propName, value).set(value);
126556
+ } else if (value_type === 'string') {
126557
+ this.acquireString(propName, value).set(value);
126558
+ }
126559
+
126560
+ }
126561
+ }
126562
+ }
126563
+
126564
+ Blackboard.typeName = 'Blackboard';
126565
+
125811
126566
  exports.AmbientOcclusionPostProcessEffect = AmbientOcclusionPostProcessEffect;
126567
+ exports.Behavior = Behavior;
126568
+ exports.BehaviorStatus = BehaviorStatus;
126569
+ exports.Blackboard = Blackboard;
125812
126570
  exports.Cache = Cache;
125813
126571
  exports.EngineConfiguration = EngineConfiguration;
125814
126572
  exports.EngineHarness = EngineHarness;
@@ -125816,11 +126574,16 @@ exports.EntityBuilderFlags = EntityBuilderFlags;
125816
126574
  exports.ForwardPlusRenderingPlugin = ForwardPlusRenderingPlugin;
125817
126575
  exports.HashMap = HashMap;
125818
126576
  exports.Light = Light;
126577
+ exports.ParallelBehavior = ParallelBehavior;
126578
+ exports.ParallelBehaviorPolicy = ParallelBehaviorPolicy;
125819
126579
  exports.SGMesh = SGMesh;
125820
126580
  exports.SGMeshSystem = SGMeshSystem;
126581
+ exports.SelectorBehavior = SelectorBehavior;
126582
+ exports.SequenceBehavior = SequenceBehavior;
125821
126583
  exports.ShadedGeometry = ShadedGeometry;
125822
126584
  exports.ShadedGeometrySystem = ShadedGeometrySystem;
125823
126585
  exports.Signal = Signal;
126586
+ exports.SignalBinding = SignalBinding;
125824
126587
  exports.Transform = Transform;
125825
126588
  exports.dispatchViaProxy = dispatchViaProxy;
125826
126589
  exports.findSignalHandlerIndexByHandle = findSignalHandlerIndexByHandle;