Lua: added classes concept
This commit is contained in:
parent
50736928e4
commit
29469232db
26
lua/classes/main.lua
Normal file
26
lua/classes/main.lua
Normal 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)
|
Loading…
x
Reference in New Issue
Block a user