From a45f73c7388b3f5a5901bfcc66fd2d1df58d3001 Mon Sep 17 00:00:00 2001 From: sanine Date: Wed, 22 Mar 2023 10:02:47 -0500 Subject: add update function system --- main.lua | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) (limited to 'main.lua') diff --git a/main.lua b/main.lua index c9b1733..251f466 100644 --- a/main.lua +++ b/main.lua @@ -34,16 +34,16 @@ function recursiveComputeTransform(entity) return entity._transform end -local world = ecs.World() +local level = ecs.Level() -- update transforms -world:addSystem{ +level:addSystem{ filter=ecs.Filter.AND{"transform", "parent"}, update=function(entity, dt) recursiveComputeTransform(entity) end, priority=1, } -world:addSystem{ +level:addSystem{ filter=ecs.Filter.AND{"transform", "parent"}, update=function(entity, dt) entity._transform = nil @@ -53,7 +53,7 @@ world:addSystem{ } -- render objects -world:addSystem{ +level:addSystem{ filter=ecs.Filter.AND{"mesh", "shader", "transform"}, update=function(entity, dt) entity.shader:use() @@ -69,7 +69,14 @@ world:addSystem{ priority=99, } - +-- run custom scripts +level:addSystem{ + filter=ecs.Filter.AND{"update"}, + update=function(entity, dt) + entity.update(entity, dt) + end, + priority=50, +} @@ -132,6 +139,12 @@ local dodeca = honey.mesh.loadFile("assets/dodecahedron.obj")[1] local icosa = honey.mesh.loadFile("assets/icosahedron.obj")[1] +function updateTransform(self, dt) + self.transform:rotateY(0.3 * math.pi * dt) + self.transform:rotateX(0.1 * math.pi * dt) +end + + function growLine(prev, depth) if depth == 0 then return prev end @@ -139,10 +152,11 @@ function growLine(prev, depth) transform=Mat4.Identity(), mesh=octa, shader=shader, + update=updateTransform, } entity.transform:translate(Vec3{2, 0, 0}) prev.parent = entity - world:addEntity(prev) + level:addEntity(prev) return growLine(entity, depth-1) end @@ -154,18 +168,7 @@ local leaf = { } local root = growLine(leaf, 24) root.parent = false -world:addEntity(root) - - -function updateTransforms(dt) - local entity = leaf - while entity ~= false do - entity.transform:rotateY(0.3 * math.pi * dt) - entity.transform:rotateX(0.1 * math.pi * dt) - entity = entity.parent - end -end - +level:addEntity(root) window:setKeyCallback(function(_, key) @@ -182,12 +185,10 @@ while not window:shouldClose() do local dt = time - prevTime prevTime = time - updateTransforms(dt) - gl.ClearColor(0.2, 0.4, 1.0, 1.0) gl.Clear(gl.COLOR_BUFFER_BIT + gl.DEPTH_BUFFER_BIT) - world:update(dt) + level:update(dt) window:swapBuffers() glfw.PollEvents() -- cgit v1.2.1