diff --git a/lib/tiny-debug.lua b/lib/tiny-debug.lua new file mode 100644 index 0000000..6aaec92 --- /dev/null +++ b/lib/tiny-debug.lua @@ -0,0 +1,6 @@ +getCurrentTimeMilliseconds = playdate.getCurrentTimeMilliseconds + +tinyTrackEntityAges = true +ENTITY_INIT_MS = { "ENTITY_INIT_MS" } + +tinyLogSystemUpdateTime = false \ No newline at end of file diff --git a/lib/tiny.lua b/lib/tiny.lua index ce305a6..ef29000 100644 --- a/lib/tiny.lua +++ b/lib/tiny.lua @@ -484,9 +484,20 @@ function tiny.addEntity(world, entity) end tiny_addEntity = tiny.addEntity +if tinyTrackEntityAges then + local wrapped = tiny.addEntity + function tiny.addEntity(world, entity) + local added = wrapped(world, entity) + added[ENTITY_INIT_MS] = getCurrentTimeMilliseconds() + end + tiny_addEntity = tiny.addEntity +end + --- Adds a System to the world. Returns the System. function tiny.addSystem(world, system) - assert(system.world == nil, "System already belongs to a World.") + if system.world ~= nil then + error("System " .. system.name .. " already belongs to a World.") + end local s2a = world.systemsToAdd s2a[#s2a + 1] = system system.world = world @@ -521,7 +532,9 @@ tiny_removeEntity = tiny.removeEntity --- Removes a System from the world. Returns the System. function tiny.removeSystem(world, system) - assert(system.world == world, "System does not belong to this World.") + if system.world ~= world then + error("System " .. system.name .. " does not belong to this World.") + end local s2r = world.systemsToRemove s2r[#s2r + 1] = system return system @@ -778,15 +791,15 @@ function tiny.update(world, dt, filter) end end + local tinyLogSystemUpdateTime = tinyLogSystemUpdateTime -- Iterate through Systems IN ORDER for i = 1, #systems do local system = systems[i] if system.active and ((not filter) or filter(world, system)) then - -- TODO: Track how long each system runs for during any given frame. -- Update Systems that have an update method (most Systems) local update = system.update if update then - --local currentMs = playdate.getCurrentTimeMilliseconds() + local currentMs = tinyLogSystemUpdateTime and getCurrentTimeMilliseconds() local interval = system.interval if interval then local bufferedTime = (system.bufferedTime or 0) + dt @@ -798,14 +811,18 @@ function tiny.update(world, dt, filter) else update(system, dt) end - --local endTimeMs = playdate.getCurrentTimeMilliseconds() - --print(tostring(endTimeMs - currentMs) .. "ms taken to update " .. system.name) + if tinyLogSystemUpdateTime then + local endTimeMs = getCurrentTimeMilliseconds() + print(tostring(endTimeMs - currentMs) .. "ms taken to update system '" .. system.name .. "'") + end end system.modified = false end end - --print("") + if tinyLogSystemUpdateTime then + print("") + end -- Iterate through Systems IN ORDER AGAIN for i = 1, #systems do diff --git a/src/main.lua b/src/main.lua index 1e6dd2b..c32b07b 100644 --- a/src/main.lua +++ b/src/main.lua @@ -7,6 +7,7 @@ import("CoreLibs/timer.lua") import("CoreLibs/ui.lua") import("CoreLibs/utilities/where.lua") +import("../lib/tiny-debug.lua") import("../lib/tiny.lua") local tiny = tiny diff --git a/src/systems/camera-pan.lua b/src/systems/camera-pan.lua index 6f11c89..b9a2574 100644 --- a/src/systems/camera-pan.lua +++ b/src/systems/camera-pan.lua @@ -1,7 +1,5 @@ local gfx = playdate.graphics -local focusPriority = {} - Camera = { pan = { x = 0, @@ -11,6 +9,8 @@ Camera = { expireBelowScreenSystem = filteredSystem("expireBelowScreen", { position = T.XyPair, expireBelowScreenBy = T.number }) +local focusPriority = {} + cameraPanSystem = filteredSystem("cameraPan", { focusPriority = T.number, position = T.XyPair }, function(e, dt) if e.focusPriority >= focusPriority.priority then focusPriority.position = e.position diff --git a/src/tiny-tools.lua b/src/tiny-tools.lua index 106c379..26d6917 100644 --- a/src/tiny-tools.lua +++ b/src/tiny-tools.lua @@ -3,6 +3,10 @@ ---@param process fun(entity: T, dt: number, system: System) ---@return System | { entities: T[] } function filteredSystem(name, shape, process) + assert(type(name) == "string") + assert(type(shape) == "table") + assert(process == nil or type(process) == "function") + local system = tiny.processingSystem() system.name = name local keys = {}