Add Types

This commit is contained in:
Ukendio 2024-05-03 17:09:15 +02:00
parent c5daddd505
commit ed9a9538c4
2 changed files with 104 additions and 6 deletions

View file

@ -632,9 +632,89 @@ function World.observer(world: World, ...)
}
end
export type Component<T> = {
__nominal_type_only_dont_use_or_you_will_be_fired: T
}
type It<T...> = typeof(setmetatable({} :: {
without: <U...>(self: It<T...>, U...) ->
}, {} :: {
__iter: (self: It<T...>) -> (number, T...),
}))
type WorldShim = {
observer: <T...>(self: WorldShim, T...) -> { event: (event: number) -> () -> (number, T...) },
new: () -> WorldShim,
component: (self: WorldShim) -> Component<unknown>,
entity: (self: WorldShim) -> number,
delete: (self: WorldShim, entity: number) -> (),
set: (<T>(self: WorldShim, entity: number, component: Component<T>, data: T) -> ())
& ((self: WorldShim, entity: number, component: number, data: unknown) -> ()),
get: (<T>(self: WorldShim, entity: number, component: Component<T>) -> T)
& ((self: WorldShim, entity: number, component: number) -> any),
remove: (<T>(self: WorldShim, entity: number, component: Component<T>) -> T)
& ((self: WorldShim, entity: number, component: number) -> any),
query: <T...>(self: WorldShim, T...) -> It<T...>
--[[
(<a>(self: WorldShim, a: Component<a>) -> () -> (number, a))
& (<a, b>(self: WorldShim, a: Component<a>, b: Component<b>) -> () -> (number, a, b))
& (<a, b, c>(
self: WorldShim,
a: Component<a>,
b: Component<b>,
c: Component<c>
) -> (number, a, b, c))
& (<a, b, c, d>(
self: WorldShim,
a: Component<a>,
b: Component<b>,
c: Component<c>,
d: Component<d>
) -> (number, a, b, c, d))
& (<a, b, c, d, e>(
self: WorldShim,
a: Component<a>,
b: Component<b>,
c: Component<c>,
d: Component<d>,
e: Component<e>
) -> (number, a, b, c, d, e))
& (<a, b, c, d, e, f>(
self: WorldShim,
a: Component<a>,
b: Component<b>,
c: Component<c>,
d: Component<d>,
e: Component<e>,
f: Component<f>
) -> (number, a, b, c, d, e, f))
& (<a, b, c, d, e, f, g>(
self: WorldShim,
a: Component<a>,
b: Component<b>,
c: Component<c>,
d: Component<d>,
e: Component<e>,
f: Component<f>,
g: Component<g>
) -> (number, a, b, c, d, e, f, g))
& (<a, b, c, d, e, f, g, h>(
self: WorldShim,
a: Component<a>,
b: Component<b>,
c: Component<c>,
d: Component<d>,
e: Component<e>,
f: Component<f>,
g: Component<g>,
g: Component<g>
) -> (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,
})

View file

@ -1,8 +1,13 @@
--!strict
local jecs = require(script.Parent)
local world = jecs.World.new()
type Component<T> = jecs.Component<T>
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<boolean>
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<nil>
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<true>
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<Vector3>
for id, t in world:query(test) do
print(t)
end
end)
end)
end