Compare commits

..

2 commits

153
main.lua
View file

@ -1,7 +1,6 @@
-- Bootstrap -- Bootstrap
appName = "lovr-playspace" appName = "lovr-playspace"
-- Function to get the current time
function getCurrentTime() function getCurrentTime()
local time = os.time() local time = os.time()
local date = os.date("*t", time) local date = os.date("*t", time)
@ -11,21 +10,21 @@ end
-- App -- App
hands = {"hand/right","hand/left"} hands = {"hand/right","hand/left"}
limbs = { limbs = {
"head", "head",
"hand/left", "hand/left",
"hand/right", "hand/right",
"hand/left/grip", "hand/left/grip",
"hand/right/grip", "hand/right/grip",
"elbow/left", "elbow/left",
"elbow/right", "elbow/right",
"shoulder/left", "shoulder/left",
"shoulder/right", "shoulder/right",
"chest", "chest",
"waist", "waist",
"knee/left", "knee/left",
"knee/right", "knee/right",
"foot/left", "foot/left",
"foot/right" "foot/right"
} }
configDirs = {} configDirs = {}
@ -99,9 +98,9 @@ function getClosestDistanceToPerimeter(x, y, z, points)
end end
function getButton(method,button,devices) function getButton(method,button,devices)
for _,device in ipairs(devices) do for _,device in ipairs(devices) do
if method(device,button) == true then return device end if method(device,button) == true then return device end
end end
end end
function drawSinglePointGrid(pass, point1, point2, cornerColor, miscColor) function drawSinglePointGrid(pass, point1, point2, cornerColor, miscColor)
@ -177,38 +176,39 @@ function lovr.load()
color_close_grid = {0.45, 0.69, 0.79, 0.5}, color_close_grid = {0.45, 0.69, 0.79, 0.5},
color_far_corners = {0.45, 0.69, 0.79, 0}, color_far_corners = {0.45, 0.69, 0.79, 0},
color_far_grid = {0.45, 0.69, 0.79, 0}, color_far_grid = {0.45, 0.69, 0.79, 0},
show_time = 0,
points = {} points = {}
} }
-- Helper function to read file with fallback to default -- Helper function to read file with fallback to default
local function loadSetting(filename, default, parser) local function loadSetting(filename, default, parser)
print("Checking file:", filename) print("Checking file:", filename)
if not lovr.filesystem.isFile(filename) then if not lovr.filesystem.isFile(filename) then
print("File doesn't exist, creating:", filename) print("File doesn't exist, creating:", filename)
local valueToSave local valueToSave
if type(default) == "table" then if type(default) == "table" then
valueToSave = json.encode(default) valueToSave = json.encode(default)
else else
valueToSave = tostring(default) valueToSave = tostring(default)
end end
local success = lovr.filesystem.write(filename, valueToSave) local success = lovr.filesystem.write(filename, valueToSave)
print("Write success:", success, "for", filename, "with value:", valueToSave) print("Write success:", success, "for", filename, "with value:", valueToSave)
return default return default
end end
-- File exists, try to read it -- File exists, try to read it
local content = lovr.filesystem.read(filename) local content = lovr.filesystem.read(filename)
if content and parser then if content and parser then
local success, value = pcall(parser, content) local success, value = pcall(parser, content)
if success then return value end if success then return value end
elseif content then elseif content then
return content return content
end end
return default return default
end end
-- Initialize settings with fallbacks -- Initialize settings with fallbacks
settings = { settings = {
@ -223,6 +223,7 @@ function lovr.load()
color_close_grid = loadSetting("color_close_grid.json", defaults.color_close_grid, json.decode), color_close_grid = loadSetting("color_close_grid.json", defaults.color_close_grid, json.decode),
color_far_corners = loadSetting("color_far_corners.json", defaults.color_far_corners, json.decode), color_far_corners = loadSetting("color_far_corners.json", defaults.color_far_corners, json.decode),
color_far_grid = loadSetting("color_far_grid.json", defaults.color_far_grid, json.decode), color_far_grid = loadSetting("color_far_grid.json", defaults.color_far_grid, json.decode),
show_time = loadSetting("show_time.json", defaults.show_time, json.decode),
points = {}, points = {},
transformed = false transformed = false
} }
@ -303,37 +304,37 @@ function modeConfigure(pass)
end end
end end
local inputDev = getButton(lovr.headset.wasReleased,settings.action_button,hands) local inputDev = getButton(lovr.headset.wasReleased,settings.action_button,hands)
if inputDev ~= nil and lovr.headset.isTracked(inputDev) then if inputDev ~= nil and lovr.headset.isTracked(inputDev) then
local cx, _, cz = lovr.headset.getPosition(inputDev) local cx, _, cz = lovr.headset.getPosition(inputDev)
table.insert(settings.points,{cx,cz}) table.insert(settings.points,{cx,cz})
end end
inputDev = getButton(lovr.headset.isDown,settings.action_button,hands) inputDev = getButton(lovr.headset.isDown,settings.action_button,hands)
if inputDev ~= nil then if inputDev ~= nil then
saveProg = saveProg - (deltaTime / 3) saveProg = saveProg - (deltaTime / 3)
if saveProg <= 0 then if saveProg <= 0 then
local floorSpacePoints = {} local floorSpacePoints = {}
for _,point in ipairs(settings.points) do for _,point in ipairs(settings.points) do
local floorSpacePoint = lovr.math.vec3(point[1], 0, point[2]):transform(floorMatrixInv) local floorSpacePoint = lovr.math.vec3(point[1], 0, point[2]):transform(floorMatrixInv)
local x, _, z = floorSpacePoint:unpack() local x, _, z = floorSpacePoint:unpack()
table.insert(floorSpacePoints,{x,z}) table.insert(floorSpacePoints,{x,z})
end end
lovr.filesystem.write("points.json", json.encode(floorSpacePoints)) lovr.filesystem.write("points.json", json.encode(floorSpacePoints))
deinitConfigure() deinitConfigure()
modeDraw(pass) modeDraw(pass)
return return
end end
else else
saveProg = 1.0 saveProg = 1.0
end end
pass:setColor(1,0,0,0.5) pass:setColor(1,0,0,0.5)
for _,point in ipairs(settings.points) do for _,point in ipairs(settings.points) do
pass:sphere(point[1],1.5,point[2],0.1) pass:sphere(point[1],1.5,point[2],0.1)
end end
modeDraw(pass) modeDraw(pass)
end end
function modeDraw(pass) function modeDraw(pass)
@ -392,15 +393,17 @@ end
function lovr.draw(pass) function lovr.draw(pass)
mode(pass) mode(pass)
local hx, hy, hz = lovr.headset.getPosition("head") if settings.show_time == 1 then
local hangle, hax, hay, haz = lovr.headset.getOrientation("head") local hx, hy, hz = lovr.headset.getPosition("head")
local currentTime = getCurrentTime() local hangle, hax, hay, haz = lovr.headset.getOrientation("head")
pass:setColor(1, 1, 1, 0.25) local currentTime = getCurrentTime()
local transform = lovr.math.newMat4() pass:setColor(1, 1, 1, 0.25)
transform:translate(hx, hy + 1.5, hz) local transform = lovr.math.newMat4()
transform:rotate(math.pi / 2, 1, 0, 0) transform:translate(hx, hy + 1.5, hz)
transform:rotate(math.pi - math.atan2(hax, haz)*2, 0, 0, 1) transform:rotate(math.pi / 2, 1, 0, 0)
transform:translate(0, -0.1, 0) transform:rotate(math.pi - math.atan2(hax, haz)*2, 0, 0, 1)
transform:scale(0.1, 0.1, 0.1) transform:translate(0, -0.1, 0)
pass:text(currentTime, transform) transform:scale(0.1, 0.1, 0.1)
pass:text(currentTime, transform)
end
end end