ericlathrop / entity-component-system

MIT License
34 stars 3 forks source link

Typescript support #7

Open eranimo opened 6 years ago

eranimo commented 6 years ago

Would be great.

Also, would it be possible to define a component as a Typescript interface?

bencoveney commented 6 years ago

I have some rough typings. Feel free to use them or I can tidy up them up and make a PR if the author wants them?

bencoveney commented 5 years ago

That link is dead now - here's the typings:

// entity-component-system.d.ts
declare module "entity-component-system" {
    export class EntityComponentSystem {
        constructor();
        add(system: System): void;
        addEach(system: System, search: string): void;
        run(entityPool: EntityPool, elapsedTime: number): void;
        runs(): number;
        timings(): { [key: string]: number };
        resetTimings(): void;
    }

    export class EntityPool {
        constructor();
        create(): number;
        destroy(id: number): void;
        registerComponent<Component>(
            component: string,
            factory: () => Component,
            reset?: (component: Component) => void,
            size?: number,
        ): void;
        addComponent<Component>(
            id: number,
            component: string,
        ): Component;
        getComponent<Component>(
            id: number,
            component: string,
        ): Component;
        setComponent<Component>(
            id: number,
            component: string,
            value: Component
        ): void;
        onAddComponent<Component>(
            component: string,
            callback: Callback<Component>
        ): void;
        onRemoveComponent<Component>(
            component: string,
            callback: Callback<Component>
        ): void;
        registerSearch(
            search: string,
            components: string[]
        ): void;
        find(
            search: string,
        ): number[];
        load(entities: EntityJson[]): void;
        save(): EntityJson[];
    }

    export interface System {
        (entityPool: EntityPool, elapsedTime: number): void
    }

    export interface Callback<Component> {
        (id: number, component: string, value: Component): void;
    }

    export interface EntityJson {
        id: number,
        [componentName: string]: any
    }
}