The function you chose is not likely to be suitable for your purpose. First, if you go this route a failed random condition should result in a delay before the next time you roll the dice. Also, it is unlikely that keeping the spawn timer within a constant range good for your cause.
Let's think of Math.random()
as dice. Now let's figure out the rules we'll use to increase spawn rate as time goes bygoal.
Let thresh
be a difficulty threshold you think is high. We use that later to decide when to slow down the pace at which the game gets harder.
Let rate
be the amount of monsters you want coming in every minute or so(or round).
Let GROWTH_RATE
be the rate in which difficulty increases (for now it'll be 0.2
).
LetsLet's say you start with rate = 10
. Now the player killed 5
monsters so you can increase rate by GROWTH_RATE * 5 = 1.0
and the new rate = 11
.
How to implement a condition that will generate rate
monsters every minute:
Set spawn timer to a number between 0.5
to 1.0
*multiplied by 60 seconds/ rateor
. Also have a round-time0and divided by
rate.25 Also leave a
change0.25` chance that no monster is spawned when the timer reaches 0 and the time is randomized again.
ifIf rate
ever reaches thresh
you need to slow down. Now instead of increasing rate
by GROWTH_RATE
, you could increase it by 1 / math.sqrt(rate)
. That way the player won't be destroyed instantly in the harder setting.
You can reset thresh
to be around 80% of the rate
at which the player lost the game.
If you wish to randomize the strength of the monster be cautious about the timer.
For instance, if you decide that player-score
(determined by monsters killed so far) will be used to determine the max strength of a monster that could spawn. You, you can do something like this: max-monster-strength = player-score / 1000
. Then randomize a float
or double
between 0.0
to 1.0
and multiply the result by itself.
float monster_power = Math.random();
monster_power *= monster_power; // Multiply result by itself
// Makes hard monsters less frequent than weak ones
monster_power *= max_monster_strength;
Now when you randomize the timer you should probably take the power level into account. For instance you could multiply the timer result by the square root of the next monsters power.
spawn_timer *= math.sqrt(monster_power);