mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-28 02:40:03 +00:00
Short circuit return for non zero generation entities
This commit is contained in:
parent
d0af80577b
commit
6922019ed9
2 changed files with 45 additions and 44 deletions
48
jecs.luau
48
jecs.luau
|
@ -772,39 +772,27 @@ local function world_entity(world: ecs_world_t, entity: i53?): i53
|
|||
local r = sparse_array[index]
|
||||
if r then
|
||||
local dense = r.dense
|
||||
if not dense or dense == 0 then
|
||||
dense = index
|
||||
end
|
||||
local any = dense_array[dense]
|
||||
if any == entity then
|
||||
if alive_count > dense then
|
||||
r.dense = dense
|
||||
return entity
|
||||
if dense and r.dense ~= 0 then
|
||||
local any = dense_array[dense]
|
||||
if any ~= entity then
|
||||
if dense > alive_count then
|
||||
alive_count += 1
|
||||
entity_index.alive_count = alive_count
|
||||
local e_swap = dense_array[alive_count]
|
||||
local r_swap = sparse_array[alive_count]
|
||||
r_swap.dense = dense
|
||||
r.dense = alive_count
|
||||
dense_array[alive_count] = any
|
||||
dense_array[dense] = e_swap
|
||||
end
|
||||
return any
|
||||
end
|
||||
local e_swap = dense_array[alive_count]
|
||||
local r_swap = sparse_array[alive_count]
|
||||
r_swap.dense = dense
|
||||
r.dense = alive_count
|
||||
dense_array[alive_count] = entity
|
||||
dense_array[dense] = e_swap
|
||||
return entity
|
||||
else
|
||||
r.dense = index
|
||||
dense_array[index] = entity
|
||||
return entity
|
||||
end
|
||||
|
||||
-- assert(any ~= 0) should never happen
|
||||
|
||||
local e_swap = dense_array[alive_count]
|
||||
local r_swap = sparse_array[alive_count]
|
||||
|
||||
if dense <= alive_count then
|
||||
alive_count += 1
|
||||
entity_index.alive_count = alive_count
|
||||
end
|
||||
|
||||
r_swap.dense = dense
|
||||
r.dense = alive_count
|
||||
dense_array[alive_count] = any
|
||||
dense_array[dense] = e_swap
|
||||
return any
|
||||
else
|
||||
for i = max_id + 1, index do
|
||||
sparse_array[i] = { dense = i } :: ecs_record_t
|
||||
|
|
|
@ -659,6 +659,7 @@ end)
|
|||
TEST("world:entity()", function()
|
||||
do CASE "desired id"
|
||||
local world = jecs.world()
|
||||
world:range(400, 1000)
|
||||
local id = world:entity()
|
||||
local e = world:entity(id + 5)
|
||||
CHECK(e == id + 5)
|
||||
|
@ -667,27 +668,39 @@ TEST("world:entity()", function()
|
|||
CHECK(world:contains(e2))
|
||||
|
||||
-- world:print_entity_index()
|
||||
local e3 = world:entity(275)
|
||||
print("-----")
|
||||
local e3 = world:entity(375)
|
||||
world:entity(376)
|
||||
print("-----")
|
||||
|
||||
CHECK(e3 == 275)
|
||||
print(e3)
|
||||
CHECK(e3 == 375)
|
||||
CHECK(world:contains(e3))
|
||||
|
||||
CHECK(e3 == world:entity(e3))
|
||||
|
||||
world:delete(e3)
|
||||
|
||||
local e3v1 = world:entity(275)
|
||||
CHECK(not world:contains(275))
|
||||
print("-------call 1-------")
|
||||
local e3v1 = world:entity(375)
|
||||
CHECK(not world:contains(375))
|
||||
CHECK(jecs.ECS_GENERATION(e3v1) == 1)
|
||||
CHECK(jecs.ECS_ID(e3v1) == 275)
|
||||
|
||||
|
||||
CHECK(jecs.ECS_ID(e3v1) == 375)
|
||||
CHECK(world:contains(e3v1))
|
||||
-- world:print_entity_index()
|
||||
world:entity(e3)
|
||||
|
||||
world:entity(e3)
|
||||
world:entity(275)
|
||||
print("-------call 2-------")
|
||||
world:entity(382)
|
||||
world:delete(382)
|
||||
world:entity(376)
|
||||
world:delete(e3v1)
|
||||
print("--------------------")
|
||||
print("-------call 3-------")
|
||||
local e3v2 = world:entity(375)
|
||||
CHECK(jecs.ECS_GENERATION(e3v2) == 2)
|
||||
CHECK(jecs.ECS_ID(e3v2) == 375)
|
||||
CHECK(not world:contains(e3v1))
|
||||
CHECK(world:contains(e3v2))
|
||||
print("----------------------")
|
||||
print("-----call 4-------")
|
||||
CHECK(world:entity(375) == e3v2)
|
||||
print("------call 5")
|
||||
end
|
||||
local N = 2^8
|
||||
|
||||
|
|
Loading…
Reference in a new issue