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

premiere={hab=75,x=24,y=24,flip=true,frame=12,wait=0,step=0,anim={12,13,12,14},bb={x=4,y=0,w=8,h=16},scene_intro=false,scene_object=false}

function premiere.init()
    premiere.x=24
    premiere.y=24
    premiere.frame=12
    premiere.wait=0
    premiere.step=0
    premiere.update=premiere.update_normal
    premiere.scene_intro=false
    premiere.scene_object=false
    local habs={75,44,20,0}
    premiere.hab=habs[1+math.random(0,3)]
end

function premiere.draw()
    -- draw.surf((premiere.frame&7)*16,(premiere.frame>>3)*16,16,16,premiere.x,premiere.y,16,16,premiere.flip)
	draw.surf((premiere.frame&7)*cw,(premiere.frame>>cxr2)*ch,cw,ch,premiere.x*o2aX,premiere.y*o2aX,cw,ch,premiere.flip)
end

function premiere.hit()
end

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

    if premiere.wait==6 then
        premiere.wait=0
        premiere.step=(premiere.step+1)%4
        premiere.frame=premiere.anim[premiere.step+1]

        local inc=12 if premiere.flip then inc=2 end
        if not premiere.flip and premiere.x==84 then
            premiere.hab=premiere.hab+1
            premiere.x=-4
        elseif check_tile(premiere.hab,premiere.x+inc,premiere.y+14)<tiletype.half then
            if premiere.flip then
                premiere.x=premiere.x-2
            else
                premiere.x=premiere.x+2
            end
            if premiere.x<-4 then
                premiere.hab=premiere.hab-1
                premiere.x=84
            end
        elseif check_tile(premiere.hab,premiere.x+inc,premiere.y+6)<tiletype.half then
            premiere.update=premiere.update_jumping
            premiere.step=0
        else
            --if check_tile(premiere.hab,premiere.x+4,premiere.y+16)==tiletype.void and ((premiere.x+4)&7==0 or check_tile(premiere.hab,premiere.x+12,premiere.y+16)==tiletype.void) then
                --premiere.update=premiere.update_falling
            --else
                premiere.flip=not premiere.flip
            --end
        end
        if check_tile(premiere.hab,premiere.x+4,premiere.y+16)==tiletype.void and ((premiere.x+4)&7==0 or check_tile(premiere.hab,premiere.x+12,premiere.y+16)==tiletype.void) then
            premiere.update=premiere.update_falling
        end

        if premiere.hab==abad.hab then
            if aabb(abad,premiere) then
                if not premiere.scene_intro then
                    if abad.objects.gorro~=nil then
                        start_scene(scenes.premiere_intro_gorro)
                        premiere.scene_object=true
                        abad.objects.clau_premiere=true
                        abad.objects.gorro=nil
                    else
                        start_scene(scenes.premiere_intro)
                    end
                    premiere.scene_intro=true
                elseif not premiere.scene_object and abad.objects.gorro~=nil then
                    start_scene(scenes.premiere_gorro)
                    premiere.scene_object=true
                    abad.objects.clau_premiere=true
                    abad.objects.gorro=nil
                end
            end
        end

    end
end

function premiere.update_jumping()
    premiere.frame=13
    premiere.wait=premiere.wait+1

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

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

end

function premiere.update_falling()
    premiere.frame=13
    premiere.wait=premiere.wait+1

    if premiere.wait==6 then
        premiere.wait=0

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

        local xx=math.floor((premiere.x+4)/8)
        local yy=math.floor((premiere.y+16)/8)
        
        if premiere.y<32 then
            if (premiere.y+16)&7==0 and (check_tile(premiere.hab,premiere.x+4,premiere.y+16)>=tiletype.half or ((premiere.x+4)&7~=0 and check_tile(premiere.hab,premiere.x+12,premiere.y+16)>=tiletype.half)) then
                premiere.update=premiere.update_normal
                return
            end
            premiere.y=premiere.y+2
        else
            premiere.hab=premiere.hab+10
            premiere.y=0
        end
    end
end
