@galacean/engine-core 2.0.0-alpha.45 → 2.0.0-alpha.46

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/dist/module.js CHANGED
@@ -5018,10 +5018,25 @@ registerDefaultCloneMode(Component, CloneMode.Remap);
5018
5018
  };
5019
5019
  /**
5020
5020
  * @internal
5021
- */ ComponentsDependencies._removeCheck = function _removeCheck(entity, type) {
5021
+ */ ComponentsDependencies._hasDependencies = function _hasDependencies(type) {
5022
+ while(type !== Component){
5023
+ if (ComponentsDependencies._dependenciesMap.has(type)) {
5024
+ return true;
5025
+ }
5026
+ type = Object.getPrototypeOf(type);
5027
+ }
5028
+ return false;
5029
+ };
5030
+ /**
5031
+ * @internal
5032
+ */ ComponentsDependencies._removeCheck = function _removeCheck(entity, type, replace) {
5022
5033
  var components = entity._components;
5023
5034
  var n = components.length;
5024
5035
  while(type !== Component){
5036
+ // The replacement still satisfies this type and all of its base types
5037
+ if (replace && (replace === type || _instanceof(replace.prototype, type))) {
5038
+ return;
5039
+ }
5025
5040
  var count = 0;
5026
5041
  for(var i = 0; i < n; i++){
5027
5042
  if (_instanceof(components[i], type) && ++count > 1) return;
@@ -5138,6 +5153,7 @@ registerDefaultCloneMode(UpdateFlag, CloneMode.Ignore);
5138
5153
 
5139
5154
  /**
5140
5155
  * Used to implement transformation related functions.
5156
+ * @remarks A `Transform` subclass must not declare component dependencies.
5141
5157
  */ var Transform = /*#__PURE__*/ function(Component) {
5142
5158
  _inherits(Transform, Component);
5143
5159
  function Transform(entity) {
@@ -5167,6 +5183,13 @@ registerDefaultCloneMode(UpdateFlag, CloneMode.Ignore);
5167
5183
  return _this;
5168
5184
  }
5169
5185
  var _proto = Transform.prototype;
5186
+ _proto.destroy = function destroy() {
5187
+ var entity = this._entity;
5188
+ if (entity.transform === this && !entity.destroyed) {
5189
+ throw "Transform cannot be destroyed directly; replace it by adding another Transform-compatible component";
5190
+ }
5191
+ Component.prototype.destroy.call(this);
5192
+ };
5170
5193
  /**
5171
5194
  * Set local position by X, Y, Z value.
5172
5195
  * @param x - X coordinate
@@ -5305,22 +5328,35 @@ registerDefaultCloneMode(UpdateFlag, CloneMode.Ignore);
5305
5328
  return isInvert;
5306
5329
  };
5307
5330
  /**
5331
+ * @internal
5332
+ */ _proto._copyLocalPoseFrom = function _copyLocalPoseFrom(source) {
5333
+ var _this = this, position = _this._position, rotation = _this._rotation, rotationQuaternion = _this._rotationQuaternion, scale = _this._scale;
5334
+ //@ts-ignore
5335
+ position._onValueChanged = rotation._onValueChanged = scale._onValueChanged = null;
5336
+ //@ts-ignore
5337
+ rotationQuaternion._onValueChanged = null;
5338
+ position.copyFrom(source._position);
5339
+ scale.copyFrom(source._scale);
5340
+ rotation.copyFrom(source._rotation);
5341
+ rotationQuaternion.copyFrom(source._rotationQuaternion);
5342
+ //@ts-ignore
5343
+ position._onValueChanged = this._onPositionChanged;
5344
+ //@ts-ignore
5345
+ rotation._onValueChanged = this._onRotationChanged;
5346
+ //@ts-ignore
5347
+ rotationQuaternion._onValueChanged = this._onRotationQuaternionChanged;
5348
+ //@ts-ignore
5349
+ scale._onValueChanged = this._onScaleChanged;
5350
+ this._localUniformScaling = source._localUniformScaling;
5351
+ var rotationDirtyBits = 1 | 2;
5352
+ this._dirtyFlag = this._dirtyFlag & ~rotationDirtyBits | source._dirtyFlag & rotationDirtyBits;
5353
+ this._setDirtyFlagTrue(64);
5354
+ this._updateAllWorldFlag(444);
5355
+ };
5356
+ /**
5308
5357
  * @inheritdoc
5309
5358
  */ _proto._onClone = function _onClone(target) {
5310
- var position = target._position, rotation = target._rotation, scale = target._scale;
5311
- // @ts-ignore
5312
- position._onValueChanged = rotation._onValueChanged = scale._onValueChanged = null;
5313
- position.copyFrom(this.position);
5314
- rotation.copyFrom(this.rotation);
5315
- scale.copyFrom(this.scale);
5316
- // @ts-ignore
5317
- position._onValueChanged = target._onPositionChanged;
5318
- // @ts-ignore
5319
- rotation._onValueChanged = target._onRotationChanged;
5320
- // @ts-ignore
5321
- scale._onValueChanged = target._onScaleChanged;
5322
- // When cloning, other components may obtain properties such as `rotationQuaternion` in the constructor, local related dirty flags need to be corrected
5323
- target._setDirtyFlagTrue(2 | 64);
5359
+ target._copyLocalPoseFrom(this);
5324
5360
  };
5325
5361
  _proto._onLocalMatrixChanging = function _onLocalMatrixChanging() {
5326
5362
  this._setDirtyFlagFalse(64);
@@ -6171,10 +6207,27 @@ function _construct(Parent, args, Class) {
6171
6207
  _this = EngineObject.call(this, engine) || this, /** The layer the entity belongs to. */ _this.layer = Layer.Layer0, /** @internal */ _this._isActiveInHierarchy = false, /** @internal */ _this._isActiveInScene = false, /** @internal */ _this._components = [], /** @internal */ _this._scripts = new DisorderedArray(), /** @internal */ _this._scriptsVersion = 0, /** @internal */ _this._children = [], /** @internal */ _this._isRoot = false, /** @internal */ _this._isActive = true, /** @internal */ _this._siblingIndex = -1, /** @internal */ _this._isTemplate = false, /** @internal */ _this._updateFlagManager = new UpdateFlagManager(), _this._parent = null, //--------------------------------------------------------------deprecated----------------------------------------------------------------
6172
6208
  _this._invModelMatrix = new Matrix();
6173
6209
  _this.name = name != null ? name : "Entity";
6174
- for(var i = 0, n = components.length; i < n; i++){
6175
- _this.addComponent(components[i]);
6210
+ var transformType = Transform;
6211
+ var n = components.length;
6212
+ for(var i = n - 1; i >= 0; i--){
6213
+ var componentType = components[i];
6214
+ if (Entity._isTransformType(componentType)) {
6215
+ transformType = componentType;
6216
+ break;
6217
+ }
6218
+ }
6219
+ Entity._checkTransformDependencies(transformType);
6220
+ var transform = new transformType(_this);
6221
+ // Constructors may add components, but Transform always owns slot zero
6222
+ _this._components.unshift(transform);
6223
+ _this._transform = transform;
6224
+ transform._setActive(true, ActiveChangeFlag.All);
6225
+ for(var i1 = 0; i1 < n; i1++){
6226
+ var componentType1 = components[i1];
6227
+ if (!Entity._isTransformType(componentType1)) {
6228
+ _this.addComponent(componentType1);
6229
+ }
6176
6230
  }
6177
- !_this._transform && _this.addComponent(Transform);
6178
6231
  _this._inverseWorldMatFlag = _this.registerWorldChangeFlag();
6179
6232
  return _this;
6180
6233
  }
@@ -6188,13 +6241,21 @@ function _construct(Parent, args, Class) {
6188
6241
  for(var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++){
6189
6242
  args[_key - 1] = arguments[_key];
6190
6243
  }
6191
- ComponentsDependencies._addCheck(this, type);
6244
+ var isTransform = Entity._isTransformType(type);
6245
+ if (isTransform) {
6246
+ Entity._checkTransformDependencies(type);
6247
+ ComponentsDependencies._removeCheck(this, this._transform.constructor, type);
6248
+ } else {
6249
+ ComponentsDependencies._addCheck(this, type);
6250
+ }
6192
6251
  var component = _construct(type, [].concat([
6193
6252
  this
6194
6253
  ], args));
6195
- this._components.push(component);
6196
- // @todo: temporary solution
6197
- if (_instanceof(component, Transform)) this._setTransform(component);
6254
+ if (isTransform) {
6255
+ this._replaceTransform(component);
6256
+ } else {
6257
+ this._components.push(component);
6258
+ }
6198
6259
  component._setActive(true, ActiveChangeFlag.All);
6199
6260
  return component;
6200
6261
  };
@@ -6342,6 +6403,7 @@ function _construct(Parent, args, Class) {
6342
6403
  _proto._createCloneEntity = function _createCloneEntity(cloneMap) {
6343
6404
  var componentConstructors = Entity._tempComponentConstructors;
6344
6405
  var components = this._components;
6406
+ componentConstructors.length = components.length;
6345
6407
  for(var i = 0, n = components.length; i < n; i++){
6346
6408
  componentConstructors[i] = components[i].constructor;
6347
6409
  }
@@ -6409,9 +6471,13 @@ function _construct(Parent, args, Class) {
6409
6471
  /**
6410
6472
  * @internal
6411
6473
  */ _proto._removeComponent = function _removeComponent(component) {
6412
- ComponentsDependencies._removeCheck(this, component.constructor);
6413
6474
  var components = this._components;
6414
- components.splice(components.indexOf(component), 1);
6475
+ var index = components.indexOf(component);
6476
+ // A replaced Transform is detached from the component slot immediately but
6477
+ // can still reach here later because object destruction may be deferred
6478
+ if (index < 0) return;
6479
+ ComponentsDependencies._removeCheck(this, component.constructor);
6480
+ components.splice(index, 1);
6415
6481
  };
6416
6482
  /**
6417
6483
  * @internal
@@ -6613,15 +6679,16 @@ function _construct(Parent, args, Class) {
6613
6679
  }
6614
6680
  }
6615
6681
  };
6616
- _proto._setTransform = function _setTransform(value) {
6617
- var _this__transform;
6618
- (_this__transform = this._transform) == null ? void 0 : _this__transform.destroy();
6619
- this._transform = value;
6682
+ _proto._replaceTransform = function _replaceTransform(replacement) {
6683
+ var previous = this._transform;
6684
+ replacement._copyLocalPoseFrom(previous);
6685
+ this._components[0] = replacement;
6686
+ this._transform = replacement;
6620
6687
  var children = this._children;
6621
6688
  for(var i = 0, n = children.length; i < n; i++){
6622
- var _children_i_transform;
6623
- (_children_i_transform = children[i].transform) == null ? void 0 : _children_i_transform._parentChange();
6689
+ children[i].transform._parentChange();
6624
6690
  }
6691
+ previous.destroy();
6625
6692
  };
6626
6693
  /**
6627
6694
  * @deprecated
@@ -6632,6 +6699,14 @@ function _construct(Parent, args, Class) {
6632
6699
  }
6633
6700
  return this._invModelMatrix;
6634
6701
  };
6702
+ Entity._isTransformType = function _isTransformType(type) {
6703
+ return type === Transform || _instanceof(type.prototype, Transform);
6704
+ };
6705
+ Entity._checkTransformDependencies = function _checkTransformDependencies(type) {
6706
+ if (ComponentsDependencies._hasDependencies(type)) {
6707
+ throw "Transform-compatible component " + type.name + " cannot declare component dependencies";
6708
+ }
6709
+ };
6635
6710
  /**
6636
6711
  * @internal
6637
6712
  */ Entity._findChildByName = function _findChildByName(entity, childIndex, paths, pathIndex) {