From 20494a0fe10011601c7573e6a7fa53bd891b1bc0 Mon Sep 17 00:00:00 2001 From: sanine Date: Wed, 12 Apr 2023 21:31:37 -0500 Subject: wrap entityDb and systemDb into single Database class --- main.lua | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) (limited to 'main.lua') diff --git a/main.lua b/main.lua index 449696d..eda08f7 100644 --- a/main.lua +++ b/main.lua @@ -21,26 +21,25 @@ local vw, vh = 640, 480 -- setup ecs -local edb = ecs.EntityDb() -local sdb = ecs.SystemDb() +local db = ecs.Database() -local camera = edb:createEntity() -edb:addComponent(camera, "camera", { +local camera = db:createEntity() +db:addComponent(camera, "camera", { projection=Mat4():perspective(math.rad(45), 640/480, 0.1, 100), }) -edb:addComponent(camera, "transform", { +db:addComponent(camera, "transform", { matrix=Mat4():identity():translate(Vec3{0, 0, -6}), }) -edb:addComponent(camera, "script", { +db:addComponent(camera, "script", { name = "cameraRotationScript", }) -sdb:addSystem(stdSystem.transform, {db=edb}) -sdb:addSystem(stdSystem.renderCamera, {camera=camera, db=edb}) -sdb:addSystem(stdSystem.script, {db=edb}) +db:addSystem(stdSystem.transform) +db:addSystem(stdSystem.renderCamera, {camera=camera}) +db:addSystem(stdSystem.script) package.loaded['baseRotationScript'] = function(db, id, dt) - local transform = edb:getComponent(id, "transform") + local transform = db:getComponent(id, "transform") transform.matrix:rotateZ(math.pi * dt) end package.loaded['cameraRotationScript'] = function(db, id, dt) @@ -49,27 +48,27 @@ package.loaded['cameraRotationScript'] = function(db, id, dt) end -local id = edb:createEntity() -edb:addComponent(id, "renderMesh", { +local id = db:createEntity() +db:addComponent(id, "renderMesh", { textures = { ourTexture={ filename="77155.png" }, }, shader = { vertex="vertex.glsl", fragment="fragment.glsl" }, mesh = { filename="assets/icosahedron.obj", index=1 }, }) -edb:addComponent(id, "transform", { +db:addComponent(id, "transform", { matrix = Mat4():identity():rotateZ(math.rad(45)), }) -edb:addComponent(id, "script", { +db:addComponent(id, "script", { name = "baseRotationScript", }) -local id2 = edb:createEntity() -edb:addComponent(id2, "renderMesh", { +local id2 = db:createEntity() +db:addComponent(id2, "renderMesh", { shader = { vertex="vertex.glsl", fragment="fragment.glsl" }, mesh = { filename="assets/tetrahedron.obj", index=1 }, }) -edb:addComponent(id2, "transform", { +db:addComponent(id2, "transform", { parent=id, matrix=Mat4():identity():translate(Vec3{0, 2, 0}), }) @@ -88,7 +87,7 @@ end) -- resize window correctly window:setFramebufferSizeCallback(function(_, width, height) gl.Viewport(0, 0, width, height) - local cameraParams = edb:getComponent(camera, "camera") + local cameraParams = db:getComponent(camera, "camera") cameraParams.projection:perspectiveResize(width/height) vw, vh = width, height end) @@ -116,11 +115,11 @@ honey.loop(window, function(dt) gl.Enable(gl.DEPTH_TEST) gl.Disable(gl.CULL_FACE) - sdb:update(dt) + db:update(dt) nvg.BeginFrame(vg, vw, vh, 1.0) 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.Text(vg, 50, 50, "fps: "..tostring(math.floor(1/dt))) nvg.EndFrame(vg) end) -- cgit v1.2.1