Following one object (sprite) after another (Tracking)

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
sdm
Posts: 306
Joined: Tue Apr 11, 2006 4:08 am
Location: Poland

Following one object (sprite) after another (Tracking)

Post by sdm » Sun Dec 29, 2019 7:28 am

I have a problem with the object 'vibrating' when two are on one Y / X line (caused by the change of opposite directions of movement per frame). Sometimes I use a delay that slightly masks the problem. I don't know any other ways, so I would love to improve it somehow (in many commercial games I also noticed the "vibrations" of similar objects as they follow the player).


Follow1.nes:

Code: Select all

;---------------------

ENEMY1_Loop:

	JSR ENEMY1_Loop_UD
	JSR ENEMY1_Loop_LR

	RTS

;---------------------

ENEMY1_Loop_UD:

	LDA HERO_1_Y
	CMP ENEMY1_1_Y
	BCC ENEMY1_Loop_MoveUp
	BCS ENEMY1_Loop_MoveDown

	RTS

;---------------------

ENEMY1_Loop_MoveUp:

	DEC ENEMY1_1_Y

	RTS

;---------------------

ENEMY1_Loop_MoveDown:

	INC ENEMY1_1_Y

	RTS

;---------------------

ENEMY1_Loop_LR:

	LDA HERO_1_X
	CMP ENEMY1_1_X
	BCC ENEMY1_Loop_MoveLeft
	BCS ENEMY1_Loop_MoveRight
	RTS

;---------------------

ENEMY1_Loop_MoveLeft:

	DEC ENEMY1_1_X

	RTS

;---------------------

ENEMY1_Loop_MoveRight:

	INC ENEMY1_1_X

	RTS
Follow2.nes:

Code: Select all

;---------------------

ENEMY1_Loop:

	JSR ENEMY1_Loop_UD
	JSR ENEMY1_Loop_LR

	RTS

;---------------------

ENEMY1_Loop_UD:

	LDA HERO_1_Y
	CLC
	ADC ENEMY1_Delay_Y
	CMP ENEMY1_1_Y
	BCC ENEMY1_Loop_MoveUp
	BCS ENEMY1_Loop_MoveDown

	RTS

;---------------------

ENEMY1_Loop_MoveUp:

	LDA #$F8
	STA ENEMY1_Delay_Y

	DEC ENEMY1_1_Y

	RTS

;---------------------

ENEMY1_Loop_MoveDown:

	LDA #$08
	STA ENEMY1_Delay_Y

	INC ENEMY1_1_Y

	RTS

;---------------------

ENEMY1_Loop_LR:

	LDA HERO_1_X
	CLC
	ADC ENEMY1_Delay_X
	CMP ENEMY1_1_X
	BCC ENEMY1_Loop_MoveLeft
	BCS ENEMY1_Loop_MoveRight
	RTS

;---------------------

ENEMY1_Loop_MoveLeft:

	LDA #$F8
	STA ENEMY1_Delay_X

	DEC ENEMY1_1_X

	RTS

;---------------------

ENEMY1_Loop_MoveRight:

	LDA #$08
	STA ENEMY1_Delay_X

	INC ENEMY1_1_X

	RTS
Attachments
follow1.nes
(128.02 KiB) Downloaded 78 times
follow2.nes
(128.02 KiB) Downloaded 71 times
Last edited by sdm on Sat Jan 04, 2020 10:32 am, edited 2 times in total.

nocash
Posts: 1211
Joined: Fri Feb 24, 2012 12:09 pm
Contact:

Re: Following one object (sprite) after another (Tracking)

Post by nocash » Sun Dec 29, 2019 7:55 am

Some random ideas...

1) Use step=0 when distance<4
2) Use step=distance/4
3) Use step=NonlinearTable[distance]

And of course, clip to max values so that you won't end up with too large values.

PS. Why are you using BCC+BCS for checking the direction, that would move even when EQual. Better would be BCC=negative, BNE=positive (and no jump when EQual).
homepage - patreon - you can think of a bit as a bottle that is either half full or half empty

sdm
Posts: 306
Joined: Tue Apr 11, 2006 4:08 am
Location: Poland

Re: Following one object (sprite) after another (Tracking)

Post by sdm » Sun Dec 29, 2019 9:54 am

Of course, I made the code that does not have this problem, but it is quite large.

Follow3.nes:

Code: Select all

;-------------------------------

ENEMY1_Loop:

	JSR ENEMY1_CheckMoveDirection

	JSR ENEMY1_Loop_U
	JSR ENEMY1_Loop_D
	JSR ENEMY1_Loop_L
	JSR ENEMY1_Loop_R
	JSR ENEMY1_Loop_RU
	JSR ENEMY1_Loop_RD
	JSR ENEMY1_Loop_LD
	JSR ENEMY1_Loop_LU

ENEMY1_Loop_Skip:
	RTS

;-------------------------------

ENEMY1_Loop_U:

	LDA ENEMY1_MoveDirection_Flags
	CMP #%00000001
	BNE ENEMY1_Loop_U_no

	DEC ENEMY1_1_Y

ENEMY1_Loop_U_no:
	RTS

;-------------------------------

ENEMY1_Loop_D:

	LDA ENEMY1_MoveDirection_Flags
	CMP #%00000010
	BNE ENEMY1_Loop_D_no

	INC ENEMY1_1_Y

ENEMY1_Loop_D_no:
	RTS

;-------------------------------

ENEMY1_Loop_L:

	LDA ENEMY1_MoveDirection_Flags
	CMP #%00000100
	BNE ENEMY1_Loop_L_no

	DEC ENEMY1_1_X

ENEMY1_Loop_L_no:
	RTS

;-------------------------------

ENEMY1_Loop_R:

	LDA ENEMY1_MoveDirection_Flags
	CMP #%00001000
	BNE ENEMY1_Loop_R_no

	INC ENEMY1_1_X

ENEMY1_Loop_R_no:
	RTS

;-------------------------------

ENEMY1_Loop_RU:

	LDA ENEMY1_MoveDirection_Flags
	CMP #%00001001
	BNE ENEMY1_Loop_RU_no

	DEC ENEMY1_1_Y
	INC ENEMY1_1_X

ENEMY1_Loop_RU_no:
	RTS

;-------------------------------

ENEMY1_Loop_RD:

	LDA ENEMY1_MoveDirection_Flags
	CMP #%00001010
	BNE ENEMY1_Loop_RD_no

	INC ENEMY1_1_Y
	INC ENEMY1_1_X

ENEMY1_Loop_RD_no:
	RTS

;-------------------------------

ENEMY1_Loop_LD:

	LDA ENEMY1_MoveDirection_Flags
	CMP #%00000110
	BNE ENEMY1_Loop_LD_no

	INC ENEMY1_1_Y
	DEC ENEMY1_1_X

ENEMY1_Loop_LD_no:
	RTS

;-------------------------------

ENEMY1_Loop_LU:

	LDA ENEMY1_MoveDirection_Flags
	CMP #%00000101
	BNE ENEMY1_Loop_LU_no

	DEC ENEMY1_1_Y
	DEC ENEMY1_1_X

ENEMY1_Loop_LU_no:
	RTS

;-------------------------------
;-------------------------------
;-------------------------------
;-------------------------------
;-------------------------------
;-------------------------------

ENEMY1_CheckMoveDirection:

	JSR ENEMY1_CheckMoveDirection_Y
	JSR ENEMY1_CheckMoveDirection_X
	JSR ENEMY1_CheckMoveY
	JSR ENEMY1_CheckMoveX

	RTS

;-------------------------------

ENEMY1_CheckMoveDirection_Y:

	LDA HERO_1_Y
	CMP ENEMY1_1_Y
	BCC ENEMY1_CheckMoveDirection_U
	BCS ENEMY1_CheckMoveDirection_D

	RTS

;-------------------------------

ENEMY1_CheckMoveDirection_U:

	LDA ENEMY1_MoveDirection_Flags
	ORA #%00000001
	AND #%11111101
	STA ENEMY1_MoveDirection_Flags

	RTS

;-------------------------------

ENEMY1_CheckMoveDirection_D:

	LDA ENEMY1_MoveDirection_Flags
	ORA #%00000010
	AND #%11111110
	STA ENEMY1_MoveDirection_Flags

	RTS

;-------------------------------

ENEMY1_CheckMoveDirection_X:

	LDA HERO_1_X
	CMP ENEMY1_1_X
	BCC ENEMY1_CheckMoveDirection_L
	BCS ENEMY1_CheckMoveDirection_R

	RTS

;-------------------------------

ENEMY1_CheckMoveDirection_L:

	LDA ENEMY1_MoveDirection_Flags
	ORA #%00000100
	AND #%11110111
	STA ENEMY1_MoveDirection_Flags

	RTS

;-------------------------------

ENEMY1_CheckMoveDirection_R:

	LDA ENEMY1_MoveDirection_Flags
	ORA #%00001000
	AND #%11111011
	STA ENEMY1_MoveDirection_Flags

	RTS

;-------------------------------

ENEMY1_CheckMoveY:

;	LDA ENEMY1_1_Y
;	SEC
;	SBC #2
;	CMP HERO_1_Y
;	BEQ ENEMY1_CheckMoveY_run

	LDA ENEMY1_1_Y
	SEC
	SBC #1
	CMP HERO_1_Y
	BEQ ENEMY1_CheckMoveY_run

	LDA ENEMY1_1_Y
	CMP HERO_1_Y
	BEQ ENEMY1_CheckMoveY_run

	LDA ENEMY1_1_Y
	CLC
	ADC #1
	CMP HERO_1_Y
	BEQ ENEMY1_CheckMoveY_run

;	LDA ENEMY1_1_Y
;	CLC
;	ADC #2
;	CMP HERO_1_Y
;	BEQ ENEMY1_CheckMoveY_run

	RTS

;-------------------------------

ENEMY1_CheckMoveY_run:

	LDA HERO_1_X
	CMP ENEMY1_1_X
	BCC ENEMY1_CheckMoveY_run_L
	BCS ENEMY1_CheckMoveY_run_R

ENEMY1_CheckMoveY_run_L:

	LDA #%00000100
	STA ENEMY1_MoveDirection_Flags

	RTS

ENEMY1_CheckMoveY_run_R:

	LDA #%00001000
	STA ENEMY1_MoveDirection_Flags

	RTS

;-------------------------------

ENEMY1_CheckMoveX:

;	LDA ENEMY1_1_X
;	SEC
;	SBC #2
;	CMP HERO_1_X
;	BEQ ENEMY1_CheckMoveX_run

	LDA ENEMY1_1_X
	SEC
	SBC #1
	CMP HERO_1_X
	BEQ ENEMY1_CheckMoveX_run

	LDA ENEMY1_1_X
	CMP HERO_1_X
	BEQ ENEMY1_CheckMoveX_run

	LDA ENEMY1_1_X
	CLC
	ADC #1
	CMP HERO_1_X
	BEQ ENEMY1_CheckMoveX_run

;	LDA ENEMY1_1_X
;	CLC
;	ADC #2
;	CMP HERO_1_X
;	BEQ ENEMY1_CheckMoveX_run

	RTS
	
;-------------------------------

ENEMY1_CheckMoveX_run:

	LDA HERO_1_Y
	CMP ENEMY1_1_Y
	BCC ENEMY1_CheckMoveY_run_U
	BCS ENEMY1_CheckMoveY_run_D

ENEMY1_CheckMoveY_run_U:

	LDA #%00000001
	STA ENEMY1_MoveDirection_Flags

	RTS

ENEMY1_CheckMoveY_run_D:

	LDA #%00000010
	STA ENEMY1_MoveDirection_Flags

	RTS
Attachments
follow3.nes
(128.02 KiB) Downloaded 77 times

nocash
Posts: 1211
Joined: Fri Feb 24, 2012 12:09 pm
Contact:

Re: Following one object (sprite) after another (Tracking)

Post by nocash » Sun Dec 29, 2019 12:28 pm

If you have working code, why are you posting the all those three bugged examples with the BCC+BCS pairs?
And, if the larger code is already working, what is/was the problem with that code?
Maybe I have completely misunderstood the question.
homepage - patreon - you can think of a bit as a bottle that is either half full or half empty

sdm
Posts: 306
Joined: Tue Apr 11, 2006 4:08 am
Location: Poland

Re: Following one object (sprite) after another (Tracking)

Post by sdm » Sun Dec 29, 2019 1:10 pm

It works of course. I am interested in how it can be done better, simpler, faster etc. I did it as best I can.


Thanks. It works great.

Code: Select all

ENEMY1_Loop_UD:
	LDA HERO_1_Y
	CMP ENEMY1_1_Y
	BCC ENEMY1_Loop_MoveUp
	BNE ENEMY1_Loop_MoveDown
	RTS

ENEMY1_Loop_LR:
	LDA HERO_1_X
	CMP ENEMY1_1_X
	BCC ENEMY1_Loop_MoveLeft
	BNE ENEMY1_Loop_MoveRight
	RTS

User avatar
Alp
Posts: 221
Joined: Mon Oct 06, 2014 12:37 am

Re: Following one object (sprite) after another (Tracking)

Post by Alp » Sun Dec 29, 2019 3:40 pm

Here's a heavily optimized Atan2 demo I had put together a while back, for my (unpublished) NES programming tutorial:
demo_atan2_4.nes
(24.02 KiB) Downloaded 96 times
Unfortunately, I seem to have misplaced the source code, when upgrading from my old laptop to a more powerful desktop PC. Oops.

EDIT: (Forgot to list the controls, joypad 1 controls the yellow smiley, and joypad 2 controls the red smiley. Pushing select on joypad 1 switches between the green smiley following player 1 or player 2.)

sdm
Posts: 306
Joined: Tue Apr 11, 2006 4:08 am
Location: Poland

Re: Following one object (sprite) after another (Tracking)

Post by sdm » Mon Dec 30, 2019 6:14 am

I think I can guess how it works.
I wrote the code quickly, it may look chaotic, but probably made in the right direction.

Code: Select all

	lda Timer_Counter
	inc Timer_Counter
	eor #$ff
	and Timer_Counter
	sta Timer

;---------------------

ENEMY1_Loop:

	lda ENEMY1_1_Y
	sec
	sbc HERO_1_Y
	sta DistanceU

	lda HERO_1_Y
	sec
	sbc ENEMY1_1_Y
	sta DistanceD

	lda ENEMY1_1_X
	sec
	sbc HERO_1_X
	sta DistanceL

	lda HERO_1_X
	sec
	sbc ENEMY1_1_X
	sta DistanceR

	jsr speedcheckU
	jsr speedcheckD
	jsr speedcheckL
	jsr speedcheckR

	JSR ENEMY1_Loop_UD
	JSR ENEMY1_Loop_LR

	RTS

;---------------------

ENEMY1_Loop_UD:

	LDA HERO_1_Y
	CMP ENEMY1_1_Y
	BCC ENEMY1_Loop_MoveUp
	BNE ENEMY1_Loop_MoveDown

	lda #$00
	sta DistanceU
	sta DistanceD

	RTS

;---------------------

ENEMY1_Loop_MoveUp:

	LDA Timer
	AND speedU
	BEQ ENEMY1_Loop_MoveUp_Skip

	DEC ENEMY1_1_Y

ENEMY1_Loop_MoveUp_Skip:
	RTS

;---------------------

ENEMY1_Loop_MoveDown:

	LDA Timer
	AND speedD
	BEQ ENEMY1_Loop_MoveDown_Skip

	INC ENEMY1_1_Y

ENEMY1_Loop_MoveDown_Skip:
	RTS

;---------------------

ENEMY1_Loop_LR:

	LDA HERO_1_X
	CMP ENEMY1_1_X
	BCC ENEMY1_Loop_MoveLeft
	BNE ENEMY1_Loop_MoveRight

	lda #$00
	sta DistanceL
	sta DistanceR

	RTS

;---------------------

ENEMY1_Loop_MoveLeft:

	LDA Timer
	AND speedL
	BEQ ENEMY1_Loop_MoveLeft_Skip

	DEC ENEMY1_1_X

ENEMY1_Loop_MoveLeft_Skip:
	RTS

;---------------------

ENEMY1_Loop_MoveRight:

	LDA Timer
	AND speedR
	BEQ ENEMY1_Loop_MoveRight_Skip

	INC ENEMY1_1_X

ENEMY1_Loop_MoveRight_Skip:
	RTS

;---------------------
;---------------------
;---------------------

speedcheckU:

	lda DistanceU
	cmp #$00
	bcc speedcheckU_skip1

	lda #%00000100
	sta speedU

speedcheckU_skip1:

	lda DistanceU
	cmp #$08
	bcc speedcheckU_skip2

	lda #%00000010
	sta speedU

speedcheckU_skip2:

	lda DistanceU
	cmp #$16
	bcc speedcheckU_skip3

	lda #%00000001
	sta speedU

speedcheckU_skip3:

	lda DistanceU
	cmp #$32
	bcc speedcheckU_skip4

	lda #%11111111
	sta speedU

speedcheckU_skip4:

	rts

;---------------------
;---------------------
;---------------------

speedcheckD:

	lda DistanceD
	cmp #$00
	bcc speedcheckD_skip1

	lda #%00000100
	sta speedD

speedcheckD_skip1:

	lda DistanceD
	cmp #$08
	bcc speedcheckD_skip2

	lda #%00000010
	sta speedD

speedcheckD_skip2:

	lda DistanceD
	cmp #$16
	bcc speedcheckD_skip3

	lda #%00000001
	sta speedD

speedcheckD_skip3:

	lda DistanceD
	cmp #$32
	bcc speedcheckD_skip4

	lda #%11111111
	sta speedD

speedcheckD_skip4:

	rts

;---------------------
;---------------------
;---------------------

speedcheckL:

	lda DistanceL
	cmp #$00
	bcc speedcheckL_skip1

	lda #%00000100
	sta speedL

speedcheckL_skip1:

	lda DistanceL
	cmp #$08
	bcc speedcheckL_skip2

	lda #%00000010
	sta speedL

speedcheckL_skip2:

	lda DistanceL
	cmp #$16
	bcc speedcheckL_skip3

	lda #%00000001
	sta speedL

speedcheckL_skip3:

	lda DistanceL
	cmp #$32
	bcc speedcheckL_skip4

	lda #%11111111
	sta speedL

speedcheckL_skip4:

	rts

;---------------------
;---------------------
;---------------------

speedcheckR:

	lda DistanceR
	cmp #$00
	bcc speedcheckR_skip1

	lda #%00000100
	sta speedR

speedcheckR_skip1:

	lda DistanceR
	cmp #$08
	bcc speedcheckR_skip2

	lda #%00000010
	sta speedR

speedcheckR_skip2:

	lda DistanceR
	cmp #$24
	bcc speedcheckR_skip3

	lda #%00000001
	sta speedR

speedcheckR_skip3:

	lda DistanceR
	cmp #$48
	bcc speedcheckR_skip4

	lda #%11111111
	sta speedR

speedcheckR_skip4:

	rts
Attachments
follow4.nes
(128.02 KiB) Downloaded 76 times

User avatar
olddb
Posts: 113
Joined: Thu Oct 26, 2017 12:29 pm
Contact:

Re: Following one object (sprite) after another (Tracking)

Post by olddb » Mon Dec 30, 2019 2:13 pm

How about this?

EDIT:

Code: Select all


ENEMY1_Loop:
	
	;make enemy slower than hero
	inc Timer_Counter
	lda Timer_Counter
	and #01
	bne +
		rts
	+:

	lda ENEMY1_1_X
	cmp HERO_1_X
	beq ++
	bcs +
		;< so increase
		inc ENEMY1_1_X
		jmp ++
	+:	
		;> so decrease
		dec ENEMY1_1_X
	++:

	lda ENEMY1_1_Y
	cmp HERO_1_Y
	beq ++
	bcs +
		;< so increase
		inc ENEMY1_1_Y
		jmp ++
	+:	
		;> so decrease
		dec ENEMY1_1_Y
	++:
	
rts


...

sdm
Posts: 306
Joined: Tue Apr 11, 2006 4:08 am
Location: Poland

Re: Following one object (sprite) after another (Tracking)

Post by sdm » Tue Dec 31, 2019 5:52 am

Works great, thanks :)

My earlier code has changed a bit, unfortunately it is larger, but more accurate when it comes to changing the speed of the object.

I have to optimize it all to make it as small as possible.

Follow5.nes:

Code: Select all

	lda Timer_Counter
	inc Timer_Counter
	eor #$ff
	and Timer_Counter
	sta Timer

;---------------------

ENEMY1_Loop:

	lda ENEMY1_1_Y
	sec
	sbc HERO_1_Y
	sta DistanceU

	lda HERO_1_Y
	sec
	sbc ENEMY1_1_Y
	sta DistanceD

	lda ENEMY1_1_X
	sec
	sbc HERO_1_X
	sta DistanceL

	lda HERO_1_X
	sec
	sbc ENEMY1_1_X
	sta DistanceR

	jsr speedcheckU
	jsr speedcheckD
	jsr speedcheckL
	jsr speedcheckR

	JSR ENEMY1_Loop_UD
	JSR ENEMY1_Loop_LR

	RTS

;---------------------

ENEMY1_Loop_UD:

	LDA HERO_1_Y
	CMP ENEMY1_1_Y
	BCC ENEMY1_Loop_MoveUp
	BNE ENEMY1_Loop_MoveDown

	lda #$00
	sta DistanceU
	sta DistanceD

	RTS

;---------------------

ENEMY1_Loop_MoveUp:

	LDA Timer
	AND speedU
	BNE ENEMY1_Loop_MoveUp_Skip

	DEC ENEMY1_1_Y

ENEMY1_Loop_MoveUp_Skip:
	RTS

;---------------------

ENEMY1_Loop_MoveDown:

	LDA Timer
	AND speedD
	BNE ENEMY1_Loop_MoveDown_Skip

	INC ENEMY1_1_Y

ENEMY1_Loop_MoveDown_Skip:
	RTS

;---------------------

ENEMY1_Loop_LR:

	LDA HERO_1_X
	CMP ENEMY1_1_X
	BCC ENEMY1_Loop_MoveLeft
	BNE ENEMY1_Loop_MoveRight

	lda #$00
	sta DistanceL
	sta DistanceR

	RTS

;---------------------

ENEMY1_Loop_MoveLeft:

	LDA Timer
	AND speedL
	BNE ENEMY1_Loop_MoveLeft_Skip

	DEC ENEMY1_1_X

ENEMY1_Loop_MoveLeft_Skip:
	RTS

;---------------------

ENEMY1_Loop_MoveRight:

	LDA Timer
	AND speedR
	BNE ENEMY1_Loop_MoveRight_Skip

	INC ENEMY1_1_X

ENEMY1_Loop_MoveRight_Skip:
	RTS

;---------------------
;---------------------
;---------------------

speedcheckU:

	lda DistanceU
	cmp #$00
	bcc speedcheckU_skip01

	lda #%00001111
	sta speedU

speedcheckU_skip01:

	lda DistanceU
	cmp #$08
	bcc speedcheckU_skip02

	lda #%00000111
	sta speedU

speedcheckU_skip02:

	lda DistanceU
	cmp #$10
	bcc speedcheckU_skip03

	lda #%00001011
	sta speedU

speedcheckU_skip03:

	lda DistanceU
	cmp #$18
	bcc speedcheckU_skip04

	lda #%00000011
	sta speedU

speedcheckU_skip04:

	lda DistanceU
	cmp #$20
	bcc speedcheckU_skip05

	lda #%00001101
	sta speedU

speedcheckU_skip05:

	lda DistanceU
	cmp #$28
	bcc speedcheckU_skip06

	lda #%00000101
	sta speedU

speedcheckU_skip06:

	lda DistanceU
	cmp #$30
	bcc speedcheckU_skip07

	lda #%00001001
	sta speedU

speedcheckU_skip07:

	lda DistanceU
	cmp #$38
	bcc speedcheckU_skip08

	lda #%00000001
	sta speedU

speedcheckU_skip08:

	lda DistanceU
	cmp #$40
	bcc speedcheckU_skip09

	lda #%00001110
	sta speedU

speedcheckU_skip09:

	lda DistanceU
	cmp #$48
	bcc speedcheckU_skip10

	lda #%00000110
	sta speedU

speedcheckU_skip10:

	lda DistanceU
	cmp #$50
	bcc speedcheckU_skip11

	lda #%00001010
	sta speedU

speedcheckU_skip11:

	lda DistanceU
	cmp #$58
	bcc speedcheckU_skip12

	lda #%00000010
	sta speedU

speedcheckU_skip12:

	lda DistanceU
	cmp #$60
	bcc speedcheckU_skip13

	lda #%00001100
	sta speedU

speedcheckU_skip13:

	lda DistanceU
	cmp #$68
	bcc speedcheckU_skip14

	lda #%00000100
	sta speedU

speedcheckU_skip14:

	lda DistanceU
	cmp #$70
	bcc speedcheckU_skip15

	lda #%00001000
	sta speedU

speedcheckU_skip15:

	lda DistanceU
	cmp #$78
	bcc speedcheckU_skip16

	lda #%00000000
	sta speedU

speedcheckU_skip16:
	rts

;---------------------
;---------------------
;---------------------

speedcheckD:

	lda DistanceD
	cmp #$00
	bcc speedcheckD_skip01

	lda #%00001111
	sta speedD

speedcheckD_skip01:

	lda DistanceD
	cmp #$08
	bcc speedcheckD_skip02

	lda #%00000111
	sta speedD

speedcheckD_skip02:

	lda DistanceD
	cmp #$10
	bcc speedcheckD_skip03

	lda #%00001011
	sta speedD

speedcheckD_skip03:

	lda DistanceD
	cmp #$18
	bcc speedcheckD_skip04

	lda #%00000011
	sta speedD

speedcheckD_skip04:

	lda DistanceD
	cmp #$20
	bcc speedcheckD_skip05

	lda #%00001101
	sta speedD

speedcheckD_skip05:

	lda DistanceD
	cmp #$28
	bcc speedcheckD_skip06

	lda #%00000101
	sta speedD

speedcheckD_skip06:

	lda DistanceD
	cmp #$30
	bcc speedcheckD_skip07

	lda #%00001001
	sta speedD

speedcheckD_skip07:

	lda DistanceD
	cmp #$38
	bcc speedcheckD_skip08

	lda #%00000001
	sta speedD

speedcheckD_skip08:

	lda DistanceD
	cmp #$40
	bcc speedcheckD_skip09

	lda #%00001110
	sta speedD

speedcheckD_skip09:

	lda DistanceD
	cmp #$48
	bcc speedcheckD_skip10

	lda #%00000110
	sta speedD

speedcheckD_skip10:

	lda DistanceD
	cmp #$50
	bcc speedcheckD_skip11

	lda #%00001010
	sta speedD

speedcheckD_skip11:

	lda DistanceD
	cmp #$58
	bcc speedcheckD_skip12

	lda #%00000010
	sta speedD

speedcheckD_skip12:

	lda DistanceD
	cmp #$60
	bcc speedcheckD_skip13

	lda #%00001100
	sta speedD

speedcheckD_skip13:

	lda DistanceD
	cmp #$68
	bcc speedcheckD_skip14

	lda #%00000100
	sta speedD

speedcheckD_skip14:

	lda DistanceD
	cmp #$70
	bcc speedcheckD_skip15

	lda #%00001000
	sta speedD

speedcheckD_skip15:

	lda DistanceD
	cmp #$78
	bcc speedcheckD_skip16

	lda #%00000000
	sta speedD

speedcheckD_skip16:
	rts

;---------------------
;---------------------
;---------------------

speedcheckL:

	lda DistanceL
	cmp #$00
	bcc speedcheckL_skip01

	lda #%00001111
	sta speedL

speedcheckL_skip01:

	lda DistanceL
	cmp #$08
	bcc speedcheckL_skip02

	lda #%00000111
	sta speedL

speedcheckL_skip02:

	lda DistanceL
	cmp #$10
	bcc speedcheckL_skip03

	lda #%00001011
	sta speedL

speedcheckL_skip03:

	lda DistanceL
	cmp #$18
	bcc speedcheckL_skip04

	lda #%00000011
	sta speedL

speedcheckL_skip04:

	lda DistanceL
	cmp #$20
	bcc speedcheckL_skip05

	lda #%00001101
	sta speedL

speedcheckL_skip05:

	lda DistanceL
	cmp #$28
	bcc speedcheckL_skip06

	lda #%00000101
	sta speedL

speedcheckL_skip06:

	lda DistanceL
	cmp #$30
	bcc speedcheckL_skip07

	lda #%00001001
	sta speedL

speedcheckL_skip07:

	lda DistanceL
	cmp #$38
	bcc speedcheckL_skip08

	lda #%00000001
	sta speedL

speedcheckL_skip08:

	lda DistanceL
	cmp #$40
	bcc speedcheckL_skip09

	lda #%00001110
	sta speedL

speedcheckL_skip09:

	lda DistanceL
	cmp #$48
	bcc speedcheckL_skip10

	lda #%00000110
	sta speedL

speedcheckL_skip10:

	lda DistanceL
	cmp #$50
	bcc speedcheckL_skip11

	lda #%00001010
	sta speedL

speedcheckL_skip11:

	lda DistanceL
	cmp #$58
	bcc speedcheckL_skip12

	lda #%00000010
	sta speedL

speedcheckL_skip12:

	lda DistanceL
	cmp #$60
	bcc speedcheckL_skip13

	lda #%00001100
	sta speedL

speedcheckL_skip13:

	lda DistanceL
	cmp #$68
	bcc speedcheckL_skip14

	lda #%00000100
	sta speedL

speedcheckL_skip14:

	lda DistanceL
	cmp #$70
	bcc speedcheckL_skip15

	lda #%00001000
	sta speedL

speedcheckL_skip15:

	lda DistanceL
	cmp #$78
	bcc speedcheckL_skip16

	lda #%00000000
	sta speedL

speedcheckL_skip16:
	rts

;---------------------
;---------------------
;---------------------

speedcheckR:

	lda DistanceR
	cmp #$00
	bcc speedcheckR_skip01

	lda #%00001111
	sta speedR

speedcheckR_skip01:

	lda DistanceR
	cmp #$08
	bcc speedcheckR_skip02

	lda #%00000111
	sta speedR

speedcheckR_skip02:

	lda DistanceR
	cmp #$10
	bcc speedcheckR_skip03

	lda #%00001011
	sta speedR

speedcheckR_skip03:

	lda DistanceR
	cmp #$18
	bcc speedcheckR_skip04

	lda #%00000011
	sta speedR

speedcheckR_skip04:

	lda DistanceR
	cmp #$20
	bcc speedcheckR_skip05

	lda #%00001101
	sta speedR

speedcheckR_skip05:

	lda DistanceR
	cmp #$28
	bcc speedcheckR_skip06

	lda #%00000101
	sta speedR

speedcheckR_skip06:

	lda DistanceR
	cmp #$30
	bcc speedcheckR_skip07

	lda #%00001001
	sta speedR

speedcheckR_skip07:

	lda DistanceR
	cmp #$38
	bcc speedcheckR_skip08

	lda #%00000001
	sta speedR

speedcheckR_skip08:

	lda DistanceR
	cmp #$40
	bcc speedcheckR_skip09

	lda #%00001110
	sta speedR

speedcheckR_skip09:

	lda DistanceR
	cmp #$48
	bcc speedcheckR_skip10

	lda #%00000110
	sta speedR

speedcheckR_skip10:

	lda DistanceR
	cmp #$50
	bcc speedcheckR_skip11

	lda #%00001010
	sta speedR

speedcheckR_skip11:

	lda DistanceR
	cmp #$58
	bcc speedcheckR_skip12

	lda #%00000010
	sta speedR

speedcheckR_skip12:

	lda DistanceR
	cmp #$60
	bcc speedcheckR_skip13

	lda #%00001100
	sta speedR

speedcheckR_skip13:

	lda DistanceR
	cmp #$68
	bcc speedcheckR_skip14

	lda #%00000100
	sta speedR

speedcheckR_skip14:

	lda DistanceR
	cmp #$70
	bcc speedcheckR_skip15

	lda #%00001000
	sta speedR

speedcheckR_skip15:

	lda DistanceR
	cmp #$78
	bcc speedcheckR_skip16

	lda #%00000000
	sta speedR

speedcheckR_skip16:
	rts
Edit: Simplified code (using olddb code) with a smooth movement of the opponent:

Code: Select all

ENEMY1_Loop:

	lda ENEMY1_1_Y
	sec
	sbc HERO_1_Y
	sta DistanceU

	lda HERO_1_Y
	sec
	sbc ENEMY1_1_Y
	sta DistanceD

	lda ENEMY1_1_X
	sec
	sbc HERO_1_X
	sta DistanceL

	lda HERO_1_X
	sec
	sbc ENEMY1_1_X
	sta DistanceR

	jsr speedcheckU
	jsr speedcheckD
	jsr speedcheckL
	jsr speedcheckR

;-------
	
	lda ENEMY1_1_X
	cmp HERO_1_X
	beq aaa
	bcs bbb

	lda Timer
	and speedR
	bne skip1

	inc ENEMY1_1_X

skip1:

	jmp aaa

bbb:

	lda Timer
	and speedL
	bne aaa

	dec ENEMY1_1_X

aaa:

	lda ENEMY1_1_Y
	cmp HERO_1_Y
	beq ccc
	bcs ddd

	lda Timer
	and speedD
	bne skip2

	inc ENEMY1_1_Y

skip2:

	jmp ccc

ddd:

	LDA Timer
	AND speedU
	BNE ccc

	dec ENEMY1_1_Y

ccc:

	rts

;---------------------
;---------------------
;---------------------

speedcheckU:

	lda DistanceU
	cmp #$00
	bcc speedcheckU_skip01

	lda #%00001111
	sta speedU

speedcheckU_skip01:

	lda DistanceU
	cmp #$08
	bcc speedcheckU_skip02

	lda #%00000111
	sta speedU

speedcheckU_skip02:

	lda DistanceU
	cmp #$10
	bcc speedcheckU_skip03

	lda #%00001011
	sta speedU

speedcheckU_skip03:

	lda DistanceU
	cmp #$18
	bcc speedcheckU_skip04

	lda #%00000011
	sta speedU

speedcheckU_skip04:

	lda DistanceU
	cmp #$20
	bcc speedcheckU_skip05

	lda #%00001101
	sta speedU

speedcheckU_skip05:

	lda DistanceU
	cmp #$28
	bcc speedcheckU_skip06

	lda #%00000101
	sta speedU

speedcheckU_skip06:

	lda DistanceU
	cmp #$30
	bcc speedcheckU_skip07

	lda #%00001001
	sta speedU

speedcheckU_skip07:

	lda DistanceU
	cmp #$38
	bcc speedcheckU_skip08

	lda #%00000001
	sta speedU

speedcheckU_skip08:

	lda DistanceU
	cmp #$40
	bcc speedcheckU_skip09

	lda #%00001110
	sta speedU

speedcheckU_skip09:

	lda DistanceU
	cmp #$48
	bcc speedcheckU_skip10

	lda #%00000110
	sta speedU

speedcheckU_skip10:

	lda DistanceU
	cmp #$50
	bcc speedcheckU_skip11

	lda #%00001010
	sta speedU

speedcheckU_skip11:

	lda DistanceU
	cmp #$58
	bcc speedcheckU_skip12

	lda #%00000010
	sta speedU

speedcheckU_skip12:

	lda DistanceU
	cmp #$60
	bcc speedcheckU_skip13

	lda #%00001100
	sta speedU

speedcheckU_skip13:

	lda DistanceU
	cmp #$68
	bcc speedcheckU_skip14

	lda #%00000100
	sta speedU

speedcheckU_skip14:

	lda DistanceU
	cmp #$70
	bcc speedcheckU_skip15

	lda #%00001000
	sta speedU

speedcheckU_skip15:

	lda DistanceU
	cmp #$78
	bcc speedcheckU_skip16

	lda #%00000000
	sta speedU

speedcheckU_skip16:
	rts

;---------------------
;---------------------
;---------------------

speedcheckD:

	lda DistanceD
	cmp #$00
	bcc speedcheckD_skip01

	lda #%00001111
	sta speedD

speedcheckD_skip01:

	lda DistanceD
	cmp #$08
	bcc speedcheckD_skip02

	lda #%00000111
	sta speedD

speedcheckD_skip02:

	lda DistanceD
	cmp #$10
	bcc speedcheckD_skip03

	lda #%00001011
	sta speedD

speedcheckD_skip03:

	lda DistanceD
	cmp #$18
	bcc speedcheckD_skip04

	lda #%00000011
	sta speedD

speedcheckD_skip04:

	lda DistanceD
	cmp #$20
	bcc speedcheckD_skip05

	lda #%00001101
	sta speedD

speedcheckD_skip05:

	lda DistanceD
	cmp #$28
	bcc speedcheckD_skip06

	lda #%00000101
	sta speedD

speedcheckD_skip06:

	lda DistanceD
	cmp #$30
	bcc speedcheckD_skip07

	lda #%00001001
	sta speedD

speedcheckD_skip07:

	lda DistanceD
	cmp #$38
	bcc speedcheckD_skip08

	lda #%00000001
	sta speedD

speedcheckD_skip08:

	lda DistanceD
	cmp #$40
	bcc speedcheckD_skip09

	lda #%00001110
	sta speedD

speedcheckD_skip09:

	lda DistanceD
	cmp #$48
	bcc speedcheckD_skip10

	lda #%00000110
	sta speedD

speedcheckD_skip10:

	lda DistanceD
	cmp #$50
	bcc speedcheckD_skip11

	lda #%00001010
	sta speedD

speedcheckD_skip11:

	lda DistanceD
	cmp #$58
	bcc speedcheckD_skip12

	lda #%00000010
	sta speedD

speedcheckD_skip12:

	lda DistanceD
	cmp #$60
	bcc speedcheckD_skip13

	lda #%00001100
	sta speedD

speedcheckD_skip13:

	lda DistanceD
	cmp #$68
	bcc speedcheckD_skip14

	lda #%00000100
	sta speedD

speedcheckD_skip14:

	lda DistanceD
	cmp #$70
	bcc speedcheckD_skip15

	lda #%00001000
	sta speedD

speedcheckD_skip15:

	lda DistanceD
	cmp #$78
	bcc speedcheckD_skip16

	lda #%00000000
	sta speedD

speedcheckD_skip16:
	rts

;---------------------
;---------------------
;---------------------

speedcheckL:

	lda DistanceL
	cmp #$00
	bcc speedcheckL_skip01

	lda #%00001111
	sta speedL

speedcheckL_skip01:

	lda DistanceL
	cmp #$08
	bcc speedcheckL_skip02

	lda #%00000111
	sta speedL

speedcheckL_skip02:

	lda DistanceL
	cmp #$10
	bcc speedcheckL_skip03

	lda #%00001011
	sta speedL

speedcheckL_skip03:

	lda DistanceL
	cmp #$18
	bcc speedcheckL_skip04

	lda #%00000011
	sta speedL

speedcheckL_skip04:

	lda DistanceL
	cmp #$20
	bcc speedcheckL_skip05

	lda #%00001101
	sta speedL

speedcheckL_skip05:

	lda DistanceL
	cmp #$28
	bcc speedcheckL_skip06

	lda #%00000101
	sta speedL

speedcheckL_skip06:

	lda DistanceL
	cmp #$30
	bcc speedcheckL_skip07

	lda #%00001001
	sta speedL

speedcheckL_skip07:

	lda DistanceL
	cmp #$38
	bcc speedcheckL_skip08

	lda #%00000001
	sta speedL

speedcheckL_skip08:

	lda DistanceL
	cmp #$40
	bcc speedcheckL_skip09

	lda #%00001110
	sta speedL

speedcheckL_skip09:

	lda DistanceL
	cmp #$48
	bcc speedcheckL_skip10

	lda #%00000110
	sta speedL

speedcheckL_skip10:

	lda DistanceL
	cmp #$50
	bcc speedcheckL_skip11

	lda #%00001010
	sta speedL

speedcheckL_skip11:

	lda DistanceL
	cmp #$58
	bcc speedcheckL_skip12

	lda #%00000010
	sta speedL

speedcheckL_skip12:

	lda DistanceL
	cmp #$60
	bcc speedcheckL_skip13

	lda #%00001100
	sta speedL

speedcheckL_skip13:

	lda DistanceL
	cmp #$68
	bcc speedcheckL_skip14

	lda #%00000100
	sta speedL

speedcheckL_skip14:

	lda DistanceL
	cmp #$70
	bcc speedcheckL_skip15

	lda #%00001000
	sta speedL

speedcheckL_skip15:

	lda DistanceL
	cmp #$78
	bcc speedcheckL_skip16

	lda #%00000000
	sta speedL

speedcheckL_skip16:
	rts

;---------------------
;---------------------
;---------------------

speedcheckR:

	lda DistanceR
	cmp #$00
	bcc speedcheckR_skip01

	lda #%00001111
	sta speedR

speedcheckR_skip01:

	lda DistanceR
	cmp #$08
	bcc speedcheckR_skip02

	lda #%00000111
	sta speedR

speedcheckR_skip02:

	lda DistanceR
	cmp #$10
	bcc speedcheckR_skip03

	lda #%00001011
	sta speedR

speedcheckR_skip03:

	lda DistanceR
	cmp #$18
	bcc speedcheckR_skip04

	lda #%00000011
	sta speedR

speedcheckR_skip04:

	lda DistanceR
	cmp #$20
	bcc speedcheckR_skip05

	lda #%00001101
	sta speedR

speedcheckR_skip05:

	lda DistanceR
	cmp #$28
	bcc speedcheckR_skip06

	lda #%00000101
	sta speedR

speedcheckR_skip06:

	lda DistanceR
	cmp #$30
	bcc speedcheckR_skip07

	lda #%00001001
	sta speedR

speedcheckR_skip07:

	lda DistanceR
	cmp #$38
	bcc speedcheckR_skip08

	lda #%00000001
	sta speedR

speedcheckR_skip08:

	lda DistanceR
	cmp #$40
	bcc speedcheckR_skip09

	lda #%00001110
	sta speedR

speedcheckR_skip09:

	lda DistanceR
	cmp #$48
	bcc speedcheckR_skip10

	lda #%00000110
	sta speedR

speedcheckR_skip10:

	lda DistanceR
	cmp #$50
	bcc speedcheckR_skip11

	lda #%00001010
	sta speedR

speedcheckR_skip11:

	lda DistanceR
	cmp #$58
	bcc speedcheckR_skip12

	lda #%00000010
	sta speedR

speedcheckR_skip12:

	lda DistanceR
	cmp #$60
	bcc speedcheckR_skip13

	lda #%00001100
	sta speedR

speedcheckR_skip13:

	lda DistanceR
	cmp #$68
	bcc speedcheckR_skip14

	lda #%00000100
	sta speedR

speedcheckR_skip14:

	lda DistanceR
	cmp #$70
	bcc speedcheckR_skip15

	lda #%00001000
	sta speedR

speedcheckR_skip15:

	lda DistanceR
	cmp #$78
	bcc speedcheckR_skip16

	lda #%00000000
	sta speedR

speedcheckR_skip16:
	rts

Edit2: Simplified code (using olddb code) with a smooth movement of the opponent v2:
It's the best I've been able to do.

Code: Select all

ENEMY1_Loop:

	lda ENEMY1_1_X
	cmp HERO_1_X
	beq aaa
	bcs bbb

	lda Timer
	and speedX
	bne skip1

	lda HERO_1_X
	sec
	sbc ENEMY1_1_X
	sta DistanceX

	inc ENEMY1_1_X

skip1:

	jmp aaa

bbb:

	lda Timer
	and speedX
	bne aaa

	lda ENEMY1_1_X
	sec
	sbc HERO_1_X
	sta DistanceX

	dec ENEMY1_1_X

aaa:

	lda ENEMY1_1_Y
	cmp HERO_1_Y
	beq ccc
	bcs ddd

	lda Timer
	and speedY
	bne skip2

	lda HERO_1_Y
	sec
	sbc ENEMY1_1_Y
	sta DistanceY

	inc ENEMY1_1_Y

skip2:

	jmp ccc

ddd:

	LDA Timer
	AND speedY
	BNE ccc

	lda ENEMY1_1_Y
	sec
	sbc HERO_1_Y
	sta DistanceY

	dec ENEMY1_1_Y

ccc:

	jsr speedcheckY
	jsr speedcheckX

	rts

;---------------------
;---------------------
;---------------------

speedcheckY:

	lda DistanceY
	cmp #$00
	bcc speedcheckY_skip01

	lda #%00001111
	sta speedY

speedcheckY_skip01:

	lda DistanceY
	cmp #$08
	bcc speedcheckY_skip02

	lda #%00000111
	sta speedY

speedcheckY_skip02:

	lda DistanceY
	cmp #$10
	bcc speedcheckY_skip03

	lda #%00001011
	sta speedY

speedcheckY_skip03:

	lda DistanceY
	cmp #$18
	bcc speedcheckY_skip04

	lda #%00000011
	sta speedY

speedcheckY_skip04:

	lda DistanceY
	cmp #$20
	bcc speedcheckY_skip05

	lda #%00001101
	sta speedY

speedcheckY_skip05:

	lda DistanceY
	cmp #$28
	bcc speedcheckY_skip06

	lda #%00000101
	sta speedY

speedcheckY_skip06:

	lda DistanceY
	cmp #$30
	bcc speedcheckY_skip07

	lda #%00001001
	sta speedY

speedcheckY_skip07:

	lda DistanceY
	cmp #$38
	bcc speedcheckY_skip08

	lda #%00000001
	sta speedY

speedcheckY_skip08:

	lda DistanceY
	cmp #$40
	bcc speedcheckY_skip09

	lda #%00001110
	sta speedY

speedcheckY_skip09:

	lda DistanceY
	cmp #$48
	bcc speedcheckY_skip10

	lda #%00000110
	sta speedY

speedcheckY_skip10:

	lda DistanceY
	cmp #$50
	bcc speedcheckY_skip11

	lda #%00001010
	sta speedY

speedcheckY_skip11:

	lda DistanceY
	cmp #$58
	bcc speedcheckY_skip12

	lda #%00000010
	sta speedY

speedcheckY_skip12:

	lda DistanceY
	cmp #$60
	bcc speedcheckY_skip13

	lda #%00001100
	sta speedY

speedcheckY_skip13:

	lda DistanceY
	cmp #$68
	bcc speedcheckY_skip14

	lda #%00000100
	sta speedY

speedcheckY_skip14:

	lda DistanceY
	cmp #$70
	bcc speedcheckY_skip15

	lda #%00001000
	sta speedY

speedcheckY_skip15:

	lda DistanceY
	cmp #$78
	bcc speedcheckY_skip16

	lda #%00000000
	sta speedY

speedcheckY_skip16:
	rts

;---------------------
;---------------------
;---------------------

speedcheckX:

	lda DistanceX
	cmp #$00
	bcc speedcheckX_skip01

	lda #%00001111
	sta speedX

speedcheckX_skip01:

	lda DistanceX
	cmp #$08
	bcc speedcheckX_skip02

	lda #%00000111
	sta speedX

speedcheckX_skip02:

	lda DistanceX
	cmp #$10
	bcc speedcheckX_skip03

	lda #%00001011
	sta speedX

speedcheckX_skip03:

	lda DistanceX
	cmp #$18
	bcc speedcheckX_skip04

	lda #%00000011
	sta speedX

speedcheckX_skip04:

	lda DistanceX
	cmp #$20
	bcc speedcheckX_skip05

	lda #%00001101
	sta speedX

speedcheckX_skip05:

	lda DistanceX
	cmp #$28
	bcc speedcheckX_skip06

	lda #%00000101
	sta speedX

speedcheckX_skip06:

	lda DistanceX
	cmp #$30
	bcc speedcheckX_skip07

	lda #%00001001
	sta speedX

speedcheckX_skip07:

	lda DistanceX
	cmp #$38
	bcc speedcheckX_skip08

	lda #%00000001
	sta speedX

speedcheckX_skip08:

	lda DistanceX
	cmp #$40
	bcc speedcheckX_skip09

	lda #%00001110
	sta speedX

speedcheckX_skip09:

	lda DistanceX
	cmp #$48
	bcc speedcheckX_skip10

	lda #%00000110
	sta speedX

speedcheckX_skip10:

	lda DistanceX
	cmp #$50
	bcc speedcheckX_skip11

	lda #%00001010
	sta speedX

speedcheckX_skip11:

	lda DistanceX
	cmp #$58
	bcc speedcheckX_skip12

	lda #%00000010
	sta speedX

speedcheckX_skip12:

	lda DistanceX
	cmp #$60
	bcc speedcheckX_skip13

	lda #%00001100
	sta speedX

speedcheckX_skip13:

	lda DistanceX
	cmp #$68
	bcc speedcheckX_skip14

	lda #%00000100
	sta speedX

speedcheckX_skip14:

	lda DistanceX
	cmp #$70
	bcc speedcheckX_skip15

	lda #%00001000
	sta speedX

speedcheckX_skip15:

	lda DistanceX
	cmp #$78
	bcc speedcheckX_skip16

	lda #%00000000
	sta speedX

speedcheckX_skip16:
	rts
Attachments
follow5.nes
(128.02 KiB) Downloaded 78 times

Post Reply