I’m new to gamedev, but I’m a javascript developer for many years. So I’m trying my luck with Phaser while making some games, but many times I question myself if what I’m doing is the correct way of doing it or not.
For example, I have a patrol logic where an enemy is on a platform and moves back and forth, the logic is pretty basic, just checks for a “floor” tile in front of it, if there’s one, then continue moving, otherwise change direction. The thing it, doing this every frame is very costly and it doesnt make much sense too. I’m considering pre-calculating all enemies routes in the create function, and then make the enemies movements “static” and save a lot of per-frame-processing power.
Hi,
There’s many way to do it:
If your enemy has an animation, you can listen to enemy.on(‘animationcomplete’, () => check if tile, it’s a bit better than every frame
Or you can use an invisible layer that collides with enemies.
Your solution seems good too, perhaps a bit more complex to implement.
I would tend to go the route where you create the paths yourself. Not only does it make the AI much quicker and more efficient, but it also drastically opens up your design space as well. Enemies can patrol in patterns that aren’t dependent on the length of the ledge, flying enemies can patrol without relying on platforms, etc.
Using this method, you can also automate the process so that you get the “patrol along the entire platform” behavior without the need to check on each frame. You can simply read in the level data in the create method, and then use the tile information to determine what the patrol parameters should be and feed them to your AI. For example, you might put a PlatformPatroller on a 5 tile wide platform starting in the middle at coordinates (4,5). Your create method could then look through the tile info and figure out that the enemy needs to patrol from (2,5) to (6,5). You then tell the AI to patrol between these two points. This process might be a little slow, but you only need to do it once as the game is loading so it won’t affect your game’s actual performance.