8-direction NPC destination setting
Hello there.
I've been tearing my hair out for a couple of weeks trying to get this working, but I just can't seem to wrap my head around it.
Basically, I have NPC's in a top-down grid-based environment, but the movement is true 8-direction so it doesn't snap to the grid. The grid itself is 48x48 pixels per square. I have the following variables for each NPC:
MoveX - Determine the distance to move and in which direction. 1=right and -1=left
MoveY - 1=down, -1=up
DestX - X-Coordinate of desired destination
DestY - Y-Coord
StartX - The starting coords of the NPC
StartY
Then I have the following conditions and actions which deal with the movement (this is for X only, Y is seperate)
Every 00"-01
O_NPC:Set X Position to X("O_NPC") + MoveX("O_NPC")
#Stop moving if reached destination
X Position of O_NPC = DestX("O_NPC")
O_NPC:Set MoveX to 0
O_NPC:Set DestX to -1
#If destination set the move in correct direction
DestX of O_NPC > X("O_NPC")
O_NPC:Set MoveX to 1
DestX of O_NPC < X("O_NPC")
DestX of O_NPC >=0
O_NPC:Set MoveX to -1
#Stop moving if no destination
DestX of O_NPC = -1
O_NPC:Set MoveX to 0
Now this logic seems to work perfectly when there is only one NPC on the screen. However I run the following calculation on the DestX and DestY values every random amount of time for each O_NPC on the board.
O_NPC:DestX = StartX( "O_NPC" ) + ( 48 * (Random(3)-1) )
This calculation would mean the NPC's can never travel more then 1 cell away from their original start location. Now if you introduce two NPC's on to the board, the new NPC will operate normally but the first NPC occasionally acts erratic, goes farther away from the start point than it should, and even just keep going without stopping. It seems somewhere that logic which should only apply to one NPC, seems to apply to more than one. If I add 6 NPC's to the board, most of them go haywire and start drifting farther away from their start locations than they should.
If anyone can help me with this you will save me from having to top myself after going mad trying to solve the problem.
Alternatively, if you could offer a better solution for both creating 8-direction movement for NPC's and creating destination coords while keeping each NPC a seperate entity.
Also my apologies if I haven't typed out the logic in a format that's understood. Am new to these forums and haven't learned the lingo yet :P