JustDoesGames

Draw

Aug 7th, 2020
1,453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.43 KB | None | 0 0
  1. --[[
  2.  
  3. Draw
  4. Just Does Games
  5.  
  6. --]]
  7.  
  8. local w,h = term.getSize()
  9. local run, debug, inmenu = true, false, false
  10. local saveDir = "/saves/sv.lua" -- just throwing things around lol
  11.  
  12.  
  13. -- Main Code
  14.  
  15. function display()
  16.     term.setBackgroundColor(sv.draw.color.background)
  17.     term.clear() -- usually I do not do this because of the flickering but this is a really small game
  18.     term.setBackgroundColor(sv.draw.color.click)
  19.     term.setTextColor(sv.draw.color.moneyText)
  20.     term.setBackgroundColor(sv.draw.color.background)
  21.     term.setCursorPos(1,1)
  22.     write("Money: "..sv.money)
  23.     term.setTextColor(sv.draw.color.clickWorthText)
  24.     term.setCursorPos(w-string.len(sv.draw.game.drawWorth.."/"..sv.draw.game.overlapWorth),1)
  25.     write(sv.draw.game.drawWorth.."/"..sv.draw.game.overlapWorth)
  26.     term.setTextColor(sv.draw.color.text)
  27.     term.setCursorPos(1,h)
  28.     if not inmenu then
  29.     write("Press 'CTRL' for menu")
  30.     end
  31. end
  32.  
  33. function upgrade(t)
  34.     local cost = upgradeCal()
  35.     if sv.money >= cost then
  36.         sv.money = sv.money - cost
  37.         if t == 1 then
  38.             sv.draw.game.drawWorth = sv.draw.game.drawWorth + 1
  39.         elseif t == 2 then
  40.             sv.draw.game.overlapWorth = sv.draw.game.overlapWorth + 1
  41.         end
  42.         sleep(.1) display()
  43.     end
  44. end
  45.  
  46. function runMenu()
  47.     save(saveDir)
  48.     local sel, r = 1, true
  49.     inmenu = true
  50.     while r do -- use r to leave (to avoid rep clicking)
  51.         local menu = {
  52.             {"Upgrade Draw - $"..upgradeCal(), function() upgrade(1) end},
  53.             {"Upgrade Overlap - $"..upgradeCal(), function() upgrade(2) end},
  54.             {"Save and Exit", function() run, r = false, false end}
  55.         }
  56.         while true do -- use break to leave
  57.             term.setCursorPos(1,h)
  58.             term.clearLine()
  59.             term.setCursorPos(1,h)
  60.             term.setTextColor(sv.draw.color.menuText)
  61.             write("< "..menu[sel][1].." >")
  62.             a,b = os.pullEvent()
  63.             if a == "key" then
  64.                 if b == keys.leftCtrl or b == keys.rightCtrl or b == keys.q then
  65.                     r = false
  66.                 elseif b == keys.d or b == keys.right then
  67.                     if sel ~= #menu then
  68.                         sel = sel + 1
  69.                     end
  70.                 elseif b == keys.a or b == keys.left then
  71.                     if sel ~= 1 then
  72.                         sel = sel - 1
  73.                     end
  74.                 elseif b == keys.enter or b == keys.e then
  75.                     menu[sel][2]() break
  76.                 end
  77.             elseif a == "mouse_click" then
  78.                 r = false
  79.             end
  80.             if not r then break end
  81.         end
  82.     end
  83.     inmenu = false
  84. end
  85.  
  86. function runGame()
  87.     while run do
  88.         display()
  89.         a,b,x,y = os.pullEvent()
  90.         if a == "key" then -- menu things
  91.             if b == keys.q then
  92.                 run = false
  93.             elseif b == keys.rightCtrl or b == keys.leftCtrl then
  94.                 runMenu()
  95.             end
  96.         elseif a == "mouse_click" or a == "mouse_drag" then -- mouse drag because... ... fuck you
  97.             paintutils.drawPixel(x,y,sv.draw.color.click)
  98.             local blocks = {}
  99.             for i=1, w*h do
  100.                 table.insert(blocks, {i, 0})
  101.             end
  102.             while true do
  103.                 local a,_,x,y = os.pullEvent()
  104.                 if a == "mouse_drag" then
  105.                     if blocks[x*y][2] == 0 then
  106.                         paintutils.drawPixel(x,y,sv.draw.color.click)
  107.                         blocks[x*y][2] = blocks[x*y][2]+1
  108.                     elseif blocks[x*y][2] == 1 then
  109.                         paintutils.drawPixel(x,y,sv.draw.color.clickOverlap)
  110.                         blocks[x*y][2] = blocks[x*y][2]+1
  111.                     end
  112.                 elseif a == "mouse_up" then
  113.                     break
  114.                 end
  115.             end
  116.             local tmp = 0
  117.             for i=1, #blocks do
  118.                 tmp = tmp + math.ceil(blocks[i][2]*sv.draw.game.drawWorth)
  119.                 if blocks[i][2] == 2 then
  120.                     tmp = tmp + math.ceil(blocks[i][2]*sv.draw.game.overlapWorth)
  121.                 end
  122.             end
  123.             sv.money = sv.money + math.floor(tmp/200)
  124.         end
  125.     end
  126. end
  127.  
  128. -- Main Code
  129.  
  130.  
  131. function save(dir)
  132.     local f = fs.open(dir, "w")
  133.     if f then f.write(textutils.serialize(sv)) f.close() end
  134. end
  135.  
  136. if debug then save(saveDir) end
  137.  
  138. function load(dir)
  139.     local f = fs.open(dir, "r")
  140.     if f then sv = textutils.unserialize(f.readAll()) f.close() end
  141. end
  142.  
  143. load(saveDir)
  144. sv = sv or {}
  145. sv.money = sv.money or 0
  146.  
  147. sv.draw = {}
  148. sv.draw.player, sv.draw.game, sv.draw.color = sv.draw.player or {}, sv.draw.game or {}, sv.draw.color or {}
  149.  
  150. sv.draw.game.upgradeBase = sv.draw.game.upgradeBase or 1
  151. sv.draw.game.drawWorth = 1 -- one per block
  152. sv.draw.game.overlapWorth = 1 -- overlapWorth multiplies for every stack...?
  153.  
  154. function upgradeCal() return math.floor(((sv.draw.game.drawWorth+sv.draw.game.overlapWorth)+(sv.draw.game.drawWorth*sv.draw.game.overlapWorth))*sv.draw.game.upgradeBase) end
  155.  
  156. sv.draw.color.background = sv.draw.color.background or colors.black
  157. sv.draw.color.text = sv.draw.color.text or colors.white
  158. sv.draw.color.moneyText = sv.draw.color.moneyText or colors.lime
  159. sv.draw.color.menuText = sv.draw.color.menuText or colors.white
  160. sv.draw.color.clickWorthText = sv.draw.color.clickWorthText or colors.orange
  161. sv.draw.color.click = sv.draw.color.click or colors.white
  162. sv.draw.color.clickOverlap = sv.draw.color.clickOverlap or colors.yellow
  163.  
  164. runGame()
  165. save(saveDir)
Advertisement
Add Comment
Please, Sign In to add comment