提供component的快捷操作

This commit is contained in:
yhh
2021-08-20 19:05:40 +08:00
parent 6adea240e2
commit d54ccaf629
5 changed files with 52 additions and 3 deletions

View File

@@ -205,6 +205,11 @@ declare module es {
onDisabled(): void;
setEnabled(isEnabled: boolean): this;
setUpdateOrder(updateOrder: number): this;
addComponent<T extends Component>(component: T): T;
getComponent(type: new (...args: any[]) => Component): Component;
getComponents(typeName: any, componentList?: any[]): any[];
hasComponent(type: new (...args: any[]) => Component): boolean;
removeComponent(component?: Component): void;
}
}
declare module es {
@@ -408,7 +413,7 @@ declare module es {
* @param typeName
* @param componentList
*/
getComponents(typeName: any, componentList?: any): any[];
getComponents(typeName: any, componentList?: any[]): any[];
/**
* 从组件列表中删除组件
* @param component

View File

@@ -542,6 +542,26 @@ var es;
}
return this;
};
Component.prototype.addComponent = function (component) {
return this.entity.addComponent(component);
};
Component.prototype.getComponent = function (type) {
return this.entity.getComponent(type);
};
Component.prototype.getComponents = function (typeName, componentList) {
return this.entity.getComponents(typeName, componentList);
};
Component.prototype.hasComponent = function (type) {
return this.entity.hasComponent(type);
};
Component.prototype.removeComponent = function (component) {
if (component) {
this.entity.removeComponent(component);
}
else {
this.entity.removeComponent(this);
}
};
Component._idGenerator = 0;
return Component;
}());

File diff suppressed because one or more lines are too long

View File

@@ -118,5 +118,29 @@ module es {
return this;
}
public addComponent<T extends Component>(component: T) {
return this.entity.addComponent(component);
}
public getComponent(type: new (...args: any[])=>Component) {
return this.entity.getComponent(type);
}
public getComponents(typeName: any, componentList?: any[]) {
return this.entity.getComponents(typeName, componentList);
}
public hasComponent(type: new (...args: any[])=>Component) {
return this.entity.hasComponent(type);
}
public removeComponent(component?: Component) {
if (component) {
this.entity.removeComponent(component);
} else {
this.entity.removeComponent(this);
}
}
}
}

View File

@@ -456,7 +456,7 @@ module es {
* @param typeName
* @param componentList
*/
public getComponents(typeName: any, componentList?) {
public getComponents(typeName: any, componentList?: any[]) {
return this.components.getComponents(typeName, componentList);
}