remove get_or and exception from get

This commit is contained in:
BlackMATov
2024-11-24 09:33:43 +07:00
parent 49a454fb39
commit 03297afb4a
3 changed files with 10 additions and 28 deletions

View File

@@ -28,8 +28,7 @@ registry.entity -> (entity)
registry.guid -> entity -> (id)
registry.is_alive -> entity -> (boolean)
registry.destroy -> entity -> ()
registry.get -> entity -> entity -> (any)
registry.get_or -> entity -> entity -> any -> (any)
registry.get -> entity -> entity -> any -> (any)
registry.has -> entity -> entity -> (boolean)
registry.has_all -> entity -> entity... -> (boolean)
registry.has_any -> entity -> entity... -> (boolean)
@@ -49,8 +48,7 @@ registry.components -> chunk -> entity -> (any[])
entity:guid -> (id)
entity:is_alive -> (boolean)
entity:destroy -> ()
entity:get -> entity -> (any)
entity:get_or -> entity -> any -> (any)
entity:get -> entity -> any -> (any)
entity:has -> entity -> (boolean)
entity:has_all -> entity... -> (boolean)
entity:has_any -> entity... -> (boolean)

View File

@@ -55,20 +55,15 @@ do
local f = evo.registry.entity()
local e = evo.registry.entity()
if not os.getenv("LOCAL_LUA_DEBUGGER_VSCODE") then
assert(not pcall(e.get, e, f))
end
assert(not e:assign(f, 42))
assert(e:get_or(f) == nil)
assert(e:get_or(f, 42) == 42)
assert(e:get(f) == nil)
assert(e:get(f, 42) == 42)
assert(e:insert(f, 84))
assert(e:get(f) == 84)
assert(e:get_or(f) == 84)
assert(e:get_or(f, 42) == 84)
assert(e:get(f, 42) == 84)
assert(not e:insert(f, 42))
assert(e:get(f) == 42)

View File

@@ -300,26 +300,12 @@ function registry.destroy(entity)
idpools.release(__guids, entity.__guid)
end
---@param entity evolved.entity
---@param fragment evolved.entity
---@return any
---@nodiscard
function registry.get(entity, fragment)
local chunk_components = entity.__chunk and entity.__chunk.__components[fragment]
if chunk_components == nil then
error(string.format('entity %s does not have fragment %s', entity, fragment), 2)
end
return chunk_components[entity.__index_in_chunk]
end
---@param entity evolved.entity
---@param fragment evolved.entity
---@param default any
---@return any
---@nodiscard
function registry.get_or(entity, fragment, default)
function registry.get(entity, fragment, default)
local chunk_components = entity.__chunk and entity.__chunk.__components[fragment]
if chunk_components == nil then
@@ -364,7 +350,10 @@ function registry.assign(entity, fragment, component)
component = component == nil and true or component
local chunk_components = entity.__chunk and entity.__chunk.__components[fragment]
if chunk_components == nil then return false end
if chunk_components == nil then
return false
end
chunk_components[entity.__index_in_chunk] = component
return true