add several types of blocks.
This commit is contained in:
58
src/main.lua
58
src/main.lua
@@ -83,7 +83,25 @@ local function newdesign()
|
||||
}
|
||||
local currentfloor = 1
|
||||
local camx,camy = 0,0
|
||||
|
||||
local currentblock = 2
|
||||
local blockindex = 1
|
||||
local blocks = {
|
||||
2, -- no ceiling
|
||||
3, -- doorway
|
||||
4, -- glass
|
||||
}
|
||||
local function renderblock(index)
|
||||
local block = blocks[index]
|
||||
if block == 2 then -- no ceiling
|
||||
term.blit("C", colors.toBlit(colors.black), colors.toBlit(colors.red))
|
||||
end
|
||||
if block == 3 then -- doorway
|
||||
term.blit("D", colors.toBlit(colors.black), colors.toBlit(colors.brown))
|
||||
end
|
||||
if block == 4 then -- glass
|
||||
term.blit("W", colors.toBlit(colors.black), colors.toBlit(colors.white))
|
||||
end
|
||||
end
|
||||
|
||||
while true do
|
||||
-- render buf
|
||||
@@ -94,8 +112,9 @@ local function newdesign()
|
||||
term.setCursorPos(x, z)
|
||||
if currentbuf == 1 then -- wall
|
||||
term.blit(" ", colors.toBlit(colors.white), colors.toBlit(colors.white))
|
||||
elseif currentbuf == 2 then -- no ceiling
|
||||
term.blit(" ", colors.toBlit(colors.red), colors.toBlit(colors.red))
|
||||
elseif currentbuf > 1 then -- blocks
|
||||
renderblock(currentbuf-1)
|
||||
--term.blit("C", colors.toBlit(colors.black), colors.toBlit(colors.red))
|
||||
elseif currentbuf == 0 or currentbuf == nil then -- nothing
|
||||
term.blit("\127", colors.toBlit(colors.gray), colors.toBlit(colors.black))
|
||||
end
|
||||
@@ -109,6 +128,8 @@ local function newdesign()
|
||||
term.write("x: "..tostring(camx))
|
||||
term.write(" z: "..tostring(camy))
|
||||
term.write(" floor: "..tostring(currentfloor))
|
||||
term.write(" Block: ")
|
||||
renderblock(blockindex)
|
||||
|
||||
-- user input
|
||||
local event = table.pack(os.pullEvent())
|
||||
@@ -119,14 +140,23 @@ local function newdesign()
|
||||
if event[2] == 1 then -- left button, set
|
||||
buf[posasstring(event[3], currentfloor, event[4])] = 1
|
||||
end
|
||||
if event[2] == 3 then -- middle button, set no ceiling
|
||||
buf[posasstring(event[3], currentfloor, event[4])] = 2
|
||||
if event[2] == 3 then -- middle button, set special block
|
||||
buf[posasstring(event[3], currentfloor, event[4])] = currentblock
|
||||
end
|
||||
if event[2] == 2 then -- right button, erase
|
||||
buf[posasstring(event[3], currentfloor, event[4])] = 0
|
||||
end
|
||||
end
|
||||
end
|
||||
if event[1] == "mouse_scroll" then
|
||||
if event[2] == 1 then -- down
|
||||
if blockindex == #blocks then blockindex = 1 else blockindex = blockindex + 1 end
|
||||
end
|
||||
if event[2] == -1 then -- up
|
||||
if blockindex == 1 then blockindex = #blocks else blockindex = blockindex - 1 end
|
||||
end
|
||||
currentblock = blocks[blockindex]
|
||||
end
|
||||
if event[1] == "key" then
|
||||
if keys.getName(event[2]) == "leftCtrl" then -- menu
|
||||
local action = selopt({
|
||||
@@ -272,7 +302,7 @@ local function placebuf(buf, x, y, z)
|
||||
place("down")
|
||||
end
|
||||
end
|
||||
local function getnearestunplaced(buf, pbuf, cx,cy,cz,co, sx,sz)
|
||||
local function getnearestunplaced(buf, pbuf, cx,cy,cz,clayer, sx,sz)
|
||||
local distance = math.huge
|
||||
local selected = nil
|
||||
|
||||
@@ -280,7 +310,15 @@ local function getnearestunplaced(buf, pbuf, cx,cy,cz,co, sx,sz)
|
||||
for y=1,sz,1 do
|
||||
local needplace = buf[posasstring(x,cy,y)]
|
||||
if pbuf[posasstring(x,cy,y)] then needplace = 0 end -- already placed
|
||||
needplace = needplace == 1
|
||||
if clayer == 0 or clayer == 2 then
|
||||
needplace = needplace == 1
|
||||
end
|
||||
if clayer == 1 then
|
||||
needplace = needplace == 1 or needplace == 4
|
||||
end
|
||||
if clayer == 3 then
|
||||
needplace = needplace == 1 or needplace == 4 or needplace == 3
|
||||
end
|
||||
if needplace then -- needs to be placed, calculate distance
|
||||
local cd = math.abs(y-cz)+math.abs(x-cx) -- raw distance (amount of blocks between)
|
||||
if cd < distance then
|
||||
@@ -371,10 +409,10 @@ local function printdes(buf, dimensions)
|
||||
local cz = 1
|
||||
local direction = 0
|
||||
for cy=1,dimensions["y"],1 do
|
||||
for _=1,3,1 do -- build walls
|
||||
for clayer=1,3,1 do -- build walls
|
||||
local pbuf = {}
|
||||
while true do
|
||||
local target = getnearestunplaced(buf, pbuf, cx,cy,cz,nil, dimensions["x"],dimensions["z"])
|
||||
local target = getnearestunplaced(buf, pbuf, cx,cy,cz,clayer, dimensions["x"],dimensions["z"])
|
||||
if not target then break end
|
||||
|
||||
render(buf, pbuf, cx,cy,cz, target[1],target[2], dimensions["x"],dimensions["z"], {1})
|
||||
@@ -400,7 +438,7 @@ local function printdes(buf, dimensions)
|
||||
end
|
||||
end
|
||||
while true do
|
||||
local target = getnearestunplaced(cbuf, pbuf, cx,0,cz,nil, dimensions["x"],dimensions["z"])
|
||||
local target = getnearestunplaced(cbuf, pbuf, cx,0,cz,0, dimensions["x"],dimensions["z"])
|
||||
if not target then break end
|
||||
|
||||
render(cbuf, pbuf, cx,0,cz, target[1],target[2], dimensions["x"],dimensions["z"], {1})
|
||||
|
||||
Reference in New Issue
Block a user