Aadvert

Matrix - work

Feb 11th, 2012
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Matrix screensaver v0.7
  2. -- (C) 2012 User 'Advert' at http://www.computercraft.info/forums2/
  3. -- X11/mit licence (use/modify at your will, but please, leave credit where due)
  4. -- Spacebar to quit cleanly!
  5. local mobj = {}
  6. do -- Standart variables.
  7.     mobj.x = 0
  8.     mobj.y = 0 -- Offscreen
  9.     mobj.lingerMin = 3 -- Minimum time to linger (frames)
  10.     mobj.lingerMax = 10 -- Maximum time to linger
  11.     mobj.linger = 10 -- How long this object will linger
  12.     mobj.tailLenMin = 3 -- Minimum tail length
  13.     mobj.tailLenMax = 10 -- Maximum tail length
  14.     mobj.tailLen = 10
  15.     mobj.chars = "1234567890-=!@#$%^&*()_+qwertyuiop[]QWERTYUIOP{}asdfghjkl;'\\ASDFGHJKL:\"|zxcvbnm,./ZXCVBNM<>?"
  16.     mobj.charsLen = mobj.chars:len()
  17.     mobj.currentTail = {"a", "a", "a", "a", "a", "a", "a", "a", "a", "a"}
  18.     mobj.secretText = {}
  19. end
  20.  
  21. function mobj:new(o, ...)
  22.     local o = o or {}
  23.     setmetatable(o, self)
  24.     self.__index = self
  25.     o:__init(...)
  26.     return o
  27. end
  28.  
  29.  
  30. function mobj:__init(x)
  31.     self.linger = math.random(self.lingerMin, self.lingerMax)
  32.     self.tailLen = math.random(self.tailLenMin, self.tailLenMax)
  33.     self.x = x
  34.     self.y = 1
  35.     self.nFrame = 1
  36.     self.currentTail = {}
  37. end
  38. function mobj:randomLinger()
  39.     self.linger = math.random(self.lingerMin, self.lingerMax)
  40. end
  41. function mobj:printPos(x, y, sText)
  42.     term.setCursorPos(x, y)
  43.     term.write(sText)
  44. end
  45. function mobj:printPosT(x, y, sText)
  46.     if self.secretText[y] and self.secretText[y][x] then
  47.         sText = self.secretText[y][x]
  48.     end
  49.     return self:printPos(x, y, sText)
  50. end
  51. function mobj:randomChar()
  52.     local r = math.random(1, self.charsLen)
  53.     return string.sub(self.chars, r, r)
  54. end
  55. function mobj:printTail()
  56.     for i = 1, self.tailLen do
  57.         if not self.currentTail[i] then break end
  58.         self:printPosT(self.x, self.y - i, self.currentTail[i])
  59.     end
  60. end
  61.  
  62. local termX, termY = term.getSize()
  63. do
  64.     for y = 1, termY do
  65.         mobj.secretText[y] = {}
  66.     end
  67.     function mobj:addSecretText(x, y, sText)
  68.         if sText:len() + x > termX then
  69.             -- lolno.
  70.         else
  71.             for i = 1, sText:len() do
  72.                 local chr = sText:sub(i, i)
  73.                 self.secretText[y][x + i-1] = chr
  74.             end
  75.         end
  76.     end
  77.     function mobj:addSecretTextD(x, y, dir, sText)
  78.         -- 1 = bottomright, 2 = bottomleft, 3 = topright, 4 = topleft
  79.         local xmod = dir %2 == 1 and 1 or -1
  80.         local ymod = (dir == 1 or dir == 2) and 1 or -1
  81.         local finalx, finaly = x + (xmod * sText:len()), y + (ymod *sText:len())
  82.         --print("finalx, finaly", finalx, finaly)
  83.         --read()
  84.         local cx, cy = x, y
  85.         for i = 1, sText:len() do
  86.             local cx, cy = x + (xmod * (i -1)), y + (ymod * (i -1))
  87.             if self.secretText[cy] then
  88.                 self.secretText[cy][cx] = sText:sub(i, i)
  89.             end
  90.         end
  91.     end
  92.    
  93. end
  94.  
  95. function mobj:render()
  96.     self:printTail()
  97.     self.nFrame = self.nFrame + 1
  98.     if self.nFrame == self.linger then
  99.         table.insert(self.currentTail, 1, self:randomChar())
  100.         self:printPosT(self.x, self.y, self.currentTail[1])
  101.         self.y = self.y + 1
  102.         self.nFrame = 1
  103.         if self.y > termY + self.tailLen then
  104.             self:__init(self.x)
  105.         end
  106.     else
  107.         self:printPos(self.x, self.y, self:randomChar())
  108.     end
  109. end
  110.  
  111. local objects = {}
  112. local eventT
  113. function init()
  114.     for i = 1, termX do
  115.         objects[i] = mobj:new({}, i)
  116.     end
  117.     eventT = os.startTimer(0)
  118. end
  119.  
  120.  
  121. function render()
  122.     term.clear()
  123.     for _, k in pairs(objects) do
  124.         k:render()
  125.     end
  126. end
  127.  
  128.  
  129.  
  130.  
  131. mobj:addSecretTextD(15, 4, 1, "Casper7526")
  132. mobj:addSecretTextD(18, 4, 1, "made me")
  133. mobj:addSecretTextD(21, 4, 1, "do this.")
  134. mobj:addSecretText(15, 15, "Spacebar to exit.")
  135. init()
  136.  
  137. ---[[
  138. while true do
  139.     local e, p1, p2 = os.pullEvent()
  140.     if e == "timer" and p1 == eventT then
  141.         eventT = os.startTimer(0.05) -- 20 fps
  142.         render()
  143.     elseif e == "key" and p1 == 57 then
  144.         term.clear()
  145.         term.setCursorPos(1,1)
  146.         break
  147.     end
  148. end
  149. --]]
  150.  
  151. --[[for y = 1, termY do
  152.     for x = 1,  termX do
  153.         mobj:printPosT(x, y, " ")
  154.     end
  155. end--]]
Advertisement
Add Comment
Please, Sign In to add comment