views:

54

answers:

1

I'm looking for some idea's how best to store spawn information for a side scroller. The game I'm writing is very similar to Super Mario Bros. What I'm trying to implement is as you move through a level, enemies should spawn at particular locations. I want to store this information in the level and I don't want to just randomly spawn enemies. I'm looking for a high level strategy to store and retrieve the spawned objects when the player reaches a particular location (like mario). I couldn't find any decent tutorials or suggestions from Googleing around.

The Level is tile based so the map is Tile[][]. Each tile right now is 16x16 pixels.

The only good suggestion I've seen so far is to store the the spawning information in a 2d array where the first dimension is x and the second is y, given in tile space. The issue I have with that implementation is that if my player is at say (160, 0) I want to spawn a particular enemy. If however next time the level is reset the player is at (160, 5) I want to spawn the same enemy. So for that implementation to work I have to do a scan for a given y position.

I'm looking for a more efficient storage mechanize. I need something where I can retrieve the correct spawns quickly and not waist too much memory storing spawning locations.

Are there any common strategies in games for this?

+2  A: 

As long as there are not too many spawn-points, a simple list will do. Each list entry has the XY coordinates and spawn info. Just iterate through the list to determine whether player is near a spawn point. This is much more memory efficient than a sparsly filled 2D array, very easy to implement, and probably fast enough.

If it turns out to be not fast enough you need some kind of spatial index, kd-trees are rather simple to implement and offer good performance for proximity search. Another solution is to use a locality sensitive hash, as explained in this paper (PDF).

haffax
Thanks for the detailed answer and providing links to other implementations. What do you think about going with a map (x, spawn) and then a list. Most levels will be much wider than taller.Most levels will prob. have between 15-50 spawns, so no not much, but I'm trying to avoid scanning the list each game update cycle.
Ruggs
Depends on how you want to determine when to spawn an enemy. If you go with something like: If the player is N tiles away, then a map doesn't help a lot, as you had to have N or in case of two-dimensional proximity N^2 map lookups. So if N or N^2 is larger than the number of spawn points it is more expensive.Better: Presort spawn points into buckets. Say an array of 10, where first elem holds list to spawn points in the first 10th of the map (x-wise) next for the second 10th and so on. So you need to only check the player's segment and the two adjacent ones. Array lookup is cheaper than map.
haffax
So what you're saying is to create an array 1/10th of the map width (in your example). So array[10] where the array holds a list of spawns within that x/10th of the map?
Ruggs
For example: Your level is 200 tiles wide. Then you have a an array MyListType spawnPoints[10]; where spawnPoints[0] has all the spawn points with an X in between 0 and 19, spawnPoints[1] all spawn points with X in [20;39] and so on. If your player's X is at tile 42, then you only need to check lists in (42 * 10) / 200 == 2 (integer arithmetic) so spawnPoints[2] has spawn points in player's segment, spawnPoints[1] left from the player, spawnPoints[3] right from the player. Less items to check and index calculation is very cheap. Number 10 is just an example.
haffax
Thanks, you've been very helpful.
Ruggs