I'm trying to achieve enemy movement with the following rules:
Movement is restricted to a 32x32 grid.
- If the enemy is in the same row as the player it should turn left when the player is to its left or right when the player is to its right
- If the enemy is in the same column as the player it should turn up if the player is above it or down when the player is below it
- If both conditions are not true the enemy checks its distance to the players column and row and turns towards the player on the axis where the distance is closer.
This is what I have:
This works basically but not consistent. They mostly turn only when the player crosses them but not when skelAbsDistX becomes smaller then skelAbsDistY. Also I start with 2 enemies on the screen and then spawn a new enemy every 3 seconds. The first two enemies immediately start moving, the others don't, even when I set the initial value for their direction variable to "right". They only move when I cross them and sometimes just crossing is not enough, I have to move on their row or column for 1-2 tiles.
Any idea why this happens? Maybe the random spawning and the tile movement conflicts because they don't spawn correctly in the grid?