1
0
Fork 0
mirror of https://github.com/Reuh/ubiquitousse.git synced 2025-10-27 09:09:30 +00:00

Add ecs:get

This commit is contained in:
Étienne Fildadut 2022-09-16 19:37:17 +09:00
parent 9ea7241f58
commit 859970c7f7
2 changed files with 38 additions and 4 deletions

View file

@ -6,6 +6,8 @@
-- --
-- See the `Asset:__call` method for more details on how assets are loaded. Hopefully this will allow you to use asset which are more game-specific than "image" or "audio". -- See the `Asset:__call` method for more details on how assets are loaded. Hopefully this will allow you to use asset which are more game-specific than "image" or "audio".
-- --
-- TODO: async loading
--
-- No dependencies. -- No dependencies.
-- @module asset -- @module asset
-- @usage -- @usage

View file

@ -609,6 +609,21 @@ let system_mt = {
iter = :() iter = :()
return nextEntity, { @_first } return nextEntity, { @_first }
end, end,
--- Get the `i`th entity in the system.
-- This is a simple wrapper around `iter`; it _will_ iterate over all the entities in the system in order until we reach the desired one.
-- Complexity: O(i)
-- @tparam number i the index of the entity
-- @treturn Entity the entity; `nil` if there is no such entity in the system
get = :(i)
local n = 1
for e in @iter() do
if n == i then
return e
end
n += 1
end
return nil
end,
--- Remove every entity from the system and its subsystems. --- Remove every entity from the system and its subsystems.
clear = :() clear = :()
for e in @iter() do for e in @iter() do
@ -846,10 +861,18 @@ ecs = {
end, end,
--- If `uqt.scene` is available, returns a new scene that will consist of a ECS world with the specified systems and entities. --- If `uqt.scene` is available, returns a new scene that will consist of a ECS world with the specified systems and entities.
--
-- When suspending and resuming the scene, the `onSuspend` and `onResume` events will be emitted on the ECS world.
--
-- Note that since `uqt.scene` use `require` to load the scenes, the scenes files will be cached - including variables defined in them.
-- So if you store the entity list directly in a variable in the scene file, it will be reused every time the scene is entered, and will thus
-- be shared among the different execution of the scene. This may be problematic if an entity is modified by one scene instance, as it will affect others
-- (this should not be a problem with systems due to system instanciation). To avoid this issue, instead you would typically define entities through a
-- function that will recreate the entities on every scene load.
-- @require ubiquitousse.scene -- @require ubiquitousse.scene
-- @string name the name of the new scene -- @string name the name of the new scene
-- @tparam[opt={}] table systems list of systems to add to the world -- @tparam[opt={}] table/function systems list of systems to add to the world. If it is a function, it will be executed every time we enter the scene and returns the list of systems.
-- @tparam[opt={}] table entities list of entities to add to the world -- @tparam[opt={}] table/function entities list of entities to add to the world. If it is a function, it will be executed every time we enter the scene and returns the list of entities.
-- @treturn scene the new scene -- @treturn scene the new scene
scene = (name, systems={}, entities={}) scene = (name, systems={}, entities={})
assert(scene, "ubiquitousse.scene unavailable") assert(scene, "ubiquitousse.scene unavailable")
@ -857,12 +880,21 @@ ecs = {
let w let w
function s:enter() function s:enter()
w = ecs.world(unpack(systems)) local sys, ent = systems, entities
w:add(unpack(entities)) if type(systems) == "function" then sys = { systems() } end
if type(entities) == "function" then ent = { entities() } end
w = ecs.world(unpack(sys))
w:add(unpack(ent))
end end
function s:exit() function s:exit()
w:destroy() w:destroy()
end end
function s:suspend()
w:emit("onSuspend")
end
function s:resume()
w:emit("onResume")
end
function s:update(dt) function s:update(dt)
w:update(dt) w:update(dt)
end end