module ecs { interface EntityBindingState { decoratorGenerators:Array } export interface Component { activate(); } export interface Entity { addDecorator(decorator: Decorator): void; getDecorator(): Decorator } export function createMemberDecorator(decoratorGenerator: DecoratorGenerator) { let propertyDecorator: PropertyDecorator = (entityPrototype: eui.Component, key: string) => { bindDecoratorWithEntity(entityPrototype, decoratorGenerator) } return propertyDecorator; } function bindDecoratorWithEntity(entityPrototype: eui.Component, decoratorGenerator: DecoratorGenerator) { let state: EntityBindingState = entityPrototype["__ecs__state"]; if (!state) { let rawPartAdd = entityPrototype["partAdded"]; let overridePartAdd = function (partName, instance) { let entity: eui.Component = this; rawPartAdd.call(entity, partName, instance); for (let decorator of state.decoratorGenerators){ let handler = decorator(entity,instance); handler.activate(); } } entityPrototype["partAdded"] = overridePartAdd; state = { decoratorGenerators:[] }; entityPrototype["__ecs__state"] = state; } state.decoratorGenerators.push(decoratorGenerator); } } type Class = { new (...arg): T, prototype: T }; type DecoratorGenerator = (entity: any, component: any) => ecs.Component; interface Entity { addDecorator(d: Decorator) } interface Decorator { }