Proximity subroutine [4.5.6]

Hello guys! I made a similar but updated version of my proximity trigger script for 4.5.6

This comes in subroutine form, so that it can be called from wherever you want to store it and keep your monster AI clean.
Can be easily modified to use as part of an AI script directly if you don't plan to re-use it between monsters.
Customize to your preference using the 3 constants at the top.

Code:
sub_checkPlrProximity: ;==============================================================================================================================================
;; Check the proximity of the player to the current object in X.
;; If proximity is triggered, subroutine should exit with #$01 in tempA
PROXTRIGGERDISTANCE = $4F ;; How far (vertical or horizontal) to trigger if player enters.
PROXPLROFFX = $0C ;; Offset to correctly target player's hitbox
PROXPLROFFY = $1B ;; Offset to correctly target player's hitbox

TXA ;; Store X in stack
PHA

LDA Object_x_hi,x ;; Get this object's origin location
STA tempA
LDA Object_y_hi,x
STA tempB

LDX player1_object ;; Get the player's origin location
LDA Object_x_hi,x
CLC
ADC #PROXPLROFFX
STA tempC
LDA Object_y_hi,x
CLC
ADC #PROXPLROFFY
STA tempD

;; Find H-Direction of player
LDA tempA
CMP tempC
BCC +plrToRight
JMP +plrToLeft

+plrToRight:
LDA tempC
SEC
SBC tempA
CLC
CMP #PROXTRIGGERDISTANCE
BCC +
LDA #$00 ;; replace tempA and tempB with false flag because outside of horizontal proximity.
STA tempA
STA tempB
JMP +done_checkPlrProximity
+:
LDA #$01 ;; replace tempA with a true flag for being in horizontal proximity.
STA tempA
JMP +checkVDirection

+plrToLeft:
LDA tempA
SEC
SBC tempC
CLC
CMP #PROXTRIGGERDISTANCE
BCC +
LDA #$00 ;; replace tempA and tempB with false flag because outside of horizontal proximity.
STA tempA
STA tempB
JMP +done_checkPlrProximity
+:
LDA #$01 ;; replace tempA with a true flag for being in horizontal proximity.
STA tempA
JMP +checkVDirection

+checkVDirection:
LDA tempB
CMP tempD
BCC +plrBelow
JMP +plrAbove

+plrBelow:
LDA tempD
SEC
SBC tempB
CLC
CMP #PROXTRIGGERDISTANCE
BCC +
LDA #$00 ;; replace tempA and tempB with false flag because outside of horizontal proximity.
STA tempA
STA tempB
JMP +done_checkPlrProximity
+:
LDA #$01 ;; replace tempB with a true flag for being in horizontal proximity.
STA tempB
JMP +done_checkPlrProximity

+plrAbove:
LDA tempB
SEC
SBC tempD
CLC
CMP #PROXTRIGGERDISTANCE
BCC +
LDA #$00 ;; replace tempA and tempB with false flag because outside of horizontal proximity.
STA tempA
STA tempB
JMP +done_checkPlrProximity
+:
LDA #$01 ;; replace tempB with a true flag for being in horizontal proximity.
STA tempB
;JMP +done_checkPlrProximity

+done_checkPlrProximity:
PLA	;; Restore X from stack
TAX 

RTS
 
This seems really useful.
I picture an egg that hatches a monster when you get too close, or an invisible monster that lashes out when you get near the water.
Maybe birds that fly off the screen when you walk by or an NPC that you have to chase from screen to screen.

What does it need to be used as an AI? I made a file with this block as is and pointed as AI it but it isn't working correctly.
 
Top Bottom