2 Attachment(s)
Tower Defence - Dynamic Pathfinding for Enemies
This is an example I made demonstrating Dynamic Pathfinding for Multiple Duplicates.
I made it with a tower defence game in mind, though it can be easily applied to any game where pathfinding through obstacles is required. I noticed that people were asking how to make a 'true tower defence game' where the path that the enemies follow is dictated by the towers that the player builds.
This example uses the Path Finding extension to do just that.
Frame 1 contains automatic movement for all duplicates from their starting position to the destination. New walls/towers can be added at the mouse position at runtime by pressing 1.
Frame 2 contains a barebones template for using pathfinding for multiple duplicates in your games.
I started this example a few weeks ago just after releasing my Tower Defence Shoot Closest Enemy Example. I wanted to make it better before releasing it (optimizing the code etc.) however due to the recent interest in tower defence games I thought I would release it as soon as possible and perhaps update it later.
I had originally intended to combine these two examples and make an open source tower defence engine. But I hope you enjoy this example for now. I may still do that if I have time. :)
Re: Tower Defence - Dynamic Pathfinding for Enemies
This is nice but i dont like the fact that enemies are destroyed if they cant find a path. That would make the game too easy. So how do i prevent the player from building the towers so that they block the path? And if i sell a tower and it makes a path shorter, enemies should be able to find the shorter one. :)
If someone could help i would appreciate it :) Thanks!
Re: Tower Defence - Dynamic Pathfinding for Enemies
They are destroyed simply to demonstrate that you can detect when the path is blocked. It was not intended to be like that in a tower defence game. I just put that in so that you could add your own code in there to do whatever you want when a tower is blocking the path of the enemies.
What you could do is put down a "test" tower when you try to add a new tower. So whenever you add a tower you check using the Path Finding Extension if adding a tower there would block the path. If so, then don't allow the player to put a tower there.
The pathfinding is dynamic so if you shorten the path, they should also automatically update their path to be shorter.
Re: Tower Defence - Dynamic Pathfinding for Enemies
Thanks for the idea of the "test" tower.
But i made a quick test if i could sell towers:
-upon pressing "2"
Mouse pointer over "tower" ->destroy tower
So i made a hole in the center and after that blocked the 3 existing holes in the map. So there still was a hole for them to go but all the enemies were destroyed..
My head is empty and i just cant figure out what to do with this selling thing :D
Re: Tower Defence - Dynamic Pathfinding for Enemies
If you delete a tower you need to tell the pathfinding object that the grid space the tower was in is available again. It still thinks its blocked.
Re: Tower Defence - Dynamic Pathfinding for Enemies
Ok i told the pathfinding object when a spot that tower was sold is available again. But its kinda buggy and sometimes 1 of the enemies just stops moving at all... Its been a while since i really used mmf2 so if someone has time, could you please show the lines required in the event editor to make it work smooth?
I'm planning to make it something like this
http://www.onlinegamesquad.com/index.php?1085-1
Re: Tower Defence - Dynamic Pathfinding for Enemies
Re: Tower Defence - Dynamic Pathfinding for Enemies
Re: Tower Defence - Dynamic Pathfinding for Enemies
Why dont you use the Advanced Pathfinding Object instead?
Its a much better extension and stores all paths within the object itself. The only thing is you need .NET 2.0 or above (i think) for it to work.