;-----------------------------------------------------------;
;                                                           ;
; Invaded                                                   ;
; Version 1.0                                               ;
; Player Routines                                           ;
;                                                           ;
;-----------------------------------------------------------;

;------------------------------------------------
; moveUp - Attempt to move player up
;
; Input:    None
; Output:   CA = 1 if couldn't move
;------------------------------------------------
moveUp:
        ld      hl,(x)
        ld      a,(y)
        or      a
        ret     z
        dec     a
        push    hl
        call    checkPixel
        pop     hl
        ret     c
        ld      de,7
        add     hl,de
        ld      a,(y)
        dec     a
        call    checkPixel
        ret     c
        ld      hl,y
        dec     (hl)
        ret

;------------------------------------------------
; moveDown - Attempt to move player down
;
; Input:    None
; Output:   CA = 1 if couldn't move
;------------------------------------------------
moveDown:
        ld      hl,(x)
        ld      a,(y)
        cp      56-4
        ret     z
        add     a,4
        push    hl
        call    checkPixel
        pop     hl
        ret     c
        ld      de,7
        add     hl,de
        ld      a,(y)
        add     a,4
        call    checkPixel
        ret     c
        ld      hl,y
        inc     (hl)
        ret

;------------------------------------------------
; moveLeft - Attempt to move player left
;
; Input:    None
; Output:   CA = 1 if couldn't move
;------------------------------------------------
moveLeft:
        ld      a,(pScreenX)
        or      a
        ret     z
        ld      hl,(x)
        dec     hl
        ld      a,(y)
        push    hl
        call    checkPixel
        pop     hl
        ret     c
        ld      a,(y)
        add     a,3
        call    checkPixel
        ret     c
        ld      hl,(x)
        dec     hl
        ld      (x),hl
        ret

;------------------------------------------------
; moveRight - Attempt to move player right
;
; Input:    None
; Output:   CA = 1 if couldn't move
;------------------------------------------------
moveRight:
        ld      a,(pScreenX)
        cp      96-8
        ret     z
        ld      hl,(x)
        ld      a,(y)
        ld      de,8
        add     hl,de
        push    hl
        call    checkPixel
        pop     hl
        ret     c
        ld      a,(y)
        add     a,3
        call    checkPixel
        ret     c
        ld      hl,(x)
        inc     hl
        ld      (x),hl
        or      a
        ret

;------------------------------------------------
; getPlayerScreenX - Get X coord of player relative to the left side of the screen
;
; Input:    None
; Output:   A = Screen X Coord
;------------------------------------------------
getPlayerScreenX:
        ld      hl,(x)
        ld      de,(xScr)
        or      a
        sbc     hl,de
        ld      a,l
        ret

;------------------------------------------------
; incBeamPower - Increment beam power
;
; Input:    None
; Output:   None
;------------------------------------------------
incBeamPower:
        ld      hl,beam
        ld      a,(hl)
        cp      63
        ret     nc
        inc     (hl)
        ret

;------------------------------------------------
; playerDead - Do stuff for when a player is dead
;
; Input:    None
; Output:   None
;------------------------------------------------
playerDead:
        ld      a,(xBlock)
        ld      hl,cp3
        ld      b,3
checkCheckPoints:
        cp      (hl)
        ld      c,(hl)
        jr      nc,foundCheckPoint
        dec     hl
        djnz    checkCheckPoints
        ld      c,0
foundCheckPoint:
        ld      a,c
setCheckPoint:
        ld      (cpX),a
        ld      hl,lives
        dec     (hl)
        bit     7,(hl)
        jp      z,newLife
        inc     (hl)
        jp      gameOver

;------------------------------------------------
; drawPlayer - Draw the player
;
; Input:    None
; Output:   None
;------------------------------------------------
drawPlayer:
        ld      a,(pScreenX)
        ld      bc,(y)
        ld      b,a
        ld      (playerBG),bc
        ld      a,(dead)
        or      a
        jr      nz,drawPlayerDead
        ld      hl,sprPlayer
        ld      de,playerBG+2
        jp      PutSprite_MSB
drawPlayerDead:
        sub     INI_DEAD-20
        jr      nc,drawPlayerNoSet0
        xor     a
drawPlayerNoSet0:
        srl     a
        srl     a
        ld      l,a
        ld      h,0
        add     hl,hl
        add     hl,hl
        add     hl,hl
        add     hl,hl
        ld      de,sprExplosion0
        add     hl,de
        ld      de,playerBG+2
        jp      PutSprite_MSB

;------------------------------------------------
; erasePlayer - Erase the player
;
; Input:    None
; Output:   None
;------------------------------------------------
erasePlayer:
        ld      bc,(playerBG)
        ld      hl,playerBG+2
        jp      PutSprite

;------------------------------------------------
; updateScore - Add a value to player's score and show new value
;
; Input:    DE = Value to add to score
; Output:   None
;------------------------------------------------
updateScore:
        ld      hl,(score)
        add     hl,de
        ld      (score),hl

;------------------------------------------------
; showScore - Show player score
;
; Input:    None
; Output:   None
;------------------------------------------------
showScore:
        ld      hl,57*256+31
        ld      (pencol),hl
        ld      hl,(score)
        ld      bc,5*256+1
        call    showHL
        bcall(_vputs)
        ret

;------------------------------------------------
; storePlayerInCollideObject1 - Store player data in collideObject1
;
; Input:    None
; Output:   None
;------------------------------------------------
storePlayerInCollideObject1:
        ld      a,(pScreenX)
        ld      hl,collideObject1
        ld      (hl),a
        inc     hl
        ld      (hl),8
        inc     hl
        ld      a,(y)
        ld      (hl),a
        inc     hl
        ld      (hl),4
        ret

.end
