Redxone

[ComputerCraft] LWLGL - Light Weight Lua Game Library

May 16th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.46 KB | None | 0 0
  1. -----------------------
  2. --    Easy Terms     --
  3. -----------------------
  4.  sbc = term.setBackgroundColor
  5.  stc = term.setTextColor
  6.  scp = term.setCursorPos
  7.  clr = term.clear
  8.  w,h = term.getSize()
  9. -----------------------
  10. --  Map and Player   --
  11. -----------------------
  12. ----------------------------
  13. -- FullColor Awesomeness! --
  14. ----------------------------
  15.  
  16. -----------------------
  17. -- Level backgrounds --
  18. ----------------------------
  19. -- Load your paint files! --
  20. ----------------------------
  21. local backgrounds = {
  22.     backs = {
  23.         --]] Syntax -> {name="default",file='none',bc='lightBlue',update=function(self) end},
  24.     },
  25.  
  26.     getbyname = function(self,n)
  27.         for i = 1, #self.backs do
  28.             if(n == self.backs[i].name)then
  29.                 return self.backs[i]
  30.             end
  31.         end
  32.     end,
  33.  
  34.     add = function(self,bgtable)
  35.          self.backs[#self.backs+1] = bgtable
  36.     end,
  37.    
  38.     remove = function(self,name)
  39.         for i = 1, #self.backs do
  40.             if(name == self.backs[i].name)then
  41.                 self.backs[i] = nil
  42.             end
  43.         end
  44.     end,
  45.  
  46.     update = function(self)
  47.         for i = 1, #self.backs do
  48.             self.backs[i]:update()
  49.         end
  50.     end,
  51.  
  52.  
  53.     draw = function(self,name,x,y,bc)
  54.         local bg = self:getbyname(name)
  55.         if(bg.file ~= "none")then
  56.             bImage = paintutils.loadImage(bg.file)
  57.             term.setBackgroundColor(bg.bc)
  58.             paintutils.drawImage(bImage,x,y)
  59.         else
  60.             paintutils.drawFilledBox(x,y,w,h,colors[bg.bc])
  61.         end
  62.     end,
  63. }
  64. -----------------------
  65. --    Player Utils   --
  66. -----------------------
  67. local player = {
  68.     x = 1,
  69.     y = 1,
  70.     graphic = {g='&',tc='blue'},
  71.     events = {},
  72.     controls = {
  73.         ['up'] = {key=keys.w,func=function(p) p:move(1,-1) end},
  74.         ['down'] = {key=keys.s,func=function(p) p:move(1,1) end},
  75.         ['left'] = {key=keys.a,func=function(p) p:move(2,-1) end},
  76.         ['right'] = {key=keys.d,func=function(p) p:move(2,1) end},
  77.     },
  78.  
  79.     checkCollision = function(self,xo,yo)
  80.         return map:getBlock(self.x+xo,self.y+yo).c
  81.     end,
  82.  
  83.     setGraphic = function(self,graphic,tc)
  84.         self.graphic.g = graphic
  85.         self.graphic.tc = tc
  86.     end,
  87.  
  88.     addEvent = function(self,name,nev)
  89.         self.events[name] = {func=nev}
  90.     end,
  91.  
  92.     delEvent = function(self,name)
  93.         if(self.events[name] ~= nil)then
  94.             self.events[name] = nil
  95.         end
  96.     end,
  97.  
  98.     addControl = function(self,name,nkey,nfunc)
  99.         self.controls[name] = {key=nkey,func=nfunc}
  100.     end,
  101.  
  102.     delControl = function(self,name)
  103.         if(self.controls[name] ~= nil)then
  104.             self.controls[name] = nil
  105.         end
  106.     end,
  107.  
  108.     replaceControl = function(self,rname,nkey,nfunc)
  109.         self.controls[rname] = {key=nkey,func=nfunc}
  110.     end,
  111.  
  112.     changeControlEvent = function(self,name,nfunc)
  113.         if(self.controls[name] ~= nil)then
  114.             self.controls[name].func = nfunc
  115.         end
  116.     end,
  117.  
  118.     draw = function(self)
  119.         -- set background to back color of standing on block --
  120.         term.setBackgroundColor(colors[map:getBlock(self.x,self.y).bc])
  121.         term.setTextColor(colors[self.graphic.tc])
  122.         term.setCursorPos(self.x,self.y)
  123.         term.write(self.graphic.g)
  124.     end,
  125.  
  126.     move = function(self,dir,am)
  127.         if(dir==1)then
  128.             local willPush = map:getBlock(self.x,self.y+am)
  129.             if(self:checkCollision(0,am) and willPush.p and
  130.             not self:checkCollision(0,am+am))then
  131.                 map:moveBlock(self.x,self.y+am,self.x,self.y+am+am,'1')
  132.             elseif(not self:checkCollision(0,am))then
  133.                 self:translate(0,am)
  134.             end
  135.         elseif(dir==2)then
  136.             local willPush = map:getBlock(self.x+am,self.y)
  137.             if(self:checkCollision(am,0) and willPush.p and
  138.             not self:checkCollision(am+am,0))then
  139.                 map:moveBlock(self.x+am,self.y,self.x+am+am,self.y,'1')
  140.             elseif(not self:checkCollision(am,0))then
  141.                  self:translate(am,0)
  142.             end
  143.         end
  144.     end,
  145.  
  146.     checkKeyPress = function(self,ev)
  147.         if(ev[1] == "key")then
  148.             return ev[2]
  149.         end
  150.     end,
  151.  
  152.     update = function(self,ev)
  153.         if(ev[1] == "key")then
  154.             for k,v in pairs(self.controls) do
  155.                  if(ev[2] == v.key)then
  156.                     v.func(self)
  157.                  end
  158.             end
  159.         end
  160.  
  161.         for k,v in pairs(self.events) do
  162.             v.func(self,ev)
  163.         end
  164.     end,
  165.  
  166.     translate = function(self,ox,oy)
  167.         map:sDraw(self.x,self.y)
  168.         self.x = self.x + ox
  169.         self.y = self.y + oy
  170.         self:draw()
  171.     end,
  172.    
  173. }
  174.  
  175. -----------------------
  176. --    Map Utinsils   --
  177. -----------------------
  178. local maputils = {  
  179.    -- Level vars --  
  180.     blocks = {
  181.         ['1'] = {n='grass', g=' ',tc='lime', bc='green',c=false,p=false},
  182.         ['2'] = {n='dirt',  g=' ',tc='black',bc='brown',c=false,p=false},
  183.         ['3'] = {n='log',   g='=',tc='lime', bc='green',c=true,p=false},
  184.         ['4'] = {n='wall',  g=' ',tc='gray', bc='gray', c=true,p=true},
  185.         ['5'] = {n='flower',g='*',tc='red',  bc='green',c=false,p=false},
  186.         ['6'] = {n='m-top', g=' ',tc='brown',bc='brown',c=false,p=false},
  187.     },
  188.    ----------------
  189.  
  190.    setBlock = function(self,x,y,bid)
  191.         ---------------------- How to do this? ---------------------------------
  192.         -- Take the front of the string                                        -  
  193.         -- before the X - 1 (-1 because we want to replace the string)         -
  194.         -- then after replacing with the block, then push the rest of the table-
  195.         -- onto the back, and cast it back to map[y]                           -
  196.         ------------------------------------------------------------------------
  197.         self.gamemap[y] = table.concat{self.gamemap[y]:sub(1,x-1), tostring(bid), self.gamemap[y]:sub(x+1)}
  198.         self:sDraw(x,y)
  199.     end,
  200.  
  201.     getBID = function(self,bname)
  202.         rblock = '1'
  203.         for k,v in pairs(self.blocks) do
  204.             if(bname == v.n)then
  205.                 rblock = k
  206.             end
  207.         end
  208.         return rblock
  209.     end,
  210.  
  211.     setPushable = function(self,bid,pushable)
  212.         self.blocks[bid].p = pushable
  213.     end,
  214.  
  215.     getPushable = function(self,bid)
  216.         return self.blocks[bid].p
  217.     end,
  218.  
  219.     setSolid = function(self,bid,solid)
  220.         self.blocks[bid].c = solid
  221.     end,
  222.  
  223.     getSolid = function(self,bid)
  224.         return self.blocks[bid].c
  225.     end,
  226.  
  227.     setRegion = function(self,x,y,xt,yt,bid)
  228.         for ly=y,yt do
  229.             for lx=x,xt do
  230.                 self:setBlock(lx,ly,bid)
  231.             end
  232.         end
  233.     end,
  234.  
  235.     moveBlock = function(self,fx,fy,tx,ty,rbid)
  236.         local temp = self:getBlock(fx,fy)
  237.         self:setBlock(fx,fy,rbid)
  238.         self:sDraw(fx,fy)
  239.         self:setBlock(tx,ty,self:getBID(temp.n))
  240.         self:sDraw(tx,ty)
  241.     end,
  242.  
  243.     getBlockRaw = function(self,x,y)
  244.         return self.gamemap[y]:sub(x,x)
  245.     end,
  246.  
  247.     getBlock = function(self,x,y)
  248.         return self.blocks[self:getBlockRaw(x,y)]
  249.     end,
  250.  
  251.     getBlockCount = function(self,bid)
  252.         local c = 0
  253.         for my=0,h do
  254.             for mx=0,w do
  255.                 if(self.gamemap[my]:sub(mx,mx) == tostring(bid))then
  256.                     c = c + 1 -- no ++ WHY LUA!!!
  257.                 end
  258.             end
  259.         end
  260.         return c
  261.     end,
  262.  
  263.     ------------------------=
  264.     --- Map Drawing Utils --=
  265.     ------------------------=
  266.  
  267.     sDrawRaw = function(self,x,y)
  268.         stc(colors.white)
  269.         scp(x,y)
  270.         term.write(self.gamemap[y]:sub(x,x))
  271.     end,
  272.     aDrawRaw = function(self)
  273.         for my=0,h do
  274.             term.setCursorPos(1,my+1)
  275.             term.write(self.gamemap[my])
  276.         end
  277.     end,
  278.  
  279.     sDraw = function(self,x,y)
  280.         local b = self:getBlock(x,y)
  281.         term.setTextColor(colors[b.tc])
  282.         term.setBackgroundColor(colors[b.bc])
  283.         term.setCursorPos(x,y)
  284.         term.write(b.g)
  285.     end,
  286.  
  287.     rDraw = function(self,x,y,xt,yt)
  288.         for ly=y,yt do
  289.             for lx=x,xt do
  290.                 local b = self:getBlock(lx,ly)
  291.                 term.setTextColor(colors[b.tc])
  292.                 term.setBackgroundColor(colors[b.bc])
  293.                 term.setCursorPos(lx,ly)
  294.                 term.write(b.g)
  295.             end
  296.         end
  297.     end,
  298.  
  299.     aDraw = function(self)
  300.         for my=1,h do
  301.             for mx=1,w do
  302.                 local b = self:getBlock(mx,my)
  303.                 term.setTextColor(colors[b.tc])
  304.                 term.setBackgroundColor(colors[b.bc])
  305.                 term.setCursorPos(mx,my)
  306.                 term.write(b.g)
  307.             end
  308.         end
  309.     end,
  310.  
  311.     --------------------------
  312.     --     Level Utils      --
  313.     --------------------------
  314.  
  315.     drawBlock = function(name,x,y)
  316.         local b = self.blocks[name]
  317.         term.setTextColor(colors[b.tc])
  318.         term.setBackgroundColor(colors[b.bc])
  319.         term.setCursorPos(x,y)
  320.         term.write(b.g)
  321.     end,
  322.  
  323.     update = function(self,ev)
  324.         --self.backgrounds.update()
  325.         if(self.hasPlayer)then
  326.             self.player:update(ev)
  327.         end
  328.     end,
  329. }
  330.  
  331. --------------------------
  332.  
  333. function createGame()
  334.     local gamemap = {}
  335.     for my=0,h do
  336.         gamemap[my] = string.rep('1',w)
  337.     end
  338.     map = maputils
  339.     map.hasPlayer = false
  340.     map.gamemap={}
  341.     map.gamemap = gamemap
  342.     map.backgrounds={}
  343.     map.backgrounds = backgrounds
  344.     return map
  345. end
  346. local function newPlayer(x,y)
  347.     local mp = player
  348.     mp.x = x
  349.     mp.y = y
  350.     return mp
  351. end
  352.  
  353. function createPlayer(map,x,y)
  354.     map.player={}
  355.     map.player = newPlayer(x,y)
  356.     map.hasPlayer = true
  357.  
  358.     return map
  359. end
  360. function initBackgrounds()
  361.     return backgrounds
  362. end
  363. -------------------------------------
  364. -- Other utilities: Event handling --
  365. -------------------------------------
  366. mousebuttons = {
  367.     ['left'] = 1,
  368.     ['right'] = 2,
  369. }
  370. function catchEvents()
  371.     return {os.pullEvent()}
  372. end
  373.  
  374. function getMouseClick(e)
  375.     if(e[1] == "mouse_click" or e[1] == "mouse_drag")then
  376.         return {button=e[2],x=e[3],y=e[4]}
  377.     else
  378.         return false
  379.     end
  380. end
  381. function getKey(e)
  382.     if(e[1] == "key")then
  383.         return ev[2]
  384.     else
  385.         return false
  386.     end
  387. end
  388. -------------------------------------
  389.  
  390. print("LWLGL - Compiled Successfully!")
Add Comment
Please, Sign In to add comment