;-----------------------------------------------------------;
;                                                           ;
; Invaded                                                   ;
; Version 1.0                                               ;
; Orb Routines                                              ;
;                                                           ;
;-----------------------------------------------------------;

;------------------------------------------------
; drawOrb - Draw the Orb (if it's on screen)
;
; Input:    None
; Output:   None
;------------------------------------------------
drawOrb:
        ld      a,(orb)
        or      a
        ret     z                               ; If it's not on screen, leave
        ld      bc,(orbY)
        ld      (orbBG),bc
        ld      de,orbBG+2
        ld      hl,sprOrb
        jp      PutSprite_MSB

;------------------------------------------------
; eraseOrb - Erase the Orb (it it's on screen)
;
; Input:    None
; Output:   None
;------------------------------------------------
eraseOrb:
        ld      a,(orb)
        or      a
        ret     z                               ; If it's not on screen, leave
        ld      bc,(orbBG)
        ld      hl,orbBG+2
        jp      PutSprite

;------------------------------------------------
; tryIniOrb - Try to initialise the Orb
;
; Input:    None
; Output:   CA = 1 if successfully initialised
;------------------------------------------------
tryIniOrb:
        ld      a,(orb)
        or      a
        ret     nz
        inc     a
        ld      (orb),a
        ld      a,3*8+2
        ld      (orbY),a
        xor     a
        ld      (orbX),a
        scf
        ret

;------------------------------------------------
; moveOrb - Move the Orb (if it's on screen)
;
; Input:    None
; Output:   None
;------------------------------------------------
moveOrb:
        ld      a,(orb)
        or      a
        ret     z
        bit     3,a
        jr      nz,orbShootingOff               ; Orb is being shot off player
        bit     1,a
        jr      nz,moveOrbWithPlayer            ; Orb is attached to player
        ld      a,(frame)
        and     $01
        ret     nz                              ; If Orb not attached to player, only update every 2nd frame
        ld      a,(pScreenX)
        cp      5*8+4
        ld      b,16
        jr      nc,moveOrbX
        ld      b,96-20
moveOrbX:
        ld      hl,orbX
        ld      a,(hl)
        cp      b
        jr      z,moveOrbY
        jr      c,moveOrbRight
moveOrbLeft:
        dec     (hl)
        jr      checkOrbNowAttachedToBack
moveOrbRight:
        inc     (hl)
        jr      checkOrbNowAttachedToBack
moveOrbY:
        ld      a,(y)
        ld      hl,orbY
        sub     (hl)
        ret     z
        jr      c,moveOrbUp
moveOrbDown:
        inc     (hl)
        jr      checkOrbNowAttachedToBack
moveOrbUp:
        dec     (hl)
checkOrbNowAttachedToBack:
        ld      a,(y)
        ld      hl,orbY
        cp      (hl)
        ret     nz                              ; If Y Coords not equal, don't attach Orb
        ld      a,(pScreenX)
        ld      c,a
        sub     4
        ld      hl,orbX
        sub     (hl)
        jr      nc,coabXOK
        neg
coabXOK:
        cp      3
        jr      nc,checkOrbNowAttachedOnFront
        ld      a,(orb)
        or      $06
        ld      (orb),a
        jr      moveOrbWithPlayer
checkOrbNowAttachedOnFront:
        ld      a,c
        add     a,8
        sub     (hl)
        jr      nc,coafXOK
        neg
coafXOK:
        cp      3
        ret     nc
        ld      a,(orb)
        or      $02
        ld      (orb),a
moveOrbWithPlayer:
        bit     2,a
        ld      b,8
        jr      z,moveOrbOnFront
        ld      b,-4
moveOrbOnFront:
        ld      a,(pScreenX)
        add     a,b
        ld      (orbX),a
        ld      a,(y)
        ld      (orbY),a
        ret
orbShootingOff:
        ld      hl,orbX
        bit     2,a
        jr      nz,orbShootingOffBack
; Orb is shooting off the front
        inc     (hl)
        ld      a,(hl)
        cp      96-4
        ret     c
orbFinishedShootingOff:
        ld      a,1
        ld      (orb),a
        ret
orbShootingOffBack:
        dec     (hl)
        ld      a,(hl)
        cp      1
        ret     p
        ld      (hl),0
        jr      orbFinishedShootingOff

;------------------------------------------------
; tryShootOffOrb - Try to shoot off the Orb
;
; Input:    None
; Output:   None
;------------------------------------------------
tryShootOffOrb:
        ld      a,(orb)
        or      a
        ret     z
        or      $08                             ; Set bit 3
        and     $0D                             ; Clear bit 1
        ld      (orb),a
        ret

;------------------------------------------------
; storeOrbInCollideObject - Store Orb data in a collideObject array
;
; Input:    HL => collideObject1 or collideObject2
; Output:   None
;------------------------------------------------
storeOrbInCollideObject:
        ld      a,(orbX)
        ld      (hl),a
        inc     hl
        ld      (hl),4
        inc     hl
        ld      a,(orbY)
        ld      (hl),a
        inc     hl
        ld      (hl),4
        ret

.end
