
local function OnTaskTick(inst, self, period)
    self:DoDec(period)
end

local function oncurrent(self,current)
    self.inst.currentmana:set(current)
end
local Mana = Class(function(self, inst)
    self.inst = inst
    self.max = 200
    self.current = 50
    self:DoInit()

self.overridestarvefn = nil
local period = 10
self.inst:DoPeriodicTask(period, OnTaskTick, nil, self, period)
end,
nil,
{
    current = oncurrent
})

function Mana:OnSave()
    local data = {
        current = self.current,
    }
    return data
end

function Mana:OnLoad(data)
    self.current = data.current or self.max
end

function Mana:DoDec(dt, ignore_damage)
    
        if self.current >= 0 then
            self:DoDelta(1 * dt * 1 * .1, true)
        elseif not ignore_damage then
            if self.overridestarvefn ~= nil then
                self.overridestarvefn(self.inst, dt)
           
            end
        end
end

function Mana:GetPercent()
    return self.current / self.max
end


function Mana:DoInit()

end

function Mana:DoDelta(delta)
    local oldval = self.current
    local val = self.current + delta
    if val >= self.max then
        self.current = self.max
    elseif val <= 0 then
        self.current = 0
    else
        self.current = val
    end
    local del = self.current - oldval
    self.inst:PushEvent("Mana_delta", {val = self.current, oldval = oldval, delta = del})
    --self.current = math.clamp(self.current + delta, 0, self.max)
    --return self.current
end

return Mana