# 2D Shooter Enemy Spawning

## Recommended Posts

For a vertical scrolling shoot 'em up e.g. SWIV, Radiant Silvergun etc. I'm wondering about how to synchronize enemy "formations" so that they look right after they've spawned. Like if you wanted a diagonal line of enemy planes to fly down the screen in formation at the player. What concerns me is, obviously these planes have to be spawned at a specific time in the game. For instance, they might be spawned when they're 2 map tiles off screen. So, depending on how quickly the map is scrolling and how fast the enemies are, that could result in a very staggered formation. They might be one diagonal tile away from each other but when 1 gets spawned it might shoot off and be miles away before the 2nd or 3rd in the line are spawned. How might this be solved? It's not hugely important I suppose but it seems a common enough concept in these kinds of games. I had the idea of arranging enemies in groups but that seems very time consuming and tedious, not to mention difficult with my basic in-game level editor.

##### Share on other sites
fng    154
Hi Sean_Seanston,

for such a game you probably would give the enemies strict motion patterns, e.g. fly forward for 3 seconds, fly to the right for 5 seconds, fly forward for 3 seconds, etc. If you then spawn a group of enemies simultaneously (read: in the same frame) they will always stay in the formation they were spawned. If you spawn them with a short delay (e.g. 1 second) they all follow the same line.

Cheers,
fng

##### Share on other sites
Quote:
 Original post by fngIf you then spawn a group of enemies simultaneously (read: in the same frame) they will always stay in the formation they were spawned.

That's the thing, if we say that enemies who are 2 tiles away from the top of the screen will be spawned but the other enemies in formation are higher, the first 1 is going to be spawned earlier.

Unless I was to in some way split the map into sections and spawn everything in a certain section at once, then make sure to always put formations together but that seems a fairly strange way of going about it and possibly troublesome.

##### Share on other sites
I assume you have your usual method of spawning an enemy. Suppose it looks like this:

Enemy * e = someFactory->spawnNewEnemy("helicopter");e->setPosition(.....);e->setVariousOtherStuff(....);

You need a way to store patterns of things to be spawned. Try this:

struct EnemySpawnInstruction{    string enemyType;    vec2 relativePosition;    .... various other properties ...};struct EnemySpawnList{    void addEntry(EnemySpawnInstruction e){instructions.push_back(e);}    void setGroupPosition(vec2 position){groupPosition = position;}    SomeStorageContainer instructions;        vec2 groupPosition;};

You can build an enemy spawn list by iterating through the storage container, instancing a monster from the name and properties of each EnemySpawnInstruction, and then adding the groupPosition to the position in the EnemySpawnInstruction.

Using a bit of inheritance, you could have various types of EnemySpawnList which extend the base class EnemySpawnList but in their constructors, they add some default enemies. Or, you could have one which loads a file and takes its data from there for extra customisability.

##### Share on other sites
So you'd be in favour of a sort of spawn group system, if I understand correctly?

Have Spawn Lists, add enemies to a spawn list and then the game spawns by Spawn Lists rather than individual enemies?

One problem I have with this method is the basic editor I've made. I don't think something like this would be very easy to implement though I suppose it could be shoved in somewhat sloppily but functionally if I tried.

An idea I just had... I don't know how feasible it would really be: What if the game was made to detect patterns automatically? It would see what looked like a group of enemies that were meant to spawn together and then spawn them all at once. It might be hard at all.

The game might read the level info, find an enemy and then check if there's an enemy on the next row of tiles. If there is, it keeps going until it finds a gap. The gap with no enemies indicates the end of a spawn group and so it spawns all of the enemies it's found from the first to the one before the gap.

Sound good? I can't believe I didn't think of that idea before. There's no horribly glaring flaw is there? If it could be done, it would be less fiddly and the editor doesn't have to change. Shouldn't be too hard, right?

##### Share on other sites
Quote:
 Original post by Sean_SeanstonSo you'd be in favour of a sort of spawn group system, if I understand correctly?Have Spawn Lists, add enemies to a spawn list and then the game spawns by Spawn Lists rather than individual enemies?One problem I have with this method is the basic editor I've made. I don't think something like this would be very easy to implement though I suppose it could be shoved in somewhat sloppily but functionally if I tried.An idea I just had... I don't know how feasible it would really be: What if the game was made to detect patterns automatically? It would see what looked like a group of enemies that were meant to spawn together and then spawn them all at once. It might be hard at all.The game might read the level info, find an enemy and then check if there's an enemy on the next row of tiles. If there is, it keeps going until it finds a gap. The gap with no enemies indicates the end of a spawn group and so it spawns all of the enemies it's found from the first to the one before the gap.Sound good? I can't believe I didn't think of that idea before. There's no horribly glaring flaw is there? If it could be done, it would be less fiddly and the editor doesn't have to change. Shouldn't be too hard, right?

It sounds simple in theory.... but I'm not sure how you would work out the logic. There would be lots of edge cases where it looks like one pattern has been started when in reality its a single lone ship left stuck behind. My system lets you build very intriciate patterns with ease and plonk them down anywhere you like; I really think pattern matching is a lot harder than loading an XML file but thats my opinion.

Your file could be structured thusly. This one has two lines of 6 ships spawning simultaneously. Iterate them in order:

<spawnlist name="zomg">    <spawnentry ship="helicopter" xrel="12" yrel="0" pause="0"/>    <spawnentry ship="helicopter" xrel="-12" yrel="0" pause="0"/>    <spawnentry ship="helicopter" xrel="12" yrel="12" pause="20"/>    <spawnentry ship="helicopter" xrel="-12" yrel="12" pause="0"/>    <spawnentry ship="helicopter" xrel="12" yrel="24" pause="20"/>    <spawnentry ship="helicopter" xrel="-12" yrel="24" pause="0"/>    <spawnentry ship="helicopter" xrel="12" yrel="36" pause="20"/>    <spawnentry ship="helicopter" xrel="-12" yrel="36" pause="0"/>    <spawnentry ship="helicopter" xrel="12" yrel="48" pause="20"/>    <spawnentry ship="helicopter" xrel="-12" yrel="48" pause="0"/>    <spawnentry ship="helicopter" xrel="12" yrel="60" pause="20"/>    <spawnentry ship="helicopter" xrel="-12" yrel="60" pause="0"/></spawnlist>

Its more complicated than the example I gave earlier but it allows you to have a 20ms delay between each "wave" of that formation being spawned. In your editor, you would just plonk down some object to indicate that a "zomg" spawn list is to be spawned there.

##### Share on other sites
Quote:
 Original post by speciesUnknownI really think pattern matching is a lot harder than loading an XML file but thats my opinion.

My main concern is how I'd create the XML file. I can't really hand code it all since that would take ages and be hard to imagine as a playable level.

Quote:
 Original post by speciesUnknownYour file could be structured thusly. This one has two lines of 6 ships spawning simultaneously. Iterate them in order:

Hmmm... from looking at that, I get the idea that maybe I could just place all enemies individually and then group them in the XML by hand. Just a simple <group> tag or something might work even. Just something to tell the game that it should spawn all of them at the same time. Might be tedious though depending on the amount of enemies which could be a lot.

Quote:
 Original post by speciesUnknownIts more complicated than the example I gave earlier but it allows you to have a 20ms delay between each "wave" of that formation being spawned.

Would it not be simpler to code if you just put 2 separate spawn lists instead of 1 with 2 waves? There wouldn't be a need for any wave system to be coded then. Might be easier to read too. Maybe I'm missing something.

Quote:
 Original post by speciesUnknownIn your editor, you would just plonk down some object to indicate that a "zomg" spawn list is to be spawned there.

Right, so you'd have a sort of library of predefined formations. Not a bad idea... I hadn't thought of that. I suppose you could have the editor give the ability to place individual enemies of a type or certain predefined formations.

##### Share on other sites
The more I've thought about it since, the more I'm inclined to agree with your system of creating predefined patterns which can then be placed into a level. It would solve the problem and really apart from a small bit of work getting the system going it wouldn't involve any extra work since you'd only be defining a pattern once at most as opposed to many times with another system that grouped individual entities. Should be workable with my rudimentary editor too.

So unless someone else wants to chime in with a different idea, I think I'll go with that, thanks.

##### Share on other sites
why xml? why cant you save it all to a txt file? thats what i did for my game. all i did was create a level editor attatched to the game (inaccessible by others via button combinations and password) and send all data to a txt file. after that, i programmed it so that the game automatically loaded the files without rendering any code. an example of this is:

-0.000000 -0.000000 1.000000
0
5
3 0.000000 -2.000000 0.000000 1.000000 1.000000 1.000000 20.000000 1.000000 1.000000 1 0.000000 0.000000 0.000000
10 0.000000 -2.000000 -15.000000 1.000000 1.000000 1.000000 5.000000 20.000000 20.000000 1 0.000000 0.000000 0.000000
6 0.000000 18.000000 -33.000000 1.000000 1.000000 1.000000 16.000000 1.000000 1.000000 1 0.000000 0.000000 0.000000
11 0.000000 18.000000 -39.000000 1.000000 1.000000 1.000000 16.000000 1.000000 1.000000 2 0.000000 0.000000 0.000000
14 0.000000 10.000000 0.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1 0.000000 0.000000 0.000000

the first line is the background color,
the second is just a switch to turn fog on or off.
the third shows how many objects we will be loading, then the rest are objects.

the number at the beginning of the object is the object type, then the other numbers are its x, y, z, r, b, g, length, width, height, some objects warp the player or require extra data so you have next_level, next_x, next_y, and next_z;

in my game all the data is saved to multiple arrays and i have a statement that increments and integer (for statement) and lists all the data necessary.

##### Share on other sites
Quote:
 Original post by dantheman1337why xml? why cant you save it all to a txt file? thats what i did for my game. all i did was create a level editor attatched to the game (inaccessible by others via button combinations and password) and send all data to a txt file. after that, i programmed it so that the game automatically loaded the files without rendering any code. an example of this is:-0.000000 -0.000000 1.000000053 0.000000 -2.000000 0.000000 1.000000 1.000000 1.000000 20.000000 1.000000 1.000000 1 0.000000 0.000000 0.00000010 0.000000 -2.000000 -15.000000 1.000000 1.000000 1.000000 5.000000 20.000000 20.000000 1 0.000000 0.000000 0.0000006 0.000000 18.000000 -33.000000 1.000000 1.000000 1.000000 16.000000 1.000000 1.000000 1 0.000000 0.000000 0.00000011 0.000000 18.000000 -39.000000 1.000000 1.000000 1.000000 16.000000 1.000000 1.000000 2 0.000000 0.000000 0.00000014 0.000000 10.000000 0.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1 0.000000 0.000000 0.000000the first line is the background color,the second is just a switch to turn fog on or off.the third shows how many objects we will be loading, then the rest are objects.the number at the beginning of the object is the object type, then the other numbers are its x, y, z, r, b, g, length, width, height, some objects warp the player or require extra data so you have next_level, next_x, next_y, and next_z;in my game all the data is saved to multiple arrays and i have a statement that increments and integer (for statement) and lists all the data necessary.

It doesnt have to be XML, but with XML you can write up complex structures quickly and use a parser such as tinyxml; as the complexity of your file format increases you will find that the conceptual overheads of using XML become irrelevent.

That said, for files which I know will never be more than a flat list, I often use my own parser / format.

##### Share on other sites
oh that makes sence, just had to look at it from another perspective