diff options
-rw-r--r-- | honey/ecs-systems.lua | 24 | ||||
-rw-r--r-- | honey/ecs.lua | 56 | ||||
-rw-r--r-- | main.lua | 130 | ||||
-rw-r--r-- | save | 6 |
4 files changed, 86 insertions, 130 deletions
diff --git a/honey/ecs-systems.lua b/honey/ecs-systems.lua index 54bac89..47c0f6d 100644 --- a/honey/ecs-systems.lua +++ b/honey/ecs-systems.lua @@ -157,9 +157,11 @@ end --===== physics =====-- + physics = function(params) local interval = params.interval or 0.016 local groupSize = params.groupSize or 20 + local refs = {} return { db=params.db, space=params.space, @@ -169,14 +171,21 @@ physics = function(params) priority=0, update=function(self, dt) + for i, ref in ipairs(refs) do + print(i, ref.tbl, ref.physics) + end local query = self.db:queryComponent("physics") + for id, physics in pairs(query) do if not physics._body then - physics._body = ode.BodyCreate(self.world) + print("add physics body for "..id) + local body = ode.BodyCreate(self.world) physics._gc = honey.util.gc_canary(function() print("releasing physics body for " .. id) - ode.BodyDestroy(physics._body) + ode.BodyDestroy(body) + body = nil end) + local mass = ode.MassCreate() local class = physics.mass.class if not class then @@ -188,30 +197,31 @@ physics = function(params) physics.mass.radius ) end - ode.BodySetMass(physics._body, mass) + ode.BodySetMass(body, mass) local m = self.db:getComponent(id, "transform").matrix ode.BodySetPosition( - physics._body, + body, m[1][4], m[2][4], m[3][4] ) ode.BodySetRotation( - physics._body, + 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] ) ode.BodySetLinearVel( - physics._body, + body, physics.velocity[1], physics.velocity[2], physics.velocity[3] ) ode.BodySetAngularVel( - physics._body, + body, physics.angularVelocity[1], physics.angularVelocity[2], physics.angularVelocity[3] ) + physics._body = body end end diff --git a/honey/ecs.lua b/honey/ecs.lua index d544a37..260c389 100644 --- a/honey/ecs.lua +++ b/honey/ecs.lua @@ -77,11 +77,13 @@ end -- load database from file function EntityDb.load(self, filename) + print(collectgarbage("count")) self.entities = {} self.components = {} + collectgarbage() + print(collectgarbage("count")) local env = { Entity = function(id, components) - print("add entity:", id) self:createEntity(id) self:addComponents(id, components) end, @@ -261,56 +263,4 @@ 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 @@ -35,25 +35,8 @@ end) local entities = ecs.EntityDb() local systems = ecs.SystemDb(entities) -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}), - }, - z = {value=-60}, - script={ - script="cameraRotationScript", - }, - onKey = { - script="cameraKeyHandler", - }, -}) - systems:addSystem(sys.transform) -systems:addSystem(sys.renderCamera, {camera=camera}) +systems:addSystem(sys.renderCamera) systems:addSystem(sys.script) systems:addSystem(sys.physics, {space=space, world=world}) package.loaded['baseRotationScript'] = function(entities, id, dt) @@ -76,39 +59,61 @@ package.loaded['cameraKeyHandler'] = function(entities, id, data) end end - -local id = entities:createEntity() -entities:addComponents(id, { - renderMesh = { - textures = { ourTexture={ filename="77155.png" } }, - shader = { vertex="vertex.glsl", fragment="fragment.glsl" }, - mesh = { filename="assets/icosahedron.obj", index=1 }, - }, - transform = { - matrix = Mat4():identity():rotateZ(math.rad(45)), - }, - physics = { - mass = { - class = "sphere", - density = 1, - radius = 1, +function setupEntities() + local camera = entities:createEntity() + entities:addComponents(camera, { + camera={ + projection=Mat4():perspective(math.rad(45), 640/480, 0.1, 100), }, - velocity = Vec3{ 0, 10, 0 }, - angularVelocity = Vec3{ 0, 1, 0 }, - }, -}) - -local id2 = entities:createEntity() -entities:addComponents(id2, { - renderMesh = { - shader = { vertex="vertex.glsl", fragment="fragment.glsl" }, - mesh = { filename="assets/tetrahedron.obj", index=1 }, - }, - transform = { - parent=id, - matrix=Mat4():identity():translate(Vec3{0, 2, 0}), - }, -}) + transform={ + matrix=Mat4():identity():translate(Vec3{0, 0, -6}), + }, + z = {value=-60}, + script={ + script="cameraRotationScript", + }, + onKey = { + script="cameraKeyHandler", + }, + onWindowResize = { script = "cameraHandleResize" }, + }) + + + + local id = entities:createEntity() + entities:addComponents(id, { + renderMesh = { + textures = { ourTexture={ filename="77155.png" } }, + shader = { vertex="vertex.glsl", fragment="fragment.glsl" }, + mesh = { filename="assets/icosahedron.obj", index=1 }, + }, + transform = { + matrix = Mat4():identity():rotateZ(math.rad(45)), + }, + physics = { + mass = { + class = "sphere", + density = 1, + radius = 1, + }, + velocity = Vec3{ 0, 10, 0 }, + angularVelocity = Vec3{ 0, 1, 0 }, + }, + }) + + local id2 = entities:createEntity() + entities:addComponents(id2, { + renderMesh = { + shader = { vertex="vertex.glsl", fragment="fragment.glsl" }, + mesh = { filename="assets/tetrahedron.obj", index=1 }, + }, + transform = { + parent=id, + matrix=Mat4():identity():translate(Vec3{0, 2, 0}), + }, + }) +end +setupEntities() @@ -121,6 +126,7 @@ window:setKeyCallback(function(_, key, scancode, action) elseif key == glfw.KEY_SPACE then entities:save("save") elseif key == glfw.KEY_L then + entities:load("save") end end end) @@ -136,29 +142,19 @@ package.loaded["cameraHandleResize"] = function(entities, id, data) local camera = entities:getComponent(id, "camera") camera.projection:perspectiveResize(data.width/data.height) end -entities:addComponent(camera, "onWindowResize", { script = "cameraHandleResize" }) - --- averager (for fps) -function averager(memory) - local buf = {} - local avg = 0 - for i=1,memory do table.insert(buf, 0) end - return function(value) - table.insert(buf, value) - local val = table.remove(buf, 1) - avg = avg + value - val - return avg / memory - end -end -local fpsAverage = averager(200) - -entities:load("save") +--entities:load("save") -- main loop +local time = 0 honey.loop(window, function(dt) + time = time + dt + if time > 1 then + time = time-1 + print(collectgarbage("count")) + end gl.ClearColor(0.2, 0.4, 1.0, 1.0) gl.Clear(gl.COLOR_BUFFER_BIT + gl.DEPTH_BUFFER_BIT + gl.STENCIL_BUFFER_BIT) @@ -1,3 +1,3 @@ -Entity("84c91244-b225-4757-9ae2-501e4b933e12", {script={script="cameraRotationScript"},z={value=-60},onKey={script="cameraKeyHandler"},transform={matrix=Mat4{1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000, -59.2666, 0.0000, 0.0000, 0.0000, 1.0000}},camera={projection=Mat4{1.8107, 0.0000, 0.0000, 0.0000, 0.0000, 2.4142, 0.0000, 0.0000, 0.0000, 0.0000, -1.0020, -0.2002, 0.0000, 0.0000, -1.0000, 0.0000}},onWindowResize={script="cameraHandleResize"}}) -Entity("2a902813-02b8-49d6-9602-76ec6394cc86", {transform={matrix=Mat4{0.5222, -0.5222, 0.6743, 0.0000, 0.7071, 0.7071, -0.0000, 4.6250, -0.4768, 0.4768, 0.7385, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000}},renderMesh={textures={ourTexture={filename="77155.png"}},mesh={filename="assets/icosahedron.obj",index=1},shader={fragment="fragment.glsl",vertex="vertex.glsl"}},physics={velocity=Vec3{0.0000, 2.6000, 0.0000},angularVelocity=Vec3{0.0000, 1.0000, 0.0000},mass={radius=1,class="sphere",density=1}}}) -Entity("1dd0a59e-83ed-4358-a493-4a3c604f10bf", {transform={parent="2a902813-02b8-49d6-9602-76ec6394cc86",matrix=Mat4{1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000, 0.0000, 2.0000, 0.0000, 0.0000, 1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000}},renderMesh={shader={fragment="fragment.glsl",vertex="vertex.glsl"},mesh={filename="assets/tetrahedron.obj",index=1}}}) +Entity("3b5d66ec-e448-4d45-81b4-55ea9664c2f9", {camera={projection=Mat4{1.8107, 0.0000, 0.0000, 0.0000, 0.0000, 2.4142, 0.0000, 0.0000, 0.0000, 0.0000, -1.0020, -0.2002, 0.0000, 0.0000, -1.0000, 0.0000}},z={value=-60},script={script="cameraRotationScript"},onKey={script="cameraKeyHandler"},onWindowResize={script="cameraHandleResize"},transform={matrix=Mat4{1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000, -59.0040, 0.0000, 0.0000, 0.0000, 1.0000}}}) +Entity("8513af47-a427-4ff0-a1fc-bfe80c1e5c68", {renderMesh={shader={vertex="vertex.glsl",fragment="fragment.glsl"},mesh={index=1,filename="assets/icosahedron.obj"},textures={ourTexture={filename="77155.png"}}},physics={velocity=Vec3{0.0000, -12.7200, 0.0000},angularVelocity=Vec3{0.0000, 1.0000, -0.0000},mass={density=1,radius=1,class="sphere"}},transform={matrix=Mat4{-0.4562, 0.4562, 0.7641, 0.0000, 0.7071, 0.7071, 0.0000, -3.2717, -0.5403, 0.5403, -0.6451, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000}}}) +Entity("fe3dd9ef-198c-4708-8d23-a05b06442dbf", {renderMesh={mesh={index=1,filename="assets/tetrahedron.obj"},shader={vertex="vertex.glsl",fragment="fragment.glsl"}},transform={parent="8513af47-a427-4ff0-a1fc-bfe80c1e5c68",matrix=Mat4{1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000, 0.0000, 2.0000, 0.0000, 0.0000, 1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 1.0000}}}) |