;-----------------------------------------------------------;
;                                                           ;
; Invaded                                                   ;
; Version 1.0                                               ;
; Generic Bullet Routines                                   ;
;                                                           ;
;-----------------------------------------------------------;

;------------------------------------------------
; doMoveBullets - Update bullets in either bulletArray or eBulletArray
;
; Input:    HL => Start of array to update
;           B = Number of entries in array
; Output:   None
;------------------------------------------------
doMoveBullets:
        push    bc
        push    hl
        ld      a,(hl)
        or      a
        jr      z,endDoMoveBulletsLoop
        inc     hl
        inc     hl
        inc     hl
        ld      a,(hl)
        dec     hl
        dec     hl
        ex      de,hl
        ld      l,a
        ld      h,0
        add     hl,hl
        ld      bc,bulletDirections
        add     hl,bc
        ex      de,hl
        ld      a,(de)
        add     a,(hl)
        ld      (hl),a
        inc     hl
        inc     de
        ld      a,(de)
        add     a,(hl)
        ld      (hl),a
endDoMoveBulletsLoop:
        pop     hl
        pop     bc
        inc     hl
        inc     hl
        inc     hl
        inc     hl
        djnz    doMoveBullets
        ret

;------------------------------------------------
; doCheckBulletsOnScreen - Check to make sure either players or enemies bullets are on screen
;
; Input:    HL => Start of array of bullets to check
;           B = Number of entries to check
; Output:   None
;------------------------------------------------
doCheckBulletsOnScreen:
        push    bc
        ld      a,(hl)                          ; HL => Type
        or      a
        jr      z,endBulletsOnScreenLoop
        inc     hl                              ; HL => X
        ld      a,(hl)
        cp      96-8
        jr      nc,bulletNotOnScreen
        inc     hl                              ; HL => Y
        ld      a,(hl)
        cp      56-3
        dec     hl                              ; HL => X
        jr      c,bulletOnScreenDecHL
bulletNotOnScreen:
        dec     hl                              ; HL => Type
        ld      (hl),0
        jr      endBulletsOnScreenLoop
bulletOnScreenDecHL:
        dec     hl                              ; HL => Type
endBulletsOnScreenLoop:
        pop     bc
        inc     hl
        inc     hl
        inc     hl
        inc     hl                              ; HL => Next entry
        djnz    doCheckBulletsOnScreen
        ret

;------------------------------------------------
; storeBulletInCollideObject2 - Store a player bullet at (collideObject2)
;
; Input:    A = Type
;           HL => Bullet entry in bulletArray
; Output:   None
;------------------------------------------------
storeBulletInCollideObject2:
        dec     a
        inc     hl
        ex      de,hl                           ; DE => Bullet X Coord
        add     a,a
        ld      c,a
        ld      b,0                             ; BC = Offset in size table
        ld      a,(de)
        ld      (collideObject2),a              ; Save X Coord
        ld      hl,bulletSizeTable
_sbco2Size              = $-2
        add     hl,bc
        ld      a,(hl)
        ld      (collideObject2+1),a            ; Save width
        inc     de                              ; DE => Bullet Y Coord
        ld      a,(de)
        ld      (collideObject2+2),a            ; Save Y Coord
        inc     hl
        ld      a,(hl)
        ld      (collideObject2+3),a            ; Save height
        ret

;------------------------------------------------
; doCheckBulletsHitSolid - Check bullets in either bulletArray or eBulletArray to see if they're hitting solid walls
;
; Input:    HL => Start of array
;           B = Number of entries
;           C = 0 is for player bullets, C = 1 is for enemy bullets
;           DE => Table of offsets to center of X Coord of each bullet type
;           IX => Table of offsets to center of Y Coord of each bullet type
; Output:   None
;------------------------------------------------
doCheckBulletsHitSolid:
        ld      (_bulletCenterX),de
        ld      (_bulletCenterY),ix
        ld      a,$B7
        dec     c
        jr      nz,dcbhsSaveORA
        ld      a,$37
dcbhsSaveORA:
        ld      (_checkBulletsHitSolid),a
bulletsHitSolidLoop:
        push    bc
        push    hl
        ld      a,(hl)
        or      a
        jr      z,endBulletsHitSolidLoop
        or      a
_checkBulletsHitSolid   = $-1
        jr      nc,noCheckBGoThroughSolid
        cp      EBULLET_THROUGH_SOLIDS
        jr      z,endBulletsHitSolidLoop
noCheckBGoThroughSolid:
        ex      de,hl                           ; DE => Start of entry
        ld      c,a
        ld      b,0
        dec     c
        ld      hl,$0000
_bulletCenterX          = $-2
        add     hl,bc
        inc     de                              ; DE => X Coord
        ld      a,(de)
        add     a,(hl)
        ld      hl,$0000
_bulletCenterY          = $-2
        add     hl,bc
        ld      c,a                             ; C = X Coord on screen to check
        inc     de                              ; DE => Y Coord
        ld      a,(de)
        add     a,(hl)                          ; A = Y Coord to check
        ld      hl,(xScr)
        add     hl,bc                           ; HL = X Coord to check
        call    getTileAtExactCoords
        ld      hl,solidTiles
        cp      (hl)
        jr      c,endBulletsHitSolidLoop
        pop     hl
        push    hl
        ld      (hl),0
endBulletsHitSolidLoop:
        pop     hl
        pop     bc
        inc     hl
        inc     hl
        inc     hl
        inc     hl
        djnz    bulletsHitSolidLoop
        ret

.end
