So, I have a grid-based RPG. My NPCs will normally just sit there and play cards (pretend to play cards while technically being what you might all “asleep”), unless the player talks to them, or the player provokes them, by invading upon something that they are guarding (their personal possessions, their home, etc.). Then, they will become enemies, and chase the player.
But they have to pathfind very slowly, 2 gridsquares per second, and they have to move suddenly, rather than gradually sliding from gridsquare to gridsquare, so it matches the player’s pixel perfect movement.
My best idea of how to do that is to, instead of constantly pathfinding to the player, just finding the path and going one square in the direction they need to go every .5 seconds.
And I can’t change the core idea there, with the player having pixel-perfect, grid based movement, because this is a first-person 2D game. I also can’t do 3D because the whole point of everything is to employ a 2D viewport-rendering system I’ve come up with. To be clear, the game is actually top-down, but I need it to be grid-based and pixel perfect movement in order to render the scene in the viewport properly.
If you’re running into slowdown and you’re sure that pathfinding is causing the problem, then you could reduce the frequency of path updates. But you can’t just save one path and move along that because the player will inevitably move and then your path is not correct. Although, this could be desirable in certain situations, if you want a “blind” enemy, or perhaps you want an enemy that will complete the path if the player goes out of view, hoping to pick up the trail again.
The pathfinder creates a sort of array of nodes. When trigged you could trigger a pathfinder move action to the player, you could then change the path to the 2nd or whatever node using the get node X and Y based on the index. Someone can provide more details. I can’t right now.
You can choose between allowing diagonals. I can’t remember or test it now but I think when the option to do diagonals is off, it creates a node at every grid position. When it’s in the opposite position, it only creates nodes at every change of direction. That would create different spacing between nodes while the 1st version would create evenly spaced nodes.
When it reached its destination, you could then either change back to the idle mode or move another X nodes with an updated path. You probably need to check the number of nodes first…
No, the idea is that, every half second, the path will be updated, and immediately, the enemy will move a single square in the direction that the path found dictates.