Sign in to follow this  

enemy spawning algorithm

This topic is 2840 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am making a small (3 level) horizontal shooter. I have 7 enemies to pick from, that all move and shoot in different patterns, thus giving them varying degrees of difficulty. I need a system for spawning these enemies. I want the harder ones to show up in level 2 and 3 of course so it gets more difficult as it goes on. Other then that i don't know how to pick when to spawn which enemies. I thought about doing a purely stats model, like the most common enemy shows up 50% of the time, and everyone else goes down from there, so the total equals 100% but i dont know how that would work with more then 1 enemy at a time. Thinking back to the days of horizontal shooters on the NES, it seemed like enemy positions and what enemies appear where in each level was pre-determined and would show up at the same spot every time through. I thought to make the game more interesting i would make enemy placement more random so what i need is an algorithm to determine when to place which enemies as the user moves through each level, placing the easier enemies more often then the harder ones. Anyone have any thoughts? Thanks.

Share this post


Link to post
Share on other sites
For positions of where they spawn I have to say you're probably better off with manual placement of enemies. But if you want to go random, you could simply partition space (from 1/2 a screen to a few or more screen lengths) and place some # of random enemies in each one, with random positions through the partition. The partitioning is to make spawning somewhat uniform.

For which enemies to use, just use a spawnlist. For each level, have a list of enemies that can spawn, with multiples of the easier ones. (Example below). Put them in an array, call a random Int of the array's Size, spawn that creature.

Example of first level list.

Enemy 1; enemy 1; enemy 1; enemy 2; enemy 3;
No enemies harder than enemy 3 show up. 60% of enemy 1, 20% of enemy 2/3.

It also makes it fairly easy to modify the spawning/lists, just add/remove whichever enemy you need more/less of. Have a list for each level/area.

Share this post


Link to post
Share on other sites
What i did for an asteroids style game with infinite levels was to use a point system where each enemy unit was worth a fixed amount of points and each level had a point budget (say 3 + level*5 points) , each unit also had a "weight" (from 0 to 1).

so at level 3 we had a budget of 18 points, and lets say our units are:

Unit1: 1 point 0.5 weight
Unit2: 3 points 0.3 weight
Unit3: 5 points 0.2 weight
Unit4: 10 points 0.2 weight

We then generate a random number between 0 and 1.2(the sum of the weights of the available units),

if the number is below 0.5 it spawns the first unit, removes one point from the budget and starts over, if its 0.5-0.8 it spawns the second unit, removes 3 points, and so on, once the budget falls below 10 points we ignore unit4 and use a random number between 0 and 1.0 instead.

This ofcourse meant that all units spawned at the start of the level but its fairly easy to add all units to a stack instead and then spawn them as the level progresses.

Then just count the number of units in the stack, place that many spawnpoints reasonably evenly spread on the level and just pop the stack as the the player approaches each spawnpoint.

Share this post


Link to post
Share on other sites
Dekasa -- I do like this method of placing enemies. However, the random placement sounds complicated so i'll go with manual placement at least until i get the rest of it down, then we'll see about randomizing the placement positions.

I also like the idea of a spawn list for each level. What i am thinking is to have each enemy have a unique int ID associated with it. Then the spawn list for each level would be an array of ints each int representing the unique id of 1 of the enemies. Then to spawn an enemy i randomly choose one of these ID's and thats the enemy that gets drawn at that spot. That way, the placement positions would always be the same but the enemy that would be placed there would be random, but limited to only those enemies who's IDs are in the spawn list for that level.

For example:

Enemies with ID's

1. Enemy1
2. Enemy2
3. Enemy3

spawnList for level 1:
1, 1, 1, 2, 3

Randomly pick a number, spawn the enemy who's ID matches that number at a given x, y corrdinate in the level.

When the enemy is about to be within the viewable area of the map, update and draw that enemy.

So all enemies are spawned when the level first loads at predetermined x, y positions, but they don't move or are drawn to the screen until they are on the screen with the player.


Does that sound like a good implementation of your spawnlist idea? Any tips or suggestions on this?

Thanks.

Share this post


Link to post
Share on other sites

This topic is 2840 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this