diff options
Diffstat (limited to 'main.lua')
-rw-r--r-- | main.lua | 86 |
1 files changed, 56 insertions, 30 deletions
@@ -7,6 +7,7 @@ local Mat4 = honey.Mat4 local Quaternion = honey.Quaternion local ecs = honey.ecs local sys = honey.standardSystems +local dispatch = sys.dispatch local ode = honey.ode local nvg = honey.nvg @@ -19,6 +20,16 @@ local vg = nvg.CreateContext() local november = nvg.CreateFont(vg, "November", "assets/november.regular.ttf") local vw, vh = 640, 480 +-- setup physics +local space = ode.HashSpaceCreate(ode.Space0) +local world = ode.WorldCreate() +ode.WorldSetGravity(world, 0, -10, 0) +ode.WorldSetCFM(world, 1e-5) +local physicsGc = honey.util.gc_canary(function() + ode.SpaceDestroy(space) + ode.WorldDestroy(world) +end) + -- setup ecs local entities = ecs.EntityDb() @@ -32,63 +43,78 @@ entities:addComponents(camera, { transform={ matrix=Mat4():identity():translate(Vec3{0, 0, -6}), }, + z = {value=-6}, script={ script="cameraRotationScript", }, + onKey = { + script="cameraKeyHandler", + }, }) ---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) +systems:addSystem(sys.physics, {space=space, world=world}) package.loaded['baseRotationScript'] = function(entities, id, dt) local transform = entities:getComponent(id, "transform") transform.matrix:rotateZ(math.pi * dt) end 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())}) + local z = entities:getComponent(id, "z") + transform.matrix + :identity() + :translate(Vec3{0, 0, z.value + math.sin(math.pi * glfw.GetTime())}) +end +package.loaded['cameraKeyHandler'] = function(entities, id, data) + local z = entities:getComponent(id, "z") + if data.key == glfw.KEY_W then + z.value = z.value + 1 + elseif data.key == glfw.KEY_S then + z.value = z.value - 1 + end end local id = entities:createEntity() -entities:addComponent(id, "renderMesh", { - textures = { - ourTexture={ filename="77155.png" }, +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, 0, 0 }, + angularVelocity = Vec3{ 0, 0, 0 }, }, - shader = { vertex="vertex.glsl", fragment="fragment.glsl" }, - mesh = { filename="assets/icosahedron.obj", index=1 }, -}) -entities:addComponent(id, "transform", { - matrix = Mat4():identity():rotateZ(math.rad(45)), -}) -entities:addComponent(id, "script", { - script = "baseRotationScript", }) local id2 = entities:createEntity() -entities:addComponent(id2, "renderMesh", { - shader = { vertex="vertex.glsl", fragment="fragment.glsl" }, - mesh = { filename="assets/tetrahedron.obj", index=1 }, -}) -entities:addComponent(id2, "transform", { - parent=id, - matrix=Mat4():identity():translate(Vec3{0, 2, 0}), +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}), + }, }) -- close window on ESCAPE key window:setKeyCallback(function(_, key, scancode, action) + dispatch(entities, "onKey", {key=key, scancode=scancode, action=action}) if action == glfw.PRESS then if key == glfw.KEY_ESCAPE then window:setShouldClose(true) @@ -100,7 +126,7 @@ end) window:setFramebufferSizeCallback(function(_, width, height) gl.Viewport(0, 0, width, height) vw, vh = width, height - sys.dispatch(entities, "onWindowResize", { width=width, height=height }) + dispatch(entities, "onWindowResize", { width=width, height=height }) end) package.loaded["cameraHandleResize"] = function(entities, id, data) |