2020-02-17 11:56:41 +00:00
|
|
|
import { Entity, ComponentConstructor, Component } from "ecsy"
|
2020-02-06 10:57:43 +00:00
|
|
|
|
|
|
|
|
|
2020-02-17 11:56:41 +00:00
|
|
|
export function addOrSetComponent(entity: Entity, Component: ComponentConstructor<Component>, values: any) {
|
2020-02-06 10:57:43 +00:00
|
|
|
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) {
|
2020-02-17 11:56:41 +00:00
|
|
|
component[name]= values[name]
|
2020-02-06 10:57:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
//component doesn't exist, add new one
|
|
|
|
|
entity.addComponent(Component, values)
|
|
|
|
|
}
|
2020-02-17 11:56:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function clamp(value: number, min:number, max:number) {
|
|
|
|
|
return Math.min(Math.max(value, min), max)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function lerp(from: number, to:number, at:number) {
|
|
|
|
|
return from + (to - from) * at;
|
|
|
|
|
};
|