Arrays and game levels
-
Hello all,
I'm just posting to get some opinions (as I'm probably doing this completely a..e backwards as usual.)The amount of enemies in my game varies level per level, so I'm wondering what is a decent way of dealing with this?
Should I simply recreate the enemy array every level and increase it when needed or is that a really dumb idea?
-
Is it a randomised or fixed number of enemies in each level?
If it's fixed, you might want to add a dimension to your enemies array, and use the extra dimension as the current level. Let's say you want to have set starting positions for each enemy too. I'm assuming you want each enemy to have properties like a position, hp and perhaps a type or something, which you'd use in your main loop to draw and animate different enemy types. The contents of the array aren't really important, it's more the structure I'm making a point about:
enemies = [ [ // level 1 - we have three enemies, two of the type "1" and one of type "2" [.pos = {200, 300}, .type = 1, .hp = 100], [.pos = {700, 300}, .type = 1, hp = 100], [.pos = {400, 600}, .type = 2, hp = 200] ], [ // level 2 - four enemies, one of each type [.pos = {200, 300}, .type = 1, hp = 100], [.pos = {700, 300}, .type = 2, hp = 200], [.pos = {400, 600}, .type = 3, hp = 200], [.pos = {400, 600}, .type = 4, hp = 400] ] ]
Now you could do something like this:
currentLevel = 0 for i = 0 to len(enemies[currentLevel]) loop // do all the stuff you need, for eg: enemies[currentLevel][i].pos.x += 1 // move each enemy in the current level toward the right of the screen repeat
-
You could initialize the size of your array with the maximum amount of enemies possible. For example
maxEnemies = 50 array enemies[maxEnemies]
And while you're playing a certain level with 24 enemies (for example) limit your loop to those 24.
for i = 0 to levelEnemies loop //Do enemy related stuff here repeat
Given the fact you're storing those different enemy counts in a variable.
This way you could save the time that is needed to recreate the enemy array every level.
It would even be possible to increase the enemy count with every level likelevelEnemies = 10 + ((level*2) % 3) if levelEnemies > maxEnemies then levelEnemies = maxEnemies
Or something like that.
*€dit: And someone from the team was faster...again :-D
-
Thank you all! very helpful as usual.