Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function NewPlayer(HP,Level)
- if (type(HP) ~= "number") or (HP < 1) then
- return false;
- end
- if (type(Level) ~= "number") or (Level < 1) then
- return false;
- end
- local Private = {}
- do
- local PrivateFunctions = {}
- -- cannot be seen from the outside
- local PrivateVariables = {
- HP = {
- Max = HP,
- Current = HP,
- },
- Level = Level,
- };
- -- private functions
- PrivateFunctions.__index = PrivateFunctions;
- PrivateFunctions.__metatable = "Permission denied";
- function PrivateFunctions:SetMaxHP(Amount)
- Amount = math.floor(Amount);
- if (type(Amount) == "number") and (Amount > 0) then
- PrivateVariables.HP.Max = Amount;
- end
- end
- function PrivateFunctions:GetMaxHP()
- return PrivateVariables.HP.Max;
- end
- function PrivateFunctions:SetCurrentHP(Amount)
- Amount = math.floor(Amount);
- if (type(Amount) == "number") and (Amount > 0) then
- PrivateVariables.HP.Current = Amount;
- end
- end
- function PrivateFunctions:GetCurrentHP()
- return PrivateVariables.HP.Current;
- end
- function PrivateFunctions:SetLevel(Amount)
- Amount = math.floor(Amount);
- if (type(Amount) == "number") and (Amount > 0) then
- PrivateVariables.Level = Amount;
- end
- end
- function PrivateFunctions:GetLevel()
- return PrivateVariables.Level;
- end
- setmetatable(Private, PrivateFunctions);
- end
- local Public = {};
- Public.__index = Public;
- Public.__metatable = "Permission denied"
- -- Public functions
- function Public:GetHP()
- return Private:GetMaxHP(), Private:GetCurrentHP();
- end
- function Public:TakeDamage(Damage)
- Damage = math.floor(Damage);
- if (type(Damage) == "number") and (Damage > 0) then
- if ((Private:GetCurrentHP() - Damage) < 0) then
- Private:SetCurrentHP(0);
- else
- Private:SetCurrentHP(Private:GetCurrentHP() - Damage);
- end
- end
- end
- function Public:Heal(Amount)
- Amount = math.floor(Amount);
- if (type(Amount) == "number") and (Amount > 0) then
- local NewHP = Private:GetCurrentHP() + Amount;
- if (NewHP >= Private:GetMaxHP()) then
- Private:SetCurrentHP(Private:GetMaxHP());
- else
- Private:SetCurrentHP(NewHP);
- end
- end
- end
- function Public:GetLevel()
- return Private:GetLevel();
- end
- function Public:LevelUP()
- Private:SetLevel(Private:GetLevel() + 1);
- Private:SetCurrentHP(Private:GetMaxHP());
- end
- local o = {}
- setmetatable(o,Public);
- return o;
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement