Skip to content

Commit 54349aa

Browse files
feat: support parameter passing in component constructor
1 parent 5669ab9 commit 54349aa

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

packages/core/src/Entity.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,12 @@ export class Entity extends EngineObject {
205205
* @param type - The type of the component
206206
* @returns The component which has been added
207207
*/
208-
addComponent<T extends Component>(type: new (entity: Entity) => T): T {
208+
addComponent<T extends new (entity: Entity, ...args: any[]) => Component>(
209+
type: T,
210+
...args: ComponentArguments<T>
211+
): InstanceType<T> {
209212
ComponentsDependencies._addCheck(this, type);
210-
const component = new type(this);
213+
const component = new type(this, ...args) as InstanceType<T>;
211214
this._components.push(component);
212215
component._setActive(true, ActiveChangeFlag.All);
213216
return component;
@@ -751,3 +754,10 @@ export class Entity extends EngineObject {
751754
return this._invModelMatrix;
752755
}
753756
}
757+
758+
type ComponentArguments<T extends new (entity: Entity, ...args: any[]) => Component> = T extends new (
759+
entity: Entity,
760+
...args: infer P
761+
) => Component
762+
? P
763+
: never;

0 commit comments

Comments
 (0)