Advertisement
Redxone

[CC[ - GameField

Aug 17th, 2016
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.18 KB | None | 0 0
  1. local function gamemap(ww,hh,cc)
  2.  
  3.     print("Generating map....")
  4.     buffmap = {}
  5.     for yy = 1, hh do
  6.         buffmap[yy] = {}
  7.         for xx = 1, ww do
  8.             buffmap[yy][xx] = {}
  9.         end
  10.     end
  11.  
  12.     paintutils.drawFilledBox(0,0,ww,hh,cc)
  13.     term.setCursorPos(1,1)
  14.  
  15.     local field = {
  16.         screen = buffmap,
  17.         cls = cc,
  18.         w = ww,
  19.         h = hh,
  20.     -- Basic blueprint - {x,y,tcolor,bcolor,chr}
  21.         addpixel = function(self,xx,yy,chr,tcolor,bcolor)
  22.             if(#chr > 1)then
  23.                 for i = 1, #chr do
  24.                     self.screen[yy][xx+(i-1)] = {tc=tcolor,bc=bcolor,ch=chr:sub(i,i)}
  25.                     self:drawpixel(xx+(i-1),yy)
  26.                 end
  27.             else
  28.                 self.screen[yy][xx] = {tc=tcolor,bc=bcolor,ch=chr}
  29.                 self:drawpixel(xx,yy)
  30.             end
  31.         end,
  32.  
  33.         addtag = function(self,x,y,tname,tdata)
  34.             self.screen[y][x][tname] = tdata
  35.         end,
  36.  
  37.         removepixel = function(self,x,y)
  38.             self.screen[y][x] = {}
  39.             self:drawpixel(x,y)
  40.         end,
  41.  
  42.         buff_drawpixel = function(npix)
  43.             if(not npix.tc)then
  44.                 term.setBackgroundColor(self.cls)
  45.                 write(" ")
  46.             end
  47.             term.setBackgroundColor(npix.bc)
  48.             term.setTextColor(npix.tc)
  49.             term.setCursorPos(npix.x, npix.y)
  50.             write(npix.ch)
  51.             term.setBackgroundColor(self.cls)
  52.         end,
  53.        
  54.         drawpixel = function(self,xx,yy)
  55.             if(self.screen[yy][xx].ch)then
  56.                 term.setBackgroundColor(self.screen[yy][xx].bc)
  57.                 term.setTextColor(self.screen[yy][xx].tc)
  58.                 term.setCursorPos(xx, yy)
  59.                 write(self.screen[yy][xx].ch)
  60.                 term.setBackgroundColor(self.cls)
  61.             else
  62.                 term.setBackgroundColor(self.cls)
  63.                 term.setCursorPos(xx, yy)
  64.                 write(" ")
  65.             end
  66.         end,
  67.  
  68.         movepixel = function(self,x,y,xx,yy)
  69.             if(self.screen[y+yy] ~= nil)then
  70.                 if(self.screen[y+yy][x+xx] ~= nil)then
  71.                     tmp = self.screen[y][x]
  72.                     self.screen[y][x] = {}
  73.                     self:drawpixel(x,y)
  74.                     self.screen[y+yy][x+xx] = tmp
  75.                     self:drawpixel(x+xx,y+yy)
  76.                     return true
  77.                 end
  78.             end
  79.             return false --]] Spot not available (Most likely it doesnt exist)
  80.         end,
  81.  
  82.         --]] Useful for moving pixels and changing tags like pixel x and y's
  83.         movepixel_tags = function(self,x,y,xx,yy,tagandvals)
  84.             self:movepixel(x,y,xx,yy)
  85.             local pix = self.screen[y+yy][x+xx]
  86.             for k, v in pairs(tagandvals) do
  87.                 if(pix[k])then pix[k] = v end
  88.             end
  89.             self.screen[y+yy][x+xx] = pix
  90.         end,
  91.  
  92.         checkforpixel = function(self,x,y)
  93.             return (self.screen[y][x].ch ~= nil)
  94.         end,
  95.  
  96.         getpixel = function(self,x,y)
  97.             return self.screen[y][x]
  98.         end,
  99.  
  100.         gettag = function(self,x,y,tname)
  101.             return self.screen[y][x][tname]
  102.         end,
  103.  
  104.         setpixelcolor = function(self,x,y,t,b)
  105.             if(self.screen[y][x])then
  106.                 if(self.screen[y][x].ch)then
  107.                     self.screen[y][x].tc = t
  108.                     self.screen[y][x].bc = b
  109.                     self:drawpixel(x,y)
  110.                 end
  111.             end
  112.         end,
  113.  
  114.  
  115.         setclearcolor = function(self,c)
  116.             self.clear = c
  117.         end,
  118.  
  119.         drawscreen = function(self)
  120.             term.setBackgroundColor(self.cls)
  121.             for yy = 1, self.h do
  122.                 for xx = 1, self.w do
  123.                     if(self.screen[yy][xx].ch)then
  124.                         term.setBackgroundColor(self.screen[yy][xx].bc)
  125.                         term.setTextColor(self.screen[yy][xx].tc)
  126.                         term.setCursorPos(xx, yy)
  127.                         write(self.screen[yy][xx].ch)
  128.                         term.setBackgroundColor(self.cls)
  129.                     else
  130.                         term.setCursorPos(xx, yy)
  131.                         write(" ")
  132.                     end
  133.                 end
  134.             end
  135.         end,
  136.  
  137.         shift = function(self,w,h,xo,yo)
  138.             for yy = 1, h do
  139.                 for xx = 1, w do
  140.                     tmp = self.screen[yy][xx]
  141.                     self.screen[yy][xx] = {}
  142.                     self:drawpixel(yy,xx)
  143.                     self.screen[yy+yo][xx+xo] = tmp
  144.                 end
  145.             end
  146.         end,
  147.     }
  148.  
  149.     field:drawscreen()
  150.     return field
  151. end
  152.  
  153. local mybuff = gamemap(51,19,colors.black)
  154. mybuff:addpixel(5,5,"HEYEYYY A STRING",colors.white,colors.blue)
  155. mybuff:addpixel(5,7,"LOLOLOL A LONNNNNNGGGERRR STRING XD",colors.white,colors.blue)
  156. term.setCursorPos(1,15)
  157. term.setBackgroundColor(colors.black)
  158.  
  159. while true do
  160.     local e = {os.pullEvent()}
  161.     if(e[1] == "mouse_click" or e[1] == "mouse_drag" )then
  162.         if(e[4] <= mybuff.h)then
  163.             mybuff:setpixelcolor(e[3],e[4],colors.blue,colors.white)
  164.             sleep(0.2)
  165.             if(mybuff:checkforpixel(e[3],e[4]))then
  166.                 mybuff:setpixelcolor(e[3],e[4],colors.white,colors.blue)
  167.                 mybuff:movepixel(e[3],e[4],math.random(20),math.random(19))
  168.             end
  169.         end
  170.     end
  171. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement