This wonderful topic here construct.net/en/forum/construct-3/how-do-i-8/enemies-move-unoccupied-tile-156335 gave a good solution to deal with hex based player movement.
Here's how it works :
- player and enemy sprite are associated (with container) each with a location sprite (CharacterPath and EnemyPath)
- These sprite have a Pathfinding behavior directed for the player towards an hexagon on the board, and for the enemy towards the player
- When while moving the location sprite collides with an hexagon, the caracter/enemy sprite is put on this hexagon.
I managed to enhance this so that after player's movement, enemy moves in the player direction.
I now would like to have 3 enemies randomly spawn one after the other onto the board and have them all move towards the player. I was thinking of using a "For each enemy" loop to make them move but have no idea on how to do this.
Any idea ?
Here is the capx :
Hey Laurent - I made a quick For each video tutorial for you.
You will essentially just want to create the sprites called "your-name" then you can call
For Each "your name" and it will loop through each one allowing you to individually check each "your name" instance on the map.
You should already have your code set up to make the single enemy move - just replace it with what you know about For each and all of them will go.
Feel free to ask specifics if you get stuck.
Thank you very much for taking time to show me about For each!
Unfortunately, I'm afraid my engine would need a more complex use of for each as you can see in the capx provided with my original post.
Here's a capture of the enemy move routine that starts when the player movement is completed :
In my project, since enemies will be spawn randomly runtime, their UID won't be known in advance.
How can I adapt my engine for 3 enemies ?
Took me a while to decipher your code. From what I can see you need a For each for you enemy as well as a for each for your enemy path. This will stop your your enemies from both going to the same position.
That's wonderful, winstreak !
Thank you very much.
Have you an idea on how to avoid enemy overlaping on each other ?
Develop games in your browser. Powerful, performant & highly capable.
Just a thought:
You can have a check built in that see if there is more than 1 enemy overlapping.
If yes you have a built in to set one enemy to a position to the left. (You will need to check the position to the left is open before moving)...
You can also have to enemies overlap and show a text above the enemies saying 2 (or more)
Then if the enemy catches you with more than 1 enemy is does 2 X damage.
The point is that the enemies move together at the same time, so the pathfinding obstacle map is in constant changes.
So I thought about an approach moving the enemies one after another, like a turn based system, so that their pathfinding function would be calculated from a still obstacle map on screen.
The sequence would be so :
- The Enemies don't move.
- each hexagon where an Enemy stands becomes Solid, creating an obstacle map on the screen.
- Pathfinding for Enemy 1 is calculated
- Enemy 1 moves and then stops on a hexagon that becomes Solid. Obstacle map changes.
- Pathfinding for Enemy 2 is calculated from new obstacle map
- enemy 2 moves and then stops on a hexagon that becomes Solid. Obstacle map changes.
- Repeat until every Enemy has moved.
Unfortunately, I'm not good enough programming to achieve this.
I went deeper into this after my last post and I came out with a turn-based engine where enemies move one after the other.
Code is enclosed here :
So far, unfortunately, I have a problem with the pathfinding obstacle map generated from Enemies positions on the game board.
Here's is how the pathfinding routine works :
- Obstacles for Enemy pathfinding are : Terrain (here Water hexagons) and all other Enemies' hexagons
- Unfortunately, when every Enemy's hexagons are Solid (including current Enemy), pathfinding can not be process because current Enemy's own hex is Solid. Pathfinding stops here.
- So in order to have Pathfinding to work, we have to disable current Enemy hexagon but keep all the others enemies hex.
I tried several approaches using the functions in the SetBlockSolid group but none of them worked.