Skip to content

Commit 1405f8b

Browse files
authored
Merge pull request galacean#2464 from cptbtptpbcptdtptp/feat/transform
Replace `Transform`
2 parents afc9087 + a8392e7 commit 1405f8b

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

packages/core/src/ComponentsDependencies.ts

+6
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ export class ComponentsDependencies {
3939
* @internal
4040
*/
4141
static _removeCheck(entity: Entity, type: ComponentConstructor): void {
42+
const components = entity._components;
43+
const n = components.length;
4244
while (type !== Component) {
45+
let count = 0;
46+
for (let i = 0; i < n; i++) {
47+
if (components[i] instanceof type && ++count > 1) return;
48+
}
4349
const invDependencies = ComponentsDependencies._invDependenciesMap.get(type);
4450
if (invDependencies) {
4551
for (let i = 0, len = invDependencies.length; i < len; i++) {

packages/core/src/Entity.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,8 @@ export class Entity extends EngineObject {
9797
_isTemplate: boolean = false;
9898
/** @internal */
9999
_updateFlagManager: UpdateFlagManager = new UpdateFlagManager();
100-
/** @internal */
101-
_transform: Transform;
102100

101+
private _transform: Transform;
103102
private _templateResource: ReferResource;
104103
private _parent: Entity = null;
105104
private _activeChangedComponents: Component[];
@@ -227,6 +226,13 @@ export class Entity extends EngineObject {
227226
ComponentsDependencies._addCheck(this, type);
228227
const component = new type(this, ...args) as InstanceType<T>;
229228
this._components.push(component);
229+
230+
// @todo: temporary solution
231+
if (component instanceof Transform) {
232+
const transform = this._transform;
233+
this._transform = component;
234+
transform?.destroy();
235+
}
230236
component._setActive(true, ActiveChangeFlag.All);
231237
return component;
232238
}

packages/core/src/Transform.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { BoolUpdateFlag } from "./BoolUpdateFlag";
33
import { Component } from "./Component";
44
import { Entity } from "./Entity";
55
import { assignmentClone, deepClone, ignoreClone } from "./clone/CloneManager";
6+
import { Logger } from "./base";
67

78
/**
89
* Used to implement transformation related functions.
@@ -340,12 +341,6 @@ export class Transform extends Component {
340341
this._worldRotationQuaternion._onValueChanged = this._onWorldRotationQuaternionChanged;
341342
//@ts-ignore
342343
this._scale._onValueChanged = this._onScaleChanged;
343-
344-
if (entity._transform) {
345-
throw `Entity already has a transform.`;
346-
} else {
347-
entity._transform = this;
348-
}
349344
}
350345

351346
/**

tests/src/core/Transform.test.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,17 @@ describe("Transform test", function () {
9898
expect((entity5.transform as SubClassOfTransform).size).to.deep.include({ x: 100, y: 100 });
9999

100100
// Add component
101-
expect(() => {
102-
entity0.addComponent(SubClassOfTransform);
103-
}).to.throw("Entity already has a transform.");
104-
expect(() => {
105-
entity1.addComponent(Transform);
106-
}).to.throw("Entity already has a transform.");
101+
const preTransform0 = entity0.transform;
102+
entity0.addComponent(SubClassOfTransform);
103+
expect(preTransform0.destroyed).to.equal(true);
104+
expect(entity0.transform instanceof Transform).to.equal(true);
105+
expect(entity0.transform instanceof SubClassOfTransform).to.equal(true);
106+
107+
const preTransform1 = entity1.transform;
108+
entity1.addComponent(Transform);
109+
expect(preTransform1.destroyed).to.equal(true);
110+
expect(entity1.transform instanceof Transform).to.equal(true);
111+
expect(entity1.transform instanceof SubClassOfTransform).to.equal(false);
107112
});
108113
});
109114

0 commit comments

Comments
 (0)