TheCryptek

[LUA] Matrix

Aug 12th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.69 KB | None | 0 0
  1. term.redirect(peripheral.wrap("top"))
  2. local chars = {}
  3.  
  4. local function new(t)
  5.     t.y = 0
  6.     table.insert(chars, t)
  7. end
  8.  
  9. setmetatable(chars, {__call = function(t)
  10.     local i = 0
  11.     return function()
  12.         i = i + 1
  13.         return t[i], i
  14.     end
  15. end})
  16.  
  17. local function timer(period)
  18.     return {
  19.         id = nil;
  20.         run = function(self)
  21.             self.id = os.startTimer(period)
  22.         end;
  23.     }
  24. end
  25.  
  26. local updateTimer = timer(0.1)
  27. local creator = timer(0.1)
  28.  
  29. updateTimer:run()
  30. creator:run()
  31.  
  32. term.setBackgroundColor(colors.black)
  33. term.clear()
  34. term.setCursorPos(1,1)
  35.  
  36. while true do
  37.     local _, t = os.pullEvent('timer')
  38.     local w,h = term.getSize()
  39.    
  40.     if t == updateTimer.id then
  41.         for char, i in chars() do
  42.             char.y = char.y + 1
  43.            
  44.             if char.y > h then
  45.                 table.remove(chars, i)
  46.             else
  47.                 term.setCursorPos(char.x, char.y)
  48.                 if char.color == colors.black then
  49.                     term.setBackgroundColor(colors.black)
  50.                     term.write(' ')
  51.                 else
  52.                     term.setTextColor(char.color)
  53.                     term.write(string.char(math.random(38, 126)))
  54.                 end
  55.             end
  56.         end
  57.        
  58.         updateTimer:run()
  59.     elseif t == creator.id then
  60.        
  61.         local time = math.random()*0.8 + 0.5
  62.         local tail = timer(time)
  63.         local eraser = timer(time + math.random()*0.25 + 0.5)
  64.        
  65.         new {
  66.             x = math.random(w);
  67.             color = colors.green;
  68.             tail = tail;
  69.             eraser = eraser;
  70.             ishead = true;
  71.         }
  72.        
  73.         creator:run()
  74.         tail:run()
  75.         eraser:run()
  76.     else
  77.         for char in chars() do
  78.             if char.ishead then
  79.                 if t == char.tail.id then
  80.                     new {
  81.                         x = char.x;
  82.                         color = colors.green;
  83.                     }
  84.                     break
  85.                 elseif t == char.eraser.id then
  86.                     new {
  87.                         x = char.x;
  88.                         color = colors.black;
  89.                     }
  90.                     break
  91.                 end
  92.             end
  93.         end
  94.     end
  95. end
Add Comment
Please, Sign In to add comment