score = {
    points = 0,
    color = 21,
    ypos = 3,
    surf = nil,
    font = nil
} local me = score

function me.init(reinit)
    if not reinit then me.points = 0 end
    me.ypos = 3
    me.surf = surf.load("gfx/sprites.gif")
    me.font = font.load("gfx/score_font.fnt")
end

local function draw_text()
    font.current(me.font)
    font.spacing(0)
    local txt = string.format("%03d", me.points)
    draw.text(txt,2,me.ypos-1,27)
    draw.text(txt,3,me.ypos-1,27)
    draw.text(txt,4,me.ypos-1,27)
    draw.text(txt,2,me.ypos,27)
    draw.text(txt,4,me.ypos,27)
    draw.text(txt,2,me.ypos+1,27)
    draw.text(txt,4,me.ypos+1,27)
    draw.text(txt,2,me.ypos+2,27)
    draw.text(txt,3,me.ypos+2,27)
    draw.text(txt,4,me.ypos+2,27)
    draw.text(txt,3,me.ypos+1,1)
    draw.text(txt,3,me.ypos,me.color)
    font.current(font.DEFAULT)
    font.spacing(1)
    surf.source(me.surf)
end

local function draw_key(sx,sy,dx,dy)
    pal.subpal(1,26,27)
    for y=-1,1 do
        for x=-1,1 do
            if x~=0 and y~=0 then draw.surf(sx,sy,16,8, dx+x,dy+y) end
        end
    end
    pal.subpal()
    draw.surf(sx,sy,16,8, dx,dy)
end

function me.draw()
    if sprites.hero.state == templates.DEAD then
        if me.ypos > -100 then me.ypos=me.ypos-1 end
    end
    view.origin(0,0)
    surf.target(0)
    surf.clip()

    draw_text()

    local y = score.ypos
    if sprites.hero.keys["verda"] then draw_key(16,48,140,y) y=y+8 end
    if sprites.hero.keys["groga"] then draw_key(16,56,140,y) y=y+8 end
    if sprites.hero.keys["roja"]  then draw_key(32,48,140,y) y=y+8 end
    if sprites.hero.keys["blava"] then draw_key(32,56,140,y) y=y+8 end
end

function me.inc(value)
    me.points = me.points + value
    tweening.add(8, 0, 0.5, easing.linear, function(val,progress,finished) me.color = (math.floor(val)&1) == 0 and 21 or 14 end)
    tweening.add(5, 3, 0.5, easing.easeOutElastic, function(val,progress,finished) me.ypos = math.floor(val) end)
end
