2025-02-02 01:22:08 +00:00
|
|
|
local FONT = "fonts/Lunchtype21_Regular.ttf"
|
|
|
|
|
2025-02-02 00:58:33 +00:00
|
|
|
local Vector3 = require "vector3"
|
2025-02-02 01:22:08 +00:00
|
|
|
local capture = false
|
|
|
|
|
|
|
|
local head_rotation = 0
|
|
|
|
local rotate_speed = 0.05
|
2025-02-02 00:26:32 +00:00
|
|
|
|
2025-02-02 01:22:08 +00:00
|
|
|
local mouse_sensitivity = 0.01
|
2025-02-02 00:26:32 +00:00
|
|
|
|
|
|
|
local spd = 0.07
|
|
|
|
|
2025-02-02 01:22:08 +00:00
|
|
|
local position = Vector3()
|
|
|
|
local velocity = Vector3()
|
2025-02-02 00:26:32 +00:00
|
|
|
|
|
|
|
local function Vector2(x, y)
|
|
|
|
if y == nil then
|
|
|
|
return {x = x, y = x}
|
|
|
|
end
|
|
|
|
return {x = x, y = y}
|
|
|
|
end
|
|
|
|
|
|
|
|
local function wrap(value, min, max)
|
|
|
|
local range = max - min
|
|
|
|
if value == 0 then
|
|
|
|
return min
|
|
|
|
end
|
|
|
|
|
|
|
|
local result = value - (range * math.floor((value - min) / range))
|
|
|
|
if result == max then
|
|
|
|
return min
|
|
|
|
end
|
|
|
|
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
local function b2n(value)
|
|
|
|
return value and 1 or 0
|
|
|
|
end
|
|
|
|
|
|
|
|
function game_tick()
|
|
|
|
-- ctx.initialization_needed is true first frame and every time dynamic reload is performed
|
|
|
|
if ctx.initialization_needed then
|
|
|
|
-- ctx.udata persists on reload
|
|
|
|
ctx.udata = {}
|
|
|
|
end
|
|
|
|
ctx.mouse_capture = capture
|
2025-02-02 01:22:08 +00:00
|
|
|
input_action{name="toggle_mouse", control="ESCAPE"}
|
2025-02-02 00:26:32 +00:00
|
|
|
|
|
|
|
input_action{name = "left", control = "A"}
|
|
|
|
input_action{name = "right", control = "D"}
|
|
|
|
input_action{name = "forward", control = "W"}
|
|
|
|
input_action{name = "back", control = "S"}
|
|
|
|
|
2025-02-02 01:22:08 +00:00
|
|
|
if input_action_just_pressed{name = "toggle_mouse"} then
|
2025-02-02 00:26:32 +00:00
|
|
|
if not capture then
|
|
|
|
capture = true
|
|
|
|
else
|
|
|
|
capture = false
|
|
|
|
end
|
|
|
|
end
|
2025-02-02 01:22:08 +00:00
|
|
|
|
|
|
|
local dir = Vector3(draw_camera_from_principal_axes{position = position, yaw = head_rotation}.direction)
|
2025-02-02 00:26:32 +00:00
|
|
|
dir.y = 0
|
2025-02-02 00:58:33 +00:00
|
|
|
dir = dir:normalized()
|
2025-02-02 00:26:32 +00:00
|
|
|
|
2025-02-02 01:22:08 +00:00
|
|
|
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
|
2025-02-02 00:26:32 +00:00
|
|
|
|
|
|
|
local mov_d = b2n(input_action_pressed{name = "forward"}) - b2n(input_action_pressed{name = "back"})
|
2025-02-02 01:22:08 +00:00
|
|
|
velocity = dir * spd * mov_d
|
|
|
|
|
|
|
|
position = position + velocity
|
2025-02-02 00:26:32 +00:00
|
|
|
|
|
|
|
draw_billboard{position = Vector3(5, 0, 0), size = Vector2(1, 1), texture = "images/duck.png"}
|
2025-02-02 01:22:08 +00:00
|
|
|
|
|
|
|
draw_text{position = Vector2(0), string = "vel: " .. tostring(velocity), font = FONT}
|
2025-02-02 00:26:32 +00:00
|
|
|
end
|