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 +++++++++++++++++++++++++++++++++++++++++++++++++++ honey/ecs.lua | 12 +++++++++ honey/mat4.lua | 16 ++++++----- honey/std.lua | 13 ++++----- 4 files changed, 103 insertions(+), 13 deletions(-) create mode 100644 honey/ecs-systems.lua (limited to 'honey') 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 diff --git a/honey/ecs.lua b/honey/ecs.lua index 4eee343..23bb0ba 100644 --- a/honey/ecs.lua +++ b/honey/ecs.lua @@ -194,10 +194,22 @@ end function Level.update(self, dt) for _, system in ipairs(self.systems) do + if system.preUpdate then + for id in pairs(system.entities) do + local entity = self.entities[id] + system.preUpdate(entity) + end + end for id in pairs(system.entities) do local entity = self.entities[id] system.update(entity, dt) end + if system.postUpdate then + for id in pairs(system.entities) do + local entity = self.entities[id] + system.postUpdate(entity) + end + end end end diff --git a/honey/mat4.lua b/honey/mat4.lua index 8ea73d4..6ea17fd 100644 --- a/honey/mat4.lua +++ b/honey/mat4.lua @@ -57,13 +57,6 @@ end setmetatable(Mat4, {__call=Mat4.new}) -function Mat4.Identity() - local m = Mat4() - m:identity() - return m -end - - function Mat4.__index(self, key) if type(key) == "number" then return RowLookup(key, self.data) @@ -111,40 +104,49 @@ end function Mat4.identity(self) glm.mat4_identity(self.data) + return self end function Mat4.zero(self) glm.mat4_zero(self.data) + return self end function Mat4.translate(self, vec) glm.translate(self.data, vec.data) + return self end function Mat4.rotateX(self, angle) glm.rotate_x(self.data, angle, self.data) + return self end function Mat4.rotateY(self, angle) glm.rotate_y(self.data, angle, self.data) + return self end function Mat4.rotateZ(self, angle) glm.rotate_z(self.data, angle, self.data) + return self end function Mat4.scale(self, vec) glm.scale(self.data, vec.data) + return self end function Mat4.perspective(self, fovy, aspect, near, far) glm.perspective(fovy, aspect, near, far, self.data) + return self end function Mat4.perspectiveResize(self, aspect) glm.perspective_resize(aspect, self.data) + return self end diff --git a/honey/std.lua b/honey/std.lua index 752df4d..028a2a8 100644 --- a/honey/std.lua +++ b/honey/std.lua @@ -3,9 +3,10 @@ local init = require 'honey.init' honey.init = init.init honey.terminate = init.terminate -honey.ecs = require 'honey.ecs' -honey.Mat4 = require 'honey.mat4' -honey.mesh = require 'honey.mesh' -honey.Shader = require 'honey.shader' -honey.Vec3 = require 'honey.vec3' -honey.Window = require 'honey.window' +honey.ecs = require 'honey.ecs' +honey.standardSystems = require 'honey.ecs-systems' +honey.Mat4 = require 'honey.mat4' +honey.mesh = require 'honey.mesh' +honey.Shader = require 'honey.shader' +honey.Vec3 = require 'honey.vec3' +honey.Window = require 'honey.window' -- cgit v1.2.1