Driftix

Untitled

Mar 31st, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.37 KB | None | 0 0
  1. taken from https://github.com/blunty666/CC-Programs-and-APIs/blob/master/games/WhereIsDan/WhereIsDan.lua
  2.  
  3. local BOX = {
  4.     WIDTH = 20,
  5.     HEIGHT = 9,
  6. }
  7.  
  8. local BUTTON = {
  9.     WIDTH = 18,
  10. }
  11.  
  12. local TIMEOUT = 300 -- cache quotes for 5 minutes
  13.  
  14. local URL = "https://raw.githubusercontent.com/blunty666/CC-Programs-and-APIs/master/games/WhereIsDan/quotes.lua"
  15.  
  16. local WIDTH, HEIGHT = term.getSize()
  17. local CENTER_X, CENTER_Y = math.ceil(WIDTH/2), math.ceil(HEIGHT/2)
  18.  
  19. do
  20.     local DELTA_X, DELTA_Y = math.floor(BOX.WIDTH/2), math.floor(BOX.HEIGHT/2)
  21.    
  22.     BOX.X_MIN = CENTER_X - DELTA_X
  23.     BOX.X_MAX = BOX.X_MIN + BOX.WIDTH - 1
  24.    
  25.     BOX.Y_MIN = CENTER_Y - DELTA_Y
  26.     BOX.Y_MAX = BOX.Y_MIN + BOX.HEIGHT - 1
  27.  
  28.     BOX.BUTTON_TEXT = {
  29.         X = CENTER_X,
  30.         Y = BOX.Y_MIN + 2,
  31.     }
  32.  
  33.     BOX.BUTTON = {
  34.         X_MIN = CENTER_X - math.floor(BUTTON.WIDTH/2),
  35.         X_MAX = CENTER_X - math.floor(BUTTON.WIDTH/2) + BUTTON.WIDTH - 1,
  36.  
  37.         Y_MIN = BOX.Y_MIN + 1,
  38.         Y_MAX = BOX.Y_MIN + 3,
  39.     }
  40.  
  41.     BOX.TEXT = {
  42.         X = CENTER_X,
  43.         Y = BOX.Y_MIN + 5,
  44.     }
  45. end
  46.  
  47. local CHANGE_PROBABILITY = 0.15
  48.  
  49. local particles = {}
  50.  
  51. local function randomColour()
  52.     return 2^(math.random(0, 14))
  53. end
  54.  
  55. local directions = {
  56.     [0] = vector.new(0, -1, 0),
  57.     [1] = vector.new(1, 0, 0),
  58.     [2] = vector.new(0, 1, 0),
  59.     [3] = vector.new(-1, 0, 0),
  60. }
  61.  
  62. local function newParticle(x, y, colour, directionIndex)
  63.     return {
  64.         position = vector.new(x, y, 0),
  65.         colour = colour,
  66.         directionIndex = directionIndex,
  67.         direction = directions[directionIndex],
  68.         distanceTravelled = 0,
  69.     }
  70. end
  71.  
  72. local function isInTerm(x, y)
  73.     return 1 <= x and x <= WIDTH and 1 <= y and y <= HEIGHT
  74. end
  75.  
  76. local function shouldChangeDirection(particle)
  77.     return math.random() < CHANGE_PROBABILITY
  78. end
  79.  
  80. local function shouldDraw(particle)
  81.     local x, y = particle.position.x, particle.position.y
  82.     return not (BOX.X_MIN <= x and x <= BOX.X_MAX and BOX.Y_MIN <= y and y <= BOX.Y_MAX)
  83. end
  84.  
  85. local function draw(particle)
  86.     term.setCursorPos(particle.position.x, particle.position.y)
  87.     term.setBackgroundColour(particle.colour)
  88.     term.write(" ")
  89. end
  90.  
  91. local function updateParticles()
  92.     while true do
  93.         for _, particle in ipairs(particles) do
  94.             local nextPosition = particle.position + particle.direction
  95.             if not isInTerm(nextPosition.x, nextPosition.y) or shouldChangeDirection(particle) then
  96.  
  97.                 local directionDelta = (math.random() > 0.5 and 1) or -1
  98.                 local newDirectionIndex, newDirection = particle.directionIndex, nil
  99.                 repeat
  100.                     newDirectionIndex = (newDirectionIndex + directionDelta) % 4
  101.                     newDirection = directions[newDirectionIndex]
  102.                     nextPosition = particle.position + newDirection
  103.                 until isInTerm(nextPosition.x, nextPosition.y)
  104.                
  105.                 -- update direction
  106.                 particle.directionIndex = newDirectionIndex
  107.                 particle.direction = newDirection
  108.  
  109.                 -- reset distanceTravelled
  110.                 particle.distanceTravelled = 0
  111.             end
  112.  
  113.             -- move particle
  114.             particle.position = nextPosition
  115.  
  116.             -- increase distanceTravelled
  117.             particle.distanceTravelled = particle.distanceTravelled + 1
  118.  
  119.             -- check if should draw particle and draw
  120.             if shouldDraw(particle) then
  121.                 draw(particle)
  122.             end
  123.         end
  124.         sleep(0.05)
  125.     end
  126. end
  127.  
  128. local function writeText(xPos, yPos, text, bgCol, textCol)
  129.     if bgCol then term.setBackgroundColour(bgCol) end
  130.     if textCol then term.setTextColour(textCol) end
  131.     term.setCursorPos(xPos, yPos)
  132.     term.write(text)
  133. end
  134.  
  135. local function writeTextAligned(text, xPos, yPos, bgCol, textCol)
  136.     local startX = xPos - math.ceil(#text/2)
  137.     writeText(startX, yPos, text, bgCol, textCol)
  138. end
  139.  
  140. local function drawButton(colour)
  141.     paintutils.drawFilledBox(BOX.BUTTON.X_MIN, BOX.BUTTON.Y_MIN, BOX.BUTTON.X_MAX, BOX.BUTTON.Y_MAX, colour)
  142.     writeTextAligned("WHERE IS DAN?", BOX.BUTTON_TEXT.X, BOX.BUTTON_TEXT.Y, colour, colours.white)
  143. end
  144.  
  145. local function searchBarTimer()
  146.     return math.random() / 4
  147. end
  148.  
  149. local function handleEvents()
  150.     local quotes = {
  151.         "<INSERT TEXT HERE>",
  152.     }
  153.     local requested = false
  154.     local cachedTime = -(math.huge)
  155.  
  156.     local clicked = false
  157.  
  158.     local updateTimer = false
  159.     local counter = 1
  160.  
  161.     local event = {os.pullEventRaw()}
  162.     while true do
  163.         if event[1] == "terminate" or (event[1] == "char" and string.lower(event[2]) == "q") then
  164.             break
  165.         elseif event[1] == "mouse_click" then
  166.             if event[2] == 1 and (BOX.BUTTON.X_MIN <= event[3] and event[3] <= BOX.BUTTON.X_MAX) and (BOX.BUTTON.Y_MIN <= event[4] and event[4] <= BOX.BUTTON.Y_MAX) then
  167.                 clicked = true
  168.                 drawButton(colours.red)
  169.             end
  170.         elseif event[1] == "mouse_up" then
  171.             if clicked then
  172.                 drawButton(colours.green)
  173.                 if os.clock() - cachedTime > TIMEOUT then
  174.                     if not requested then
  175.                         http.request(URL)
  176.                         requested = true
  177.                     end
  178.                 end
  179.                 if not updateTimer then
  180.                     updateTimer = os.startTimer(searchBarTimer())
  181.                     writeText(BOX.X_MIN, BOX.TEXT.Y, string.rep(" ", BOX.WIDTH), colours.black)
  182.                     writeTextAligned("Searching for Dan...", BOX.TEXT.X, BOX.TEXT.Y + 1, colours.black, colours.white)
  183.                 end
  184.                 clicked = false
  185.             end
  186.         elseif event[1] == "http_success" and event[2] == URL then
  187.  
  188.             quotes = textutils.unserialise(event[3].readAll())
  189.             requested = false
  190.             cachedTime = os.clock()
  191.        
  192.         elseif event[1] == "timer" then
  193.             if event[2] == updateTimer then
  194.                 if counter <= BOX.WIDTH - 2 then
  195.                     writeText(BOX.X_MIN + 1, BOX.TEXT.Y, string.rep(" ", counter), colours.cyan)
  196.                     updateTimer = os.startTimer(searchBarTimer())
  197.                     counter = counter + 1
  198.                 else
  199.                     writeText(BOX.X_MIN, BOX.TEXT.Y, string.rep(" ", BOX.WIDTH), colours.black)
  200.                     writeText(BOX.X_MIN, BOX.TEXT.Y + 1, string.rep(" ", BOX.WIDTH), colours.black)
  201.                     writeTextAligned(quotes[math.random(1, #quotes)], BOX.TEXT.X, BOX.TEXT.Y, colours.black, colours.white)
  202.                     updateTimer = false
  203.                     counter = 1
  204.                 end
  205.             end
  206.         end
  207.         event = {os.pullEventRaw()}
  208.     end
  209. end
  210.  
  211. term.setBackgroundColour(colours.black)
  212. term.clear()
  213.  
  214. drawButton(colours.green)
  215. writeTextAligned("Press 'q' to Quit", BOX.TEXT.X, BOX.TEXT.Y + 3, colours.black, colours.white)
  216.  
  217. for i = 1, 25 do
  218.     local x = math.random(1, WIDTH)
  219.     local y = math.random(1, HEIGHT)
  220.     local colour = randomColour()
  221.     local directionIndex = math.random(0, 3)
  222.     table.insert(particles, newParticle(x, y, colour, directionIndex))
  223. end
  224.  
  225. parallel.waitForAny(handleEvents, updateParticles)
  226.  
  227. term.setBackgroundColour(colours.black)
  228. term.setTextColour(colours.white)
  229. term.setCursorPos(1, 1)
  230. term.clear()
Advertisement
Add Comment
Please, Sign In to add comment