Advertisement
Guest User

shop

a guest
Jun 10th, 2014
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.80 KB | None | 0 0
  1. --The main shop program
  2. local m=peripheral.wrap("top")
  3. local mw,mh=m.getSize()
  4. local shopWindow="main"
  5. local cartPos=1
  6. local selectedItem
  7. local cartItems={}
  8.  
  9. local options={
  10.   [1]={--TechMaterials x16-29
  11.     "Monitor"={cost=5},
  12.     "Adv.Monitor"={cost=6},
  13.     "Computer",
  14.     "Adv.Computer",
  15.     "Turtle",
  16.     "Adv.Turtle",
  17.     "Modem"
  18.   },
  19.   [2]={--ProgramDisks x31-44
  20.     "Compressor",
  21.     "PasswordLock",
  22.     "Shop Api"
  23.   },
  24.   [3]={--QuikKits x46-61
  25.     "Logger",
  26.     "Quarry",
  27.     "Mob Grinder"
  28.   }
  29. }
  30.  
  31. --Functions
  32. function title()
  33.   m.setBackgroundColor(colors.black)
  34.   m.setTextColor(colors.yellow)
  35.   m.setCursorPos(((mw/2)-8),1)
  36.   m.write("King's Computers")
  37.   m.setTextColor(colors.lime)
  38.   m.setCursorPos(((mw/2)-8),2)
  39.   m.write("----------------")
  40.   m.setCursorPos(16,3)
  41.   m.setTextColor(colors.purple)
  42.   m.write("Tech Materials")
  43.   m.setCursorPos(31,3)
  44.   m.setTextColor(colors.red)
  45.   m.write("Program Disks ")
  46.   m.setCursorPos(46,3)
  47.   m.setTextColor(colors.green)
  48.   m.write("Turtle QuikKits")
  49.   m.setCursorPos(1,1)
  50.   m.setTextColor(colors.gray)
  51.   m.write("*Right-click>Select")
  52. end
  53.  
  54. function cart()
  55.   local start=(mw/4)-14
  56.   m.setTextColor(colors.blue)
  57.   m.setCursorPos(1,3)
  58.   m.write("Your Cart:    ")
  59.   m.setCursorPos(start,4)
  60.   m.setTextColor(colors.white)
  61.   m.write("--------------")
  62.   m.setCursorPos(start,5)
  63.   m.setTextColor(colors.blue)
  64.   m.write("Any items you ")
  65.   m.setCursorPos(start,6)
  66.   m.write("have selected ")
  67.   m.setCursorPos(start,7)
  68.   m.write("will show here")
  69.   m.setCursorPos(start,8)
  70.   m.setTextColor(colors.white)
  71.   m.write("--------------")
  72.   m.setCursorPos(start,19)
  73.   m.setBackgroundColor(colors.red)
  74.   m.setTextColor(colors.white)
  75.   m.write(" Clear  Cart  ")
  76.   m.setBackgroundColor(colors.lime)
  77.   m.setCursorPos(start,18)
  78.   m.write("   Purchase   ")  
  79.   m.setBackgroundColor(colors.black)
  80.   m.setTextColor(colors.lime)
  81. end
  82.  
  83. function dividers()
  84.   m.setTextColor(colors.lightBlue)
  85.   for i1=3,20 do
  86.     m.setCursorPos(mw/4,i1)
  87.     m.write("|")
  88.   end
  89.   for i2=3,20 do
  90.     m.setCursorPos(mw/2,i2)
  91.     m.write("|")
  92.   end
  93.   for i3=3,20 do
  94.     m.setCursorPos(mw-(mw/4),i3)
  95.     m.write("|")
  96.   end
  97. end
  98.  
  99. function showOptions()
  100.   m.setTextColor(colors.orange)
  101.   for a=1,#options[1] do
  102.     m.setCursorPos(16,a+3)
  103.     m.write(options[1][a])
  104.   end
  105.   for b=1,#options[2] do
  106.     m.setCursorPos(31,b+3)
  107.     m.write(options[2][b])
  108.   end
  109.   for c=1,#options[3] do
  110.     m.setCursorPos(46,c+3)
  111.     m.write(options[3][c])
  112.   end
  113. end
  114.  
  115. --Main Display Function:
  116. function layoutMain()
  117.   m.clear()
  118.   title()
  119.   cart()
  120.   dividers()
  121.   showOptions()
  122. end
  123.  
  124. function clearCart()
  125.   for i=9,17 do
  126.     m.setCursorPos(1,i)
  127.     m.write("              ")
  128.     cartPos=1
  129.   end
  130. end
  131.  
  132. function eventHandlerMain()
  133.   event,arbitrary,x,y=os.pullEvent("monitor_touch")
  134.   if x<=29 and x>=16 then
  135.     for i=1,#options[1] do
  136.       if y==i+3 then
  137.         selectedItem=options[1][i]
  138.       end
  139.     end
  140.   end
  141.   if x>=31 and x<=44 then
  142.     for i=1,#options[2] do
  143.       if y==i+3 then
  144.         selectedItem=options[2][i]
  145.       end
  146.     end
  147.   end
  148.   if x>=46 and x<=61 then
  149.     for i=1,#options[3] do
  150.       if y==i+3 then
  151.         selectedItem=options[3][i]
  152.       end
  153.     end
  154.   end
  155.   --Clear
  156.   if (x>=1 and x<=14) and y==19 then
  157.     clearCart()
  158.     selectedItem="notItem"
  159.     cartItems={}
  160.   end
  161.   --Checkout
  162.   if (x>=1 and x<=14) and y==18 then
  163.     selectedItem="notItem"
  164.   end
  165. end
  166.  
  167. function cartDynamic()
  168.   m.setCursorPos(1,cartPos+8)
  169.   if selectedItem~="notItem" and cartPos<=9 then
  170.     m.setTextColor(colors.lime)
  171.     m.write(selectedItem)
  172.     table.insert(cartItems, selectedItem)
  173.     cartPos=cartPos+1
  174.   end
  175. end  
  176.  
  177. m.clear()
  178. title()
  179. cart()
  180. dividers()
  181. showOptions()
  182.  
  183. while true do
  184.   eventHandlerMain()
  185.   cartDynamic()
  186. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement