Advertisement
Guest User

matrix.lua

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