cxr2 = arcade_config.character_per_row_base2
cw = arcade_config.character_width
ch = arcade_config.character_height

caco={}

function caco.new(_hab,_x,_y,_flip)
    local world_x, world_y = coords.room_to_world(_hab,_x,_y)
    return {name="caco",
            hab=_hab,
            x=world_x,
            y=world_y,
            w=arcade_config.sprite_size.w,
            h=arcade_config.sprite_size.h,
            flip=_flip,
            frame=19,
            wait=0,
            step=0,
            can_warp=true,
            warping=false,
            shrink=1,
            d_shrink=1,
            angle=0,
            d_angle=15,
            hit=caco.hit,
            update=caco.update_normal,
            draw=caco.draw,
            jumpfwd=false,
            dying=false,
            death_time=40,
            anim={19,19,20,21},
            bb={x=4,y=4,w=24,h=16},
            enabled= true,
            disable_reason="" }
end

function caco:draw()
    if not self.enabled then return end
    -- if self.frame>0 then draw.surf((self.frame&7)*16,(self.frame>>3)*16,16,16,self.x,self.y,16,16,self.flip) end
    
    if self.warping then
        -- local shrink_w = self.w*self.shrink
        -- local shrink_h = self.h*self.shrink
        -- local offset_x = math.floor((self.w-shrink_w)/2)
        -- local offset_y = math.floor((self.h-shrink_h)/2)
        -- local scr_x, scr_y = viewp:screen_coords( self.x+offset_x, self.y+offset_y )
        -- shrink_w = math.floor(shrink_w)
        -- shrink_h = math.floor(shrink_h)
        -- 
        -- if shrink_w>0 and shrink_h>0 then
        --     draw.surfrot((self.frame&7)*cw, (self.frame>>cxr2)*ch, 
        --                 cw, ch, 
        --                 scr_x, scr_y, 
        --                 self.angle,
        --                 shrink_w, shrink_h,
        --                 self.flip)
        -- end
        actor_warp_draw(self)
    else 
        if self.frame>0 then
            local scr_x, scr_y = viewp:screen_coords( self.x, self.y )
            draw.surf((self.frame&7)*cw, (self.frame>>cxr2)*ch, cw, ch, scr_x, scr_y, cw, ch, self.flip)
        end
    end
end

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

    if self.wait==6 then
        self.wait=0
        self.step=self.step+1
        if self.step<8 then
            self.frame=19
        elseif self.step==12 then
            self.frame=20
        elseif self.step==13 then
            self.frame=21
        elseif self.step==15 then
            self.step=0
        end
    end
    
    local step = 1
    if self.flip then step = -step end
    local inc = 0
    if self.flip then inc = self.bb.w end
    
    local check_x = self.x+self.bb.x+step
    local check_ywall1 = self.y+self.bb.y
    local check_ywall2 = self.y+self.h-2
    if not self.flip then
        check_x = self.x+self.bb.w+self.bb.x+step
    end
    
    local check_no_wall1= arc_check_tile(check_x,check_ywall1)<tiletype.block
    local check_no_wall2= arc_check_tile(check_x,check_ywall2)<tiletype.block
    if check_no_wall1 and check_no_wall2 then
        self.x=self.x+step
    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)
        end
    end
    
    -- if self.hab==abad.hab then
    --     if aabb(abad,self) then
    --         abad_hurt(1)
    --     end
    -- end
    
        --if check_tile(self.hab,self.x+4,self.y+16)==tiletype.void and ((self.x+4)&7==0 or check_tile(self.hab,self.x+12,self.y+16)==tiletype.void) then
        --    self.flip=not self.flip
        --end
end

function caco:update_hit()
    if not self.enabled then return end
    local step_time = self.death_time
    if self.can_warp then 
        step_time = self.death_time/3; -- warp, wait, appear
    end
        
    self.wait=self.wait+1

    if self.wait>=6 then
        self.wait=0
        self.step=self.step+1
        
        if self.can_warp then
        --    -- warp, wait, respawn
        --    if self.warping then
        --        if self.step<self.death_time then
        --            self.shrink=self.shrink-self.d_shrink
        --            self.angle=self.angle+self.d_angle
        --            if self.angle>=360 then self.angle = self.angle % 360 end
        --            if self.shrink<=0 then 
        --                self.shrink=1
        --                self.d_shrink=1
        --                self.angle=0
        --                self.d_angle=1
        --                self.warping = false
        --                self.frame = -1
        --                self.step = 0
        --            end
        --        end
        --    elseif self.step<self.death_time/2 then
        --        self.frame = -1
        --    elseif self.step<self.death_time then
        --        if self.step%2==0 then
        --            self.frame=21
        --        else
        --            self.frame=-1
        --        end
        --    elseif self.step>=self.death_time then
        --        self.frame=19
        --        self.step=0
        --        self.wait=0
        --        self.update=caco.update_normal
        --        self.dying = false
        --    end
            actor_warp_update(self)
        else
            if self.step<self.death_time then
                if self.step%2==0 then
                    -- self.frame=21
                    self.frame=self.anim[#self.anim]
                else
                    self.frame=-1
                end
            elseif self.step>=self.death_time then
                -- self.frame=19
                self.frame=self.anim[1]
                self.step=0
                self.wait=0
                self.update=caco.update_normal
                self.dying = false
            end
        end
    end
end

function caco:hit()
    if not self.enabled then return end
    self.update=caco.update_hit
    
    if self.can_warp then self.warping=true end
    self.shrink=1
    self.angle=0
    self.dying=true
    -- calcular velocitat per al warp
    local warp_time = self.death_time/3
    self.d_angle = 720 / warp_time; -- 720 = 2 voltes
    self.d_shrink = self.shrink / warp_time
end
