@woosh/meep-engine 2.46.22 → 2.46.23

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