From 16bcc6daab84373cac0f4125c1580d3cb1261baf Mon Sep 17 00:00:00 2001 From: sanine-a Date: Wed, 22 Mar 2023 12:12:31 -0500 Subject: move ecs systems into separate module --- honey/ecs-systems.lua | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 honey/ecs-systems.lua (limited to 'honey/ecs-systems.lua') diff --git a/honey/ecs-systems.lua b/honey/ecs-systems.lua new file mode 100644 index 0000000..a511b3d --- /dev/null +++ b/honey/ecs-systems.lua @@ -0,0 +1,75 @@ +local ecs = require 'honey.ecs' + + +local module = {} +setmetatable(module, {__index=_G}) +setfenv(1, module) + + + +--===== transform cascading =====-- + +local function recursiveComputeTransform(entity) + if entity._transformComputed then + return entity._transform + end + if entity.parent == false then + entity._transformComputed = true + entity._transform = entity.transform + return entity.transform + end + + entity._transformComputed = true + local parentTransform = recursiveComputeTransform(entity.parent) + entity._transform = parentTransform * entity.transform + return entity._transform +end + +-- update transforms +transformCascade = { + filter=ecs.Filter.AND{"transform", "parent"}, + preUpdate=function(entity) + entity._transform = nil + entity._transformComputed = false + end, + update=function(entity, dt) + recursiveComputeTransform(entity) + end, + priority=98, +} + + +--===== rendering =====-- + +function renderCam(camera, priority) + local priority = priority or 99 + return { + filter=ecs.Filter.AND{"mesh", "shader", "transform"}, + update=function(entity, dt) + entity.shader:use() + entity.shader:configure{ + matrix={ + model=entity._transform, + view=camera.view, + projection=camera.projection, + }, + } + entity.mesh:drawElements() + end, + priority=priority, + } +end + +--===== update functions =====-- + +update = { + filter=ecs.Filter.AND{"update"}, + update=function(entity, dt) + entity.update(entity, dt) + end, + priority=50, +} + + + +return module -- cgit v1.2.1