From 54349aa1571cf1fc3ff1e6cca445bcdf3eccb83c Mon Sep 17 00:00:00 2001 From: cptbtptpbcptdtptp Date: Tue, 2 Jul 2024 18:42:19 +0800 Subject: [PATCH 1/3] feat: support parameter passing in component constructor --- packages/core/src/Entity.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/core/src/Entity.ts b/packages/core/src/Entity.ts index 6fab543b26..b2aad72166 100644 --- a/packages/core/src/Entity.ts +++ b/packages/core/src/Entity.ts @@ -205,9 +205,12 @@ export class Entity extends EngineObject { * @param type - The type of the component * @returns The component which has been added */ - addComponent(type: new (entity: Entity) => T): T { + addComponent Component>( + type: T, + ...args: ComponentArguments + ): InstanceType { ComponentsDependencies._addCheck(this, type); - const component = new type(this); + const component = new type(this, ...args) as InstanceType; this._components.push(component); component._setActive(true, ActiveChangeFlag.All); return component; @@ -751,3 +754,10 @@ export class Entity extends EngineObject { return this._invModelMatrix; } } + +type ComponentArguments Component> = T extends new ( + entity: Entity, + ...args: infer P +) => Component + ? P + : never; From 6545e23e4437c54b1ed8f33abe2c2fa4d6c5b830 Mon Sep 17 00:00:00 2001 From: cptbtptpbcptdtptp Date: Tue, 2 Jul 2024 18:56:02 +0800 Subject: [PATCH 2/3] feat: support parameter passing in component constructor --- packages/core/src/Entity.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/core/src/Entity.ts b/packages/core/src/Entity.ts index b2aad72166..cfe9f9de4f 100644 --- a/packages/core/src/Entity.ts +++ b/packages/core/src/Entity.ts @@ -203,6 +203,7 @@ export class Entity extends EngineObject { /** * Add component based on the component type. * @param type - The type of the component + * @param args - The arguments of the component * @returns The component which has been added */ addComponent Component>( From 5e479110c2cbdb9fde7698a622936ee279d24079 Mon Sep 17 00:00:00 2001 From: cptbtptpbcptdtptp Date: Wed, 3 Jul 2024 18:08:43 +0800 Subject: [PATCH 3/3] feat: update code --- packages/xr/src/XRManagerExtended.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/xr/src/XRManagerExtended.ts b/packages/xr/src/XRManagerExtended.ts index 2376a09a9e..edd4b6ace8 100644 --- a/packages/xr/src/XRManagerExtended.ts +++ b/packages/xr/src/XRManagerExtended.ts @@ -60,7 +60,7 @@ export class XRManagerExtended extends XRManager { override addFeature XRFeature>( type: T, ...args: TFeatureConstructorArguments - ): XRFeature | null { + ): InstanceType | null { if (this.sessionManager._platformSession) { throw new Error("Cannot add feature when the session is initialized."); } @@ -69,7 +69,7 @@ export class XRManagerExtended extends XRManager { const feature = features[i]; if (feature instanceof type) throw new Error("The feature has been added"); } - const feature = new type(this, ...args); + const feature = new type(this, ...args) as InstanceType; this._features.push(feature); return feature; }