Module:Test: Difference between revisions

From Elwiki
No edit summary
No edit summary
Line 20: Line 20:
             return tostring(o)
             return tostring(o)
         end
         end
    end
    function CreateClass(...)
        -- "cls" is the new class
        local cls, bases = {}, {...}
        -- copy base class contents into the new class
        for i, base in ipairs(bases) do
            for k, v in pairs(base) do
                cls[k] = v
            end
        end
        -- set the class's __index, and start filling an "is_a" table that contains this class and all of its bases
        -- so you can do an "instance of" check using my_instance.is_a[MyClass]
        cls.__index, cls.is_a = cls, {
            [cls] = true
        }
        for i, base in ipairs(bases) do
            for c in pairs(base.is_a) do
                cls.is_a[c] = true
            end
            cls.is_a[base] = true
        end
        -- the class's __call metamethod
        setmetatable(cls, {
            __call = function(c, ...)
                local instance = setmetatable({}, c)
                -- run the init method if it's there
                local init = instance._init
                if init then
                    init(instance, ...)
                end
                return instance
            end
        })
        -- return the new class table, that's ready to fill with methods
        return cls
     end
     end


Line 69: Line 33:
     Trait = {
     Trait = {
         name = "",
         name = "",
         values = {
         multipliers = {
             mp_multiplier = 100,
             mp_cost = 100,
             cd_multiplier = 100,
             cooldown = 100,
             duration_multiplier = 100
             duration = 100
         },
         },
         details = {
         details = {
Line 102: Line 66:


     function Trait:math()
     function Trait:math()
         for i = 1, 2, 1 do
         for k, v in pairs(self.details) do
            self.details.mp_cost[i] = self.details.mp_cost[i] * self.values.mp_multiplier
            for i = 1, 2, 1 do
            self.details.cooldown[i] = self.details.cooldown[i] * self.values.cd_multiplier
                self.details[k][i] = v * (self.multipliers[k] / 100)
             self.details.duration[i] = self.details.duration[i] * self.values.duration_multiplier
             end
         end
         end
         return self
         return self