Compare commits

...

2 Commits

Author SHA1 Message Date
2515fe7b27
add throw signal to player 2025-02-14 15:17:05 +03:00
aff1bfd247
add signal module 2025-02-14 15:16:42 +03:00
5 changed files with 82 additions and 0 deletions

View File

@ -1,5 +1,6 @@
local Vector3 = require("types.vector3")
local util = require("util")
local Signal = require("types.signal")
local Player = {
position = Vector3(0, 1, 0),
@ -10,6 +11,8 @@ local Player = {
yaw = 0,
yaw_speed = 0.05,
ThrowPressed = Signal.new(),
}
function Player:tick(ctx)
@ -18,6 +21,8 @@ function Player:tick(ctx)
input_action{name = "forward", control = "W"}
input_action{name = "back", control = "S"}
input_action{name = "throw", control = "SPACE"}
local camera_forward = Vector3(draw_camera_from_principal_axes(self).direction)
camera_forward.y = 0
camera_forward = camera_forward:normalized()
@ -29,6 +34,10 @@ function Player:tick(ctx)
local direction = ((camera_forward * forward_input) + (camera_right * strafe_input)):normalized()
self.velocity = direction * self.speed
if input_action_just_pressed{name = "throw"} then
self.ThrowPressed:emit(self.position:copy(), camera_forward:copy())
end
if ctx.mouse_capture then
self.yaw = self.yaw + self.mouse_sensitivity * ctx.mouse_movement.x
end

View File

@ -1,6 +1,7 @@
local player = require "classes.player"
local util = require "util"
local Vector3 = require "types.vector3"
local Signal = require("types.signal")
-- called every frame, with constant delta time
function game_tick()
@ -10,6 +11,11 @@ function game_tick()
ctx.udata = {
capture = false
}
player.ThrowPressed:connect(
function (position, direction)
print(position, " ", direction)
end
)
end
ctx.mouse_capture = ctx.udata.capture

View File

@ -0,0 +1,30 @@
local util = require "util"
local Signal = {}
Signal.__index = Signal
function Signal:connect(f)
util.list_insert_once(self._connections, f)
end
function Signal:disconnect(f)
util.list_remove_value(self._connections, f)
end
function Signal:emit(...)
-- don't care about order
for _, v in pairs(self._connections) do
v(...)
end
end
function Signal.new()
local s = {
_connections = {},
}
return setmetatable(s, Signal)
end
return Signal

View File

@ -211,6 +211,11 @@ function Vector3:rotated(axis, angle)
return Vector3(v)
end
---Returns a copy of this vector.
---@return Vector3
function Vector3:copy()
return Vector3(self)
end
---- CONSTANTS
Vector3.UP = Vector3(0, 1, 0)

View File

@ -65,4 +65,36 @@ function util.merge(t1, t2, overwrite)
return t3
end
---Finds the index of value in list t. If the value does not exist in the list, returns -1.
---@param t table
---@param value any
---@return integer
function util.list_find(t, value)
local idx = -1
for i, v in ipairs(t) do
if v == value then
idx = i
break
end
end
return idx
end
---Removes an element from a list, if it exists. Does nothing if the value doesn't exist.
---@param t table
---@param value any
function util.list_remove_value(t, value)
local idx = util.list_find(t, value)
if idx ~= -1 then
table.remove(t, idx)
end
end
---Inserts value into the list only if it does not exist.
---@param t table
---@param value any
function util.list_insert_once(t, value)
if util.list_find(t, value) ~= -1 then return end
table.insert(t, value)
end
return util