Advertisement
massacring

startup

Jan 5th, 2025 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.24 KB | None | 0 0
  1. local Button = require('Button')
  2. local Commons = require('Commons')
  3. local Plinko = require('plinko')
  4. local cashout = require('cashout')
  5. local monitor = peripheral.find("monitor")
  6. local input = peripheral.find("minecraft:hopper")
  7. local storage = peripheral.find("minecraft:chest")
  8. local modem = peripheral.wrap("bottom")
  9. local printer = peripheral.find("printer")
  10. if printer.getInkLevel() == 0 and printer.getPaperLevel() == 0 then
  11.   error("Cannot start a new page. Do you have ink and paper?")
  12. end
  13.  
  14. monitor.setBackgroundColor(colors.black)
  15. monitor.clear()
  16. monitor.setTextScale(1)
  17. local window = window.create(monitor, 1, 1, 40, 19)
  18. local width, height = window.getSize()
  19. local startButton, resetButton
  20. local gameStarted = false
  21.  
  22. local defaultBackgroundColor = colors.lightGray
  23. local defaultTextColor = colors.white
  24.  
  25. local function drawSquare(x, y, span, length, color)
  26.     window.setCursorPos(x,y)
  27.     window.setBackgroundColor(color)
  28.     for row = 1, length, 1 do
  29.         window.setCursorPos(x,y+row-1)
  30.         window.write(string.rep(" ", span))
  31.     end
  32. end
  33.  
  34. local function write(text, x, y, textColor, backGroundColor)
  35.     backGroundColor = backGroundColor or defaultBackgroundColor
  36.     textColor = textColor or defaultTextColor
  37.     window.setCursorPos(x,y)
  38.     window.setBackgroundColor(backGroundColor)
  39.     window.setTextColor(textColor)
  40.     window.write(text)
  41. end
  42.  
  43. local function clear()
  44.     window.setBackgroundColor(defaultBackgroundColor)
  45.     window.setTextColor(defaultTextColor)
  46.     window.clear()
  47.     window.setCursorPos(1,1)
  48. end
  49.  
  50. local function getCenter()
  51.     return math.floor(width/2), math.floor(height/2)
  52. end
  53.  
  54. local function drawScoreboard()
  55.     local scoreTitle = "Score:"
  56.     local scoreText = tostring(Commons.Score:getScore())
  57.     local scoreTextLen = string.len(scoreText) + 2
  58.     if scoreTextLen < 9 then scoreTextLen = 9 end
  59.     local x, y = getCenter()
  60.     x = x - math.floor(scoreTextLen/2)
  61.     y = y - 2
  62.  
  63.     window.setTextColor(colors.white)
  64.     drawSquare(x, y, scoreTextLen+1, 3, colors.purple, true)
  65.     window.setCursorPos(x+1, y+1)
  66.     window.write(scoreText)
  67.     drawSquare(x+1, y-1, 6, 1, colors.gray, true)
  68.     window.setCursorPos(x+1, y-1)
  69.     window.write(scoreTitle)
  70. end
  71.  
  72. local function startGame()
  73.     if (Commons.Score:getScore() < Commons.Score.min) then
  74.         return
  75.     end
  76.     gameStarted = true
  77.     Plinko.init()
  78.     Plinko.drawScreen()
  79.     Plinko.runGame()
  80. end
  81.  
  82. local function createStartButton()
  83.     local label = "Start!"
  84.     local labelLength = string.len(label)
  85.     local x, y = getCenter()
  86.     x = x - math.floor(labelLength/2)
  87.     y = y + 2
  88.     if y > height - 5 then y = height - 5 end
  89.     local span = labelLength
  90.     local length = 1
  91.     local labelPad = 0
  92.     local backgroundColorNormal = colors.yellow
  93.     local borderColorNormal = colors.orange
  94.     local textColorNormal = colors.gray
  95.     startButton = Button.new(label, startGame, x, y, span, length, labelPad, backgroundColorNormal, borderColorNormal, textColorNormal)
  96. end
  97.  
  98. local function reset()
  99.     if (Commons.Credits.selectedCredit == nil) then return end
  100.     clear()
  101.     cashout()
  102.     os.reboot()
  103. end
  104.  
  105. local function createResetButton()
  106.     local label = "Reset "
  107.     local labelLength = string.len(label)
  108.     local x, y = getCenter()
  109.     x = x - math.floor(labelLength/2)
  110.     y = y + 6
  111.     local span = labelLength
  112.     local length = 1
  113.     local labelPad = 0
  114.     local backgroundColorNormal = colors.blue
  115.     local borderColorNormal = colors.blue
  116.     local textColorNormal = colors.black
  117.     resetButton = Button.new(label, reset, x, y, span, length, labelPad, backgroundColorNormal, borderColorNormal, textColorNormal)
  118. end
  119.  
  120. local function drawCredit()
  121.     local label = "Credit Type: " .. Commons.Credits.selectedCredit
  122.     local x, y = 2, 2
  123.  
  124.     write(label, x, y, colors.white, colors.red)
  125. end
  126.  
  127. local function drawScreen()
  128.     clear()
  129.     drawScoreboard()
  130.     createStartButton()
  131.     createResetButton()
  132.     startButton:displayOnScreen(drawSquare, write)
  133.     resetButton:displayOnScreen(drawSquare, write)
  134. end
  135.  
  136. local function countCoins()
  137.     local shouldThrow = false
  138.     for i = 1,input.size(),1 do
  139.         local item = input.getItemDetail(i)
  140.         if item ~= nil
  141.             and Commons.Credits:validCurrency(item.name)
  142.             and (Commons.Credits.selectedCredit == nil or Commons.Credits.selectedCredit == Commons.Credits:getName(item.name))
  143.         then
  144.             if Commons.Credits.selectedCredit == nil then
  145.                 Commons.Credits.selectedCredit = Commons.Credits:getName(item.name)
  146.                 drawCredit()
  147.             end
  148.             local count = item.count
  149.             local value = Commons.Credits:getValueByName(item.name)
  150.             for _ = 1, count, 1 do
  151.                 local newValue = Commons.Score:getScore() + value
  152.                 if (newValue > Commons.Score.max) then
  153.                     input.pushItems(modem.getNameLocal(), i, 1)
  154.                     shouldThrow = true
  155.                 else
  156.                     Commons.Score:updateScore(newValue)
  157.                     drawScoreboard()
  158.                     input.pushItems(peripheral.getName(storage), i, 1)
  159.                 end
  160.             end
  161.         elseif item ~= nil then
  162.             input.pushItems(modem.getNameLocal(), i)
  163.             shouldThrow = true
  164.         end
  165.     end
  166.     if shouldThrow then
  167.         for i = 1,16,1 do
  168.             turtle.select(i)
  169.             turtle.drop()
  170.         end
  171.         turtle.select(1)
  172.     end
  173. end
  174.  
  175. local function tick()
  176.     while true do
  177.         if (not gameStarted) then countCoins() end
  178.         sleep(0.05)
  179.     end
  180. end
  181.  
  182. local function events()
  183.     while true do
  184.         local eventData = {os.pullEvent()}
  185.         local event = eventData[1]
  186.         if event == "key_up" and eventData[2] == keys.q then
  187.             return
  188.         elseif event == "monitor_touch" then
  189.             local x, y = eventData[3], eventData[4]
  190.             if startButton:collides(x, y) then
  191.                 startButton.clickEvent()
  192.                 return
  193.             elseif resetButton:collides(x, y) then
  194.                 resetButton.clickEvent()
  195.                 return
  196.             end
  197.         end
  198.     end
  199. end
  200.  
  201. drawScreen()
  202. parallel.waitForAny(tick, events)
  203.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement