From 02c4525b87067c355e5af9f2d7656b40435e5d37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Sun, 2 Feb 2025 03:58:33 +0300 Subject: [PATCH] gut trig and use vector3 lib in game script --- data/scripts/game.lua | 21 +++++++-------------- data/scripts/trig.lua | 35 ----------------------------------- 2 files changed, 7 insertions(+), 49 deletions(-) diff --git a/data/scripts/game.lua b/data/scripts/game.lua index 081d58b..b865acf 100644 --- a/data/scripts/game.lua +++ b/data/scripts/game.lua @@ -1,22 +1,13 @@ -- called every frame, with constant delta time local capture = false -local trig = require "trig" -local Vector3l = require "vector3" - - -local function Vector3(x, y, z) - if y == nil then - return {x = x, y = x, z = x} - end - return {x = x, y = y, z = z} -end +local Vector3 = require "vector3" local rot = 0 local rot_spd = 0.05 local spd = 0.07 -local pos = Vector3(0) +local pos = Vector3() local function Vector2(x, y) if y == nil then @@ -48,6 +39,7 @@ function game_tick() if ctx.initialization_needed then -- ctx.udata persists on reload ctx.udata = {} + print(Vector3(5, 0, 0)) end ctx.mouse_capture = capture input_action{name="quit", control="SPACE"} @@ -64,15 +56,16 @@ function game_tick() capture = false end end - local dir = draw_camera_from_principal_axes{position = pos, yaw = rot}.direction + local dir = Vector3(draw_camera_from_principal_axes{position = pos, yaw = rot}.direction) dir.y = 0 - dir = trig.v3_normalized(dir) + dir = dir:normalized() local rot_d = b2n(input_action_pressed{name = "right"}) - b2n(input_action_pressed{name = "left"}) rot = rot + rot_d * rot_spd local mov_d = b2n(input_action_pressed{name = "forward"}) - b2n(input_action_pressed{name = "back"}) - pos = trig.v3_add(pos, trig.v3_mult(dir, spd * mov_d)) + -- pos = trig.v3_add(pos, trig.v3_mult(dir, spd * mov_d)) + pos = pos + dir * spd * mov_d draw_billboard{position = Vector3(5, 0, 0), size = Vector2(1, 1), texture = "images/duck.png"} end diff --git a/data/scripts/trig.lua b/data/scripts/trig.lua index 26bd0a8..72d5050 100644 --- a/data/scripts/trig.lua +++ b/data/scripts/trig.lua @@ -1,38 +1,3 @@ local M = {} -function M.v3_length_squared(v) - local x2 = v.x * v.x - local y2 = v.y * v.y - local z2 = v.z * v.z - - return x2 + y2 + z2 -end - -function M.v3_normalized(v) - local length = math.sqrt(M.v3_length_squared(v)) - local new_vec = { - x = v.x / length, - y = v.y / length, - z = v.z / length, - } - - return new_vec -end - -function M.v3_mult(v, f) - return { - x = v.x * f, - y = v.y * f, - z = v.z * f, - } -end - - -function M.v3_add(v1, v2) - return { - x = v1.x + v2.x, - y = v1.y + v2.y, - z = v1.z + v2.z, - } -end return M \ No newline at end of file