diff options
author | sanine <sanine.not@pm.me> | 2023-04-13 23:40:31 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-04-13 23:40:31 -0500 |
commit | 371e3a188e190a9c8b53dc6eef02d4eea72db826 (patch) | |
tree | 9d82a01b790682cc1c9889bb13494cfd8db9b545 /main.lua | |
parent | 64be3e5d5788e5e866d7f5fb7b1c6f754735f2f4 (diff) |
move message dispatching into ecs-systems.lua
Diffstat (limited to 'main.lua')
-rw-r--r-- | main.lua | 87 |
1 files changed, 45 insertions, 42 deletions
@@ -6,7 +6,7 @@ local Vec3 = honey.Vec3 local Mat4 = honey.Mat4 local Quaternion = honey.Quaternion local ecs = honey.ecs -local stdSystem = honey.standardSystems +local sys = honey.standardSystems local ode = honey.ode local nvg = honey.nvg @@ -21,63 +21,66 @@ local vw, vh = 640, 480 -- setup ecs -local db = ecs.Database() +local entities = ecs.EntityDb() +local systems = ecs.SystemDb(entities) -local camera = db:createEntity() -db:addComponent(camera, "camera", { - projection=Mat4():perspective(math.rad(45), 640/480, 0.1, 100), -}) -db:addComponent(camera, "transform", { - matrix=Mat4():identity():translate(Vec3{0, 0, -6}), -}) -db:addComponent(camera, "script", { - name = "cameraRotationScript", +local camera = entities:createEntity() +entities:addComponents(camera, { + camera={ + projection=Mat4():perspective(math.rad(45), 640/480, 0.1, 100), + }, + transform={ + matrix=Mat4():identity():translate(Vec3{0, 0, -6}), + }, + script={ + script="cameraRotationScript", + }, }) - - -db:addSystem(stdSystem.transform) -db:addSystem(stdSystem.renderCamera, {camera=camera}) -db:addSystem(stdSystem.script) -package.loaded['baseRotationScript'] = function(db, id, dt) - local transform = db:getComponent(id, "transform") +--entities:addComponent(camera, "camera", { +-- projection=Mat4():perspective(math.rad(45), 640/480, 0.1, 100), +--}) +--entities:addComponent(camera, "transform", { +-- matrix=Mat4():identity():translate(Vec3{0, 0, -6}), +--}) +--entities:addComponent(camera, "script", { +-- name = "cameraRotationScript", +--}) +-- + +systems:addSystem(sys.transform) +systems:addSystem(sys.renderCamera, {camera=camera}) +systems:addSystem(sys.script) +package.loaded['baseRotationScript'] = function(entities, id, dt) + local transform = entities:getComponent(id, "transform") transform.matrix:rotateZ(math.pi * dt) end -package.loaded['cameraRotationScript'] = function(db, id, dt) - local transform = db:getComponent(id, "transform") +package.loaded['cameraRotationScript'] = function(entities, id, dt) + local transform = entities:getComponent(id, "transform") transform.matrix:identity():translate(Vec3{0, 0, -6 + math.sin(math.pi * glfw.GetTime())}) end -function transmitMessage(msg, data) - local query = db:queryComponent(msg) - for id, handler in pairs(query) do - local f = require(handler.scriptName) - f(db, id, data) - end -end - - -local id = db:createEntity() -db:addComponent(id, "renderMesh", { +local id = entities:createEntity() +entities:addComponent(id, "renderMesh", { textures = { ourTexture={ filename="77155.png" }, }, shader = { vertex="vertex.glsl", fragment="fragment.glsl" }, mesh = { filename="assets/icosahedron.obj", index=1 }, }) -db:addComponent(id, "transform", { +entities:addComponent(id, "transform", { matrix = Mat4():identity():rotateZ(math.rad(45)), }) -db:addComponent(id, "script", { - name = "baseRotationScript", +entities:addComponent(id, "script", { + script = "baseRotationScript", }) -local id2 = db:createEntity() -db:addComponent(id2, "renderMesh", { +local id2 = entities:createEntity() +entities:addComponent(id2, "renderMesh", { shader = { vertex="vertex.glsl", fragment="fragment.glsl" }, mesh = { filename="assets/tetrahedron.obj", index=1 }, }) -db:addComponent(id2, "transform", { +entities:addComponent(id2, "transform", { parent=id, matrix=Mat4():identity():translate(Vec3{0, 2, 0}), }) @@ -97,14 +100,14 @@ end) window:setFramebufferSizeCallback(function(_, width, height) gl.Viewport(0, 0, width, height) vw, vh = width, height - transmitMessage("onWindowResize", { width=width, height=height }) + sys.dispatch(entities, "onWindowResize", { width=width, height=height }) end) -package.loaded["cameraHandleResize"] = function(db, id, data) - local camera = db:getComponent(id, "camera") +package.loaded["cameraHandleResize"] = function(entities, id, data) + local camera = entities:getComponent(id, "camera") camera.projection:perspectiveResize(data.width/data.height) end -db:addComponent(camera, "onWindowResize", { scriptName = "cameraHandleResize" }) +entities:addComponent(camera, "onWindowResize", { script = "cameraHandleResize" }) -- averager (for fps) function averager(memory) @@ -129,7 +132,7 @@ honey.loop(window, function(dt) gl.Enable(gl.DEPTH_TEST) gl.Disable(gl.CULL_FACE) - db:update(dt) + systems:update(dt) nvg.BeginFrame(vg, vw, vh, 1.0) nvg.StrokeColor(vg, nvg.RGBf(1, 1, 1)) nvg.FontFace(vg, "November") |