Advertisement
massacring

Ball

Jan 5th, 2025 (edited)
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.45 KB | None | 0 0
  1. local Ball = {}
  2. Ball.__index = Ball
  3.  
  4. function Ball.new(x, y, color, foregroundColorMap, backgroundColorMap)
  5.     local ball = setmetatable({}, Ball)
  6.  
  7.     ball.x = x or 0
  8.     ball.y = y or 0
  9.     ball.color = color or colors.black
  10.     ball.foregroundColorMap = foregroundColorMap or {}
  11.     ball.backgroundColorMap = backgroundColorMap or {}
  12.     ball.prevPixels = {
  13.         ["1:1"] = colors.black,
  14.         ["1:2"] = colors.black,
  15.         ["2:1"] = colors.black,
  16.         ["2:2"] = colors.black,
  17.     }
  18.  
  19.     return ball
  20. end
  21.  
  22. function Ball:checkBelowBall(checkPinCollision)
  23.     local result = {
  24.         left_collided = checkPinCollision({x = self.x, y = self.y+2}),
  25.         right_collided = checkPinCollision({x = self.x+1, y = self.y+2})
  26.     }
  27.     return result
  28. end
  29.  
  30. function Ball:clear(monitor, fallbackColor)
  31.     fallbackColor = fallbackColor or colors.black
  32.     for x = 1, 2, 1 do
  33.         local checkX = self.x + x - 1
  34.         local backgroundColumns = self.backgroundColorMap[checkX]
  35.         local foregroundColumns = self.foregroundColorMap[checkX]
  36.         for y = 1, 2, 1 do
  37.             local checkY  =self.y + y - 1
  38.             monitor.setBackgroundColor(fallbackColor)
  39.             if backgroundColumns ~= nil and backgroundColumns[checkY] ~= nil then
  40.                 monitor.setBackgroundColor(backgroundColumns[checkY]) end
  41.             if foregroundColumns ~= nil and foregroundColumns[checkY] ~= nil then
  42.                 monitor.setBackgroundColor(foregroundColumns[checkY]) end
  43.  
  44.             monitor.setCursorPos(checkX, checkY)
  45.             monitor.write(" ")
  46.         end
  47.     end
  48.     self.isActive = false
  49. end
  50.  
  51. function Ball:move(monitor, x, y, fallbackColor)
  52.     self:clear(monitor, fallbackColor)
  53.     self.x = self.x + x
  54.     self.y = self.y + y
  55.     self:displayOnScreen(monitor)
  56. end
  57.  
  58. function Ball:displayOnScreen(monitor)
  59.     monitor.setCursorPos(self.x,self.y)
  60.     monitor.setBackgroundColor(self.color)
  61.     for x = 1, 2, 1 do
  62.         local checkX = self.x + x - 1
  63.         local columns = self.foregroundColorMap[checkX]
  64.         for y = 1, 2, 1 do
  65.             local checkY = self.y + y - 1
  66.             if columns ~= nil and columns[checkY] ~= nil then goto continue end
  67.             monitor.setCursorPos(checkX, checkY)
  68.             monitor.write(" ")
  69.             ::continue::
  70.         end
  71.     end
  72.     self.isActive = true
  73. end
  74.  
  75. function Ball:click()
  76.     if self.isActive then
  77.         self.clickEvent()
  78.     end
  79. end
  80.  
  81. return Ball
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement