Improved All-Purpose Move Towards Player Action [4.5.9][Platformer]

crazygrouptrio

Active member
I've spent the past few days working in terms of monsters movement depending on the player and I combined them all into one action. It fixes a lot of things that are troublesome with NESmaker core scripts so I wanted to share. Ideally this script is for platformer modules but could be used in others I'm sure.

What this Action does:
8 Directional Movement Towards Player
Increased Speed when Moving Towards in Player in 8 directions (Optional)
2 Directional Movement Towards Player
Handles Functional Recoil (LR Only)
MovePlayerLR.gifMovePlayer8Dir.gifMovePlayerRecoil.gif
Move Towards Player LR--------- Move Towards Player 8 Direction --------- Recoil Away from Player

All using the same Action. Neat huh?

First we need to do a few things. Go to your Zero Page Ram tab and add:
This is used so pieces of the script can be handled differently without rewriting it all.

Next, if you plan to use this for recoil, track down where recoil is handled in the physics script (around line 170 for me) and remove it however you deem fit. You can use an Action Step Flag, skip the code, or just gut it out (Unless you need it for the player. This will only recoil the monster). We just don't want both recoil systems trying to trigger at the same time.

Now here is the action script:
Code:
LDA Object_type,x
    CMP #22 ; # of monster
    BEQ move8Directions
; add 8 directional monsters here
    JMP move2Directions

move8Directions:
    TYA
    PHA
    TXA
    PHA
    STA tempx
 
        LDA Object_x_hi,x
        STA tempA
        LDA Object_y_hi,x
        STA tempB
        LDX player1_object
        LDA Object_x_hi,x
        STA tempC
        LDA Object_y_hi,x
        STA tempD
     
        LDX tempx
     
    MoveTowardsPoint tempA, tempC, tempB, tempD
        LDA Object_direction,x
        AND #%00000111
        ORA #%00001000
        STA Object_direction,x
    ; do you want the monster to move faster when they approach player? add them here
            LDA Object_type,x
            CMP #22 ; monsters you want to move faster
            BEQ speedUp
            JMP ++
 
    speedUp:
        LDA tempA
        STA Object_h_speed_lo,x
        LDA #$01
        STA Object_h_speed_hi,x
        LDA tempB
        STA Object_v_speed_lo,x
        LDA #$01
        STA Object_v_speed_hi,x
    ++
    LDA tempCGT
    BNE +
    PLA
    TAX
    PLA
    TAY
    +
RTS
 
move2Directions:
    TXA
    STA temp
    GetActionStep temp
    CMP #$07
    BNE +notHurt
        LDA #$01
        STA tempCGT ; if we are recoiling we need to ignore some parts of script
        JMP +doManualRecoil
        ; this monster is hurt so we are going to make him recoil in L or R direction
    +notHurt
 
    LDY player1_object
    LDA #$00
    STA temp
    LDA Object_x_hi,y
    CMP Object_x_hi,x
    BCC moveLeft
    moveRight:
        LDA #%000000010
        STA Object_direction,x
        TAY
        LDA DirectionTableOrdered,y
        STA temp
        TYA ;; the 0-7 value for direction
        ORA temp
        STA Object_direction,x
        JMP +
    moveLeft:
        LDA #%000000110
        STA Object_direction,x
        TAY
        LDA DirectionTableOrdered,y
        STA temp
        TYA ;; the 0-7 value for direction
        ORA temp
        STA Object_direction,x
+
    LDA tempCGT
    BEQ +
        TXA
        LDA Object_x_hi,x
        STA tempA
        LDA Object_y_hi,x
        STA tempB
        JSR speedUp
            LDA #$00
            STA tempCGT
    +
RTS

+doManualRecoil

LDY player1_object
    LDA #$00
    STA temp
    LDA Object_x_hi,y
    CMP Object_x_hi,x
    BCC moveRight
    JMP moveLeft

    RTS
Save this as an ASM in a new file and add it to your AI Actions wherever you want and label it. I've added notes that explain how it works in the script, short version is it checks the monster number up top if you want it to move in 8 directions, and defaults to 2 directions if no monster is checked (the example in the script is Monster #22). Increased speed works the same, checks a monster if you want to increase speed, if monster isn't listed it won't.

If you're using this for recoil, set the Hurt step (7) to this action and it will recoil away when its hurt and increase its speed. This functions differently than the default recoil, in that it determines where the player is, and just moves in the opposite direction with increased speed that I've set, rather than the default which is a bit more complex. So if you have any issues, that's probably why.

Enjoy!
 
Last edited:

SciNEStist

Active member
was noticing some weird behaviour here, and I figured out that the player standing on a different screen than the monster in a scrolling game was messing up the monster direction . So before loading the x coord, its a good idea to check if the screen is the same or not. so add under the "LDY player1_object" line with all this:
Code:
 LDY player1_object
;;NEW STUFF HERE
 LDA Object_screen, y ; load the players screen
 CMP Object_screen, x ; load the monsters screen
 BEQ +checkxcoords ; same screen, move down to check the coords
 BCC moveLeft ; player is on lower screen (so is more left)
 JMP moveRight ; if hes not on the same or the lower screen, must be on the higher screen (right)
 +checkxcoords
 

DarthAT

Member
Love this script; however, it is not quite working for me. The first line of the script compares #22 (which you deem the # of monster). How is this being determined?

LDA Object_type,x
CMP #22 ; # of monster
BEQ move8Directions
; add 8 directional monsters here
JMP move2Directions

Thanks.
 

SciNEStist

Active member
a monster's number is determined in the order in which they show up in nesmaker from top to bottom. numbers 0-15 are under game objects, and then under monster graphics bank >graphics bank 0>monsters> is # 16-whatever, then the list is continued under bank 1

for example, the 3rd monster under monster bank 0 would be #18, the 4th would be #19
 

DarthAT

Member
Got it!!!! Thank you for that info!

So, based on that information, this scrip will only apply to 1 monster type?

Edit:
I saw in the code that we just add (up to 8 more) monsters in the begging code.
 
Last edited:

SciNEStist

Active member
you can add more than 8, the part at the top is "8-direction" flying monsters are added here, as opposed to ground based "2 direction" monsters.

you would add more like this for example:
Rich (BB code):
CMP #22 ; # of monster
BEQ move8Directions
CMP #18 ; # of monster
BEQ move8Directions
CMP #25 ; # of monster
BEQ move8Directions

if you use this script on an action step of any monsters you don't add to the top, it will assume that monster is a ground based monster
 
Top Bottom