LuckOake 400 Postado Janeiro 24, 2013 Share Postado Janeiro 24, 2013 Olá galera Graças ao meu mestre Oneshot eu consegui aprender a poderosa Programação Orientada a Objetos, ou Oriented-object Programming (OOP), e fiz meu primeiro sistema com ela, um sistema de Potions. Script (potions.lua): local Potion = { type = "", healmin = 0, healmax = 0, vocations = {}, emptyid = 0, } function Potion:new(type, healmin, healmax, vocations, emptyid, uid) local potions = { type = type, healmin = tonumber(healmin), healmax = tonumber(healmax), vocations = vocations, emptyid = tonumber(emptyid), uid = 0, } return setmetatable(potions, {__index = self}) end function Potion:heal(cid) if self.vocations ~= "all" and not isInArray(self.vocations, getPlayerVocation(cid)) then doPlayerSendCancel(cid, "This vocation cannot use this potion.") return true elseif self.type == "health" then doCreatureAddHealth(cid, math.random(self.healmin, self.healmax)) elseif self.type == "mana" then doPlayerAddMana(cid, math.random(self.healmin, self.healmax)) end doCreatureSay(cid, "Aaaah...", TALKTYPE_MONSTER) doTransformItem(self.uid, self.emptyid) doSendMagicEffect(getCreaturePosition(cid), CONST_ME_MAGIC_BLUE) return true end Pots = { [8704] = Potion:new("health", 50, 100, "all", 7636), [7618] = Potion:new("health", 100, 200, "all", 7636), [7588] = Potion:new("health", 200, 400, {3, 4, 7, 8}, 7634), [7591] = Potion:new("health", 600, 800, {4, 8}, 7635), [8473] = Potion:new("health", 900, 1200, {4, 8}, 7635), [7620] = Potion:new("mana", 90, 150, "all", 7636), [7589] = Potion:new("mana", 190, 250, {1, 2, 5, 6}, 7634), [7590] = Potion:new("mana", 300, 400, {1, 2, 5, 6}, 7635), } function onUse(cid, item, position, itemEx) local Potions = Pots[item.itemid] Potions.uid = item.uid Potions:heal(itemEx.uid) return true end Tag (actions.xml): <action itemid="8704;7618;7588;7591;8473;7620;7589;7590" script="potions.lua"/> Configurando: Para configurar, edite aqui: Pots = { [8704] = Potion:new("health", 50, 100, "all", 7636), [7618] = Potion:new("health", 100, 200, "all", 7636), [7588] = Potion:new("health", 200, 400, {3, 4, 7, 8}, 7634), [7591] = Potion:new("health", 600, 800, {4, 8}, 7635), [8473] = Potion:new("health", 900, 1200, {4, 8}, 7635), [7620] = Potion:new("mana", 90, 150, "all", 7636), [7589] = Potion:new("mana", 190, 250, {1, 2, 5, 6}, 7634), [7590] = Potion:new("mana", 300, 400, {1, 2, 5, 6}, 7635), } Traduzindo: [item_id] = Potion:new("tipo de cura", minimo, máximo, {vocações que podem usar}, empty flask), Créditos: - LuckOake -- Pelo Script - Oneshot -- Por me ensinar OOP e ajudar com o script Link para o comentário https://xtibia.com/forum/topic/205434-potion-system-meu-primeiro-script-em-oop/ Compartilhar em outros sites More sharing options...
Gears 109 Postado Janeiro 24, 2013 Share Postado Janeiro 24, 2013 Perfeito Luck , toma o rep , bjos Link para o comentário https://xtibia.com/forum/topic/205434-potion-system-meu-primeiro-script-em-oop/#findComment-1448503 Compartilhar em outros sites More sharing options...
drakylucas 160 Postado Janeiro 27, 2013 Share Postado Janeiro 27, 2013 cara muito bem feito o script, parabens, sério! oww.. me tira uma dúvida? (tou tentando entender ele certo ) na função: function Potion:new(type, healmin, healmax, vocations, emptyid, uid) você está atribuindo o uid = 0 sempre, então nao é desnecessario esse uid? (só por curiosidade mesmo) e novamente, parabens! rep+ merecido! Link para o comentário https://xtibia.com/forum/topic/205434-potion-system-meu-primeiro-script-em-oop/#findComment-1450827 Compartilhar em outros sites More sharing options...
caotic 393 Postado Janeiro 27, 2013 Share Postado Janeiro 27, 2013 cara muito bem feito o script, parabens, sério! oww.. me tira uma dúvida? (tou tentando entender ele certo ) na função: function Potion:new(type, healmin, healmax, vocations, emptyid, uid) você está atribuindo o uid = 0 sempre, então nao é desnecessario esse uid? (só por curiosidade mesmo) e novamente, parabens! rep+ merecido! Eu falei com o luck sobre isto. Ele falo que erá para ficar mais bonito. Link para o comentário https://xtibia.com/forum/topic/205434-potion-system-meu-primeiro-script-em-oop/#findComment-1450874 Compartilhar em outros sites More sharing options...
Slicer 1070 Postado Janeiro 27, 2013 Share Postado Janeiro 27, 2013 (editado) eh q o UID vem depois neh... Potions.uid = item.uid @topic muito bom luck! para quem tava sem ideias fico um otimo script e bem limpo ^^ parabens... -falta eu aprender isso kk sei fazer isso em java mas em .lua nem tanto ^^- Editado Janeiro 27, 2013 por Slicer Link para o comentário https://xtibia.com/forum/topic/205434-potion-system-meu-primeiro-script-em-oop/#findComment-1451057 Compartilhar em outros sites More sharing options...
LuckOake 400 Postado Janeiro 27, 2013 Autor Share Postado Janeiro 27, 2013 @Topic Valeus galera, tô começando a pegar o jeito de OOP, mas ainda preciso treinar muito @DrakyLucas cara muito bem feito o script, parabens, sério! oww.. me tira uma dúvida? (tou tentando entender ele certo ) na função: functionPotion:new(type, healmin, healmax, vocations, emptyid, uid) você está atribuindo o uid = 0 sempre, então nao é desnecessario esse uid? (só por curiosidade mesmo) e novamente, parabens! rep+ merecido! Eu coloquei o UID por questão de organização mesmo, pra eu não me perder quando eu editar o script Link para o comentário https://xtibia.com/forum/topic/205434-potion-system-meu-primeiro-script-em-oop/#findComment-1451185 Compartilhar em outros sites More sharing options...
Posts Recomendados