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