0
\$\begingroup\$

I've to decide how to code enemy spawn: quantity and time. What's a good approach to do that ?

Maybe a class like this ?

class Level {
   int   levelNr ;
   int   soldierQty; // number of enemy soldier 
   int   aircraftQty; // number of aircraft 
   float maxTimeToComplete;
   ... 
   ...
}

Then, in main code

   Level[100] levels; 

   levels[0].levelNr = 1;
   levels[0].soldierQty = 5;
   levels[0].aircraftQty = 3;
   levels[0].maxTimeToComplete = 10; // 10 seconds
   levels[1].levelNr = 2;
   etc etc etc 

Is it a good approach ? Is there a different way I haven't thought ?

Thanks

\$\endgroup\$
  • 2
    \$\begingroup\$ Test it out / playtest and find what feels like a challenging but not frustrating level.. toy with the numbers yourself, there is no right answer \$\endgroup\$ – n_plum Mar 8 '17 at 19:44
  • \$\begingroup\$ My question is not about "how to set difficult", but how to set levels .. hard coding isn't a good approach maybe... \$\endgroup\$ – stighy Mar 8 '17 at 20:38
  • 1
    \$\begingroup\$ Well it's wording doesn't suggest that right now \$\endgroup\$ – n_plum Mar 9 '17 at 0:20
2
\$\begingroup\$

There isn't a perfect answer here. But one thing to note is that it isn't great to put 'magic numbers' in the code.

This is also referred to as hard coding. It would be better to get the values for the number of enemies etc from a data file by deserialising it. That way you don't need to change the code each time you need to make a change, you can just update the data file.

This makes your code more maintainable and more easily extended.

\$\endgroup\$
1
\$\begingroup\$

You might want to look if it is possible to calculate the numbers for each level at runtime. That's likely far less typing, less prone to errors and has the advantage that you can have infinitely many levels.

I would then have a constructor for Level which just takes the level number and calculates all the variables from it. Something like this:

class Level {
    int   levelNr ;
    int   soldierQty; // number of enemy soldier 
    int   aircraftQty; // number of aircraft 
    float maxTimeToComplete;

    public Level (int number) {
        this.levelNr = number;
        this.soldierQty = number * 2 + 3;
        this.aircraftQty = number / 2 - 5;
        this.maxTimeToComplete = 600.0f / (number + 9);    
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.