diff --git a/data/scripts/game.lua b/data/scripts/game.lua index b865acf..b16cb1e 100644 --- a/data/scripts/game.lua +++ b/data/scripts/game.lua @@ -1,13 +1,17 @@ --- called every frame, with constant delta time -local capture = false -local Vector3 = require "vector3" +local FONT = "fonts/Lunchtype21_Regular.ttf" -local rot = 0 -local rot_spd = 0.05 +local Vector3 = require "vector3" +local capture = false + +local head_rotation = 0 +local rotate_speed = 0.05 + +local mouse_sensitivity = 0.01 local spd = 0.07 -local pos = Vector3() +local position = Vector3() +local velocity = Vector3() local function Vector2(x, y) if y == nil then @@ -39,33 +43,40 @@ 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"} + input_action{name="toggle_mouse", control="ESCAPE"} input_action{name = "left", control = "A"} input_action{name = "right", control = "D"} input_action{name = "forward", control = "W"} input_action{name = "back", control = "S"} - if input_action_just_pressed{name = "quit"} then + if input_action_just_pressed{name = "toggle_mouse"} then if not capture then capture = true else capture = false end end - local dir = Vector3(draw_camera_from_principal_axes{position = pos, yaw = rot}.direction) + + local dir = Vector3(draw_camera_from_principal_axes{position = position, yaw = head_rotation}.direction) dir.y = 0 dir = dir:normalized() - local rot_d = b2n(input_action_pressed{name = "right"}) - b2n(input_action_pressed{name = "left"}) - rot = rot + rot_d * rot_spd + if not capture then + local rot_d = b2n(input_action_pressed{name = "right"}) - b2n(input_action_pressed{name = "left"}) + head_rotation = head_rotation + rot_d * rotate_speed + else + head_rotation = head_rotation + mouse_sensitivity * ctx.mouse_movement.x + end 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 = pos + dir * spd * mov_d + velocity = dir * spd * mov_d + + position = position + velocity draw_billboard{position = Vector3(5, 0, 0), size = Vector2(1, 1), texture = "images/duck.png"} + + draw_text{position = Vector2(0), string = "vel: " .. tostring(velocity), font = FONT} end diff --git a/data/scripts/vector3.lua b/data/scripts/vector3.lua index 0141e9c..7856d4d 100644 --- a/data/scripts/vector3.lua +++ b/data/scripts/vector3.lua @@ -129,6 +129,10 @@ function Vector3:length_squared() return x2 + y2 + z2 end +function Vector3:length() + return math.sqrt(self:length_squared()) +end + function Vector3:normalized() local length = math.sqrt(self:length_squared()) return Vector3(self.x / length, self.y / length, self.z / length)