;-----------------------------------------------------------;
;                                                           ;
; Invaded                                                   ;
; Version 1.0                                               ;
; Bullet Routines for player                                ;
;                                                           ;
;-----------------------------------------------------------;

;------------------------------------------------
; clearBulletArray - Clear bulletArray
;
; Input:    None
; Output:   None
;------------------------------------------------
clearBulletArray:
        ld      hl,bulletArray                  ; HL => Start of bulletArray
        ld      de,bulletArray+1                ; DE => Start of bulletArray+1
        ld      bc,MAX_BULLETS*4-1              ; BC = Number of bytes to clear
        ld      (hl),0                          ; Clear first byte
        ldir                                    ; And clear the rest
        ret

;------------------------------------------------
; checkSpecialNotExist - Check to make sure a special bullet isn't already on screen
;
; Input:    A = Type
;           B = X Coord (not used)
;           C = Y Coord (not used)
;           D = Direction (not used)
; Output:   A = Type
;           B = X Coord
;           C = Y Coord
;           D = Direction
;           CA = 1 if OK to create new bullet
;------------------------------------------------
checkSpecialNotExist:
        cp      6
        ret     c
        push    bc
        ld      b,MAX_BULLETS
        ld      hl,bulletArray
checkSpecialLoop:
        cp      (hl)
        jr      z,checkSpecialRetNC
        inc     hl
        inc     hl
        inc     hl
        inc     hl
        djnz    checkSpecialLoop
        pop     bc
        scf
        ret
checkSpecialRetNC:
        pop     bc
        or      a
        ret

;------------------------------------------------
; newBullet - Create a new bullet in bulletArray
;
; Input:    A = Type
;           B = X Coord
;           C = Y Coord
;           D = Direction
; Output:   None
;------------------------------------------------
newBullet:
        call    checkSpecialNotExist            ; If special weapon, make sure there isn't one on screen
        ret     nc                              ; If it does, leave
        push    af                              ; Save type
        push    bc                              ; Save coords
        push    de                              ; Save direction
        ld      hl,bulletArray                  ; HL => Start of bulletArray
        ld      b,MAX_BULLETS                   ; B = Number of entries to search
        ld      de,4
findBulletArrayEntry:
        ld      a,(hl)
        or      a                               ; Is there a bullet in this entry?
        jr      z,foundBulletArrayEntry         ; If not, we can use it :)
        add     hl,de
        djnz    findBulletArrayEntry
        pop     de
        pop     bc                              ; No empty entries, so clear stack and leave
        pop     af
        ret
foundBulletArrayEntry:
        pop     de                              ; Get direction
        pop     bc                              ; Get coords
        pop     af                              ; Get type
        ld      (hl),a                          ; Save Type
        inc     hl
        ld      (hl),b                          ; Save X Coord
        inc     hl
        ld      (hl),c                          ; Save Y Coord
        inc     hl
        ld      (hl),d                          ; Direction
        ret

;------------------------------------------------
; drawBullets - Draw/Erase bullets
;
; Input:    None
; Output:   None
;------------------------------------------------
drawBullets:
        ld      hl,bulletArray                  ; HL => Start of bulletArray
        ld      b,MAX_BULLETS                   ; B = Number of entries
drawBulletsLoop:
        push    bc
        push    hl
        ld      a,(hl)
        or      a                               ; Is there a bullet in this entry?
        jr      z,endDrawBulletsLoop            ; If not, try next entry
        ex      de,hl                           ; DE => Entry in bulletArray
        ld      l,a
        dec     l
        ld      h,0                             ; HL = Type-1
        add     hl,hl
        add     hl,hl
        add     hl,hl                           ; HL = Offset to sprite
        ex      de,hl                           ; DE = Offset to sprite, HL => Entry in bulletArray
        inc     hl
        ld      a,(hl)                          ; A = XCoord
        inc     hl
        ld      l,(hl)                          ; L = YCoord
        ld      ix,sprBullets                   ; IX => Start of bullet sprites
        add     ix,de                           ; IX => Sprite to show
#ifndef MIRAGE
        ld      b,8                             ; B = 8 rows of sprite to show
        call    ionPutSprite                    ; Draw bullet
#else
        call    putSprite8                      ; Draw bullet
#endif
endDrawBulletsLoop:
        pop     hl                              ; HL => Start of current entry
        pop     bc                              ; B = Counter
        ld      de,4
        add     hl,de
        djnz    drawBulletsLoop                 ; Repeat until finished
        ret

;------------------------------------------------
; moveBullets - Move the bullets
;
; Input:    None
; Output:   None
;------------------------------------------------
moveBullets:
        ld      hl,bulletArray
        ld      b,MAX_BULLETS
        jp      doMoveBullets

;------------------------------------------------
; checkBulletsOnScreen - Check to make sure all player bullets are still on screen
;
; Input:    None
; Output:   None
;------------------------------------------------
checkBulletsOnScreen:
        ld      hl,bulletArray
        ld      b,MAX_BULLETS
        jp      doCheckBulletsOnScreen

;------------------------------------------------
; checkBulletsHitSolid - Check to see if any player bullets are hitting a solid tile
;
; Input:    None
; Output:   None
;------------------------------------------------
checkBulletsHitSolid:
        ld      hl,bulletArray
        ld      bc,MAX_BULLETS*256+0
        ld      de,bulletCenterX
        ld      ix,bulletCenterY
        jp      doCheckBulletsHitSolid

;------------------------------------------------
; Player Bullet Types
;------------------------------------------------
; 1.  Normal bullet
; 2.  Small beam power bullet
; 3.  Large beam power bullet
; 4.  Super beam power bullet
; 5.  Orb bullet
; 6.  Power Gun bullet
; 7.  Two-Way Gun bullet 1
; 8.  Two-Way Gun bullet 2
; 9.  Vertical Gun bullet 1
; 10. Vertical Gun bullet 2
;------------------------------------------------

.end
