packaging: Add pesde support, feat: Add searching and clearing to ref
+ Added pesde support + Added `.search()` to `ref` and made `ref()` (`.set_ref()`) & `.search()` return a clearer which removes the reference + Bumped to 0.1.6
This commit is contained in:
parent
d235e883a1
commit
b4cc94f369
14 changed files with 109 additions and 15 deletions
|
@ -1,6 +1,6 @@
|
|||
--!strict
|
||||
--!optimize 2
|
||||
local jecs = require("@jecs")
|
||||
local jecs = require("../jecs")
|
||||
type entity<T = nil> = jecs.Entity<T>
|
||||
type id<T = nil> = jecs.Id<T>
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
--!strict
|
||||
--!optimize 2
|
||||
local jecs = require("@jecs")
|
||||
local jecs = require("../jecs")
|
||||
type entity<T = nil> = jecs.Entity<T>
|
||||
type id<T = nil> = entity<T> | jecs.Pair
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
--!strict
|
||||
--!optimize 2
|
||||
local jecs = require("@jecs")
|
||||
local jecs = require("../jecs")
|
||||
|
||||
local WORLD = require("./world")
|
||||
|
||||
|
|
42
lib/ref.luau
42
lib/ref.luau
|
@ -1,17 +1,23 @@
|
|||
--!strict
|
||||
--!optimize 2
|
||||
local handle = require("./handle")
|
||||
local jecs = require("@jecs")
|
||||
local jecs = require("../jecs")
|
||||
local WORLD = require("./world").get
|
||||
|
||||
local refs: { [jecs.World]: { [any]: jecs.Entity<any> } } = {}
|
||||
|
||||
local function serve_clearer(key: any, world: jecs.World): () -> ()
|
||||
return function()
|
||||
refs[world][key] = nil
|
||||
end
|
||||
end
|
||||
|
||||
--- Gets an entity the given key references to.
|
||||
--- If the key is nil, an entirely new entity is created and returned.
|
||||
--- If the key doesn't reference an entity, a new entity is made for it to reference and returned.
|
||||
--- @param key any
|
||||
--- @return handle
|
||||
local function ref(key: any): handle.handle
|
||||
local function ref(key: any): (handle.handle, () -> ()?)
|
||||
local world = WORLD()
|
||||
if not key then
|
||||
return handle(world:entity())
|
||||
|
@ -27,7 +33,35 @@ local function ref(key: any): handle.handle
|
|||
refs[world][key] = entity
|
||||
end
|
||||
|
||||
return handle(entity)
|
||||
return handle(entity), serve_clearer(key, world)
|
||||
end
|
||||
|
||||
return ref
|
||||
-- For the `__call`` metamethod
|
||||
local function __call(_, key: any): (handle.handle, () -> ()?)
|
||||
return ref(key)
|
||||
end
|
||||
|
||||
local function search(key: any): (handle.handle?, () -> ()?)
|
||||
local world = WORLD()
|
||||
if not key then
|
||||
return nil
|
||||
end
|
||||
local entity = refs[world][key]
|
||||
|
||||
if not entity then
|
||||
return nil
|
||||
end
|
||||
|
||||
return handle(entity), serve_clearer(key, world)
|
||||
end
|
||||
|
||||
local metatable = {
|
||||
__call = __call,
|
||||
__index = {
|
||||
search = search,
|
||||
set_ref = ref,
|
||||
},
|
||||
}
|
||||
|
||||
local REF = setmetatable({}, metatable) :: typeof(ref) & typeof(metatable.__index)
|
||||
return REF
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
--!strict
|
||||
--!optimize 2
|
||||
local jecs = require("@jecs")
|
||||
local jecs = require("../jecs")
|
||||
type entity<T = nil> = jecs.Entity<T>
|
||||
type i53 = number
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
--!strict
|
||||
local jecs = require("@jecs")
|
||||
local jecs = require("../jecs")
|
||||
type entity<T = nil> = jecs.Entity<T>
|
||||
type id<T = nil> = jecs.Id<T>
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
--!strict
|
||||
--!optimize 2
|
||||
local jecs = require("@jecs")
|
||||
local jecs = require("../jecs")
|
||||
|
||||
local WORLD: jecs.World
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue