--------------------------------------------------- 
-- FBA Tutorials
--
-- Tutorial / 19 / Using classes
--
--------------------------------------------------- 
--
-- Out of the box, Lua does not have a class system, but its powerful metaprogramming facilities 
-- makes defining classic objects straightforward. In fact, there's a number of ways of doing this.
--
-- Simple inheritance is supported. For example, here a base class Animal is defined, 
-- and several specific kinds of animals are declared. All classes made using class have a is_a method, 
-- which you can use to find out the actual class at runtime.

-- This example will show how to implement an object oriented classes programming style.
-- 
--------------------------------------------------- 

-- this script is needed in order to use the classes
include("class")


-- base class Animal
local Animal = class(function(a,name)
   a.name = name
end)

-- create a Dog class inherited from Animal
local Dog = class(Animal)

-- a method inside "Dog" class
function Dog:speak()
  return "Bark bark!"
end

-- create a Dog object
local mydog = Dog("myfirstdog")


-- create a Cat class inherited from Animal
local Cat = class(Animal, function(c,name,humor)
         Animal.init(c,name)  -- must init base!
         c.humor = humor
      end)

-- a method inside "Cat" class      
function Cat:speak()
  return "meow, i'm " .. self.humor
end

-- create an happy Cat object ;)
local mycat = Cat("myfirstcat", "very very happy")

-- finally the main loop
function onmainloop()
        print(20,20,mydog:speak(), 255, 0,0)
        print(20,50,mycat:speak(), 0, 0,255)
end

function onstylusdown(x,y)
        quit()
end
