;---------------------------------------------------------------;
;                                                               ;
; Snail Maze                                                    ;
; Movement routines                                             ;
;                                                               ;
;---------------------------------------------------------------;

;------------------------------------------------
; moveUp - try to move up
;   input:  none
;   output: none
;------------------------------------------------
moveUp:
        ld      hl,(sy)
        ld      de,(sx)
        call    getTile                         ; A = tile # that snail is currently on
        bit     0,a                             ; is it 1 or 3?
        jp      nz,afterDpad                    ; if so, can't move up
        ld      hl,sy
        ld      a,(hl)
        or      a                               ; at top of maze?
        jp      z,afterDpad                     ; if so, can't move up
        dec     (hl)                            ; update y
        xor     a
        ld      (dir),a                         ; set direction
        ld      a,MOVE_COUNT_INI
        ld      (moveCount),a
        jp      afterDpad

;------------------------------------------------
; moveDown - try to move down
;   input:  none
;   output: none
;------------------------------------------------
moveDown:
        ld      hl,(sy)
        inc     hl
        ld      de,(sx)
        call    getTile                         ; A = tile # below snail
        bit     0,a                             ; is it 1 or 3?
        jp      nz,afterDpad                    ; if so, can't move down
        ld      hl,sy
        ld      a,(hl)
        cp      MAZE_HEIGHT-1                   ; at bottom of maze?
        jp      z,afterDpad
        inc     (hl)                            ; update y
        ld      a,1
        ld      (dir),a                         ; set direction
        ld      a,MOVE_COUNT_INI
        ld      (moveCount),a
        jp      afterDpad

;------------------------------------------------
; moveLeft - try to move left
;   input:  none
;   output: none
;------------------------------------------------
moveLeft:
        ld      hl,(sy)
        ld      de,(sx)
        call    getTile                         ; A = tile # that snail is currently on
        cp      2                               ; is it 2 or 3?
        jp      nc,afterDpad                    ; if so, can't move left
        ld      hl,sx
        ld      a,(hl)
        or      a
        jp      z,afterDpad
        dec     (hl)                            ; update x
        ld      a,2
        ld      (dir),a                         ; set direction
        ld      a,MOVE_COUNT_INI
        ld      (moveCount),a
        jp      afterDpad

;------------------------------------------------
; moveRight - try to move right
;   input:  none
;   output: none
;------------------------------------------------
moveRight:
        ld      hl,(sy)
        ld      de,(sx)
        inc     de
        call    getTile                         ; A = tile # to the right of snail
        cp      2                               ; is it 2 or 3?
        jp      nc,afterDpad                    ; if so, can't move right
        ld      hl,sx
        ld      a,(hl)
        cp      MAZE_WIDTH-1
        jp      z,afterDpad
        inc     (hl)                            ; update x
        ld      a,3
        ld      (dir),a                         ; set direction
        ld      a,MOVE_COUNT_INI
        ld      (moveCount),a
        jp      afterDpad

;------------------------------------------------
; continueMove - continue a move until aligned with a cell
;   input:  none
;   output: none
;------------------------------------------------
continueMove:
        ld      hl,moveCount
        dec     (hl)
        dec     (hl)
        jp      afterDpad

.end