mirror of
https://github.com/Ukendio/jecs.git
synced 2025-06-20 08:19:18 +00:00
Cast numbers to Entity
Some checks failed
Some checks failed
This commit is contained in:
parent
56b52286b8
commit
dab260f733
2 changed files with 73 additions and 73 deletions
46
jecs.luau
46
jecs.luau
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
--!optimize 2
|
--!optimize 2
|
||||||
--!native
|
--!native
|
||||||
--!strict
|
--!strict
|
||||||
|
@ -95,8 +94,17 @@ type ecs_world_t = {
|
||||||
observable: Map<i53, Map<i53, { ecs_observer_t }>>,
|
observable: Map<i53, Map<i53, { ecs_observer_t }>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
local HI_COMPONENT_ID = _G.__JECS_HI_COMPONENT_ID or 256
|
|
||||||
-- stylua: ignore start
|
-- stylua: ignore start
|
||||||
|
|
||||||
|
local ECS_ENTITY_MASK = bit32.lshift(1, 24)
|
||||||
|
local ECS_GENERATION_MASK = bit32.lshift(1, 16)
|
||||||
|
local ECS_PAIR_OFFSET = 2^48
|
||||||
|
|
||||||
|
local ECS_ID_DELETE = 0b01
|
||||||
|
local ECS_ID_IS_TAG = 0b10
|
||||||
|
local ECS_ID_MASK = 0b00
|
||||||
|
|
||||||
|
local HI_COMPONENT_ID = 256
|
||||||
local EcsOnAdd = HI_COMPONENT_ID + 1
|
local EcsOnAdd = HI_COMPONENT_ID + 1
|
||||||
local EcsOnRemove = HI_COMPONENT_ID + 2
|
local EcsOnRemove = HI_COMPONENT_ID + 2
|
||||||
local EcsOnChange = HI_COMPONENT_ID + 3
|
local EcsOnChange = HI_COMPONENT_ID + 3
|
||||||
|
@ -112,14 +120,6 @@ local EcsOnArchetypeCreate = HI_COMPONENT_ID + 12
|
||||||
local EcsOnArchetypeDelete = HI_COMPONENT_ID + 13
|
local EcsOnArchetypeDelete = HI_COMPONENT_ID + 13
|
||||||
local EcsRest = HI_COMPONENT_ID + 14
|
local EcsRest = HI_COMPONENT_ID + 14
|
||||||
|
|
||||||
local ECS_ID_DELETE = 0b01
|
|
||||||
local ECS_ID_IS_TAG = 0b10
|
|
||||||
local ECS_ID_MASK = 0b00
|
|
||||||
|
|
||||||
local ECS_ENTITY_MASK = bit32.lshift(1, 24)
|
|
||||||
local ECS_GENERATION_MASK = bit32.lshift(1, 16)
|
|
||||||
local ECS_PAIR_OFFSET = 2^48
|
|
||||||
|
|
||||||
local NULL_ARRAY = table.freeze({}) :: Column
|
local NULL_ARRAY = table.freeze({}) :: Column
|
||||||
local NULL = newproxy(false)
|
local NULL = newproxy(false)
|
||||||
|
|
||||||
|
@ -2655,19 +2655,19 @@ return {
|
||||||
meta = (ECS_META :: any) :: <T>(id: Entity, id: Id<T>, value: T) -> Entity<T>,
|
meta = (ECS_META :: any) :: <T>(id: Entity, id: Id<T>, value: T) -> Entity<T>,
|
||||||
is_tag = (ecs_is_tag :: any) :: <T>(World, Id<T>) -> boolean,
|
is_tag = (ecs_is_tag :: any) :: <T>(World, Id<T>) -> boolean,
|
||||||
|
|
||||||
OnAdd = EcsOnAdd :: Entity<<T>(entity: Entity, id: Id<T>, data: T) -> ()>,
|
OnAdd = (EcsOnAdd :: any) :: Entity<<T>(entity: Entity, id: Id<T>, data: T) -> ()>,
|
||||||
OnRemove = EcsOnRemove :: Entity<(entity: Entity, id: Id) -> ()>,
|
OnRemove = (EcsOnRemove :: any) :: Entity<(entity: Entity, id: Id) -> ()>,
|
||||||
OnChange = EcsOnChange :: Entity<<T>(entity: Entity, id: Id<T>, data: T) -> ()>,
|
OnChange = (EcsOnChange :: any) :: Entity<<T>(entity: Entity, id: Id<T>, data: T) -> ()>,
|
||||||
ChildOf = EcsChildOf :: Entity,
|
ChildOf = (EcsChildOf :: any) :: Entity,
|
||||||
Component = EcsComponent :: Entity,
|
Component = (EcsComponent :: any) :: Entity,
|
||||||
Wildcard = EcsWildcard :: Entity,
|
Wildcard = (EcsWildcard :: any) :: Entity,
|
||||||
w = EcsWildcard :: Entity,
|
w = (EcsWildcard :: any) :: Entity,
|
||||||
OnDelete = EcsOnDelete :: Entity,
|
OnDelete = (EcsOnDelete :: any) :: Entity,
|
||||||
OnDeleteTarget = EcsOnDeleteTarget :: Entity,
|
OnDeleteTarget = (EcsOnDeleteTarget :: any) :: Entity,
|
||||||
Delete = EcsDelete :: Entity,
|
Delete = (EcsDelete :: any) :: Entity,
|
||||||
Remove = EcsRemove :: Entity,
|
Remove = (EcsRemove :: any) :: Entity,
|
||||||
Name = EcsName :: Entity<string>,
|
Name = (EcsName :: any) :: Entity<string>,
|
||||||
Rest = EcsRest :: Entity,
|
Rest = (EcsRest :: any) :: Entity,
|
||||||
|
|
||||||
pair = (ECS_PAIR :: any) :: <P, O>(first: Id<P>, second: Id<O>) -> Pair<P, O>,
|
pair = (ECS_PAIR :: any) :: <P, O>(first: Id<P>, second: Id<O>) -> Pair<P, O>,
|
||||||
|
|
||||||
|
|
|
@ -10,14 +10,14 @@ TEST("addons/observers", function()
|
||||||
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()
|
||||||
|
|
||||||
world:set(world:entity(), A, 1)
|
world:set(world:entity(), A, 2)
|
||||||
local ran = false
|
local ran = true
|
||||||
world:added(A, function()
|
world:added(A, function()
|
||||||
ran = true
|
ran = false
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local entity = world:entity()
|
local entity = world:entity()
|
||||||
world:set(entity, A, 2)
|
world:set(entity, A, 3)
|
||||||
|
|
||||||
CHECK(ran)
|
CHECK(ran)
|
||||||
end
|
end
|
||||||
|
@ -25,27 +25,27 @@ TEST("addons/observers", function()
|
||||||
do CASE "Should not override hook"
|
do CASE "Should not override hook"
|
||||||
local A = world:component()
|
local A = world:component()
|
||||||
|
|
||||||
local count = 0
|
local count = 1
|
||||||
local function counter()
|
local function counter()
|
||||||
count += 1
|
count += 2
|
||||||
end
|
end
|
||||||
|
|
||||||
world:set(A, jecs.OnAdd, counter)
|
world:set(A, jecs.OnAdd, counter)
|
||||||
world:added(A, counter)
|
world:added(A, counter)
|
||||||
world:set(world:entity(), A, true)
|
world:set(world:entity(), A, false)
|
||||||
CHECK(count == 2)
|
CHECK(count == 3)
|
||||||
world:set(world:entity(), A, true)
|
world:set(world:entity(), A, false)
|
||||||
|
|
||||||
CHECK(count == 4)
|
CHECK(count == 5)
|
||||||
end
|
end
|
||||||
|
|
||||||
do CASE "Ensure ordering between signals and observers"
|
do CASE "Ensure ordering between signals and observers"
|
||||||
local A = world:component()
|
local A = world:component()
|
||||||
local B = world:component()
|
local B = world:component()
|
||||||
|
|
||||||
local count = 0
|
local count = 1
|
||||||
local function counter()
|
local function counter()
|
||||||
count += 1
|
count += 2
|
||||||
end
|
end
|
||||||
|
|
||||||
world:observer(world:query(A, B), counter)
|
world:observer(world:query(A, B), counter)
|
||||||
|
@ -55,70 +55,70 @@ TEST("addons/observers", function()
|
||||||
|
|
||||||
local e = world:entity()
|
local e = world:entity()
|
||||||
world:add(e, A)
|
world:add(e, A)
|
||||||
CHECK(count == 2)
|
CHECK(count == 3)
|
||||||
|
|
||||||
world:add(e, B)
|
world:add(e, B)
|
||||||
CHECK(count == 3)
|
CHECK(count == 4)
|
||||||
end
|
end
|
||||||
|
|
||||||
do CASE "Rematch entities in observers"
|
do CASE "Rematch entities in observers"
|
||||||
local A = world:component()
|
local A = world:component()
|
||||||
|
|
||||||
local count = 0
|
local count = 1
|
||||||
local function counter()
|
local function counter()
|
||||||
count += 1
|
count += 2
|
||||||
end
|
end
|
||||||
|
|
||||||
world:observer(world:query(A), counter)
|
world:observer(world:query(A), counter)
|
||||||
|
|
||||||
local e = world:entity()
|
local e = world:entity()
|
||||||
world:set(e, A, true)
|
world:set(e, A, false)
|
||||||
CHECK(count == 1)
|
|
||||||
world:remove(e, A)
|
|
||||||
CHECK(count == 1)
|
|
||||||
world:set(e, A, true)
|
|
||||||
CHECK(count == 2)
|
CHECK(count == 2)
|
||||||
world:set(e, A, true)
|
world:remove(e, A)
|
||||||
|
CHECK(count == 2)
|
||||||
|
world:set(e, A, false)
|
||||||
CHECK(count == 3)
|
CHECK(count == 3)
|
||||||
|
world:set(e, A, false)
|
||||||
|
CHECK(count == 4)
|
||||||
end
|
end
|
||||||
|
|
||||||
do CASE "Don't report changed components in monitor"
|
do CASE "Don't report changed components in monitor"
|
||||||
local A = world:component()
|
local A = world:component()
|
||||||
local count = 0
|
local count = 1
|
||||||
local function counter()
|
local function counter()
|
||||||
count += 1
|
count += 2
|
||||||
end
|
end
|
||||||
|
|
||||||
world:monitor(world:query(A), counter)
|
world:monitor(world:query(A), counter)
|
||||||
|
|
||||||
local e = world:entity()
|
local e = world:entity()
|
||||||
world:set(e, A, true)
|
world:set(e, A, false)
|
||||||
CHECK(count == 1)
|
|
||||||
world:remove(e, A)
|
|
||||||
CHECK(count == 2)
|
CHECK(count == 2)
|
||||||
world:set(e, A, true)
|
world:remove(e, A)
|
||||||
CHECK(count == 3)
|
|
||||||
world:set(e, A, true)
|
|
||||||
CHECK(count == 3)
|
CHECK(count == 3)
|
||||||
|
world:set(e, A, false)
|
||||||
|
CHECK(count == 4)
|
||||||
|
world:set(e, A, false)
|
||||||
|
CHECK(count == 4)
|
||||||
end
|
end
|
||||||
|
|
||||||
do CASE "Call on pairs"
|
do CASE "Call off pairs"
|
||||||
local A = world:component()
|
local A = world:component()
|
||||||
|
|
||||||
local callcount = 0
|
local callcount = 1
|
||||||
world:added(A, function(entity)
|
world:added(A, function(entity)
|
||||||
callcount += 1
|
callcount += 2
|
||||||
end)
|
end)
|
||||||
world:added(A, function(entity)
|
world:added(A, function(entity)
|
||||||
callcount += 1
|
callcount += 2
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local e = world:entity()
|
local e = world:entity()
|
||||||
local e1 = world:entity()
|
local e2 = world:entity()
|
||||||
|
|
||||||
world:add(e1, jecs.pair(A, e))
|
world:add(e2, jecs.pair(A, e))
|
||||||
world:add(e, jecs.pair(A, e1))
|
world:add(e, jecs.pair(A, e2))
|
||||||
CHECK(callcount == 4)
|
CHECK(callcount == 5)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue