o2aX = arcade_config.org2arc_escala
cxr = arcade_config.character_per_row-1
cxr2 = arcade_config.character_per_row_base2
cw = arcade_config.character_width
ch = arcade_config.character_height
-- posició en el gif
gota_gif_col = 7
gota_gif_row = 0

gota={}

function gota.new(_hab,_x,_y,_freq,_x_offset, _y_offset)
    local world_x, world_y = coords.room_to_world(_hab,_x,_y)
    _x_offset = _x_offset or 0
    _y_offset = _y_offset or 0
    world_x = world_x+_x_offset
    world_y = world_y+_y_offset
    return {name="gota",
            hab=_hab,
            ix=world_x,
            iy=world_y,
            x=world_x,
            y=world_y,
            w=16,
            h=16,
            freq=_freq,
            cx=0,
            dx=0,
            dy=0,
            floor_y=0,
            wait=0,
            step=0,
            hit=gota.hit,
            update=gota.update_normal, 
            draw=gota.draw_normal,
            draw_drop= gota.draw_drop,
            bb={x=4,y=0,w=8,h=12},
            enabled= true,
            disable_reason="" }
end

function gota:draw_normal()
    if not self.enabled then return end
    local x = gota_gif_col*cw
	local y = gota_gif_row*ch
    local scr_x, scr_y = viewp:screen_coords( self.x, self.y )
    draw.surf(x,y,self.w,self.h,scr_x,scr_y)
    -- local x_check = scr_x+self.bb.x+(self.bb.w/2)-1
    -- local y_check = scr_y+self.bb.h
    -- draw.rectf(x_check,y_check,1,1,4)
end

function gota:update_normal()
    if not self.enabled then return end
    self.wait=self.wait+1

    if self.wait==18 then
        self.wait=0
        self.step=self.step+1
    end
    
    if self.step > self.freq then
        self.y=self.y+2
        local x_check = self.x+self.bb.x+(self.bb.w/2)-1
        local y_check = self.y+self.bb.h
        
        if arc_check_tile(x_check,y_check)>=tiletype.block then
            self.update=gota.update_splash
            self.draw=gota.draw_splash
            self.floor_y=self.y+8
            self.cx=self.x+2
            self.dx=0
            self.dy=-4
            self.dy=-2
            if self.hab==abad.hab then sound.play(audio_low) end
        else
            self.draw=gota.draw_falling
        end
    else
        --self.flip=not self.flip
    end

    if viewp:inside(self.x, self.y, self.w, self.h) then
        if collision(abad,self) then
            abad_hurt(1)
            self.update=gota.update_splash
            self.draw=gota.draw_splash
            self.cx=self.x+2
            self.dx=0
            self.dy=-4
        end
    end

    -- if self.hab==abad.hab then
    --     if aabb(abad,self) then
    --         abad_hurt(1)
    --         self.update=gota.update_splash
    --         self.draw=gota.draw_splash
    --         self.cx=self.x+2
    --         self.dx=0
    --         self.dy=-4
    --     end
    -- end

end

function gota:hit()
end

function gota:draw_falling()
    if not self.enabled then return end
    local gota_x, gota_y = viewp:screen_coords( self.x, self.y )
    draw.circf( gota_x+4, gota_y+12, 4, 16)
    draw.circf( gota_x+5, gota_y+13, 3, 11)
    draw.pixel( gota_x+3, gota_y+13, 2)
end 

function gota:draw_drop( x, y )
    if not self.enabled then return end
    splash_x, splash_y = viewp:screen_coords( x, y )
    draw.circf( splash_x, splash_y, 2, 16)
    draw.circf( splash_x, splash_y, 1, 11)
end

function gota:draw_splash()
    if not self.enabled then return end
    local splash_L1_x = (self.cx-self.dx)
    local splash_L2_x = (self.cx-(self.dx/2))
    local splash_R1_x = (self.cx+self.dx)
    local splash_R2_x = (self.cx+(self.dx/2))
    local splash_U_y = self.y
    local splash_D_y = self.y-1
    
    self:draw_drop( splash_L1_x, splash_U_y )
    self:draw_drop( splash_L2_x, splash_D_y )
    self:draw_drop( splash_R1_x, splash_U_y )
    self:draw_drop( splash_R2_x, splash_D_y )
end

function gota:update_splash()
    if not self.enabled then return end
    self.dx=self.dx-1
    self.dy=self.dy+0.25
    self.x=self.cx+self.dx
    self.y=self.y+math.floor(self.dy)
   
    if self.y>self.floor_y then
        self.step=0
        self.update=gota.update_normal
        self.draw=gota.draw_normal
        self.x=self.ix
        self.y=self.iy
    end
end