{"version":3,"sources":["../src/aspect.ts","../src/component.ts"],"sourcesContent":["import { Component, ComponentConstructor } from './component';\n\nexport class Aspect {\n    public bitFlag: number;\n    public component: ComponentConstructor;\n\n    constructor(component: { new (...args: never): Component }) {\n        this.component = component as unknown as ComponentConstructor;\n        const id = Component.ComponentIdMap.get(this.component);\n        this.bitFlag = (id ?? 0) - 1;\n    }\n}\n\nexport class HasAspect extends Aspect {}\nexport class WithoutAspect extends Aspect {}\n\nexport const Has = <T extends { new (...args: never): Component }>(\n    component: T\n): Aspect => {\n    return new HasAspect(component);\n};\n\nexport const Without = <T extends { new (...args: never): Component }>(\n    component: T\n): Aspect => {\n    return new WithoutAspect(component);\n};\n","/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { Entity } from './entity';\nimport { pendingFields, pendingInitializers } from './decorators';\n\nexport type ComponentId = number;\nexport type ComponentName = string;\nexport type ComponentField = {\n    fieldName: string;\n    defaultValue?: unknown;\n};\n\n/**\n * Component that can be attached to entities.\n */\nexport abstract class Component {\n    /**\n     * Bitflag id assigned to a component class at registration time.\n     * Keyed by the class reference itself, not by class name, so the ECS\n     * survives identifier mangling by minifiers and obfuscators.\n     */\n    static ComponentIdMap: Map<ComponentConstructor, ComponentId> = new Map();\n\n    static ComponentFieldMap: Map<\n        ComponentConstructor,\n        Map<string, ComponentField>\n    > = new Map();\n\n    static ComponentFieldInitializeMap: Map<\n        ComponentConstructor,\n        Map<string, Array<string>>\n    > = new Map();\n\n    static maxId = 0;\n\n    declare componentId: ComponentId;\n\n    private declare entity: Entity;\n\n    setValues(values: Record<string, any>): void {\n        const ctor = this.constructor as ComponentConstructor;\n        const fields = Component.ComponentFieldMap.get(ctor);\n        const initializers = Component.ComponentFieldInitializeMap.get(ctor);\n\n        if (!fields) {\n            return;\n        }\n\n        fields.forEach(({ fieldName }) => {\n            if (typeof values[fieldName] === 'undefined') {\n                throw new Error(\n                    `Value not provided for ${fieldName} on component ${this.constructor.name}.`\n                );\n            } else {\n                (this as any)[fieldName] = values[fieldName];\n\n                if (initializers && initializers.has(fieldName)) {\n                    initializers.get(fieldName)?.forEach((otherField) => {\n                        (this as any)[otherField] = values[fieldName];\n                    });\n                }\n            }\n        });\n    }\n    setEntity(entity: Entity): void {\n        this.entity = entity;\n    }\n\n    getEntity(): Entity {\n        if (!this.entity) {\n            throw new Error(\n                \"Component tried to fetch it's entity, but none was found\"\n            );\n        }\n        return this.entity;\n    }\n\n    onComponentRemoved() {\n        // no-op\n    }\n\n    toString(): string {\n        return `${this.constructor.name}`;\n    }\n}\n\ntype NonFunctionPropertyNames<T> = {\n    // eslint-disable-next-line @typescript-eslint/ban-types\n    [K in keyof T]: T[K] extends Function ? never : K;\n}[keyof T];\n\nexport type ComponentArgs<C> = {\n    [Property in Exclude<\n        NonFunctionPropertyNames<C>,\n        keyof Component\n    >]: C[Property];\n};\nexport type ComponentConstructor = { new (...args: any[]): Component };\n\nexport const registerComponentWithSpecificId = <T extends ComponentConstructor>(\n    constructor: T,\n    id: number\n) => {\n    registerComponent(constructor, id);\n};\n\nexport function RegisterComponent<T extends ComponentConstructor>(\n    value: T,\n    _context: ClassDecoratorContext\n): void {\n    Component.maxId++;\n    const newComponentId = Component.maxId;\n    registerComponent(value, newComponentId);\n\n    const fields = pendingFields.splice(0);\n    const fieldMap = Component.ComponentFieldMap.get(value);\n    if (fieldMap) {\n        for (const f of fields) {\n            fieldMap.set(f.fieldName, f);\n        }\n    }\n\n    const initializers = pendingInitializers.splice(0);\n    if (initializers.length > 0) {\n        let initMap = Component.ComponentFieldInitializeMap.get(value);\n        if (!initMap) {\n            initMap = new Map();\n            Component.ComponentFieldInitializeMap.set(value, initMap);\n        }\n        for (const init of initializers) {\n            if (!initMap.has(init.initFrom)) {\n                initMap.set(init.initFrom, []);\n            }\n            initMap.get(init.initFrom)?.push(init.field);\n        }\n    }\n}\n\nconst registerComponent = (\n    constructor: ComponentConstructor,\n    newComponentId: number\n) => {\n    constructor.prototype.componentId = newComponentId;\n    Component.ComponentIdMap.set(constructor, newComponentId);\n\n    if (!Component.ComponentFieldMap.has(constructor)) {\n        Component.ComponentFieldMap.set(constructor, new Map());\n    }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACcO,IAAe,aAAf,MAAe,WAAU;AAAA,EAwB5B,UAAU,QAAmC;AACzC,UAAM,OAAO,KAAK;AAClB,UAAM,SAAS,WAAU,kBAAkB,IAAI,IAAI;AACnD,UAAM,eAAe,WAAU,4BAA4B,IAAI,IAAI;AAEnE,QAAI,CAAC,QAAQ;AACT;AAAA,IACJ;AAEA,WAAO,QAAQ,CAAC,EAAE,UAAU,MAAM;AAC9B,UAAI,OAAO,OAAO,SAAS,MAAM,aAAa;AAC1C,cAAM,IAAI;AAAA,UACN,0BAA0B,SAAS,iBAAiB,KAAK,YAAY,IAAI;AAAA,QAC7E;AAAA,MACJ,OAAO;AACH,QAAC,KAAa,SAAS,IAAI,OAAO,SAAS;AAE3C,YAAI,gBAAgB,aAAa,IAAI,SAAS,GAAG;AAC7C,uBAAa,IAAI,SAAS,GAAG,QAAQ,CAAC,eAAe;AACjD,YAAC,KAAa,UAAU,IAAI,OAAO,SAAS;AAAA,UAChD,CAAC;AAAA,QACL;AAAA,MACJ;AAAA,IACJ,CAAC;AAAA,EACL;AAAA,EACA,UAAU,QAAsB;AAC5B,SAAK,SAAS;AAAA,EAClB;AAAA,EAEA,YAAoB;AAChB,QAAI,CAAC,KAAK,QAAQ;AACd,YAAM,IAAI;AAAA,QACN;AAAA,MACJ;AAAA,IACJ;AACA,WAAO,KAAK;AAAA,EAChB;AAAA,EAEA,qBAAqB;AAAA,EAErB;AAAA,EAEA,WAAmB;AACf,WAAO,GAAG,KAAK,YAAY,IAAI;AAAA,EACnC;AACJ;AAAA;AAAA;AAAA;AAAA;AAAA;AArEsB,WAMX,iBAAyD,oBAAI,IAAI;AANtD,WAQX,oBAGH,oBAAI,IAAI;AAXM,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