This repository has been archived on 2025-02-14. You can view files and clone it, but cannot push or open issues or pull requests.
quack-twn/data/scripts/classes/player.lua

41 lines
1.2 KiB
Lua
Raw Normal View History

local Vector3 = require "vector3"
local util = require "util"
-- this is a static class, so no instancing shenanigans needed
local Player = {
position = Vector3(),
velocity = Vector3(),
speed = 0.07,
mouse_sensitivity = 0.01,
yaw = 0,
yaw_speed = 0.05,
}
function Player:tick(ctx)
input_action{name = "left", control = "A"}
input_action{name = "right", control = "D"}
input_action{name = "forward", control = "W"}
input_action{name = "back", control = "S"}
local camera_forward = Vector3(draw_camera_from_principal_axes(self).direction)
camera_forward.y = 0
camera_forward = camera_forward:normalized()
local camera_right = camera_forward:cross(Vector3.UP)
local forward_input = util.b2n(input_action_pressed{name = "forward"}) - util.b2n(input_action_pressed{name = "back"})
local strafe_input = util.b2n(input_action_pressed{name = "right"}) - util.b2n(input_action_pressed{name = "left"})
local direction = ((camera_forward * forward_input) + (camera_right * strafe_input)):normalized()
self.velocity = direction * self.speed
if ctx.mouse_capture then
self.yaw = self.yaw + self.mouse_sensitivity * ctx.mouse_movement.x
end
self.position = self.position + self.velocity
end
return Player