diff options
author | sanine <sanine.not@pm.me> | 2023-04-12 21:31:37 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-04-12 21:31:37 -0500 |
commit | 20494a0fe10011601c7573e6a7fa53bd891b1bc0 (patch) | |
tree | d4d0f9eaef76c7af926e318478695edc0ebbb389 | |
parent | 4a737a44777d440682699dcad35c04a23205730e (diff) |
wrap entityDb and systemDb into single Database class
-rw-r--r-- | honey/ecs.lua | 113 | ||||
-rw-r--r-- | main.lua | 39 |
2 files changed, 75 insertions, 77 deletions
diff --git a/honey/ecs.lua b/honey/ecs.lua index 72cc41d..ef5d6cb 100644 --- a/honey/ecs.lua +++ b/honey/ecs.lua @@ -5,62 +5,6 @@ setmetatable(module, {__index=_G}) setfenv(1, module) ---===== Components =====-- - --- Components provide a kind-of-type-safe way of storing values, since they are --- guaranteed to have a specific set of keys and no other values in them. They also serialize --- nicely for storage. - -Component = {} - -function Component.newFactory(name, params, serialize) - -- create the metatable for this component type - local metatable = {} - metatable.__tostring = serialize or Component.serialize -- nice serialization - metatable.__newindex = function(tbl, index, value) - if params[index] ~= nil then - tbl[index] = value - else - -- throw an error if we try to set a key that is not in the valid set - error(string.format("%q is not a valid key for a component of type %q", index, name)) - end - end - - -- the factory function for creating components - return function(tbl) - local tbl = tbl or {} - local self = { __type=name } - - -- ensure that all keys are present - for k, v in pairs(params) do - self[k] = v - end - setmetatable(self, metatable) - - -- set the keys from the factory call - for k, v in pairs(tbl) do - self[k] = v - end - - return self - end -end - - -function Component.serialize(self) - local str = "{" - for k, v in pairs(self) do - if type(v) == "string" then - str = str .. string.format("%s=%q,", k, v) - else - str = str .. string.format("%s=%s,", k, tostring(v)) - end - end - str = string.sub(str, 1, -2) .. "}" - return str -end - - --===== EntityDb =====-- @@ -188,10 +132,11 @@ SystemDb = {} SystemDb.__index = SystemDb -function SystemDb.new(_) +function SystemDb.new(_, entityDb) local self = { systems = {}, sorted = {}, + entityDb = entityDb, } setmetatable(self, SystemDb) return self @@ -212,6 +157,8 @@ function SystemDb.addSystem(self, systemFunc, params) if type(systemFunc) == "table" then system = systemFunc else + local params = params or {} + params.db = self.entityDb system = systemFunc(params) end @@ -243,4 +190,56 @@ function SystemDb.removeSystem(self, id) end +--===== Database =====-- + +Database = {} +Database.__index = Database + +function Database.new(_) + local self = {} + self.entityDb = EntityDb() + self.systemDb = SystemDb(self.entityDb) + setmetatable(self, Database) + return self +end +setmetatable(Database, {__call=Database.new}) + + +function Database.isValidEntity(self, id) + return self.entityDb:checkIsValid(id) +end +function Database.createEntity(self, id) + return self.entityDb:createEntity(id) +end +function Database.addComponent(self, id, name, value) + return self.entityDb:addComponent(id, name, value) +end +function Database.queryComponent(self, name) + return self.entityDb:queryComponent(name) +end +function Database.queryEntity(self, id) + return self.entityDb:queryEntity(id) +end +function Database.getComponent(self, id, name) + return self.entityDb:getComponent(id, name) +end +function Database.removeComponent(self, id, name) + return self.entityDb:removeComponent(id, name) +end +function Database.deleteEntity(self, id) + return self.entityDb.deleteEntity(id) +end + + +function Database.addSystem(self, func, params) + return self.systemDb:addSystem(func, params) +end +function Database.update(self, dt) + return self.systemDb:update(dt) +end +function Database.removeSystem(self, id) + return self.systemDb:removeSystem(id) +end + + return module @@ -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) |