
require "dead"

abad={x=40,y=24,flip=false,frame=1,wait=0,respawning=0,hab=10,vides=3,energia=40,falling=0,step=0, mustep=1,hurting=0,update=nil,jumpfwd=false,anim={0,1,0,2},bb={x=4,y=0,w=8,h=16},safe={hab=10,x=40,y=24}}

function abad_nop()
end

function abad_init()
    abad={x=40,y=24,flip=false,frame=1,wait=0,respawning=0,hab=10,vides=3,energia=40,falling=0,step=0,mustep=1,draw=abad.draw,hurting=0,update=nil,jumpfwd=false,anim={0,1,0,2},bb={x=4,y=0,w=8,h=16},safe={hab=10,x=40,y=24}}
    abad.update=abad_state_normal
    abad.objects={}

end

function abad:draw()
    local flip=abad.flip
    if abad.update==abad_state_stairs then
        flip=(((abad.x>>1)+(abad.y>>1))%2)==0
    end
    if (abad.respawning==0) or (math.floor(abad.respawning/15)%2==0) then
        draw.surf(abad.frame*16,0,16,16,abad.x,abad.y,16,16,flip)
    end

    if abad.respawning > 0 then 
        abad.respawning=abad.respawning-1
    end
end

function abad_make_safe()
    if abad.safe.hab~=abad.hab then
        abad.safe.hab=abad.hab
        abad.safe.x=abad.x
        abad.safe.y=abad.y
        --cls(4)
    end
end

function abad_hurt(howmuch)
    if abad.hurting == 0 and abad.respawning==0 then
        sound.play(audio_abad_hit)
        abad.energia=abad.energia-howmuch
        if abad.energia<=0 then
            abad.vides=abad.vides-1
            if abad.vides==0 then
                --final de la partida
                dead.start()
                music.play(audio_game_over,0)
            else
                music.play(audio_life_lost,0)
                abad.energia=40
                abad.hab=abad.safe.hab
                abad.x=abad.safe.x
                abad.y=abad.safe.y
                abad.hurting=60
                abad.update=abad_state_normal
                abad.bb.y=0
                abad.bb.h=16
                abad.respawning=240
                surf.cls(3)
            end
        else
            surf.cls(3)
        end
    end
end

function abad_state_normal()
    abad.frame=0
    abad.wait=0
    abad.step=0
    abad.jumpfwd=false
    if abad.hurting > 0 then 
        abad.hurting=abad.hurting-1
        return
    end

    if abad.falling>0 then
        if abad.falling>12 then
            abad_hurt(abad.falling-12)
        end
        abad.falling=0
    end
    abad_make_safe()

    if key.down(keyRight) or pad.down(btnRight) then
        abad.update=abad_state_walking
        abad.flip=false
    elseif key.down(keyLeft) or pad.down(btnLeft) then
        abad.update=abad_state_walking
        abad.flip=true
    elseif key.down(keyJump) or pad.down(btnJump) then
        abad.update=abad_state_jumping
        abad.step=0
        abad.jumpfwd=false
        if abad.jump then
            sound.stop(abad.jump)
            abad.jump=nil
        end
        sound.play(audio_abad_jump)
    elseif key.down(keyDown) or pad.down(btnDown) then
        abad.update=abad_state_crouch
    --elseif btn(KEY_Z) then
    --    abad.respawning=240
    elseif key.down(keyShoot) or pad.down(btnShoot) and cacau.hab==-1 then
        sound.play(audio_abad_shot)
        abad.update=abad_state_fire
        abad.wait=0
        cacau.init(abad.hab,abad.x+8,abad.y+8,abad.flip)
    end
end

function abad_state_crouch()
    abad.bb.y=8
    abad.bb.h=8
    abad.frame=5
    abad.wait=0
    abad.step=0
    abad.jumpfwd=false

    if not (key.down(keyDown) or pad.down(btnDown)) then
        abad.update=abad_state_normal
        abad.bb.y=0
        abad.bb.h=16
    end
end

function abad_state_fire()
    abad.frame=6

    abad.wait=abad.wait+1

    if abad.wait==6 then
        abad.wait=0
        abad.update=abad_state_normal
    end
end

function abad_advance()
    local inc=12 if abad.flip then inc=2 end
    local limit=tiletype.block
    if abad.update~=abad_state_walking then limit=tiletype.half end
    if not abad.flip and abad.x==84 then
        abad.hab=abad.hab+1
        imp.reset()
        abad.x=-4
    elseif check_tile(abad.hab,abad.x+inc,abad.y+14)<limit then
        if abad.flip then
            abad.x=abad.x-2
        else
            abad.x=abad.x+2
        end
        if abad.x<-4 then
            abad.hab=abad.hab-1
            imp.reset()
            abad.x=84
        end
    end
    if check_tile(abad.hab,abad.x+6,abad.y+8)==tiletype.stair then
        abad.update=abad_state_stairs
    elseif abad.update==abad_state_stairs then
        abad.update=abad_state_normal
        abad.frame=0
    end
end

function abad_state_walking()
    abad.wait=abad.wait+1

    if abad.wait==6 then
        abad.wait=0
        abad.step=(abad.step+1)%2
        if abad.step==0 then
            sound.play(audio_abad_step[abad.mustep],32)
            abad.mustep = abad.mustep + 1
            if abad.mustep == 5 then abad.mustep=1 end
        end
        abad.frame=abad.anim[abad.step+1]

        abad_advance()

        if check_tile(abad.hab,abad.x+4,abad.y+16)==tiletype.void and ((abad.x+4)&7==0 or check_tile(abad.hab,abad.x+12,abad.y+16)==tiletype.void) then
            abad.update=abad_state_falling
            return
        end
    end

    abad_make_safe()

    if key.down(keyJump) or pad.down(btnJump) then
        abad.update=abad_state_jumping
        abad.step=0
        abad.jumpfwd=true
        if abad.jump then
            sound.stop(abad.jump)
            abad.jump=nil
        end
        sound.play(audio_abad_jump)
        return
    elseif key.down(keyDown) or pad.down(btnDown) then
        abad.update=abad_state_crouch
    elseif key.down(keyShoot) or pad.down(btnShoot) and cacau.hab==-1 then
        sound.play(audio_abad_shot)
        abad.update=abad_state_fire
        abad.wait=0
        cacau.init(abad.hab,abad.x+8,abad.y+8,abad.flip)
    end

    if key.down(keyRight) or pad.down(btnRight) then
        abad.flip=false
    elseif key.down(keyLeft) or pad.down(btnLeft) then
        abad.flip=true
    elseif abad.wait==0 then
        abad.update=abad_state_normal
    end

end

function abad_state_jumping()
    abad.frame=3
    abad.wait=abad.wait+1
    --print(abad.x,1,50)
    --print(abad.y,10,50)
    if abad.wait==6 then
        abad.wait=0
        
        if abad.jumpfwd then abad_advance() end

        --local xx=math.floor((abad.x+4)/8)
        --local yy=math.floor(abad.y/8)
        --rect(xx*8,yy*8, (xx+2)*8,(yy+1)*8)
        if abad.step<6 then
            if abad.y>0 then
                if check_tile(abad.hab,abad.x+4,abad.y-2)~=tiletype.block then
                    if (abad.x+4)&7==0 or check_tile(abad.hab,abad.x+12,abad.y-2)~=tiletype.block then

                        if check_tile(abad.hab,abad.x+4,abad.y-2)==tiletype.switch then
                            -- Executar el switch
                            local xx=math.min(11,math.max(0,math.floor((abad.x+4)/8)))
                            local yy=math.min(5,math.max(0,math.floor((abad.y-2)/8)))
                            switches.start(abad.hab,1+xx+yy*12)
                            --cls(4)
                        elseif (abad.x+4)&7~=0 and check_tile(abad.hab,abad.x+12,abad.y-2)==tiletype.switch then
                            -- Executar el switch
                            local xx=math.min(11,math.max(0,math.floor((abad.x+12)/8)))
                            local yy=math.min(5,math.max(0,math.floor((abad.y-2)/8)))
                            switches.start(abad.hab,1+xx+yy*12)
                            --cls(4)
                        else
                            abad.y=abad.y-2
                        end
                    end
                end
            else
                abad.hab=abad.hab-10
                imp.reset()
                abad.y=32
            end
        elseif abad.step>6 then
            abad.update=abad_state_falling
            abad.jump=sound.play(audio_abad_fall)
        end
        abad.step=abad.step+1
    end

end

function abad_state_falling()
    abad.frame=3
    abad.wait=abad.wait+1

    if abad.wait==6 then
        abad.wait=0

        if abad.jumpfwd then abad_advance() end

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

function abad_state_stairs()
    abad.frame=4
    abad.wait=abad.wait+1

    if abad.wait==6 then
        abad.wait=0

        if key.down(keyRight) or pad.down(btnRight) then
            abad.flip=false
            abad_advance()
            sound.play(audio_low)
        elseif key.down(keyLeft) or pad.down(btnLeft) then
            abad.flip=true
            abad_advance()
            sound.play(audio_low)
        elseif key.down(keyUp) or pad.down(btnUp) then
            if abad.y>0 then
                if check_tile(abad.hab,abad.x+4,abad.y+8)==tiletype.stair or (abad.x+4)&7~=0 and check_tile(abad.hab,abad.x+12,abad.y+8)==tiletype.stair then
                    abad.y=abad.y-2
                    sound.play(audio_low)
                end
            else
                abad.hab=abad.hab-10
                imp.reset()
                abad.y=32
            end
        elseif key.down(keyDown) or pad.down(btnDown) then
            if abad.y<32 then
                if check_tile(abad.hab,abad.x+4,abad.y+16)==tiletype.stair or (abad.x+4)&7~=0 and check_tile(abad.hab,abad.x+12,abad.y+16)==tiletype.stair then
                    abad.y=abad.y+2
                    sound.play(audio_low)
                end
            else
                abad.hab=abad.hab+10
                imp.reset()
                abad.y=0
            end
        end    
    end
end