Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("grid")
- local blocks = {
- [-1]={name='void',char=" ",tc=colors.black,bc=colors.black,solid=true},
- [0]={name='player',char="&",tc=colors.blue,solid=true},
- {name='grass',char=' ',tc=colors.lime,bc=colors.green,solid=false},
- {name='wall',char='L',tc=colors.gray,bc=colors.lightGray,solid=true},
- {name='sand',char='.',tc=colors.white,bc=colors.yellow,solid=false},
- {name='rose',char='*',tc=colors.red,bc=colors.green,solid=false},
- {name='dandilion',char='*',tc=colors.yellow,bc=colors.green,solid=false},
- {name='water',char='~',tc=colors.lightBlue,bc=colors.blue,solid=false},
- {name='treetrunk',char='Y',tc=colors.brown,bc=colors.green,solid=true},
- {name='treetop',char='@',tc=colors.green,bc=colors.lime,solid=true},
- {name='hillleft',char='\\',tc=colors.gray,bc=colors.brown,solid=true},
- {name='hillright',char='/',tc=colors.gray,bc=colors.brown,solid=true},
- {name='hill',char='|',tc=colors.gray,bc=colors.brown,solid=true},
- {name='road',char=' ',tc=colors.lightGray,bc=colors.lightGray,solid=false},
- {name='rupee',char='!',tc=colors.blue,bc=colors.green,solid=false},
- {name='shadow',char=" ",tc=colors.gray,bc=colors.gray,solid=false},
- {name='flowerbed',char="o",tc=colors.red,bc=colors.orange,solid=false},
- {name='flowerbedgreen',char="o",tc=colors.green,bc=colors.lime,solid=false},
- --{name='enemy',char='&',tc=colors.red,solid=true},
- {name='chest',char='\169',tc=colors.yellow,bc=colors.brown,solid=true},
- --{name='grass',char='"',tc=colors.lime,bc=colors.green,solid=false},
- }
- local map = grid.create(150,150)
- map:fill(1)
- local playerx = 10
- local playery = 10
- local w,h = term.getSize()
- local overx = 0
- local overy = 0
- local selblock = 2
- local states = {inMenu=false,inGame=false,scrollX=false,scrollY=false}
- function drawMap(x,y)
- -- Draw Map --
- if(type(map:get(x,y)) == "number")then
- term.setCursorPos(x-overx,y-overy)
- term.setTextColor(blocks[map:get(x,y)].tc)
- term.setBackgroundColor(blocks[map:get(x,y)].bc)
- term.write(blocks[map:get(x,y)].char)
- else
- term.setCursorPos(x-overx,y-overy)
- term.setBackgroundColor(colors.blue)
- term.setTextColor(colors.lightBlue)
- term.write(" ")
- end
- end
- function drawPlayer()
- -- Draw Player Map (Pmap) --
- if(not states.inGame)then
- term.current().setVisible(false)
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.yellow)
- print("PlayerX: " .. playerx .. " PlayerY: " .. playery .. " ")
- print("ScrollX: " .. overx .. " ScrollY: " .. overy .. " ")
- term.current().setVisible(true)
- end
- term.setCursorPos(playerx-overx,playery-overy)
- term.setTextColor(blocks[0].tc)
- term.setBackgroundColor(blocks[map:get(playerx,playery)].bc)
- if(blocks[map:get(playerx,playery)].bc == blocks[0].tc)then
- term.setTextColor(colors.lightBlue)
- end
- term.write(blocks[0].char)
- end
- function draw()
- map:loop(drawMap,overx,overy,overx+w,overy+h)
- drawPlayer()
- end
- function collides(cx,cy)
- if(type(map:get(playerx+cx,playery+cy)) == 'number')then
- return blocks[map:get(playerx+cx,playery+cy)].solid
- else
- return true
- end
- end
- function panTo(onx,ony)
- local xmx = w-5
- local ymx = h-5
- if(xmx < 1)then xmx = 1 end
- if(ymx < 1)then ymx = 1 end
- if(onx > 1)then onx = 1 end
- if(onx < -1)then onx = -1 end
- if(ony > 1)then ony = 1 end
- if(ony < -1)then ony = -1 end
- if(onx ~= 0)then
- for i = 1, xmx do
- overx = overx + onx
- sleep(0.1)
- draw()
- if(not inGame)then drawSelected() end
- end
- end
- if(ony ~= 0)then
- for i = 1, ymx do
- overy= overy + ony
- sleep(0.1)
- draw()
- if(not inGame)then drawSelected() end
- end
- end
- end
- function calcOver()
- overx = playerx-5
- overy = playery-5
- if(overx < 0)then overx = playerx-1 end
- if(overy < 0)then overy = playery-1 end
- draw()
- end
- function panOver(time)
- local xmx = w-5
- local ymx = h-5
- local lerptime = 0.1
- if(time ~= nil)then lerptime = time end
- if(xmx < 1)then xmx = 1 end
- if(ymx < 1)then ymx = 1 end
- if(playerx > overx+w)then
- for i = 1, xmx do
- overx = overx + 1
- sleep(lerptime)
- draw()
- end
- end
- if(playery > overy+h)then
- for i = 1, ymx do
- overy = overy + 1
- sleep(lerptime)
- draw()
- end
- end
- if(playerx <= overx)then
- for i = 1, xmx do
- if(overx > 0)then overx = overx - 1 else break end
- sleep(lerptime)
- draw()
- end
- end
- if(playery <= overy)then
- for i = 1, ymx do
- if(overy > 0)then overy = overy - 1 else break end
- sleep(lerptime)
- draw()
- end
- end
- end
- function drawEditor()
- term.setCursorPos(1,h)
- term.setBackgroundColor(colors.gray)
- term.clearLine()
- local overd = selblock - 8
- if(overd < 0)then overd = 0 end
- for i = 0+overd, 8+overd do
- if(blocks[i] ~= nil)then
- ix = ( (i-overd) + 1 ) * 5
- term.setCursorPos(ix, h)
- term.setTextColor(blocks[i].tc)
- local bcolor = colors.gray
- if(blocks[i].bc ~= nil)then bcolor = blocks[i].bc end
- term.setBackgroundColor(bcolor)
- write(blocks[i].char)
- if(selblock == i)then
- term.setTextColor(colors.yellow)
- term.setBackgroundColor(colors.gray)
- term.setCursorPos(ix-1, h)
- write(">")
- term.setCursorPos(ix+#blocks[i].char, h)
- write("<")
- end
- end
- end
- term.current().setVisible(false)
- -- scroll bars --
- paintutils.drawLine(w, 1, w, h-1, colors.lightGray)
- term.setTextColor(colors.white)
- term.setCursorPos(w,1)
- write("^")
- term.setCursorPos(w,h-1)
- write("v")
- local hscale = overy+2
- local tscale = math.floor((overy+2)/h)
- if(hscale >= h-1)then hscale = math.floor((overy+2)/h)+2 end
- if(tscale <= 0)then tscale = "=" end
- term.setCursorPos(w,hscale)
- term.setBackgroundColor(colors.gray)
- write(tscale)
- -- X Scale --
- paintutils.drawLine(1, h-1, w-1, h-1, colors.lightGray)
- term.setTextColor(colors.white)
- term.setCursorPos(1,h-1)
- write("<")
- term.setCursorPos(w-1,h-1)
- write(">")
- local wscale = overx+2
- local tscale = math.floor((overx+2)/w)
- if(wscale >= w-1)then wscale = math.floor((overx+2)/w)+2 end
- if(tscale <= 0)then tscale = "=" end
- term.setCursorPos(wscale,h-1)
- term.setBackgroundColor(colors.gray)
- write(tscale)
- term.current().setVisible(true)
- end
- draw()
- drawEditor()
- while true do
- e = {os.pullEvent()}
- if(states.inGame)then
- if(e[1]=="key")then
- drawMap(playerx,playery)
- if(e[2] == keys.w and playery > 1 and not collides(0,-1)) then
- playery = playery - 1
- end
- if(e[2] == keys.s and playery < map:getHeight() and not collides(0,1)) then
- playery = playery + 1
- end
- if(e[2] == keys.a and playerx > 1 and not collides(-1,0)) then
- playerx = playerx - 1
- end
- if(e[2] == keys.d and playerx < map:getWidth() and not collides(1,0)) then
- playerx = playerx + 1
- end
- drawPlayer()
- panOver()
- end
- end
- if(states.inMenu and not states.inGame)then
- end
- if(not states.inMenu and not states.inGame)then
- if(e[1] == "mouse_scroll")then
- if(e[3] ~= w and e[4] ~= h-1)then
- if(e[2] == -1 and selblock > 0)then selblock = selblock - 1 end
- if(e[2] == 1 and selblock < #blocks)then selblock = selblock + 1 end
- end
- if(e[3] == w and e[4] ~= h-1)then overy = overy + e[2] draw() end
- if(e[4] == h-1 and e[3] ~= w)then overx = overx + e[2] draw() end
- if(overx > (map:getWidth()-w)+1)then overx = (map:getWidth()-w)+1 draw() end
- if(overx < 0)then overx = 0 draw() end
- if(overy < 0)then overy = 0 draw() end
- if(overy > (map:getHeight()-h)+1)then overy = (map:getHeight()-h)+1 draw() end
- drawEditor()
- end
- if(e[1] == "key")then
- pk = e[2]
- if(pk == keys.g)then states.inGame = true calcOver() end
- if(pk == keys.w and overy > 0)then overy = overy - 1 draw() end
- if(pk == keys.s and overy < (map:getHeight()-h)+2)then overy = overy + 1 draw() end
- if(pk == keys.a and overx > 0)then overx = overx - 1 draw() end
- if(pk == keys.d and overx < (map:getWidth()-w)+1)then overx = overx + 1 draw() end
- if(pk == keys.left and selblock > 0)then selblock = selblock - 1 end
- if(pk == keys.right and selblock < #blocks)then selblock = selblock + 1 end
- if(not states.inGame and not states.inMenu)then drawEditor() end
- end
- -- Placing blocks in edit mode --
- if(e[1] == "mouse_click" or e[1] == "mouse_drag")then
- if(e[2] == 1)then
- if(selblock == 0)then
- drawMap(playerx,playery)
- playerx = e[3]+overx
- playery = e[4]+overy
- drawPlayer()
- elseif(e[3]+overx <= map:getWidth() and e[4]+overy <= map:getHeight() and e[3]+overx >= 0 and e[3]+overy >= 0)then
- map:set(e[3]+overx,e[4]+overy,selblock)
- drawMap(e[3]+overx,e[4]+overy)
- if(e[3]+overx == playerx and e[4]+overy == playery)then
- drawPlayer()
- end
- end
- elseif(e[2] == 2)then
- map:set(e[3]+overx,e[4]+overy,-1)
- drawMap(e[3]+overx,e[4]+overy)
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment