@galacean/engine-core 0.0.0-experimental-2.0-migrate.1 → 0.0.0-experimental-2.0-migrate.3

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/main.js CHANGED
@@ -5317,10 +5317,12 @@ __decorate([
5317
5317
  };
5318
5318
  /**
5319
5319
  * @internal
5320
- */ ComponentsDependencies._removeCheck = function _removeCheck(entity, type) {
5320
+ */ ComponentsDependencies._removeCheck = function _removeCheck(entity, type, replace) {
5321
5321
  var components = entity._components;
5322
5322
  var n = components.length;
5323
5323
  while(type !== Component){
5324
+ // The replacement still satisfies this type and all of its base types.
5325
+ if (replace && (replace === type || _instanceof(replace.prototype, type))) return;
5324
5326
  var count = 0;
5325
5327
  for(var i = 0; i < n; i++){
5326
5328
  if (_instanceof(components[i], type) && ++count > 1) return;
@@ -6479,10 +6481,22 @@ var ComponentCloner = /*#__PURE__*/ function() {
6479
6481
  _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----------------------------------------------------------------
6480
6482
  _this._invModelMatrix = new engineMath.Matrix();
6481
6483
  _this.name = name != null ? name : "Entity";
6482
- for(var i = 0, n = components.length; i < n; i++){
6483
- _this.addComponent(components[i]);
6484
+ var transformType = Transform;
6485
+ var n = components.length;
6486
+ for(var i = n - 1; i >= 0; i--){
6487
+ var componentType = components[i];
6488
+ if (Entity._isTransformType(componentType)) {
6489
+ transformType = componentType;
6490
+ break;
6491
+ }
6492
+ }
6493
+ _this._transform = _this.addComponent(transformType);
6494
+ for(var i1 = 0; i1 < n; i1++){
6495
+ var componentType1 = components[i1];
6496
+ if (!Entity._isTransformType(componentType1)) {
6497
+ _this.addComponent(componentType1);
6498
+ }
6484
6499
  }
6485
- !_this._transform && _this.addComponent(Transform);
6486
6500
  _this._inverseWorldMatFlag = _this.registerWorldChangeFlag();
6487
6501
  return _this;
6488
6502
  }
@@ -6496,13 +6510,19 @@ var ComponentCloner = /*#__PURE__*/ function() {
6496
6510
  for(var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++){
6497
6511
  args[_key - 1] = arguments[_key];
6498
6512
  }
6513
+ var needReplaceTransform = Entity._isTransformType(type) && this._transform;
6514
+ if (needReplaceTransform) {
6515
+ ComponentsDependencies._removeCheck(this, this._transform.constructor, type);
6516
+ }
6499
6517
  ComponentsDependencies._addCheck(this, type);
6500
6518
  var component = _construct(type, [].concat([
6501
6519
  this
6502
6520
  ], args));
6503
- this._components.push(component);
6504
- // @todo: temporary solution
6505
- if (_instanceof(component, Transform)) this._setTransform(component);
6521
+ if (needReplaceTransform) {
6522
+ this._replaceTransform(component);
6523
+ } else {
6524
+ this._components.push(component);
6525
+ }
6506
6526
  component._setActive(true, ActiveChangeFlag.All);
6507
6527
  return component;
6508
6528
  };
@@ -6615,21 +6635,9 @@ var ComponentCloner = /*#__PURE__*/ function() {
6615
6635
  * Clear children entities.
6616
6636
  */ _proto.clearChildren = function clearChildren() {
6617
6637
  var children = this._children;
6618
- for(var i = children.length - 1; i >= 0; i--){
6619
- var child = children[i];
6620
- child._parent = null;
6621
- child._siblingIndex = -1;
6622
- var activeChangeFlag = ActiveChangeFlag.None;
6623
- child._isActiveInHierarchy && (activeChangeFlag |= ActiveChangeFlag.Hierarchy);
6624
- child._isActiveInScene && (activeChangeFlag |= ActiveChangeFlag.Scene);
6625
- activeChangeFlag && child._processInActive(activeChangeFlag);
6626
- Entity._traverseSetOwnerScene(child, null); // Must after child._processInActive().
6627
- child._setParentChange();
6628
- }
6629
- children.length = 0;
6630
- // Dispatch a single `Child` modify event for the whole clear so subscribers
6631
- // (e.g. UICanvas) can invalidate their cached hierarchy state once.
6632
- this._dispatchModify(EntityModifyFlags.Child, this);
6638
+ while(children.length > 0){
6639
+ children[children.length - 1]._setParent(null);
6640
+ }
6633
6641
  };
6634
6642
  /**
6635
6643
  * Clone this entity include children and components.
@@ -6722,9 +6730,13 @@ var ComponentCloner = /*#__PURE__*/ function() {
6722
6730
  /**
6723
6731
  * @internal
6724
6732
  */ _proto._removeComponent = function _removeComponent(component) {
6725
- ComponentsDependencies._removeCheck(this, component.constructor);
6726
6733
  var components = this._components;
6727
- components.splice(components.indexOf(component), 1);
6734
+ var index = components.indexOf(component);
6735
+ // A replaced Transform is detached from the component slot immediately but
6736
+ // can still reach here later because object destruction may be deferred.
6737
+ if (index < 0) return;
6738
+ ComponentsDependencies._removeCheck(this, component.constructor);
6739
+ components.splice(index, 1);
6728
6740
  };
6729
6741
  /**
6730
6742
  * @internal
@@ -6926,15 +6938,18 @@ var ComponentCloner = /*#__PURE__*/ function() {
6926
6938
  }
6927
6939
  }
6928
6940
  };
6929
- _proto._setTransform = function _setTransform(value) {
6941
+ _proto._replaceTransform = function _replaceTransform(value) {
6930
6942
  var previous = this._transform;
6931
- if (previous) {
6932
- value.position.copyFrom(previous.position);
6933
- value.rotationQuaternion.copyFrom(previous.rotationQuaternion);
6934
- value.scale.copyFrom(previous.scale);
6935
- previous.destroy();
6936
- }
6943
+ value.position.copyFrom(previous.position);
6944
+ value.rotationQuaternion.copyFrom(previous.rotationQuaternion);
6945
+ value.scale.copyFrom(previous.scale);
6946
+ // Keep the unique Transform in the same component slot. Detach the old
6947
+ // instance before destroy because destroy can be deferred during a frame.
6948
+ var components = this._components;
6949
+ var previousIndex = components.indexOf(previous);
6950
+ components[previousIndex] = value;
6937
6951
  this._transform = value;
6952
+ previous.destroy();
6938
6953
  var children = this._children;
6939
6954
  for(var i = 0, n = children.length; i < n; i++){
6940
6955
  var _children_i_transform;
@@ -6950,6 +6965,9 @@ var ComponentCloner = /*#__PURE__*/ function() {
6950
6965
  }
6951
6966
  return this._invModelMatrix;
6952
6967
  };
6968
+ Entity._isTransformType = function _isTransformType(type) {
6969
+ return type === Transform || _instanceof(type.prototype, Transform);
6970
+ };
6953
6971
  /**
6954
6972
  * @internal
6955
6973
  */ Entity._findChildByName = function _findChildByName(entity, childIndex, paths, pathIndex) {