Hi, I'm working on my game, and I've run into an issue with how to spawn enemies as the player moves around.
In Super Mario Bros 3, normal levels are hard-coded to maximum be up to 16 "screens" wide (and a static vertical height). So when it comes to spawning enemies as Mario moves, the game simply has all the enemy data neatly sorted according to their x position. The very first Goomba you encounter in a level is the first entry in the list, and the very last enemy you encounter in a level is the last entry in the list. In addition there is also 16 pointers (one for each "screen") that points to where the first relevant enemy in that corresponding screen is on the enemy list.
So if Mario is on "screen" 2, then the 2nd pointer is checked, which points to a point in the enemy list where the first relevant enemy is located. A tiny loop them checks that enemy's data and the next couple of enemies data to see if it's time to spawn them based on the x position. I assume these 16 pointers are to speed up the process, as a level can have up to 48 objects, and getting a jump start to the right location means the game doesn't need to loop over all 48 enemy entries.
However, my game uses multi-directional scrolling and has arbitrary-sized levels, so one level might be 1 screen high and 8 screens wide, while another might be 3 screens high and 3 screens wide, etc. So not only do I have to check for spawning enemies on both the x and y axis, but this neat sorting trick doesn't work anymore.
So I turn to you, what sort of technique can I use instead? Obviously I don't want to loop over every single enemy entry every frame and manually check x,y every time, it would be too much of a waste.
In Super Mario Bros 3, normal levels are hard-coded to maximum be up to 16 "screens" wide (and a static vertical height). So when it comes to spawning enemies as Mario moves, the game simply has all the enemy data neatly sorted according to their x position. The very first Goomba you encounter in a level is the first entry in the list, and the very last enemy you encounter in a level is the last entry in the list. In addition there is also 16 pointers (one for each "screen") that points to where the first relevant enemy in that corresponding screen is on the enemy list.
So if Mario is on "screen" 2, then the 2nd pointer is checked, which points to a point in the enemy list where the first relevant enemy is located. A tiny loop them checks that enemy's data and the next couple of enemies data to see if it's time to spawn them based on the x position. I assume these 16 pointers are to speed up the process, as a level can have up to 48 objects, and getting a jump start to the right location means the game doesn't need to loop over all 48 enemy entries.
However, my game uses multi-directional scrolling and has arbitrary-sized levels, so one level might be 1 screen high and 8 screens wide, while another might be 3 screens high and 3 screens wide, etc. So not only do I have to check for spawning enemies on both the x and y axis, but this neat sorting trick doesn't work anymore.
So I turn to you, what sort of technique can I use instead? Obviously I don't want to loop over every single enemy entry every frame and manually check x,y every time, it would be too much of a waste.