Read Gamepad And Follow Player AI Script

Jacotomo

Member
I was trying to make a script for my game, and came up with this. It's not really that great, but for my first script that I wrote mostly by myself, I'm pretty proud of it. I want to thank to Bucket Mouse for pointing me into the right direction!
So what this script does is make the monster follow the player based on his controller input. It can work on any module, but It works best with scrolling platformers.

So first off, you are going to duplicate the HandleObjectCollisions.asm script in Routines\Basic\System and open it.
Somewhere around line 236, you should see this.
Code:
playerWasHurtDuringCollision:	
	LDX tempx
	JMP skipThisOtherObject
otherIsNotAMonsterTypeCollision:

Underneath that, insert this bit of code. This will make the monster stop when it touches the player.
Code:
    LDA Object_flags,x ;;;;;;;;;;;;;;;;;; CUSTOM CODE STARTS HERE ;;;;;;;;;;;;;;;
	AND #%01000000 ;;; is it a target?
	BEQ otherIsNotPartner
	;; stop moving
	LDA Object_movement,x
	AND #%00000111
	STA Object_movement,x
    ChangeObjectState #$01,#$02 ;;;; change to idle state ;;;;
otherIsNotPartner:

Save it, and assign it to "Handle Object Collision" in script settings.

We will now make a completely new script, and insert this code into it. This will tell the monster what buttons are being pressed, and where to go.
Code:
	 LDA #$00
	 STA Object_movement,x
	
	 LDA #$00
	 STA temp
	
	 LDA gamepad
	 AND #%11110000 ; Is the DPad being pressed?
	 BNE didNotMove
	 JMP startMoving
	
didNotMove:

     ChangeObjectState #$01, #$02 ; change to idle state
	 
startMoving:

     LDA gamepad
	 AND #%10000000 ;;; Is right being pressed?
	 BEQ didNotGoRight
	 
	 LDA temp
	 ORA #MOVE_RIGHT
	 ORA #FACE_RIGHT
	 STA temp
	 JMP doneMoving
	 
didNotGoRight:

     LDA gamepad
	 AND #%01000000 ; Is left being pressed?
	 BEQ didNotGoLeft
	 
	 LDA temp
	 ORA #MOVE_LEFT
	 ORA #FACE_LEFT
	 STA temp
	 JMP doneMoving
	 
didNotGoLeft:

	 LDA gamepad
	 AND #%00100000 ; Is down being pressed?
	 BEQ didNotGoDown
	 
	 LDA temp
	 ORA #MOVE_DOWN
	 ORA #FACE_DOWN
	 STA temp
	 JMP doneMoving
	 
didNotGoDown:

	 LDA gamepad
	 AND #%00010000 ; Is up being pressed?
	 BEQ didNotGoUp
	 
	 LDA temp
	 ORA #MOVE_UP
	 ORA #FACE_UP
	 STA temp
	 JMP doneMoving
	 
didNotGoUp:
     
doneMoving:
	 
     LDA temp
     STA Object_movement,x	 
	 
	 LDA gamepad ;;; THIS IS FOR JUMPING ;;;; COMMENT THIS IF YOU DON'T NEED IT ;;;;;
	 AND #%00000001 ; Is A being pressed? ;;;; Change to #%00000010 if you want the B Button instead
	 BEQ didNotJump
	 
	 ChangeObjectState #$03, #$02
	 
didNotJump:  ;;;; DONE WITH JUMP CODE ;;;;

Save it, and assign it to an "AI Action" in script settings.

If you want the monster to jump when the player jumps, make a new script and paste this in, if not, just skip to the next part.
By the way, Dale Coop wrote this script, not me.
Code:
    LDA #00
	SEC
	SBC #04 ;; JUMP VALUE
	STA Object_v_speed_hi,x
	LDA Object_status,x
	AND #%11111011
	STA Object_status,x

Now make a monster, or "partner", in game objects (you could make it in monster objects, but this will be more convenient).

In monster details, set the type to "target type". Make it's speed the same as the player, and set the acceleration kind of low.

In monster actions, set step 0, 1, and 3, to this. (I made this script with the idle and jumping animations being one frame in mind. You could add more frames and extend the animation time, but it might make the monster a bit slower to respond.)

ActionsZeroOneThree.PNG

Now set up step 2 like this.

ActionTwo.PNG

If you are doing jumping, set step 4 like this. (I forgot to make a jump animation, so don't mind that)

ActionFour.PNG

Place the monster on top of the player or where the player will warp to at every new screen (This is way scrolling works best, you can't really do this on single screen games), and your new friend should follow you.

FollowScript.gif
 

Attachments

  • ActionTwo.PNG
    ActionTwo.PNG
    14.5 KB · Views: 2,356
  • ActionFour.PNG
    ActionFour.PNG
    14.2 KB · Views: 2,356

dale_coop

Moderator
Staff member
Would not work because the Xtra object needs to be set like a (2nd) player.
Here the monster is not...
But at some point you could just swap for an Xtra player type object that looks like the monster, and swap also the monster for another one that looks like the main player ;)
 
Top Bottom