2025-02-20 17:10:37 +00:00
|
|
|
require("string")
|
2025-02-20 22:12:41 +00:00
|
|
|
require("level")
|
|
|
|
require("render")
|
2025-02-20 17:10:37 +00:00
|
|
|
|
2025-02-20 22:12:41 +00:00
|
|
|
function lerp(a, b, x)
|
|
|
|
return a + ((b - a) * x)
|
2025-02-20 17:10:37 +00:00
|
|
|
end
|
|
|
|
|
2025-02-20 22:12:41 +00:00
|
|
|
function qlerp(a, b, x)
|
|
|
|
return lerp(a, b, x * x)
|
|
|
|
end
|
2025-02-20 17:10:37 +00:00
|
|
|
|
|
|
|
function game_tick()
|
|
|
|
if ctx.udata == nil then
|
|
|
|
ctx.udata = {
|
|
|
|
level = load_level("levels/00.lvl")
|
|
|
|
}
|
2025-02-20 22:12:41 +00:00
|
|
|
ctx.udata.player = {
|
|
|
|
position = ctx.udata.level.classes.player_spawn.position,
|
|
|
|
position_lerp = ctx.udata.level.classes.player_spawn.position,
|
|
|
|
direction = { x = 1, y = 0, z = 0 },
|
|
|
|
direction_lerp = { x = 1, y = 0, z = 0 },
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
input_action { control = "A", name = "turn_left" }
|
|
|
|
input_action { control = "D", name = "turn_right" }
|
|
|
|
input_action { control = "W", name = "walk_forward" }
|
|
|
|
input_action { control = "S", name = "walk_backward" }
|
|
|
|
|
|
|
|
if input_action_just_released { name = "turn_left" } then
|
|
|
|
ctx.udata.player.direction = { x = ctx.udata.player.direction.z, y = ctx.udata.player.direction.y, z = -ctx.udata.player.direction.x }
|
|
|
|
end
|
|
|
|
|
|
|
|
if input_action_just_released { name = "turn_right" } then
|
|
|
|
ctx.udata.player.direction = { x = -ctx.udata.player.direction.z, y = ctx.udata.player.direction.y, z = ctx.udata.player.direction.x }
|
|
|
|
end
|
|
|
|
|
2025-02-20 22:36:46 +00:00
|
|
|
local move = { x = 0, y = 0 }
|
2025-02-20 22:12:41 +00:00
|
|
|
if input_action_just_released { name = "walk_forward" } then
|
2025-02-20 22:36:46 +00:00
|
|
|
move = { x = move.x + ctx.udata.player.direction.z, y = move.y + ctx.udata.player.direction.x }
|
2025-02-20 22:12:41 +00:00
|
|
|
end
|
|
|
|
if input_action_just_released { name = "walk_backward" } then
|
2025-02-20 22:36:46 +00:00
|
|
|
move = { x = move.x - ctx.udata.player.direction.z, y = move.y - ctx.udata.player.direction.x }
|
2025-02-20 17:10:37 +00:00
|
|
|
end
|
2025-02-20 22:12:41 +00:00
|
|
|
|
2025-02-20 22:36:46 +00:00
|
|
|
if ctx.udata.level.grid[ctx.udata.player.position.y + move.y][ctx.udata.player.position.x + move.x].solid ~= nil then
|
|
|
|
move = { x = 0, y = 0 }
|
|
|
|
end
|
|
|
|
|
|
|
|
ctx.udata.player.position = { x = ctx.udata.player.position.x + move.x, y = ctx.udata.player.position.y + move.y }
|
2025-02-20 22:12:41 +00:00
|
|
|
|
|
|
|
ctx.udata.player.position_lerp.x = qlerp(ctx.udata.player.position_lerp.x, ctx.udata.player.position.x, ctx.frame_duration * 30)
|
|
|
|
ctx.udata.player.position_lerp.y = qlerp(ctx.udata.player.position_lerp.y, ctx.udata.player.position.y, ctx.frame_duration * 30)
|
|
|
|
ctx.udata.player.direction_lerp.x = qlerp(ctx.udata.player.direction_lerp.x, ctx.udata.player.direction.x, ctx.frame_duration * 40)
|
|
|
|
ctx.udata.player.direction_lerp.z = qlerp(ctx.udata.player.direction_lerp.z, ctx.udata.player.direction.z, ctx.frame_duration * 40)
|
|
|
|
|
|
|
|
draw_camera {
|
|
|
|
position = {
|
2025-02-20 22:36:46 +00:00
|
|
|
x = ctx.udata.player.position_lerp.y * 2 + 1 - ctx.udata.player.direction.x / 2,
|
2025-02-20 22:12:41 +00:00
|
|
|
y = 1,
|
2025-02-20 22:36:46 +00:00
|
|
|
z = ctx.udata.player.position_lerp.x * 2 + 1 - ctx.udata.player.direction.z / 2,
|
2025-02-20 22:12:41 +00:00
|
|
|
},
|
|
|
|
direction = ctx.udata.player.direction_lerp,
|
|
|
|
}
|
|
|
|
|
|
|
|
render_dungeon(ctx.udata.level)
|
2025-02-20 17:10:37 +00:00
|
|
|
end
|