require ":utils.util"

nemesio = { STATIC = 1, FLOATING = 2 }

function ia.update_nemesio(spr)

    spr.timer = spr.timer + 1

    local target = {x=sprites.hero.pos.x-spr.pos.x, y=sprites.hero.pos.y-spr.pos.y}
    if target.x > 0 then
        spr.flipped = false
    else
        spr.flipped = true
    end

    if spr.substate == nemesio.STATIC then
        spr.mode = draw.NORMAL
        if spr.timer==50 then
            spr.timer=0
            spr.substate = nemesio.FLOATING
            spr.advance = {x=0,y=0}
            if math.abs(target.x) > math.abs(target.y) then
                spr.advance.x = math.sign(target.x)
            else
                spr.advance.y = math.sign(target.y)
            end
        end

        if sprites.hero.state == templates.ALIVE then
            local x1,y1,w1,h1 = util.aabb(spr) -- El meu aabb
            local x2,y2,w2,h2 = util.aabb(sprites.hero) -- el aabb del heroi
            -- Si toca al heroi...
            if util.check_aabb_collision(x1,y1,w1,h1, x2,y2,w2,h2) then
                sprites.hero.hit()
            end
        end

    elseif spr.substate == nemesio.FLOATING then
        spr.mode = draw.PATTERN
        spr.pattern = 0xa5a5
        spr.pos.x = spr.pos.x + spr.advance.x
        spr.pos.y = spr.pos.y + spr.advance.y
        if spr.timer == 32 then
            spr.timer = 0
            spr.substate = nemesio.STATIC
        end
    end
end
