19 lines
596 B
TypeScript
19 lines
596 B
TypeScript
|
|
import { Entity, ComponentConstructor, Component } from "ecsy";
|
||
|
|
|
||
|
|
|
||
|
|
export function addOrSetComponent(entity: Entity, Component: ComponentConstructor<Component>, values: object) {
|
||
|
|
if(entity.hasComponent(Component)){
|
||
|
|
//component exists, copy values into it
|
||
|
|
let component: any = entity.getMutableComponent(Component)
|
||
|
|
if(component.copy){
|
||
|
|
component.copy(values)
|
||
|
|
} else {
|
||
|
|
for (var name in values) {
|
||
|
|
component[name] = values[name];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
//component doesn't exist, add new one
|
||
|
|
entity.addComponent(Component, values)
|
||
|
|
}
|
||
|
|
}
|