Some camera controls

This commit is contained in:
2024-07-21 13:40:45 +02:00
parent 6d147ee5d0
commit 17029a0a14
6 changed files with 196 additions and 9 deletions

View File

@@ -23,6 +23,13 @@ Ping = {
payload = nil,
}
ControllerState = {
viewX = 0,
viewY = 0,
viewChanged = false,
viewLastUpdated = 0
}
function love.update2()
local now = love.timer.getTime()
if now - Ping.timeSent > 5 then
@@ -34,6 +41,24 @@ function love.update2()
love.mqtt.send("command/ping", Ping.payload)
print("Sending ping")
end
if ControllerState.viewChanged and (now - ControllerState.viewLastUpdated) >= 0.05 then
love.mqtt.send("command/set_camera_xy", toJSON({
x = -ControllerState.viewX * 0.3 + 0.5,
y = -ControllerState.viewY * 0.3 + 0.5
}))
ControllerState.viewChanged = false
ControllerState.viewLastUpdated = now
end
end
function love.joystickaxis2(joystick, axis, value)
if axis == 3 and value ~= ControllerState.viewX then
ControllerState.viewX = value
ControllerState.viewChanged = true
elseif axis == 4 and value ~= ControllerState.viewY then
ControllerState.viewY = value
ControllerState.viewChanged = true
end
end
function formatSafe(format, value, ...)
@@ -81,3 +106,22 @@ function love.gamepadpressed2(joystick, button)
UIState.showUI = not UIState.showUI
end
end
function toJSON(arg)
local t = type(arg)
if t == "number" then
return tostring(arg)
elseif t == "nil" then
return "null"
elseif t == "string" then
return '"' .. arg .. '"'
elseif t == "boolean" then
return tostring(arg)
elseif t == "table" then
local fields = {}
for key, value in pairs(arg) do
fields[#fields+1] = string.format('"%s":%s', key, toJSON(value))
end
return '{' .. table.concat(fields, ',') .. '}'
end
end