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