bar_meter={}

function bar_meter.new(scr_x, scr_y, width, height, _value, _max_value, _actor, _color_ok, _color_warn, _color_danger)
    _color_ok     = _color_ok     or 10; -- verd
    _color_warn   = _color_warn   or 8;  -- groc
    _color_danger = _color_danger or 3;  -- roig
    return {
        x = scr_x,
        y = scr_y,
        w = width,
        h = height,
        value = _value,
        max_value = _max_value,
        draw=bar_meter.draw,
        actor=_actor,
        color_ok = _color_ok,
        color_warn = _color_warn,
        color_danger = _color_danger,
    }
end

function bar_meter:draw()
    if self.actor~=nil and not empty_table(self.actor) then
        self.value=self.actor.energia
        self.max_value=self.actor.max_energia
    end
    -- local x = 0
    -- local y = 2
    -- local w = 6
    -- local h = 7
    local x = self.x
    local y = self.y
    local w = self.w
    local h = self.h
    -- borde blau
    draw.hline(x+1, y,   x+w-1, 15) 
    draw.hline(x+1, y+h, x+w-1, 15)
    
    draw.vline(x  , y+1, y+h-1, 15) 
    draw.vline(x+w, y+1, y+h-1, 15) 

    draw.rectf(x+1, y+1, w-1, h-1, 16 ); -- fons negre
    
    if self.value>self.max_value/2 then
        -- 50%
        color = self.color_ok
    elseif self.value>self.max_value/3 then
        -- 33%
        color = self.color_warn
    else
        color = self.color_danger
    end
    
    local lifebar_w = w-3
    local segment_w  = lifebar_w/self.max_value
    local life_w = math.floor(lifebar_w*(self.value/self.max_value))
    -- if self.value==self.max_value then curr_width=curr_width-2 end
    if self.value>0 then
        -- barra de vida
        draw.rectf( x+2, y+2, life_w, h-3,  color );
        -- segments
        for n_segment=1,self.value-1 do
            x_segment = math.floor((x+2)+segment_w*n_segment)
            -- if n_segment==self.max_value then 
                -- x_segment=x+w-2
            -- end
            draw.vline( x_segment, y+2, y+h-2, 16)
        end
        -- draw.vline( x+2+segment_w*n_segment, y+2, y+h-2, 16)
    end
end