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

zombie={}

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

function zombie: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.frame>0 then draw.surf((self.frame&7)*cw,(self.frame>>cxr2)*ch,cw,ch,self.x*o2aX,self.y*o2aX,cw,ch,self.flip) end
    
    if self.energy==2 then
        pal.subpal(10,14)
    elseif self.energy>2 then
        pal.subpal(10,9)
    end
    
    if self.warping then
        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
    
    pal.subpal(10)
end

function zombie: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)%4
        self.frame=self.anim[self.step+1]
    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_ywall = self.y+self.h-2
    local check_yhole = self.y+self.h+1
    if not self.flip then
        check_x = self.x+self.bb.w+self.bb.x+step
    end
    
    local check_no_wall= arc_check_tile(check_x,check_ywall)<tiletype.block
    local check_no_hole= arc_check_tile(check_x,check_yhole)~=tiletype.void
    if check_no_wall and check_no_hole 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

    self.hab = coords.world_to_room(self.x, self.y)

    -- 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 zombie:update_hit()
    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.can_warp then
            actor_warp_update(self)
        else
            -- Continuar si encara te vida
            if self.energy>0 then 
                self.update=zombie.update_normal
                return 
            end
            
            -- Parpadejar
            if self.step<self.death_time then
                if self.step%2==0 then
                    self.frame=self.anim[#self.anim]
                else
                    self.frame=-1
                end
            elseif self.step>=self.death_time then
                self.frame=self.anim[0]
                self.step=0
                self.wait=0
                self.update=zombie.update_normal
                self.dying = false
            end
        end
    end    
end

function zombie:hit()
    if not self.enabled then return end
    
    self.energy = self.energy-1
    
    if self.energy>0 then 
        self.can_warp = false
    else
        self.can_warp = true
        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
    self.update=zombie.update_hit
end
