Lua: added classes concept

This commit is contained in:
Logen Kain 2017-05-10 15:04:18 -07:00
parent 50736928e4
commit 29469232db

26
lua/classes/main.lua Normal file
View File

@ -0,0 +1,26 @@
Player = {}
Player.__index = Player
function Player:create(health, strength)
local player = {} -- Our object
setmetatable(player, Player) -- our handle lookup
player.health = health -- initializing or stuff
player.strength = strength
return player
end
function Player:damage(damage)
self.health = self.health - damage
end
--Create our main character
player_Jack = Player:create(100, 12)
print ("Jack's current health is: "..player_Jack.health)
print ("Jack's current strength is: "..player_Jack.strength)
print ("Despite his mighty strength, Jack is still an idiot and got hit for 10 damage")
player_Jack:damage(10)
print ("Well, if he keeps this up he is going to die, his health is now: "..player_Jack.health)