Initial commit with union

This commit is contained in:
Ukendio 2025-06-09 22:18:59 +02:00
parent dab260f733
commit 46d363ad5f
3 changed files with 24 additions and 13 deletions

View file

@ -2525,8 +2525,8 @@ end
World.new = world_new World.new = world_new
export type Entity<T = any> = { __T: T } export type Entity<T = any> = number | { __T: T }
export type Id<T = any> = { __T: T } export type Id<T = any> = number | { __T: T }
export type Pair<P, O> = Id<P> export type Pair<P, O> = Id<P>
type ecs_id_t<T=unknown> = Id<T> | Pair<T, "Tag"> | Pair<"Tag", T> type ecs_id_t<T=unknown> = Id<T> | Pair<T, "Tag"> | Pair<"Tag", T>
export type Item<T...> = (self: Query<T...>) -> (Entity, T...) export type Item<T...> = (self: Query<T...>) -> (Entity, T...)
@ -2601,10 +2601,10 @@ export type World = {
& (<T, a, b, c, d>(World, Entity<T>, Id<a>, Id<b>, Id<c>, Id<d>) -> (a?, b?, c?, d?)), & (<T, a, b, c, d>(World, Entity<T>, Id<a>, Id<b>, Id<c>, Id<d>) -> (a?, b?, c?, d?)),
--- Returns whether the entity has the ID. --- Returns whether the entity has the ID.
has: (<T>(World, Entity<T>, Id) -> boolean) has: (<T, a>(World, Entity<T>, Id<a>) -> boolean)
& (<T>(World, Entity<T>, Id, Id) -> boolean) & (<T, a, b >(World, Entity<T>, Id<a>, Id<a>) -> boolean)
& (<T>(World, Entity<T>, Id, Id, Id) -> boolean) & (<T, a, b, c>(World, Entity<T>, Id<a>, Id<b>, Id<c>) -> boolean)
& <T>(World, Entity<T>, Id, Id, Id, Id) -> boolean, & <T, a, b, c, d>(World, Entity<T>, Id<a>, Id<b>, Id<c>, Id<d>) -> boolean,
--- Get parent (target of ChildOf relationship) for entity. If there is no ChildOf relationship pair, it will return nil. --- Get parent (target of ChildOf relationship) for entity. If there is no ChildOf relationship pair, it will return nil.
parent: <T>(self: World, entity: Entity<T>) -> Entity, parent: <T>(self: World, entity: Entity<T>) -> Entity,

View file

@ -7,6 +7,17 @@ local observers_add = require("@addons/observers")
TEST("addons/observers", function() TEST("addons/observers", function()
local world = observers_add(jecs.world()) local world = observers_add(jecs.world())
local Test = world:component() :: jecs.Id<number>
type Q<T...> = () -> (jecs.Entity, T...)
local query:
(<A>(jecs.World, jecs.Id<A>) -> Q<A>)
& (<A, B>(jecs.World, jecs.Id<A>, jecs.Id<B>) -> Q<A, B>)
& (<A, B, C>(jecs.World, jecs.Id<A>, jecs.Id<B>, jecs.Id<C>) -> Q<A, B, C>)
= 1 :: never
for e, a in query(world, Test) do
end
do CASE "Should work even if set after the component has been used" do CASE "Should work even if set after the component has been used"
local A = world:component() local A = world:component()

View file

@ -661,7 +661,7 @@ TEST("world:range()", function()
do CASE "under range start" do CASE "under range start"
local world = jecs.world() local world = jecs.world()
world:range(400, 1000) world:range(400, 1000)
local id = world:entity() local id = world:entity() :: number
local e = world:entity(id + 5) local e = world:entity(id + 5)
CHECK(e == id + 5) CHECK(e == id + 5)
CHECK(world:contains(e)) CHECK(world:contains(e))
@ -669,7 +669,7 @@ TEST("world:range()", function()
CHECK(world:contains(e2)) CHECK(world:contains(e2))
world:delete(e2) world:delete(e2)
CHECK(not world:contains(e2)) CHECK(not world:contains(e2))
local e2v1 = world:entity(399) local e2v1 = world:entity(399) :: number
CHECK(world:contains(e2v1)) CHECK(world:contains(e2v1))
CHECK(ECS_ID(e2v1) == 399) CHECK(ECS_ID(e2v1) == 399)
CHECK(ECS_GENERATION(e2v1) == 0) CHECK(ECS_GENERATION(e2v1) == 0)
@ -682,13 +682,13 @@ TEST("world:range()", function()
CHECK(world:contains(e2)) CHECK(world:contains(e2))
world:delete(e2) world:delete(e2)
CHECK(not world:contains(e2)) CHECK(not world:contains(e2))
local e2v1 = world:entity(405) local e2v1 = world:entity(405) :: number
CHECK(world:contains(e2v1)) CHECK(world:contains(e2v1))
CHECK(ECS_ID(e2v1) == 405) CHECK(ECS_ID(e2v1) == 405)
CHECK(ECS_GENERATION(e2v1) == 0) CHECK(ECS_GENERATION(e2v1) == 0)
world:delete(e2v1) world:delete(e2v1)
local e2v2 = world:entity(e2v1) local e2v2 = world:entity(e2v1) :: number
CHECK(ECS_ID(e2v2) == 405) CHECK(ECS_ID(e2v2) == 405)
CHECK(ECS_GENERATION(e2v2) == 0) CHECK(ECS_GENERATION(e2v2) == 0)
end end
@ -697,7 +697,7 @@ end)
TEST("world:entity()", function() TEST("world:entity()", function()
do CASE "desired id" do CASE "desired id"
local world = jecs.world() local world = jecs.world()
local id = world:entity() local id = world:entity() :: number
local e = world:entity(id + 5) local e = world:entity(id + 5)
CHECK(e == id + 5) CHECK(e == id + 5)
CHECK(world:contains(e)) CHECK(world:contains(e))
@ -767,11 +767,11 @@ TEST("world:entity()", function()
local e = world:entity() local e = world:entity()
world:delete(e) world:delete(e)
end end
local e = world:entity() :: any local e = world:entity() :: number
CHECK(ECS_ID(e) == pin) CHECK(ECS_ID(e) == pin)
CHECK(ECS_GENERATION(e) == 2^16-1) CHECK(ECS_GENERATION(e) == 2^16-1)
world:delete(e) world:delete(e)
e = world:entity() e = world:entity() :: number
CHECK(ECS_ID(e) == pin) CHECK(ECS_ID(e) == pin)
CHECK(ECS_GENERATION(e) == 0) CHECK(ECS_GENERATION(e) == 0)
end end