From 01a8b2a3d98f816b8b712a974f3d1e98f05152f5 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Fri, 14 Apr 2023 15:44:38 -0500 Subject: implement loading/saving entity db --- honey/ecs-systems.lua | 103 +++++++++++++++++++++++++++++--------------------- 1 file changed, 59 insertions(+), 44 deletions(-) (limited to 'honey/ecs-systems.lua') diff --git a/honey/ecs-systems.lua b/honey/ecs-systems.lua index cfd5675..969b6fe 100644 --- a/honey/ecs-systems.lua +++ b/honey/ecs-systems.lua @@ -85,51 +85,52 @@ function renderCamera(params) db = params.db, priority = params.priority or 99, update = function(self, dt) - local cameraParams = self.db:getComponent(self.camera, "camera") - local cameraTransform = self.db:getComponent(self.camera, "transform") - local view - if cameraTransform then - view = cameraTransform._matrix - else - view = Mat4():identity() - end - - local entities = self.db:queryComponent("renderMesh") - for entity, tbl in pairs(entities) do - -- get shader - local shader = honey.shader.loadShader(tbl.shader.vertex, tbl.shader.fragment) - shader:use() - - -- bind textures - local texOffset = 0 - for name, texTbl in pairs(tbl.textures or {}) do - local texture = honey.image.loadImage(texTbl.filename, texTbl.params) - gl.BindTexture(gl.TEXTURE_2D + texOffset, texture.texture) - shader:setInt(name, texOffset) - texOffset = texOffset + 1 + for id, camera in pairs(self.db:queryComponent("camera")) do + local cameraTransform = self.db:getComponent(id, "transform") + local view + if cameraTransform then + view = cameraTransform._matrix + else + view = Mat4():identity() end - - -- configure default uniforms - local query = self.db:getComponent(entity, "transform") - local model = (query and query._matrix) or Mat4():identity() - shader:configure{ - float={ - time=glfw.GetTime(), - }, - matrix={ - view=view, - projection=cameraParams.projection, - model=model, - }, - } - - -- draw mesh - local mesh = honey.mesh.loadMesh(tbl.mesh.filename, tbl.mesh.index) - mesh:drawElements() - - -- unbind textures - for i=0,texOffset-1 do - gl.BindTexture(gl.TEXTURE_2D + i, 0) + + local entities = self.db:queryComponent("renderMesh") + for entity, tbl in pairs(entities) do + -- get shader + local shader = honey.shader.loadShader(tbl.shader.vertex, tbl.shader.fragment) + shader:use() + + -- bind textures + local texOffset = 0 + for name, texTbl in pairs(tbl.textures or {}) do + local texture = honey.image.loadImage(texTbl.filename, texTbl.params) + gl.BindTexture(gl.TEXTURE_2D + texOffset, texture.texture) + shader:setInt(name, texOffset) + texOffset = texOffset + 1 + end + + -- configure default uniforms + local query = self.db:getComponent(entity, "transform") + local model = (query and query._matrix) or Mat4():identity() + shader:configure{ + float={ + time=glfw.GetTime(), + }, + matrix={ + view=view, + projection=camera.projection, + model=model, + }, + } + + -- draw mesh + local mesh = honey.mesh.loadMesh(tbl.mesh.filename, tbl.mesh.index) + mesh:drawElements() + + -- unbind textures + for i=0,texOffset-1 do + gl.BindTexture(gl.TEXTURE_2D + i, 0) + end end end end, @@ -171,7 +172,15 @@ physics = function(params) local query = self.db:queryComponent("physics") for id, physics in pairs(query) do if not physics._body then + print("creating physics body for "..id) + for k,v in pairs(physics) do + print(k, v) + end physics._body = ode.BodyCreate(self.world) + physics._gc = honey.util.gc_canary(function() + print("releasing physics body for " .. id) + ode.BodyDestroy(physics._body) + end) local mass = ode.MassCreate() local class = physics.mass.class if not class then @@ -183,24 +192,30 @@ physics = function(params) physics.mass.radius ) end + print('mass') ode.BodySetMass(physics._body, mass) local m = self.db:getComponent(id, "transform").matrix + print(m) + print('pos') ode.BodySetPosition( physics._body, m[1][4], m[2][4], m[3][4] ) + print('rot') ode.BodySetRotation( physics._body, m[1][1], m[1][2], m[1][3], m[2][1], m[2][2], m[2][3], m[3][1], m[3][2], m[3][3] ) + print('vel') ode.BodySetLinearVel( physics._body, physics.velocity[1], physics.velocity[2], physics.velocity[3] ) + print('lvel') ode.BodySetAngularVel( physics._body, physics.angularVelocity[1], -- cgit v1.2.1