imp={hab=75,x=24,y=24,flip=true,enabled=false,counter=500,frame=28,wait=0,step=0,anim={28,29,28,30},bb={x=4,y=0,w=8,h=16},scene_intro=false,scene_object=false}

function imp.reset()
    imp.hit=imp.hit
    imp.update=imp.update
    imp.draw=imp.draw
    imp.hab=abad.hab
    imp.enabled=false
    imp.counter=500
end

function imp.init()
    imp.enabled=false
    imp.hit=imp.hit
    imp.update=imp.update
    imp.draw=imp.draw
    imp.hab=abad.hab
    if abad.x<40 then
        if check_tile(imp.hab,92,38)<tiletype.half and
           check_tile(imp.hab,84,38)<tiletype.half and
           check_tile(imp.hab,92,30)<tiletype.half and
           check_tile(imp.hab,84,30)<tiletype.half and
           check_tile(imp.hab,92,46)>=tiletype.half then
            imp.enabled=true
            imp.flip=true
            imp.x=92
        else
            imp.reset()
        end
    else
        if check_tile(imp.hab,0,38)<tiletype.half and
           check_tile(imp.hab,8,38)<tiletype.half and
           check_tile(imp.hab,0,30)<tiletype.half and
           check_tile(imp.hab,0,30)<tiletype.half and
           check_tile(imp.hab,0,46)>=tiletype.half then
                imp.enabled=true
            imp.flip=false
            imp.x=-12
        else
            imp.reset()
        end
    end
end

function imp.draw()
    if imp.enabled then
        draw.surf((imp.frame&7)*16,(imp.frame>>3)*16,16,16,imp.x,imp.y,16,16,imp.flip)
    end
end

function imp.hit()
end

function imp.update()
    imp.wait=imp.wait+1

    if imp.wait==6 then
        imp.wait=0
        if not imp.enabled then
            imp.counter=imp.counter-1
            local r = math.random(0,imp.counter-1)
            --print(r)
            if imp.counter==1 or r==1 then
                imp.init()
            end
            return
        end

        imp.step=(imp.step+1)%4
        imp.frame=imp.anim[imp.step+1]

        if imp.flip then
            imp.x=imp.x-2
            if imp.x==78 then
                fireball.init(imp.hab,imp.x+8,imp.y+4,imp.flip)
                sound.play(audio_hit)
                imp.flip=not imp.flip
            elseif imp.x==-12 then
                imp.reset()
            end
        else
            imp.x=imp.x+2
            if imp.x==4 then
                fireball.init(imp.hab,imp.x+8,imp.y+4,imp.flip)
                sound.play(audio_hit)
                imp.flip=not imp.flip
            elseif imp.x==92 then
                imp.reset()
            end
        end

    end
end

