Advertisement
Patosho

Ejemplo de Tienda

Dec 25th, 2015
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.52 KB | None | 0 0
  1. --  Ejemplo de tienda
  2.  
  3. keys = {0,2,32}
  4. _left = 0
  5. _right = 2
  6. _space = 32
  7.  
  8. --  {nombre, precio, descripción}
  9. shopData = {
  10.     {"Velocidad", 1500, "Te da un pequeño impulso de velocidad hacia los lados."};
  11.     {"Fuerza", 2000, "???"};
  12.     {"Super salto", 3000, "Te permite dar un gran salto usando ESPACIO."};
  13.     {"Cañón", 5000, "Lanza un cañón a tus oponentes."};
  14.     {"METEORIC BURST", 10000, "Desata todo el poder interior de tu rata para generar una explosión destructiva."};
  15. }
  16. shopPages = math.ceil(#shopData/3)
  17.  
  18. _velocidad = 1
  19. _fuerza = 2
  20. _superSalto = 3
  21. _canon = 4
  22. _meteoricBurst = 5
  23.  
  24. mice = {}
  25.  
  26. function main()
  27.     for n in pairs(tfm.get.room.playerList) do
  28.         eventNewPlayer(n)
  29.     end
  30. end
  31.  
  32. function eventNewPlayer(name)
  33.     mice[name] = {
  34.         shop = {},
  35.         money = 10000,
  36.         shopOpen = false,
  37.         shopPage = 1
  38.     }
  39.    
  40.     for i=1,#shopData do
  41.         mice[name].shop[i] = false
  42.     end
  43.    
  44.     for _,k in pairs(keys) do
  45.         system.bindKeyboard(name, k, true)
  46.     end
  47.    
  48.     ui.addTextArea(0, "<p align='center'><a href='event:openShop'> $ </a></p>", nil, 780, 380, 16, 16, nil, nil, 0.8, true)
  49. end
  50.  
  51. function eventKeyboard(name, key, down, px, py)
  52.     local mouse = mice[name]
  53.     local s = mouse.shop
  54.    
  55.     if key == _left and s[_velocidad] then
  56.         tfm.exec.movePlayer(name, 0, 0, true, -30, 0, true)
  57.        
  58.     elseif key == _right and s[_velocidad] then
  59.         tfm.exec.movePlayer(name, 0, 0, true, 30, 0, true)
  60.        
  61.     elseif key == _space and s[_superSalto] then
  62.         tfm.exec.movePlayer(name, 0, 0, true, 0, -50, true)
  63.     end
  64. end
  65.  
  66. function eventTextAreaCallback(id, name, cb)
  67.     local mouse = mice[name]
  68.     if cb == "openShop" then
  69.         if mouse.shopOpen then
  70.             mouse.shopOpen = false
  71.             closeShop(name)
  72.         else
  73.             mouse.shopPage = 1
  74.             mouse.shopOpen = true
  75.             openShop(name)
  76.         end
  77.        
  78.     elseif cb == "prevPage" then
  79.         if mouse.shopPage == 1 then return
  80.         elseif mouse.shopPage == 2 then
  81.             ui.removeTextArea(12, name)
  82.         end
  83.         if mouse.shopPage == shopPages then
  84.             ui.addTextArea(13, "<p align='center'><font size='26' color='#EEEE00'><a href='event:nextPage'>&gt;</a></font></p>", name, 648, 184, 40, 40, 0x5f5f5f, 0x0, 1, false)
  85.         end
  86.         mouse.shopPage = mouse.shopPage - 1
  87.         updateShop(name)
  88.        
  89.     elseif cb == "nextPage" then
  90.         if mouse.shopPage == shopPages then return
  91.         elseif mouse.shopPage == shopPages-1 then
  92.             ui.removeTextArea(13, name)
  93.         end
  94.         if mouse.shopPage == 1 then
  95.             ui.addTextArea(12, "<p align='center'><font size='26' color='#EEEE00'><a href='event:prevPage'>&lt;</a></font></p>", name, 112, 184, 40, 40, 0x5f5f5f, 0x0, 1, false)
  96.         end
  97.         mouse.shopPage = mouse.shopPage + 1
  98.         updateShop(name)
  99.        
  100.     elseif cb:sub(1,3) == "buy" then
  101.         local k = tonumber(cb:sub(4))
  102.         mouse.shop[k] = true
  103.         mouse.money = mouse.money - shopData[k][2]
  104.         updateShop(name)
  105.     end
  106. end
  107.  
  108. function openShop(name)
  109.     ui.addTextArea(10, "<p align='center'><font size='28'>Tienda</font></p>", name, 150, 54, 500, 300, 0x5f5f5f, 0x0, 1, false)
  110.     ui.addTextArea(11, "", name, 678, 312, 98, 42, 0x5f5f5f, 0x0, 1, false)
  111.     ui.addTextArea(13, "<p align='center'><font size='26' color='#EEEE00'><a href='event:nextPage'>&gt;</a></font></p>", name, 648, 184, 40, 40, 0x5f5f5f, 0x0, 1, false)
  112.     updateShop(name)
  113. end
  114.  
  115. function updateShop(name)
  116.     mouse = mice[name]
  117.     ui.updateTextArea(11, "<font color='#FFFFFF'>Dinero:</font><br><font size='18' color='#EEEE00'>$"..mouse.money.."</font>", name)
  118.    
  119.     local page = mouse.shopPage
  120.     local money = mouse.money
  121.    
  122.     if page ~= 1 then
  123.         for i=0,6,3 do
  124.             for j=14,16 do
  125.                 ui.removeTextArea(j+i, name)
  126.             end
  127.         end
  128.     end
  129.    
  130.     for i=1,3 do
  131.         local k = (page-1)*3 + i
  132.         if not shopData[k] then break end
  133.        
  134.         local data = shopData[k]
  135.         local di = 3*(i-1)
  136.         local x1 = 174 + 166*(i-1)
  137.         ui.addTextArea(14 + di, "<p align='center'><font size='14' color='#010101'><b>"..data[1].."</b></font></p>", name, x1, 112, 120, 38, 0xc5c5c5, 0x0, 1, false)
  138.         ui.addTextArea(15 + di, "<font color='#FFFFFF'>"..data[3].."</font>", name, x1, 170, 120, 100, 0x7c7c7c, 0x0, 1, false)
  139.        
  140.         local text, color
  141.         if mouse.shop[k] then
  142.             color = "7777EE"
  143.             text = "Equipado"
  144.         else
  145.             local buyText
  146.             if money >= data[2] then
  147.                 color = "55EE55"
  148.                 buyText = "<br><a href='event:buy"..k.."'>Comprar</a>"
  149.             else
  150.                 color = "EE5555"
  151.                 buyText = ""
  152.             end
  153.             text = "$"..data[2]..buyText
  154.         end
  155.         ui.addTextArea(16 + di, "<p align='center'><font size='18' color='#"..color.."'>"..text.."</font></p>", name, x1, 286, 120, 51, 0x324650, 0x0, 0, false)
  156.     end
  157.    
  158. end
  159.  
  160. function closeShop(name)
  161.     for i=10,22 do
  162.         ui.removeTextArea(i, name)
  163.     end
  164. end
  165.  
  166. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement