diff --git a/lib/init.lua b/lib/init.lua index 35a9b9c..b673e03 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -632,9 +632,89 @@ function World.observer(world: World, ...) } end +export type Component = { + __nominal_type_only_dont_use_or_you_will_be_fired: T +} + +type It = typeof(setmetatable({} :: { + without: (self: It, U...) -> +}, {} :: { + __iter: (self: It) -> (number, T...), +})) + +type WorldShim = { + observer: (self: WorldShim, T...) -> { event: (event: number) -> () -> (number, T...) }, + new: () -> WorldShim, + component: (self: WorldShim) -> Component, + entity: (self: WorldShim) -> number, + delete: (self: WorldShim, entity: number) -> (), + set: ((self: WorldShim, entity: number, component: Component, data: T) -> ()) + & ((self: WorldShim, entity: number, component: number, data: unknown) -> ()), + get: ((self: WorldShim, entity: number, component: Component) -> T) + & ((self: WorldShim, entity: number, component: number) -> any), + remove: ((self: WorldShim, entity: number, component: Component) -> T) + & ((self: WorldShim, entity: number, component: number) -> any), + query: (self: WorldShim, T...) -> It + --[[ + ((self: WorldShim, a: Component) -> () -> (number, a)) + & ((self: WorldShim, a: Component, b: Component) -> () -> (number, a, b)) + & (( + self: WorldShim, + a: Component, + b: Component, + c: Component + ) -> (number, a, b, c)) + & (( + self: WorldShim, + a: Component, + b: Component, + c: Component, + d: Component + ) -> (number, a, b, c, d)) + & (( + self: WorldShim, + a: Component, + b: Component, + c: Component, + d: Component, + e: Component + ) -> (number, a, b, c, d, e)) + & (( + self: WorldShim, + a: Component, + b: Component, + c: Component, + d: Component, + e: Component, + f: Component + ) -> (number, a, b, c, d, e, f)) + & (( + self: WorldShim, + a: Component, + b: Component, + c: Component, + d: Component, + e: Component, + f: Component, + g: Component + ) -> (number, a, b, c, d, e, f, g)) + & (( + self: WorldShim, + a: Component, + b: Component, + c: Component, + d: Component, + e: Component, + f: Component, + g: Component, + g: Component + ) -> (number, a, b, c, d, e, f, g, h)) + ]]-- +} + return table.freeze({ - World = World, + World = (World :: any) :: WorldShim, ON_ADD = ON_ADD, ON_REMOVE = ON_REMOVE, - ON_SET = ON_SET + ON_SET = ON_SET, }) diff --git a/lib/init.spec.lua b/lib/init.spec.lua index fdc8331..0820ca3 100644 --- a/lib/init.spec.lua +++ b/lib/init.spec.lua @@ -1,8 +1,13 @@ +--!strict + local jecs = require(script.Parent) local world = jecs.World.new() +type Component = jecs.Component + local A, B, C, D = world:entity(), world:entity(), world:entity(), world:entity() local E, F, G, H = world:entity(), world:entity(), world:entity(), world:entity() + print("A", A) print("B", B) print("C", C) @@ -75,6 +80,8 @@ return function() local id = world:entity() world:set(id, A, true) world:set(id, B, 1) + local s = world:component() :: jecs.Component + world:set(id, s, true) local id1 = world:entity() world:set(id1, A, "hello") @@ -112,7 +119,7 @@ return function() end) it("should not query a removed component", function() - local Tag = world:entity() + local Tag = (world:entity() :: any) :: jecs.Component local AnotherTag = world:entity() local entity = world:entity() @@ -121,7 +128,7 @@ return function() world:remove(entity, AnotherTag) local added = 0 - for e, t, a in world:query(Tag, AnotherTag) do + for e, t in world:query(Tag) do added += 1 end expect(added).to.equal(0) @@ -222,7 +229,7 @@ return function() local world = jecs.World.new() local A = world:component() - local B = world:component() + local B = world:component() :: jecs.Component local entities = {} for i = 1, N do @@ -230,7 +237,7 @@ return function() world:set(id, A, true) world:set(id, B, true) - if i > 5 then world:remove(id, B, true) end + if i > 5 then world:remove(id, B) end entities[i] = id end @@ -299,5 +306,16 @@ return function() expect(world:get(id, Poison)).to.never.be.ok() expect(world:get(id, Health)).to.never.be.ok() end) + + it("try types", function() + + local test = world:component() :: Component + + for id, t in world:query(test) do + print(t) + end + + + end) end) end \ No newline at end of file