add camera logic

This commit is contained in:
Justus Wolff
2026-02-15 11:29:23 +01:00
parent f3800ec226
commit fa6b4fc064

View File

@@ -74,12 +74,14 @@ local function newdesign()
y = 1 y = 1
} }
local currentfloor = 1 local currentfloor = 1
local camx,camy = 0,0
while true do while true do
-- render buf -- render buf
reset() reset()
for x=1,dimensions["x"],1 do for x=1+camx,dimensions["x"]+camx,1 do
for z=1,dimensions["z"],1 do for z=1+camy,dimensions["z"]+camy,1 do
local currentbuf = buf[posasstring(x, currentfloor, z)] local currentbuf = buf[posasstring(x, currentfloor, z)]
if currentbuf then if currentbuf then
term.setCursorPos(x, z) term.setCursorPos(x, z)
@@ -98,6 +100,8 @@ local function newdesign()
-- user input -- user input
local event = table.pack(os.pullEvent()) local event = table.pack(os.pullEvent())
if event[1] == "mouse_click" or event[1] == "mouse_drag" then if event[1] == "mouse_click" or event[1] == "mouse_drag" then
event[3] = event[3]+camx
event[4] = event[4]+camy
if event[3] <= dimensions["x"] and event[4] <= dimensions["z"] then if event[3] <= dimensions["x"] and event[4] <= dimensions["z"] then
if event[2] == 1 then -- left button, set if event[2] == 1 then -- left button, set
buf[posasstring(event[3], currentfloor, event[4])] = true buf[posasstring(event[3], currentfloor, event[4])] = true
@@ -107,67 +111,87 @@ local function newdesign()
end end
end end
end end
if event[1] == "key" and keys.getName(event[2]) == "leftCtrl" then -- menu if event[1] == "key" then
local action = selopt({ if keys.getName(event[2]) == "leftCtrl" then -- menu
"Save", local action = selopt({
"Load", "Save",
"Exit", "Load",
"Change X size", "Exit",
"Change floor amount", "Change X size",
"Change Z size" "Change floor amount",
}, "menu") "Change Z size"
if action == 3 then return end -- exit }, "menu")
if action == 1 then -- save if action == 3 then return end -- exit
reset() if action == 1 then -- save
for _,v in pairs(fs.list("designs")) do reset()
term.write(v) for _,v in pairs(fs.list("designs")) do
incline() term.write(v)
end incline()
write("Enter name: ") end
local name = read() write("Enter name: ")
local file = fs.open("designs/"..name, "w") local name = read()
file.write(textutils.serialiseJSON({ local file = fs.open("designs/"..name, "w")
["dimensions"] = dimensions, file.write(textutils.serialiseJSON({
["buf"] = buf ["dimensions"] = dimensions,
})) ["buf"] = buf
file.close() }))
end
if action == 2 then -- load
reset()
for _,v in pairs(fs.list("designs")) do
term.write(v)
incline()
end
write("Enter name: ")
local name = read()
if not fs.exists("designs/"..name) or name == "" then
printError("Design not found!")
else
local file = fs.open("designs/"..name, "r")
local content = file.readAll()
file.close() file.close()
content = textutils.unserialiseJSON(content) end
dimensions = content["dimensions"] if action == 2 then -- load
buf = content["buf"] reset()
for _,v in pairs(fs.list("designs")) do
term.write(v)
incline()
end
write("Enter name: ")
local name = read()
if not fs.exists("designs/"..name) or name == "" then
printError("Design not found!")
else
local file = fs.open("designs/"..name, "r")
local content = file.readAll()
file.close()
content = textutils.unserialiseJSON(content)
dimensions = content["dimensions"]
buf = content["buf"]
end
end
if action == 4 then -- change X size
reset()
write("Enter new X size: ")
local xsize = tonumber(read())
dimensions["x"] = xsize
end
if action == 5 then -- change floor amount
reset()
write("Enter new floor amount: ")
local ysize = tonumber(read())
dimensions["y"] = ysize
end
if action == 6 then -- change Z size
reset()
write("Enter new Z size: ")
local zsize = tonumber(read())
dimensions["z"] = zsize
end end
end end
if action == 4 then -- change X size if keys.getName(event[2]) == "q" and currentfloor < dimensions["y"] then -- go up
reset() currentfloor = currentfloor + 1
write("Enter new X size: ")
local xsize = tonumber(read())
dimensions["x"] = xsize
end end
if action == 5 then -- change floor amount if keys.getName(event[2]) == "e" and currentfloor > 1 then -- go down
reset() currentfloor = currentfloor - 1
write("Enter new floor amount: ")
local ysize = tonumber(read())
dimensions["y"] = ysize
end end
if action == 6 then -- change Z size if keys.getName(event[2]) == "w" and camy > 1 then -- pan up
reset() camy = camy - 1
write("Enter new Z size: ") end
local zsize = tonumber(read()) if keys.getName(event[2]) == "s" and camy < dimensions["z"] then -- pan down
dimensions["z"] = zsize camy = camy + 1
end
if keys.getName(event[2]) == "a" and camx > 1 then -- pan left
camx = camx - 1
end
if keys.getName(event[2]) == "d" and camx < dimensions["x"] then -- pan right
camx = camx + 1
end end
end end
end end