local arcade_config = require("arcade_config")
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

batman={hab=5,x=24,y=24,flip=true,goup=true,frame=8,stairscooldown=0,stepscooldown=0,stepsremember=0,wait=0,step=0,anim={24,25,24,26},bb={x=4,y=0,w=8,h=16},scene_intro=false,scene_object=false}

function batman.init()
    batman.hit=batman.hit
    batman.update=batman.update_normal
    batman.draw=batman.draw
    local habs={5,9,46,36,18}
    batman.hab=habs[1+math.random(0,#habs-1)]
    batman.x=24
    batman.y=24
end

function batman.draw()
    local flip=batman.flip
    if batman.update==batman.update_stairs then
        flip=(((batman.x>>1)+(batman.y>>1))%2)==0
    end
    -- draw.surf((batman.frame&7)*16,(batman.frame>>3)*16,16,16,batman.x,batman.y,16,16,flip)
	draw.surf((batman.frame&7)*cw,(batman.frame>>cxr2)*ch,cw,ch,batman.x*o2aX,batman.y*o2aX,cw,ch,flip)
end

function batman.hit()
    if abad.objects.bol~=nil then
        batman.endgame=true
        -- Start end of the game
        start_scene(scenes.final)
    end
end

function batman.update_normal()
    batman.wait=batman.wait+1

    if batman.wait>=6 then
        batman.wait=0
        if batman.stairscooldown>0 then batman.stairscooldown=batman.stairscooldown-1 end
        batman.step=(batman.step+1)%4
        if batman.stepscooldown>0 then batman.stepscooldown=batman.stepscooldown-1 end
        batman.frame=batman.anim[batman.step+1]

        local inc=12 if batman.flip then inc=2 end
        if not batman.flip and batman.x==84 then
            batman.hab=batman.hab+1
            batman.x=-4
        elseif check_tile(batman.hab,batman.x+inc,batman.y+14)==tiletype.nonpc then
            --if rnd(10)<8 then
                batman.update=batman.update_jumping
                batman.step=0
            --else
            --    batman.flip=not batman.flip
            --end
        elseif check_tile(batman.hab,batman.x+inc,batman.y+14)<tiletype.half then
            if batman.flip then
                batman.x=batman.x-2
            else
                batman.x=batman.x+2
            end
            if batman.x<-4 then
                batman.hab=batman.hab-1
                batman.x=84
            end
        elseif check_tile(batman.hab,batman.x+inc,batman.y+6)<tiletype.half then
            local r=math.random(0,1)
            if check_tile(batman.hab,batman.x+inc,batman.y+14)==tiletype.block then r=0 end
            if batman.stepscooldown>0 and check_tile(batman.hab,batman.x+inc,batman.y+14)==tiletype.half then r=batman.stepsremember end
            if r==0 then
                batman.update=batman.update_jumping
                batman.step=0
            else
                if batman.flip then
                    batman.x=batman.x-2
                else
                    batman.x=batman.x+2
                end
                if batman.x<-4 then
                    batman.hab=batman.hab-1
                    batman.x=84
                end
            end
            if batman.stepscooldown==0 then
                batman.stepscooldown=30
                batman.stepsremember=r
            end
        else
            --if check_tile(batman.hab,batman.x+4,batman.y+16)==tiletype.void and ((batman.x+4)&7==0 or check_tile(batman.hab,batman.x+12,batman.y+16)==tiletype.void) then
                --batman.update=batman.update_falling
            --else
                batman.flip=not batman.flip
            --end
        end

        if batman.stairscooldown==0 and
            check_tile(batman.hab,batman.x+6,batman.y+8)==tiletype.stair and
            check_tile(batman.hab,batman.x+14,batman.y+8)==tiletype.stair then
            if math.random(0,9)<8 then
                batman.update=batman.update_stairs
                if check_tile(batman.hab,batman.x+4,batman.y+16)==tiletype.block then
                    batman.goup=true
                else
                    batman.goup=false
                end
                --batman.goup=rnd(2)==0 and true or false
            else
                batman.stairscooldown=50
            end
        elseif batman.update==batman.update_stairs then
            batman.stairscooldown=50
            batman.update=batman.update_normal
            batman.frame=24
        end

        if check_tile(batman.hab,batman.x+4,batman.y+16)==tiletype.void and ((batman.x+4)&7==0 or check_tile(batman.hab,batman.x+12,batman.y+16)==tiletype.void) then
            local option=math.random(0,1)
            if batman.stairscooldown>0 then option=0 end
            if option==0 then
                batman.update=batman.update_falling
            else
                batman.update=batman.update_jumping
                batman.step=0
                batman.wait=0
            --else
            --    batman.flip=not batman.flip
            end
        end

        if batman.hab==abad.hab then
            if aabb(abad,batman) then
                abad_hurt(40)
            end
        end

    end
end

function batman.update_jumping()
    batman.frame=25
    batman.wait=batman.wait+1

    if batman.wait>=6 then
        batman.wait=0
        
        local inc=12 if batman.flip then inc=2 end
        if not batman.flip and batman.x==84 then
            batman.hab=batman.hab+1
            batman.x=-4
        elseif check_tile(batman.hab,batman.x+inc,batman.y+14)<tiletype.half then
            if batman.flip then
                batman.x=batman.x-2
            else
                batman.x=batman.x+2
            end
            if batman.x<-4 then
                batman.hab=batman.hab-1
                batman.x=84
            end
        end

        if batman.step<6 then
            if batman.y>0 then
                if check_tile(batman.hab,batman.x+4,batman.y-2)~=tiletype.block then
                    if (batman.x+4)&7==0 or check_tile(batman.hab,batman.x+12,batman.y-2)~=tiletype.block then
                            batman.y=batman.y-2
                    end
                end
            else
                batman.hab=batman.hab-10
                batman.y=32
            end
        elseif batman.step>6 then
            batman.update=batman.update_falling
        end
        batman.step=batman.step+1
    end

end

function batman.update_falling()
    batman.frame=25
    batman.wait=batman.wait+1

    if batman.wait>=6 then
        batman.wait=0

        local inc=12 if batman.flip then inc=2 end
        if not batman.flip and batman.x==84 then
            batman.hab=batman.hab+1
            batman.x=-4
        elseif check_tile(batman.hab,batman.x+inc,batman.y+14)<tiletype.half then
            if batman.flip then
                batman.x=batman.x-2
            else
                batman.x=batman.x+2
            end
            if batman.x<-4 then
                batman.hab=batman.hab-1
                batman.x=84
            end
        end

        if batman.y<32 then
            if (batman.y+16)&7==0 and (check_tile(batman.hab,batman.x+4,batman.y+16)>=tiletype.half or ((batman.x+4)&7~=0 and check_tile(batman.hab,batman.x+12,batman.y+16)>=tiletype.half)) then
                batman.update=batman.update_normal
                return
            end
            batman.y=batman.y+2
        else
            batman.hab=batman.hab+10
            batman.y=0
        end
    end
end

function batman.update_stairs()
    batman.frame=27
    batman.wait=batman.wait+1

    if batman.wait>=6 then
        batman.wait=0

        if batman.goup then
            if batman.y>0 then
                if check_tile(batman.hab,batman.x+4,batman.y+8)==tiletype.stair or (batman.x+4)&7~=0 and check_tile(batman.hab,batman.x+12,batman.y+8)==tiletype.stair then
                    batman.y=batman.y-2
                else
                    batman.update=batman.update_normal
                    if math.random(0,2)>0 then batman.flip=not batman.flip end
                    batman.stairscooldown=50
                end
            else
                batman.hab=batman.hab-10
                batman.y=32
            end
        else
            if batman.y<32 then
                if check_tile(batman.hab,batman.x+4,batman.y+16)==tiletype.stair or (batman.x+4)&7~=0 and check_tile(batman.hab,batman.x+12,batman.y+16)==tiletype.stair then
                    batman.y=batman.y+2
                else
                    batman.update=batman.update_normal
                    if math.random(0,2)>0 then batman.flip=not batman.flip end
                    batman.stairscooldown=50
                end
            else
                batman.hab=batman.hab+10
                batman.y=0
            end
        end    
    end
end