I am working on a small 2D top down shooter for a game dev competition and was wondering what would be the most effective way to find spawn points for different objects, like the player itself, the enemies and some other game objects on a half-randomly created gameplay area?
The area has borders built by walls, the opposing walls cannot be closer to each other than X and cannot be further than Y, but between these values walls can go upwards, downwards, from left to right and the mix of these.
Collision detection works fine for each object, so you can say something like "if (player.Collide(wall[0] … ", and vica versa. As the gameplay area is not rectangle shaped, I cannot simply iterate through the upper walls, then the lower walls and check each position between them as doing so would result in a lot of areas checked that are NOT between the walls (so, they can be outside of the gameplay area). And obviously I want to avoid the followings:
Checking the same place more than once.
Running into an infinite loop.
Having loops that are executed longer and longer as more and more objects appear on the level.
Spawning on the same point every single time, because that point is “always” valid (so enemies would come from the same point every time).
Here a fairly old screenshot of one half-randomly generated level (it is still good to show you my problem):
Is there any general how-to guide on this? Or do any of you have any good thoughts I could use? I can solve this problem somehow, but I fear I will break one of the rules above.
Is your question: I have a world proceduraly/randomly generated, and i want to set spawn points for players/IA ? Or did I miss something ?
If so, you could add a mask for the walkable zones, this would reduce the spawning possibilities.
It also depends on the fact that spawn points are placed and fixed for the current game, or spawn points are not the same each time a spawn is required (to prevent spawn camping).
Another bonus would be to make the player not spawn among a pack of enemies.
Adding a mask is a great idea as I can do this during the creation of walls!
Taking this further I could store which spawn point has been used (and how many times), so when I get a new spawn point, I can order this list by descending order and first try to get a valid point from the least frequently used ones.
Theres is only a small problem with this approach: the objects have different sizes. So I either need to do this per object type, or need to come up with a better solution. I do not want to use up every single bits of the memory of the player’s machine.
And yes, it is another question to exclude those points that are “too near” to the player. Good thoughts, thank you!
As with most “management” systems, the best advice generally is to keep it simple.
Make sure that your map data includes some information about “spawn points” themselves and use pools to allocate new spawned items so that you are constantly recycling memory rather than allocating / destroying / reallocating.
Also with pools you should have a finite cap on how many there can be, so you will be able to gauge performance based on the size of the pool.
Have as many / few pools as you need for each item type, possibly better to split them up rather than one big spawn pool (depending on your implementation)
As for placement, have a simple function to determine placement based on distance from player and maximum placement count for a spawn point.
What I did for this game, I generated a list of spawn point for different things (as zombies are bigger then ammunition, so they have different spawn points), added a counter of how many times they have been selected, and in the game I filtered for the least used spawn points, get one randomly where the spawnable object does not collide with anything and then used that point (also increased its counter).
Predefined spawn point could only be used for less randomly generated levels, or with special level items that can be used for spawning, regardless where they are.
Pools are also a good idea, because even for a 2-3 monitor wide level, I had 2-3000 valid spawn points for my zombies and that is quite a lot.
You also gave me a good idea with the placement. Unfortunately I had to quickly code the solution, I had no time to properly design it, but this placement should have helped quite a lot on performance side, because I had issues with the actual spawning (I did simply too many collision checks and each spawn took ~0.5 sec, so the game stoped for half a second). I should have filtered the objects near the spawn point instead of checking all of them…