Advertisement
Redxone

[ComputerCraft] Agar.CC

May 22nd, 2016
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.63 KB | None | 0 0
  1. os.loadAPI("grid")
  2. -- map drawing offsets map draw starts at xoff and yoff and ends at
  3. -- "visplanew" (width) and "visplaneh" (height)
  4. local xoff  = 0
  5. local yoff  = 0
  6. -- how fast things in the game update
  7. local gameloop = os.startTimer(0.05)
  8. -- how many food items to spawn on first map load
  9. local dif = 10000
  10. -- creating the level grid
  11. local level = grid.create(1000, 1000)
  12. -- weather or not to use a screen buffer (faster) or not (slower)
  13. local usebuffer = true
  14. -- fill the level with empty tiles.
  15. level:fill("0")
  16. -- set the visplanes equal to the width and height of the screen
  17. local visplanew, visplaneh = term.getSize()
  18. -- a table to store all block related functions
  19. local blocks = {
  20. -- creates a food item with a random x, y and color
  21.   newFood = function()
  22.  
  23.       local food = {}
  24.       local x = math.ceil(math.random(1,1000))
  25.       local y = math.ceil(math.random(1,1000))
  26.       local color = math.ceil(math.random(1,15))
  27.       -- colors can be converted easily from numbers like 1,15 by simply
  28.       -- have 2 to the power of the number.
  29.       color = 2^tonumber(color)
  30.      
  31.       food.color = color
  32.       food.x = x
  33.       food.y = y
  34.      
  35.       return food
  36.   end,
  37. }
  38. function foodSpawn()
  39.   for i = 1, dif do  --how many we spawn at once
  40.     local f = blocks.newFood()
  41.     -- put the food item in the level at the
  42.     -- random x and y of the returned food item
  43.     level:set(f.x,f.y,f)
  44.   end
  45. end
  46.  
  47. -- The players table player[1] is client and all other players are the other people playing,
  48. -- with the client on the server.
  49. local players = {
  50.   {name="player1",size=1,food=0,x=math.ceil(math.random(visplanew,1000-visplanew)),
  51.    y=math.ceil(math.random(visplaneh,1000-visplaneh))},
  52. }
  53.  
  54. --]] Player drawing methods [[--
  55.          -- the draw x and y is equal to an equation used to display the player in the middle
  56.          -- or close to the middle of the screen as the player grows in size
  57. -- clearing the player from the screen.
  58. local function undrawplayer()
  59.       local px, py, ps = visplanew/2 - math.floor((players[1].size+1)/2)+2,
  60.                       ((visplaneh/2)-1) - math.floor((players[1].size)/2)+2,
  61.         players[1].size
  62.     paintutils.drawFilledBox(px-ps,py-ps,px+ps,py+ps,colors.white)
  63.     -- drawplayerS
  64. end
  65. -- used for drawing the player
  66. local function drawplayer()
  67.     term.current().setVisible(false)
  68.       local px, py, ps = visplanew/2 - math.floor((players[1].size-2)/2)+2,
  69.                       ((visplaneh/2)-1) - math.floor((players[1].size)/4)+3,
  70.         players[1].size
  71.       term.setTextColor(colors.white)
  72.       paintutils.drawFilledBox(px-ps,py-ps,px+ps,py+ps,colors.lightBlue)
  73.       term.setCursorPos(px,py)
  74.       write("P")
  75.     term.current().setVisible(true)
  76. end
  77.  
  78. -- The update function used to update players positions and client players movement
  79. -- also used to keep the view centered when the player grows
  80. local function update(ev)
  81.     local x, y, size = players[1].x,players[1].y,players[1].size
  82.     -- attempt to grow if we have enough food
  83.     -- make sure the players grown size isnt bigger than the map
  84.     -- if it is then recenter the player away from the edge of the map
  85.     -- so that the player can grow.
  86.     -- the reason we do this is because of the constant collision check
  87.     -- reverencing the level map at the players coords plus players size.
  88.     -- if we try to reference a point that doesnt exist the game crashes.
  89.     if(players[1].food >= 10 * players[1].size)then
  90.       while players[1].x+players[1].size+2 > level:getWidth() do
  91.           players[1].x = players[1].x - 1
  92.       end
  93.       while players[1].y+players[1].size+2 > level:getHeight() do
  94.           players[1].y = players[1].y - 1
  95.       end
  96.        players[1].size = players[1].size + 2
  97.        drawplayer()
  98.        centercam()
  99.     end
  100.     -- Movement and level edge check.
  101.     if(ev[1] == "key")then
  102.       if(ev[2] == keys.w and y-size > 1)then
  103.         players[1].y = players[1].y - 1
  104.         yoff = yoff - 1
  105.          render()
  106.       elseif(ev[2] == keys.s and y+size < level:getHeight())then
  107.         players[1].y = players[1].y + 1
  108.          yoff = yoff + 1
  109.           render()
  110.       elseif(ev[2] == keys.a and x-size > 1)then
  111.         players[1].x = players[1].x - 1
  112.          xoff = xoff - 1
  113.           render()
  114.       elseif(ev[2] == keys.d and x+size < level:getWidth())then
  115.         players[1].x = players[1].x + 1
  116.          xoff = xoff + 1
  117.           render()
  118.       -- developer keys
  119.       elseif(ev[2] == keys.x)then
  120.           run = false
  121.           term.setCursorPos(1,1)
  122.           term.setBackgroundColor(colors.black)
  123.           term.setTextColor(colors.white)
  124.           term.clear()
  125.           return false
  126.       end
  127.     end
  128.  
  129.     checkfoodcollide(players[1].x,players[1].y,players[1].size)
  130.  
  131. end
  132. -- setup the buffer on first run
  133. local buffer = grid.create(visplanew,visplaneh)
  134. buffer:fill("0")
  135. local bxoff = 0
  136. local byoff = 0
  137. -- renders blocks in the level
  138. function render()
  139.   term.current().setVisible(false)
  140.   if(usebuffer)then
  141.     for by = 1, buffer:getHeight() do
  142.       for bx = 1, buffer:getWidth() do
  143.         term.setBackgroundColor(colors.white)
  144.           if(buffer:get(bx,by) ~= "0")then
  145.               -- if there is a item in the buffer
  146.               -- draw over it to remove it from the screen.
  147.               local bitem = buffer:get(bx,by)
  148.               term.setCursorPos(bitem.x - bxoff,bitem.y -byoff)
  149.               write(" ")
  150.           end    
  151.        end
  152.     end
  153.   end
  154.   -- loop through the blocks visible to the client
  155.   for y = 1+yoff, visplaneh+yoff do
  156.     for x = 1+xoff, visplanew+xoff do
  157.      term.setBackgroundColor(colors.white)
  158.      -- if theres a spot on the buffer that shouldnt have a block but does
  159.      -- clear that spot.
  160.      if(usebuffer and (level:get(x,y) == "0" or level:get(x,y) == nil) and buffer:get(x-xoff,y-yoff) ~= "0")then
  161.          buffer:set(x-xoff,y-yoff,"0")
  162.      end
  163.      -- if the block we are at is not empty and has a color then draw it
  164.       if(level:get(x,y) ~= "0" and level:get(x,y).color ~= nil)then
  165.         --render code ...
  166.         -- it doesnt see a correct background color
  167.         local item = level:get(x,y)
  168.         term.setBackgroundColor(item.color)
  169.         term.setCursorPos(x-xoff,y-yoff)
  170.         write(" ")
  171.         term.setBackgroundColor(colors.white)
  172.         -- add the drawn block to the buffer
  173.           buffer:set(x-xoff,y-yoff,item)
  174.       elseif(not usebuffer)then
  175.         term.setBackgroundColor(colors.white)
  176.         term.setCursorPos(x-xoff,y-yoff)
  177.         write(" ")
  178.       elseif(level:get(x,y) ~= "0" and level:get(x,y).color == nil)then
  179.         -- if we are at the end of the map draw a lightGray background
  180.          term.setBackgroundColor(colors.lightGray)
  181.          term.setCursorPos(x-xoff,y-yoff)
  182.          write(" ")
  183.          -- add a block to the buffer if it is at the end of the map
  184.          -- because other wise the block will draw over the light gray background
  185.          buffer:set(x-xoff,y-yoff,{x=x,y=y})
  186.       end
  187.     end
  188.   end
  189.   -- set the screen to visible and set the buffer offsets
  190.   -- the buffer offsets are equal to the previous offsets so that the buffer
  191.   -- knows to undraw the previous frame
  192.   term.current().setVisible(true)
  193.      bxoff = xoff
  194.      byoff = yoff
  195.      drawplayer()
  196. end
  197.  
  198. function renderplayers()
  199.    for i = 1, #players do
  200.        
  201.    end
  202. end
  203.  
  204.  
  205. -- checks collision between 2 points and there size.
  206. function checkCollision(x,y,size,ex,ey,esize)
  207.    --local ex, ey = players[i].x, players[i].y
  208.    --local esize = players[i].size
  209.      local left = x-size
  210.      local right = x+size
  211.      local top = y-size
  212.      local down = x-size
  213.      local eleft = ex-esize
  214.      local eright = ex+esize
  215.      local etop = ey-esize
  216.      local edown = ey+esize
  217.      local topdown = (top >= edown or bottom <= ebottom)
  218.      if(left >= eright and topdown)then
  219.          return true
  220.      end
  221.      if(right <= eleft and topdown)then
  222.          return true
  223.      end
  224.      
  225.      return false
  226. end
  227.  
  228. -- check if players collide
  229. function checkpcollide(x,y,size)
  230.    for i = 1, #players do
  231.       local collides = checkCollision(x,y,size,players[i].x,players[i].y,players[i].size)
  232.    end
  233.    return collides
  234. end
  235.  
  236. -- check if there is a collision with the food
  237. -- if there is give the food to the player and take the food
  238. -- from the map
  239. -- this is the reason the buffer check is needed, it will
  240. -- remove the instance of that food from the buffer
  241. function checkfoodcollide(x,y,size)
  242.    for iy = y-size, y+size do
  243.      for ix = x-size, x+size do
  244.          if(level:get(ix,iy) ~= "0")then
  245.              players[1].food = players[1].food + 1
  246.              level:set(ix,iy,"0")
  247.  
  248.          end
  249.      end
  250.    end
  251. end
  252.  
  253. -- clear the screen
  254. term.setBackgroundColor(colors.white)
  255. term.setTextColor(colors.black)
  256. term.clear()
  257.  
  258. -- centers the camera onto the players bounding box
  259. -- important note, the player will ALWAYS be in near the center of the screen
  260. -- but the bounding box's collision x and y's always change so we have to constantly
  261. -- line up the bounding box with the drawn player.
  262. function centercam()  
  263.   xoff = math.ceil((players[1].x) - ( visplanew/2 - math.floor((players[1].size-2)/2)+2) )
  264.   yoff = math.ceil((players[1].y) - ( ((visplaneh/2)-1) - math.floor((players[1].size)/4)+3) )
  265. end
  266.  
  267. local run = true
  268. -- center the camera spawn the food and render the screen and player.
  269. centercam()
  270. foodSpawn()
  271. render()
  272.  
  273.  
  274. function loop()
  275.   while run do
  276.       -- wait for events and use the event to
  277.       -- update the game.
  278.       local e = {os.pullEvent()}
  279.       if(e[1] == "timer")then
  280.          gameloop = os.startTimer(0.05)
  281.       end  
  282.       update(e)
  283.   end  
  284. end
  285. loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement