;=================================================================
; [coll.inc] -- Collision detection and handling code
;=================================================================


CHECK_HITDETECT:
    lda ENM_hitDetectTime
    cmp #$08
    beq CH_ok
    jmp CH_chk_allDone
CH_ok
    stz ENM_hitDetectTime

    lda shipInvulnerable
    cmp #$00
    bne CH_chk_allDone
;   jmp CH_doThemBiatch
;CH_symlink_allDone
;    jmp CH_chk_allDone

;CH_doThemBiatch
    lda ENM_13_dead
    cmp #$01
    beq CH_chk14
    lda ENM_13_alive            ; is the enemy alive?
    cmp #$00
    beq CH_chk14                ; no! branch to the next one
    lda SPRBUF1+SX13
    sta hd_cx
    lda SPRBUF1+SY13
    sta hd_cy
    jsr TEST_COLLISION
CH_chk14
    lda ENM_14_dead
    cmp #$01
    beq CH_chk15
    lda ENM_14_alive
    cmp #$00
    beq CH_chk15
    lda SPRBUF1+SX14
    sta hd_cx
    lda SPRBUF1+SY14
    sta hd_cy
    jsr TEST_COLLISION
CH_chk15
    lda ENM_15_dead
    cmp #$01
    beq CH_chk16
    lda ENM_15_alive
    cmp #$00
    beq CH_chk16
    lda SPRBUF1+SX15
    sta hd_cx
    lda SPRBUF1+SY15
    sta hd_cy
    jsr TEST_COLLISION
CH_chk16
    lda ENM_16_dead
    cmp #$01
    beq CH_chk17
    lda ENM_16_alive
    cmp #$00
    beq CH_chk17
    lda SPRBUF1+SX16
    sta hd_cx
    lda SPRBUF1+SY16
    sta hd_cy
    jsr TEST_COLLISION
CH_chk17
    lda ENM_17_dead
    cmp #$01
    beq CH_chk_allDone
    lda ENM_17_alive
    cmp #$00
    beq CH_chk_allDone
    lda SPRBUF1+SX17
    sta hd_cx
    lda SPRBUF1+SY17
    sta hd_cy
    jsr TEST_COLLISION
CH_chk_allDone
    rts



CHECK_BULLY_DETECT:
    inc ENM_hitDetectTime
    lda ENM_hitDetectTime
    cmp #$07
    beq CHB_ok
    jmp CHB_chk_allDone
CHB_ok

    lda BUL_21_alive
    cmp #$00
    beq CHB_chk_allDone_sym
    jmp CHB_doItMan
CHB_chk_allDone_sym
    jmp CHB_chk_allDone

CHB_doItMan
    lda ENM_13_dead
    cmp #$01
    beq CHB_chk14
    lda ENM_13_alive            ; is the enemy alive?
    cmp #$00
    beq CHB_chk14                ; no! branch to the next one
    lda SPRBUF1+SX13
    sta hd_cx
    lda SPRBUF1+SY13
    sta hd_cy
    jsr TEST_BULLY_COLLISION
    lda ENM_bulletHitENM
    cmp #$01
    bne CHB_chk14
    lda #$01
    sta ENM_13_dead
    inc ENM_killCounter
    ldy #$0005
    sty score_10
    ldy #$0000
    sty score_1
    jsl ADD_SCORE
    jsl bH13_doThe50p_ENM13

    lda SPRBUF1+SPRIORITY13
    sta SPRBUF1+SPRIORITY27
    lda SPRBUF1+SX13
    sta SPRBUF1+SX27
    lda SPRBUF1+SY13
    sta SPRBUF1+SY27
    lda SPRBUF1+SFRAME13
    sta SPRBUF1+SFRAME27
    lda #$30
    sta ENM_18_hp              ; *ENM_13_*blinkHowLong*
    lda #$01
    sta ENM_13_hp              ; *ENM_13_*blinkState*

CHB_chk14
    lda ENM_14_dead
    cmp #$01
    beq CHB_chk15
    lda ENM_14_alive
    cmp #$00
    beq CHB_chk15
    lda SPRBUF1+SX14
    sta hd_cx
    lda SPRBUF1+SY14
    sta hd_cy
    jsr TEST_BULLY_COLLISION
    lda ENM_bulletHitENM
    cmp #$01
    bne CHB_chk15
    lda #$01
    sta ENM_14_dead
    inc ENM_killCounter
    ldy #$0005
    sty score_10
    ldy #$0000
    sty score_1
    jsl ADD_SCORE
    jsl bH13_doThe50p_ENM14

    lda SPRBUF1+SPRIORITY14
    sta SPRBUF1+SPRIORITY28
    lda SPRBUF1+SX14
    sta SPRBUF1+SX28
    lda SPRBUF1+SY14
    sta SPRBUF1+SY28
    lda SPRBUF1+SFRAME14
    sta SPRBUF1+SFRAME28
    lda #$30
    sta ENM_13_magica          ; *ENM_14_*blinkHowLong*
    lda #$01
    sta ENM_14_hp              ; *ENM_14_*blinkState*

CHB_chk15
    lda ENM_15_dead
    cmp #$01
    beq CHB_chk16
    lda ENM_15_alive
    cmp #$00
    beq CHB_chk16
    lda SPRBUF1+SX15
    sta hd_cx
    lda SPRBUF1+SY15
    sta hd_cy
    jsr TEST_BULLY_COLLISION
    lda ENM_bulletHitENM
    cmp #$01
    bne CHB_chk16
    lda #$01
    sta ENM_15_dead
    inc ENM_killCounter
    ldy #$0005
    sty score_10
    ldy #$0000
    sty score_1
    jsl ADD_SCORE
    jsl bH13_doThe50p_ENM15

    lda SPRBUF1+SPRIORITY15
    sta SPRBUF1+SPRIORITY29
    lda SPRBUF1+SX15
    sta SPRBUF1+SX29
    lda SPRBUF1+SY15
    sta SPRBUF1+SY29
    lda SPRBUF1+SFRAME15
    sta SPRBUF1+SFRAME29
    lda #$30
    sta ENM_14_magica          ; *ENM_15_*blinkHowLong*
    lda #$01
    sta ENM_15_hp              ; *ENM_15_*blinkState*

CHB_chk16
    lda ENM_16_dead
    cmp #$01
    beq CHB_chk17
    lda ENM_16_alive
    cmp #$00
    beq CHB_chk17
    lda SPRBUF1+SX16
    sta hd_cx
    lda SPRBUF1+SY16
    sta hd_cy
    jsr TEST_BULLY_COLLISION
    lda ENM_bulletHitENM
    cmp #$01
    bne CHB_chk17
    lda #$01
    sta ENM_16_dead
    inc ENM_killCounter
    ldy #$0005
    sty score_10
    ldy #$0000
    sty score_1
    jsl ADD_SCORE
    jsl bH13_doThe50p_ENM16

    lda SPRBUF1+SPRIORITY16
    sta SPRBUF1+SPRIORITY30
    lda SPRBUF1+SX16
    sta SPRBUF1+SX30
    lda SPRBUF1+SY16
    sta SPRBUF1+SY30
    lda SPRBUF1+SFRAME16
    sta SPRBUF1+SFRAME30
    lda #$30
    sta ENM_15_magica          ; *ENM_16_*blinkHowLong*
    lda #$01
    sta ENM_16_hp              ; *ENM_16_*blinkState*

CHB_chk17
    lda ENM_17_dead
    cmp #$01
    beq CHB_chk_allDone
    lda ENM_17_alive
    cmp #$00
    beq CHB_chk_allDone
    lda SPRBUF1+SX17
    sta hd_cx
    lda SPRBUF1+SY17
    sta hd_cy
    jsr TEST_BULLY_COLLISION
    lda ENM_bulletHitENM
    cmp #$01
    bne CHB_chk_allDone
    lda #$01
    sta ENM_17_dead
    inc ENM_killCounter
    ldy #$0005
    sty score_10
    ldy #$0000
    sty score_1
    jsl ADD_SCORE
    jsl bH13_doThe50p_ENM17

    lda SPRBUF1+SPRIORITY17
    sta SPRBUF1+SPRIORITY31
    lda SPRBUF1+SX17
    sta SPRBUF1+SX31
    lda SPRBUF1+SY17
    sta SPRBUF1+SY31
    lda SPRBUF1+SFRAME17
    sta SPRBUF1+SFRAME31
    lda #$30
    sta ENM_16_magica          ; *ENM_17_*blinkHowLong*
    lda #$01
    sta ENM_17_hp              ; *ENM_17_*blinkState*

CHB_chk_allDone
    rts

;================================

; ok! The logic is simple:
;
; if((A->x>B->x+B->spr->w-1) || (B->x>A->x+A->spr->w-1) ||
;    (A->y>B->y+B->spr->h-1) || (B->y>A->y+A->spr->h-1))
;  return FALSE; else return TRUE;
;
; .. even though the solution for this game is a tad
; different (practically!!)
;--
; this code checks if one or more of angela's corners
; (one corner at a time) is located somewhere between
; enemy's x,y,x2,y2.  Obviously, this solution only
; works one way, but frankly, that's enough for this game!

TEST_COLLISION:
    phx
    lda hd_cx
    jsr tc_add32
    lda a_byte
    sta hd_cx2
    lda hd_cy
    jsr tc_add32
    lda a_byte
    sta hd_cy2

    lda PLY_SHIPX
    jsr tc_add32
    lda a_byte
    sta hd_px2
    lda PLY_SHIPY
    jsr tc_add32
    lda a_byte
    sta hd_py2

    lda PLY_SHIPY
    jsr tc_yadd8
    lda a_byte
    sta PLY_SHIPY
    lda hd_py2
    jsr tc_ysub9
    lda a_byte
    sta hd_py2

    plx
    jmp tc_00
tc_yadd8                        ; PLY_SHIPY + 8
    sta a_byte
    ldx #$08
tc_yaddloop
    inc a_byte
    dex
    cpx #$0000
    bne tc_yaddloop
    rts
tc_ysub9                        ; hd_py2 - 9
    sta a_byte
    ldx #$09
tc_ysubloop
    dec a_byte
    dex
    cpx #$0000
    bne tc_ysubloop
    rts
tc_add32                        ; e.g: hd_px2 = (PLY_SHIPX+32)
    sta a_byte
    ldx #$20
tc_addloop
    inc a_byte
    dex
    cpx #$0000
    bne tc_addloop
    rts
tc_00
    stz hd_coll
    stz hd_colc                 ; to be inc'ed if suspicious, yeah!
tc_01                           ; is upper left corner colliding?
    lda PLY_SHIPX               ; ul x-wise
    cmp hd_cx
    bpl tc_01a
    jmp tc_02
tc_01a
    inc hd_colc
tc_02
    lda PLY_SHIPX
    cmp hd_cx2
    bmi tc_02a
    jmp tc_03
tc_02a
    inc hd_colc
tc_03
    lda PLY_SHIPY               ; ul y-wise
    cmp hd_cy
    bpl tc_03a
    jmp tc_04
tc_03a
    inc hd_colc
tc_04
    lda PLY_SHIPY
    cmp hd_cy2
    bmi tc_04a
    jmp tc_05
tc_04a
    inc hd_colc
tc_05                           ; coffee-break!
    lda hd_colc
    cmp #$04
    bne tc_05a
    inc hd_coll
tc_05a
    stz hd_colc
tc_06                           ; is upper right corner colliding?
    lda hd_px2                  ; ur x-wise
    cmp hd_cx
    bpl tc_06a
    jmp tc_07
tc_06a
    inc hd_colc
tc_07
    lda hd_px2
    cmp hd_cx2
    bmi tc_07a
    jmp tc_08
tc_07a
    inc hd_colc
tc_08
    lda PLY_SHIPY               ; ur y-wise
    cmp hd_cy
    bpl tc_08a
    jmp tc_09
tc_08a
    inc hd_colc
tc_09
    lda PLY_SHIPY
    cmp hd_cy2
    bmi tc_09a
    jmp tc_0A
tc_09a
    inc hd_colc
tc_0A                           ; it's tea-time!
    lda hd_colc
    cmp #$04
    bne tc_0Aa
    inc hd_coll
tc_0Aa
    stz hd_colc
tc_0B                           ; is lower left corner colliding?
    lda PLY_SHIPX               ; ll x-wise
    cmp hd_cx
    bpl tc_0Ba
    jmp tc_0C
tc_0Ba
    inc hd_colc
tc_0C
    lda PLY_SHIPX
    cmp hd_cx2
    bmi tc_0Ca
    jmp tc_0D
tc_0Ca
    inc hd_colc
tc_0D
    lda hd_py2                  ; ll y-wise
    cmp hd_cy
    bpl tc_0Da
    jmp tc_0E
tc_0Da
    inc hd_colc
tc_0E
    lda hd_py2
    cmp hd_cy2
    bmi tc_0Ea
    jmp tc_0F
tc_0Ea
    inc hd_colc
tc_0F                           ; it's snuff-time!! :]
    lda hd_colc
    cmp #$04
    bne tc_0Fa
    inc hd_coll
tc_0Fa
    stz hd_colc
tc_10                           ; is lower right corner colliding?
    lda hd_px2                  ; lr x-wise
    cmp hd_cx
    bpl tc_10a
    jmp tc_11
tc_10a
    inc hd_colc
tc_11
    lda hd_px2
    cmp hd_cx2
    bmi tc_11a
    jmp tc_12
tc_11a
    inc hd_colc
tc_12
    lda hd_py2                  ; lr y-wise
    cmp hd_cy
    bpl tc_12a
    jmp tc_13
tc_12a
    inc hd_colc
tc_13
    lda hd_py2
    cmp hd_cy2
    bmi tc_13a
    jmp tc_14
tc_13a
    inc hd_colc
tc_14
    lda hd_colc
    cmp #$04
    bne tc_ff
    inc hd_coll
tc_ff
    jsr tc_ysub8
    stz hd_colc
    lda hd_coll
    cmp #$00
    beq tc_ffx
;   GOTOXY 10,10
;   PRINTS "HITDETECT!"
    jsl HP_DROP
tc_ffx
    rts
tc_ysub8                        ; restore original PLY_SHIPY
    lda PLY_SHIPY
    sta a_byte
    ldx #$08
tc_ysub8loop
    dec a_byte
    dex
    cpx #$0000
    bne tc_ysub8loop
    lda a_byte
    sta PLY_SHIPY
    rts


;================================


TEST_BULLY_COLLISION:
    stz ENM_bulletHitENM

    phx
    lda hd_cx
    jsr tcb_add32
    lda a_byte
    sta hd_cx2
    lda hd_cy
    jsr tcb_add32
    lda a_byte
    sta hd_cy2

    lda SPRBUF1+SX21
    jsr tcb_add32
    lda a_byte
    sta hd_px2
    lda SPRBUF1+SY21
    jsr tcb_add32
    lda a_byte
    sta hd_py2

    lda SPRBUF1+SY21
    jsr tcb_yadd8
    lda a_byte
    sta SPRBUF1+SY21
    lda hd_py2
    jsr tcb_ysub9
    lda a_byte
    sta hd_py2

    plx
    jmp tcb_00
tcb_yadd8                       ; SPRBUF1+SY21 + 8
    sta a_byte
    ldx #$08
tcb_yaddloop
    inc a_byte
    dex
    cpx #$0000
    bne tcb_yaddloop
    rts
tcb_ysub9                       ; hd_py2 - 9
    sta a_byte
    ldx #$09
tcb_ysubloop
    dec a_byte
    dex
    cpx #$0000
    bne tcb_ysubloop
    rts
tcb_add32                       ; e.g: hd_px2 = (SPRBUF1+SX21+32)
    sta a_byte
    ldx #$20
tcb_addloop
    inc a_byte
    dex
    cpx #$0000
    bne tcb_addloop
    rts
tcb_00
    stz hd_coll
    stz hd_colc                 ; to be inc'ed if suspicious, yeah!
tcb_01                          ; is upper left corner colliding?
    lda SPRBUF1+SX21               ; ul x-wise
    cmp hd_cx
    bpl tcb_01a
    jmp tcb_02
tcb_01a
    inc hd_colc
tcb_02
    lda SPRBUF1+SX21
    cmp hd_cx2
    bmi tcb_02a
    jmp tcb_03
tcb_02a
    inc hd_colc
tcb_03
    lda SPRBUF1+SY21               ; ul y-wise
    cmp hd_cy
    bpl tcb_03a
    jmp tcb_04
tcb_03a
    inc hd_colc
tcb_04
    lda SPRBUF1+SY21
    cmp hd_cy2
    bmi tcb_04a
    jmp tcb_05
tcb_04a
    inc hd_colc
tcb_05                          ; coffee-break!
    lda hd_colc
    cmp #$04
    bne tcb_05a
    inc hd_coll
tcb_05a
    stz hd_colc
tcb_06                          ; is upper right corner colliding?
    lda hd_px2                  ; ur x-wise
    cmp hd_cx
    bpl tcb_06a
    jmp tcb_07
tcb_06a
    inc hd_colc
tcb_07
    lda hd_px2
    cmp hd_cx2
    bmi tcb_07a
    jmp tcb_08
tcb_07a
    inc hd_colc
tcb_08
    lda SPRBUF1+SY21               ; ur y-wise
    cmp hd_cy
    bpl tcb_08a
    jmp tcb_09
tcb_08a
    inc hd_colc
tcb_09
    lda SPRBUF1+SY21
    cmp hd_cy2
    bmi tcb_09a
    jmp tcb_0A
tcb_09a
    inc hd_colc
tcb_0A                          ; it's tea-time!
    lda hd_colc
    cmp #$04
    bne tcb_0Aa
    inc hd_coll
tcb_0Aa
    stz hd_colc
tcb_0B                          ; is lower left corner colliding?
    lda SPRBUF1+SX21               ; ll x-wise
    cmp hd_cx
    bpl tcb_0Ba
    jmp tcb_0C
tcb_0Ba
    inc hd_colc
tcb_0C
    lda SPRBUF1+SX21
    cmp hd_cx2
    bmi tcb_0Ca
    jmp tcb_0D
tcb_0Ca
    inc hd_colc
tcb_0D
    lda hd_py2                  ; ll y-wise
    cmp hd_cy
    bpl tcb_0Da
    jmp tcb_0E
tcb_0Da
    inc hd_colc
tcb_0E
    lda hd_py2
    cmp hd_cy2
    bmi tcb_0Ea
    jmp tcb_0F
tcb_0Ea
    inc hd_colc
tcb_0F                          ; it's snuff-time!! :]
    lda hd_colc
    cmp #$04
    bne tcb_0Fa
    inc hd_coll
tcb_0Fa
    stz hd_colc
tcb_10                          ; is lower right corner colliding?
    lda hd_px2                  ; lr x-wise
    cmp hd_cx
    bpl tcb_10a
    jmp tcb_11
tcb_10a
    inc hd_colc
tcb_11
    lda hd_px2
    cmp hd_cx2
    bmi tcb_11a
    jmp tcb_12
tcb_11a
    inc hd_colc
tcb_12
    lda hd_py2                  ; lr y-wise
    cmp hd_cy
    bpl tcb_12a
    jmp tcb_13
tcb_12a
    inc hd_colc
tcb_13
    lda hd_py2
    cmp hd_cy2
    bmi tcb_13a
    jmp tcb_14
tcb_13a
    inc hd_colc
tcb_14
    lda hd_colc
    cmp #$04
    bne tcb_ff
    inc hd_coll
tcb_ff
    jsr tcb_ysub8
    stz hd_colc
    lda hd_coll
    cmp #$00
    beq tcb_ffx
;   GOTOXY 10,10
;   PRINTS "HITDETECT!"
    inc ENM_bulletHitENM
    lda #V0SFX_GLASS_SMASH      ; play random soundeffect
    sta V0SFX
    jsr AUDIO_PLAY_V0SFX
    stz BUL_21_alive            ; kill bullet
    stz BUL_21_time
    lda #$8c
    sta CURFRAME21
tcb_ffx
    rts
tcb_ysub8                       ; restore original SPRBUF1+SY21
    lda SPRBUF1+SY21
    sta a_byte
    ldx #$08
tcb_ysub8loop
    dec a_byte
    dex
    cpx #$0000
    bne tcb_ysub8loop
    lda a_byte
    sta SPRBUF1+SY21
    rts
