Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----------------------
- -- Easy Terms --
- -----------------------
- sbc = term.setBackgroundColor
- stc = term.setTextColor
- scp = term.setCursorPos
- clr = term.clear
- w,h = term.getSize()
- -----------------------
- -- Map and Player --
- -----------------------
- ----------------------------
- -- FullColor Awesomeness! --
- ----------------------------
- -----------------------
- -- Level backgrounds --
- ----------------------------
- -- Load your paint files! --
- ----------------------------
- local backgrounds = {
- backs = {
- --]] Syntax -> {name="default",file='none',bc='lightBlue',update=function(self) end},
- },
- getbyname = function(self,n)
- for i = 1, #self.backs do
- if(n == self.backs[i].name)then
- return self.backs[i]
- end
- end
- end,
- add = function(self,bgtable)
- self.backs[#self.backs+1] = bgtable
- end,
- remove = function(self,name)
- for i = 1, #self.backs do
- if(name == self.backs[i].name)then
- self.backs[i] = nil
- end
- end
- end,
- update = function(self)
- for i = 1, #self.backs do
- self.backs[i]:update()
- end
- end,
- draw = function(self,name,x,y,bc)
- local bg = self:getbyname(name)
- if(bg.file ~= "none")then
- bImage = paintutils.loadImage(bg.file)
- term.setBackgroundColor(bg.bc)
- paintutils.drawImage(bImage,x,y)
- else
- paintutils.drawFilledBox(x,y,w,h,colors[bg.bc])
- end
- end,
- }
- -----------------------
- -- Player Utils --
- -----------------------
- local player = {
- x = 1,
- y = 1,
- graphic = {g='&',tc='blue'},
- events = {},
- controls = {
- ['up'] = {key=keys.w,func=function(p) p:move(1,-1) end},
- ['down'] = {key=keys.s,func=function(p) p:move(1,1) end},
- ['left'] = {key=keys.a,func=function(p) p:move(2,-1) end},
- ['right'] = {key=keys.d,func=function(p) p:move(2,1) end},
- },
- checkCollision = function(self,xo,yo)
- return map:getBlock(self.x+xo,self.y+yo).c
- end,
- setGraphic = function(self,graphic,tc)
- self.graphic.g = graphic
- self.graphic.tc = tc
- end,
- addEvent = function(self,name,nev)
- self.events[name] = {func=nev}
- end,
- delEvent = function(self,name)
- if(self.events[name] ~= nil)then
- self.events[name] = nil
- end
- end,
- addControl = function(self,name,nkey,nfunc)
- self.controls[name] = {key=nkey,func=nfunc}
- end,
- delControl = function(self,name)
- if(self.controls[name] ~= nil)then
- self.controls[name] = nil
- end
- end,
- replaceControl = function(self,rname,nkey,nfunc)
- self.controls[rname] = {key=nkey,func=nfunc}
- end,
- changeControlEvent = function(self,name,nfunc)
- if(self.controls[name] ~= nil)then
- self.controls[name].func = nfunc
- end
- end,
- draw = function(self)
- -- set background to back color of standing on block --
- term.setBackgroundColor(colors[map:getBlock(self.x,self.y).bc])
- term.setTextColor(colors[self.graphic.tc])
- term.setCursorPos(self.x,self.y)
- term.write(self.graphic.g)
- end,
- move = function(self,dir,am)
- if(dir==1)then
- local willPush = map:getBlock(self.x,self.y+am)
- if(self:checkCollision(0,am) and willPush.p and
- not self:checkCollision(0,am+am))then
- map:moveBlock(self.x,self.y+am,self.x,self.y+am+am,'1')
- elseif(not self:checkCollision(0,am))then
- self:translate(0,am)
- end
- elseif(dir==2)then
- local willPush = map:getBlock(self.x+am,self.y)
- if(self:checkCollision(am,0) and willPush.p and
- not self:checkCollision(am+am,0))then
- map:moveBlock(self.x+am,self.y,self.x+am+am,self.y,'1')
- elseif(not self:checkCollision(am,0))then
- self:translate(am,0)
- end
- end
- end,
- checkKeyPress = function(self,ev)
- if(ev[1] == "key")then
- return ev[2]
- end
- end,
- update = function(self,ev)
- if(ev[1] == "key")then
- for k,v in pairs(self.controls) do
- if(ev[2] == v.key)then
- v.func(self)
- end
- end
- end
- for k,v in pairs(self.events) do
- v.func(self,ev)
- end
- end,
- translate = function(self,ox,oy)
- map:sDraw(self.x,self.y)
- self.x = self.x + ox
- self.y = self.y + oy
- self:draw()
- end,
- }
- -----------------------
- -- Map Utinsils --
- -----------------------
- local maputils = {
- -- Level vars --
- blocks = {
- ['1'] = {n='grass', g=' ',tc='lime', bc='green',c=false,p=false},
- ['2'] = {n='dirt', g=' ',tc='black',bc='brown',c=false,p=false},
- ['3'] = {n='log', g='=',tc='lime', bc='green',c=true,p=false},
- ['4'] = {n='wall', g=' ',tc='gray', bc='gray', c=true,p=true},
- ['5'] = {n='flower',g='*',tc='red', bc='green',c=false,p=false},
- ['6'] = {n='m-top', g=' ',tc='brown',bc='brown',c=false,p=false},
- },
- ----------------
- setBlock = function(self,x,y,bid)
- ---------------------- How to do this? ---------------------------------
- -- Take the front of the string -
- -- before the X - 1 (-1 because we want to replace the string) -
- -- then after replacing with the block, then push the rest of the table-
- -- onto the back, and cast it back to map[y] -
- ------------------------------------------------------------------------
- self.gamemap[y] = table.concat{self.gamemap[y]:sub(1,x-1), tostring(bid), self.gamemap[y]:sub(x+1)}
- self:sDraw(x,y)
- end,
- getBID = function(self,bname)
- rblock = '1'
- for k,v in pairs(self.blocks) do
- if(bname == v.n)then
- rblock = k
- end
- end
- return rblock
- end,
- setPushable = function(self,bid,pushable)
- self.blocks[bid].p = pushable
- end,
- getPushable = function(self,bid)
- return self.blocks[bid].p
- end,
- setSolid = function(self,bid,solid)
- self.blocks[bid].c = solid
- end,
- getSolid = function(self,bid)
- return self.blocks[bid].c
- end,
- setRegion = function(self,x,y,xt,yt,bid)
- for ly=y,yt do
- for lx=x,xt do
- self:setBlock(lx,ly,bid)
- end
- end
- end,
- moveBlock = function(self,fx,fy,tx,ty,rbid)
- local temp = self:getBlock(fx,fy)
- self:setBlock(fx,fy,rbid)
- self:sDraw(fx,fy)
- self:setBlock(tx,ty,self:getBID(temp.n))
- self:sDraw(tx,ty)
- end,
- getBlockRaw = function(self,x,y)
- return self.gamemap[y]:sub(x,x)
- end,
- getBlock = function(self,x,y)
- return self.blocks[self:getBlockRaw(x,y)]
- end,
- getBlockCount = function(self,bid)
- local c = 0
- for my=0,h do
- for mx=0,w do
- if(self.gamemap[my]:sub(mx,mx) == tostring(bid))then
- c = c + 1 -- no ++ WHY LUA!!!
- end
- end
- end
- return c
- end,
- ------------------------=
- --- Map Drawing Utils --=
- ------------------------=
- sDrawRaw = function(self,x,y)
- stc(colors.white)
- scp(x,y)
- term.write(self.gamemap[y]:sub(x,x))
- end,
- aDrawRaw = function(self)
- for my=0,h do
- term.setCursorPos(1,my+1)
- term.write(self.gamemap[my])
- end
- end,
- sDraw = function(self,x,y)
- local b = self:getBlock(x,y)
- term.setTextColor(colors[b.tc])
- term.setBackgroundColor(colors[b.bc])
- term.setCursorPos(x,y)
- term.write(b.g)
- end,
- rDraw = function(self,x,y,xt,yt)
- for ly=y,yt do
- for lx=x,xt do
- local b = self:getBlock(lx,ly)
- term.setTextColor(colors[b.tc])
- term.setBackgroundColor(colors[b.bc])
- term.setCursorPos(lx,ly)
- term.write(b.g)
- end
- end
- end,
- aDraw = function(self)
- for my=1,h do
- for mx=1,w do
- local b = self:getBlock(mx,my)
- term.setTextColor(colors[b.tc])
- term.setBackgroundColor(colors[b.bc])
- term.setCursorPos(mx,my)
- term.write(b.g)
- end
- end
- end,
- --------------------------
- -- Level Utils --
- --------------------------
- drawBlock = function(name,x,y)
- local b = self.blocks[name]
- term.setTextColor(colors[b.tc])
- term.setBackgroundColor(colors[b.bc])
- term.setCursorPos(x,y)
- term.write(b.g)
- end,
- update = function(self,ev)
- --self.backgrounds.update()
- if(self.hasPlayer)then
- self.player:update(ev)
- end
- end,
- }
- --------------------------
- function createGame()
- local gamemap = {}
- for my=0,h do
- gamemap[my] = string.rep('1',w)
- end
- map = maputils
- map.hasPlayer = false
- map.gamemap={}
- map.gamemap = gamemap
- map.backgrounds={}
- map.backgrounds = backgrounds
- return map
- end
- local function newPlayer(x,y)
- local mp = player
- mp.x = x
- mp.y = y
- return mp
- end
- function createPlayer(map,x,y)
- map.player={}
- map.player = newPlayer(x,y)
- map.hasPlayer = true
- return map
- end
- function initBackgrounds()
- return backgrounds
- end
- -------------------------------------
- -- Other utilities: Event handling --
- -------------------------------------
- mousebuttons = {
- ['left'] = 1,
- ['right'] = 2,
- }
- function catchEvents()
- return {os.pullEvent()}
- end
- function getMouseClick(e)
- if(e[1] == "mouse_click" or e[1] == "mouse_drag")then
- return {button=e[2],x=e[3],y=e[4]}
- else
- return false
- end
- end
- function getKey(e)
- if(e[1] == "key")then
- return ev[2]
- else
- return false
- end
- end
- -------------------------------------
- print("LWLGL - Compiled Successfully!")
Add Comment
Please, Sign In to add comment