Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.43 KB | None | 0 0
  1. function NewPlayer(HP,Level)
  2.     if (type(HP) ~= "number") or (HP < 1) then
  3.         return false;
  4.     end
  5.     if (type(Level) ~= "number") or (Level < 1) then
  6.         return false;
  7.     end
  8.    
  9.     local Private = {} 
  10.  
  11.     do
  12.         local PrivateFunctions = {}
  13.         -- cannot be seen from the outside
  14.         local PrivateVariables = {
  15.             HP = {
  16.                 Max = HP,
  17.                 Current = HP,
  18.             },
  19.             Level = Level,
  20.         };
  21.  
  22.         -- private functions
  23.         PrivateFunctions.__index = PrivateFunctions;
  24.         PrivateFunctions.__metatable = "Permission denied";
  25.  
  26.         function PrivateFunctions:SetMaxHP(Amount)
  27.             Amount = math.floor(Amount);
  28.             if (type(Amount) == "number") and (Amount > 0) then
  29.                 PrivateVariables.HP.Max = Amount;
  30.             end
  31.         end
  32.  
  33.         function PrivateFunctions:GetMaxHP()
  34.             return PrivateVariables.HP.Max;
  35.         end
  36.  
  37.         function PrivateFunctions:SetCurrentHP(Amount)
  38.             Amount = math.floor(Amount);
  39.             if (type(Amount) == "number") and (Amount > 0) then
  40.                 PrivateVariables.HP.Current = Amount;
  41.             end
  42.         end
  43.  
  44.         function PrivateFunctions:GetCurrentHP()
  45.             return PrivateVariables.HP.Current;
  46.         end
  47.  
  48.         function PrivateFunctions:SetLevel(Amount)
  49.             Amount = math.floor(Amount);
  50.             if (type(Amount) == "number") and (Amount > 0) then
  51.                 PrivateVariables.Level = Amount;
  52.             end
  53.         end
  54.  
  55.         function PrivateFunctions:GetLevel()
  56.             return PrivateVariables.Level;
  57.         end
  58.         setmetatable(Private, PrivateFunctions);
  59.     end
  60.  
  61.  
  62.     local Public = {};
  63.  
  64.     Public.__index = Public;
  65.  
  66.     Public.__metatable = "Permission denied"
  67.  
  68.     -- Public functions
  69.  
  70.     function Public:GetHP()
  71.         return Private:GetMaxHP(), Private:GetCurrentHP();
  72.     end
  73.  
  74.     function Public:TakeDamage(Damage)
  75.         Damage = math.floor(Damage);
  76.         if (type(Damage) == "number") and (Damage > 0) then
  77.             if ((Private:GetCurrentHP() - Damage) < 0) then
  78.                 Private:SetCurrentHP(0);
  79.             else
  80.                 Private:SetCurrentHP(Private:GetCurrentHP() - Damage);
  81.             end
  82.         end
  83.     end
  84.  
  85.     function Public:Heal(Amount)
  86.         Amount = math.floor(Amount);
  87.         if (type(Amount) == "number") and (Amount > 0) then
  88.             local NewHP = Private:GetCurrentHP() + Amount;
  89.             if (NewHP >= Private:GetMaxHP()) then
  90.                 Private:SetCurrentHP(Private:GetMaxHP());
  91.             else
  92.                 Private:SetCurrentHP(NewHP);
  93.             end
  94.         end
  95.     end
  96.  
  97.     function Public:GetLevel()
  98.         return Private:GetLevel();
  99.     end
  100.  
  101.     function Public:LevelUP()
  102.         Private:SetLevel(Private:GetLevel() + 1);
  103.         Private:SetCurrentHP(Private:GetMaxHP());
  104.     end
  105.  
  106.     local o = {}
  107.     setmetatable(o,Public);
  108.     return o;
  109. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement