Init
This commit is contained in:
commit
c0b5de5a4f
5 changed files with 161 additions and 0 deletions
47
objects.lua
Normal file
47
objects.lua
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
local F = {}
|
||||
|
||||
----------------------------------------
|
||||
-- Define the class attributes
|
||||
----------------------------------------
|
||||
Fighter = {
|
||||
name = "",
|
||||
health = 0,
|
||||
speed = 0,
|
||||
}
|
||||
|
||||
----------------------------------------
|
||||
-- Define the class methods
|
||||
----------------------------------------
|
||||
function Fighter:light_punch()
|
||||
print("Figher " .. self.name .. " performed a light punch")
|
||||
end
|
||||
|
||||
----------------------------------------
|
||||
-- Define the class constructor
|
||||
----------------------------------------
|
||||
--- @return Fighter
|
||||
function Fighter:new(t)
|
||||
-- In Lua, a constructor essentially
|
||||
-- binds the object to the class
|
||||
-- and returns the object
|
||||
t = t or {}
|
||||
setmetatable(t, self)
|
||||
self.__index = self
|
||||
return t
|
||||
end
|
||||
|
||||
----------------------------------------
|
||||
-- Instantiate objects
|
||||
----------------------------------------
|
||||
-- local fighter1 = Fighter:new({ name = "Fighter1", health = 100, speed = 10 })
|
||||
-- local fighter2 = Fighter:new({ name = "Fighter2", health = 100, speed = 10 })
|
||||
|
||||
----------------------------------------
|
||||
-- Call the class methods
|
||||
----------------------------------------
|
||||
-- fighter1:light_punch()
|
||||
-- fighter2:light_punch()
|
||||
|
||||
F.Fighter = Fighter
|
||||
|
||||
return F
|
||||
Loading…
Add table
Add a link
Reference in a new issue