From 4a737a44777d440682699dcad35c04a23205730e Mon Sep 17 00:00:00 2001 From: sanine Date: Wed, 12 Apr 2023 19:50:55 -0500 Subject: add script system --- honey/ecs-systems.lua | 15 +++++++++++++++ main.lua | 33 +++++++++++++++------------------ 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/honey/ecs-systems.lua b/honey/ecs-systems.lua index 9995304..5b9692b 100644 --- a/honey/ecs-systems.lua +++ b/honey/ecs-systems.lua @@ -114,5 +114,20 @@ end --===== update functions =====-- +--===== script system =====-- + +script = function(params) + return { + db=params.db, + update=function(self, dt) + local entities = self.db:queryComponent("script") + for id, script in pairs(entities) do + local f = require(script.name) + f(self.db, id, dt) + end + end + } +end + return module diff --git a/main.lua b/main.lua index c5d3aa6..449696d 100644 --- a/main.lua +++ b/main.lua @@ -31,19 +31,24 @@ edb:addComponent(camera, "camera", { edb:addComponent(camera, "transform", { matrix=Mat4():identity():translate(Vec3{0, 0, -6}), }) +edb:addComponent(camera, "script", { + name = "cameraRotationScript", +}) -sdb:addSystem{ - update = function(self, dt) - local transform = edb:getComponent(camera, "transform") +sdb:addSystem(stdSystem.transform, {db=edb}) +sdb:addSystem(stdSystem.renderCamera, {camera=camera, db=edb}) +sdb:addSystem(stdSystem.script, {db=edb}) +package.loaded['baseRotationScript'] = function(db, id, dt) + local transform = edb:getComponent(id, "transform") + transform.matrix:rotateZ(math.pi * dt) +end +package.loaded['cameraRotationScript'] = function(db, id, dt) + local transform = db:getComponent(id, "transform") transform.matrix:translate(Vec3{0, 0, -0.2*math.sin(math.pi * glfw.GetTime())}) end -} -sdb:addSystem(stdSystem.transform, {db=edb}) -sdb:addSystem(stdSystem.renderCamera, {camera=camera, db=edb}) - local id = edb:createEntity() edb:addComponent(id, "renderMesh", { textures = { @@ -55,12 +60,9 @@ edb:addComponent(id, "renderMesh", { edb:addComponent(id, "transform", { matrix = Mat4():identity():rotateZ(math.rad(45)), }) -sdb:addSystem{ - update = function(self, dt) - local transform = edb:getComponent(id, "transform") - transform.matrix:rotateZ(math.pi * dt) - end, -} +edb:addComponent(id, "script", { + name = "baseRotationScript", +}) local id2 = edb:createEntity() edb:addComponent(id2, "renderMesh", { @@ -119,11 +121,6 @@ honey.loop(window, function(dt) nvg.StrokeColor(vg, nvg.RGBf(1, 1, 1)) nvg.FontFace(vg, "November") nvg.Text(vg, 50, 50, "fps: "..tostring(math.floor(fpsAverage(1/dt)))) - nvg.BeginPath(vg) - nvg.MoveTo(vg, 50, 50) - nvg.LineTo(vg, 50, 100) - nvg.LineTo(vg, 100, 50) - nvg.Stroke(vg) nvg.EndFrame(vg) end) -- cgit v1.2.1