
fireball={
    hab=-1,
    x=0, y=0,
    w=6, h=6,
    wait=0,
    flip=false,
    step_length=3,
    power=1,
    size=1,
    bb={x=0,y=0,w=4,h=4},
    x1=0,
    y1=0,
    step_length_x=1,
    step_length_y=1,
}

function fireball.init(_hab,_x,_y,_flip, _off_x, _off_y, _power)
    local world_x, world_y = coords.room_to_world(_hab,_x,_y)
    _power = _power or 1
    if fireball.hab ~= -1 then return end
    _off_x = _off_x or 0
    _off_y = _off_y or 0
    fireball.hab=_hab
    fireball.x= world_x+_off_x
    fireball.y= world_y+_off_y
    fireball.flip=_flip
    fireball.power=_power
    fireball.size=1
    fireball.w = 6
    fireball.h = 6
    fireball.bb = {x=0,y=0,w=4,h=4}
end

function fireball.draw()
    if fireball.hab == -1 then return end
    local scr_x, scr_y = viewp:screen_coords( fireball.x, fireball.y )
    draw.circf(scr_x,scr_y,8*fireball.size,16)
    draw.circf(scr_x,scr_y,6*fireball.size,3)
    draw.circf(scr_x,scr_y,2*fireball.size,8)
    -- draw.rect(scr_x-5*fireball.size,scr_y-5*fireball.size,5*fireball.size*2,5*fireball.size*2,2)
end

function fireball.update()
    if fireball.power==2 and fireball.power>fireball.size then
        fireball.wait = fireball.wait + 1
        fireball.x1=abad.x+abad.bb.x+(abad.bb.w//2)
        fireball.y1=abad.y+(abad.bb.h/2)
        if abad.x<fireball.x then 
            fireball.step_length_x = -1 
        else
            fireball.step_length_x = 1 
        end
        if abad.y<fireball.y then 
            fireball.step_length_y = -1 
        else
            fireball.step_length_y = 1 
        end
        if fireball.wait==6 then
            fireball.size = fireball.size+0.1
            fireball.wait = 0
        end
        
        local dx = math.abs(fireball.x-fireball.x1)
        local dy = math.abs(fireball.y-fireball.y1)
        if dx<dy then
            if dy~=0 then
                fireball.step_length_x = fireball.step_length_x*(dx/dy)*2
                fireball.step_length_y = fireball.step_length_y*2
            end
        else
            if dx~=0 then 
                fireball.step_length_x = fireball.step_length_x*2
                fireball.step_length_y = fireball.step_length_y*(dy/dx)*2
            end
        end
        fireball.w=6*fireball.size
        fireball.h=fireball.w
        fireball.bb = {x=0,y=0,w=4*fireball.size,h=4*fireball.size}
        return
    end
    
    
    if fireball.hab == -1 then return end
    -- fireball.wait=fireball.wait+1

    --if fireball.wait==3 then
        fireball.wait=0

        --if viewp:inside(fireball.x, fireball.y, fireball.w, fireball.h)==false then
        --    fireball.hab=-1
        --    return
        --end
        if fireball.power==1 then
            if arc_check_tile(fireball.x,fireball.y)<tiletype.block then
                if fireball.flip then
                    fireball.x=fireball.x-fireball.step_length
                else
                    fireball.x=fireball.x+fireball.step_length
                end
            else
                fireball.hab=-1
                return
            end
        elseif fireball.power==2 then
            fireball.x = fireball.x+fireball.step_length_x
            fireball.y = fireball.y+fireball.step_length_y
        end

        if viewp:inside(fireball.x, fireball.y, fireball.w, fireball.h) then
            if collision(fireball,abad) then
                if fireball.power==1 and abad.update~=abad_state_crouch then
                    abad_hurt(1)
                    fireball.hab=-1
                elseif fireball.power==2 then
                    abad_hurt(2)                
                    fireball.hab=-1
                end
            end
        else
            fireball.hab=-1
        end
    --end
end
