66 lines
2.3 KiB
Lua
66 lines
2.3 KiB
Lua
local Vector3 = require("types.vector3")
|
|
local Signal = require("types.signal")
|
|
|
|
local Player = {
|
|
position = Vector3(0, 1, 0),
|
|
velocity = Vector3(),
|
|
|
|
speed = 0.07,
|
|
mouse_sensitivity = 0.01,
|
|
|
|
yaw = 0,
|
|
yaw_speed = 0.05,
|
|
|
|
accel = 0.3,
|
|
|
|
world = nil,
|
|
|
|
ThrowPressed = Signal.new(),
|
|
}
|
|
|
|
local CAMERA_OFFSET = Vector3(0, -1, 0)
|
|
|
|
function Player:init(world)
|
|
local x,y,z = ((self.position - Vector3(0.25, 1.0, 0.25)) * UNIT_SIZE):decomposed()
|
|
local w,h,d = (Vector3(0.25, 1.1, 0.25) * UNIT_SIZE):decomposed()
|
|
world:add(self, x,y,z,w,h,d)
|
|
self.world = world
|
|
end
|
|
|
|
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"}
|
|
|
|
input_action{name = "throw", control = "LCLICK"}
|
|
|
|
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()
|
|
local target_vel = direction * self.speed
|
|
if input_action_just_pressed{name = "throw"} then
|
|
self.ThrowPressed:emit(self.position:copy(), camera_forward:copy())
|
|
end
|
|
self.velocity = self.velocity:lerp(target_vel, self.accel)
|
|
local goal = ((self.position + CAMERA_OFFSET) + self.velocity) * UNIT_SIZE
|
|
local actual_x, actual_y, actual_z, cols, len = self.world:move(self, goal.x, goal.y, goal.z)
|
|
-- for i = 1, len do
|
|
-- print(util.printt(cols[i].other))
|
|
-- end
|
|
self.position = Vector3(actual_x / UNIT_SIZE, actual_y / UNIT_SIZE, actual_z / UNIT_SIZE) - CAMERA_OFFSET
|
|
|
|
if ctx.mouse_capture then
|
|
self.yaw = self.yaw + self.mouse_sensitivity * ctx.mouse_movement.x
|
|
end
|
|
draw_text{font = "fonts/Lunchtype21_Regular.ttf", position = {x = 0, y = 0}, string = table.concat(table.pack(self.world:getCube(self)), ";"), height = 14}
|
|
draw_text{font = "fonts/Lunchtype21_Regular.ttf", position = {x = 0, y = 14}, string = tostring(self.position), height = 14}
|
|
end
|
|
|
|
return Player |