Advertisement
CaptainSpaceCat

Starfield Lateral

Jun 12th, 2015
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.58 KB | None | 0 0
  1. local w, h = term.getSize()
  2. local stars = {}
  3. local velocity = 1
  4. local tArgs = {...}
  5. local compnum = tonumber(tArgs[1])
  6. local sendnum = compnum + 1
  7. if sendnum == 5 then sendnum = 1 end
  8.  
  9. Star = {
  10. x = 0,
  11. y = 0
  12. }
  13.  
  14. function Star:new(x, y)
  15.     local class = {x = x, y = y}
  16.     setmetatable(class, self)
  17.     self.__index = self
  18.     return class
  19. end
  20.  
  21. function generateStars()
  22.     local generatelist = {}
  23.     for i = 1, 30 do
  24.         local x = nil
  25.         local y = nil
  26.         local success = false
  27.         while not success do
  28.             x = math.random(1, w)
  29.             y = math.random(1, h)
  30.             success = true
  31.             for i, v in pairs(generatelist) do
  32.                 if i == x and v == y then
  33.                     success = false
  34.                 end
  35.             end
  36.         end
  37.         stars[i] = Star:new(x, y)
  38.         generatelist[#generatelist + 1] = {x, y}
  39.     end
  40. end
  41.  
  42. term.setBackgroundColor(colors.black)
  43. term.clear()
  44. generateStars()
  45. local deleteprev = false
  46. while true do
  47.     if velocity < 0 then
  48.         sendnum = sendnum - 2
  49.     elseif velocity > 0 then
  50.         sendnum = sendnum + 2
  51.     end
  52.     if sendnum < 0 then sendnum = sendnum + 4 end
  53.     if sendnum > 4 then sendnum = sendnum - 4 end
  54.     local newstar = nil
  55.     newstar = {rednet.receive(.05)}
  56.     if newstar and newstar[3] == tostring(compnum) then
  57.         stars[#stars + 1] = Star:new(w, tonumber(newstar[2]))
  58.     elseif newstar and newstar[2] == "velocity +" then
  59.         velocity = velocity + 1
  60.     elseif newstar and newstar[2] == "velocity -" then
  61.         velocity = velocity - 1
  62.     end
  63.     if velocity > 3 then velocity = 3 end
  64.     if velocity < -3 then velocity = -3 end
  65.     for i, _ in pairs(stars) do
  66.         if stars[i] ~= false then
  67.             local star = stars[i]
  68.             if (star.x < 1 and velocity > 0) or (star.x > w and velocity < 0) then
  69.                 term.setBackgroundColor(colors.black)
  70.                     term.setCursorPos(1, star.y)
  71.                     term.write("  ")
  72.                 rednet.broadcast(tostring(star.y), tostring(sendnum))
  73.                 stars[i] = false
  74.                 else
  75.                 term.setBackgroundColor(colors.black)
  76.                     term.setCursorPos(star.x + velocity, star.y)
  77.                     term.write(" ")
  78.                     term.setCursorPos(star.x, star.y)
  79.                 term.setBackgroundColor(colors.white)
  80.                     term.write("*")
  81.                     star.x = star.x - velocity
  82.                 stars[i] = star
  83.             end
  84.         end
  85.     end
  86.     --[[local delList = {}
  87.     for i, _ in pairs(stars) do   --note: this way is actually more efficient, but in actual use, it doesn't change
  88.         if not stars[i] then  --enough to make the extra lag worth it
  89.             delList[#delList + 1] = i
  90.         end
  91.     end
  92.     for i, v in pairs(delList) do
  93.         table.remove(stars, v - i + 1)
  94.     end]]--
  95.     for i = 1, #stars do
  96.         if stars[i] ~= nil and stars[i] == false then
  97.             table.remove(stars, i)
  98.         end
  99.     end
  100. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement