Creating an event at an increasing rate.
BlitzMax Forums/BlitzMax Beginners Area/Creating an event at an increasing rate.
| ||
I've been coding late yesterday and my mind is kind of shot. I'm working on a game at which enemies keep entering the room faster and faster. The first enemy should be created 2.75 seconds and this should get faster and faster until enemies are being created .75 seconds apart. I would like to spread this over a time period of 5 minutes so that when 5 minutes is reached enemies are spawning .75 seconds apart. This is a traditional style game where the difficulty is based on speed and number of enemies increasing... Somehow this is really hard to figure out how to handle. I'm going to take a quick nap I think... |
| ||
Have a variable called max_time and time. Each game frame, subtract the number of milliseconds since the last game frame from time. When it reaches 0 or lower, spawn an enemy, make time equal max_time and decrease max_time by an amount. Then check that max_time is at least 0.75 seconds. |
| ||
How do I work that subtraction so that after 5 minutes the spawn rate is at the fastest speed? |
| ||
const fastest = 750 ... 'after update if max_time < fastest then max_time = fastest |
| ||
Type Enemy Function Create() Print "Created an enemy" End Function Const StartRate:Float=2.75 Const EndRate:Float=0.75 Const LengthOfCycle:Float=300 ' KEEP EVERYTHING IN SECONDS FOR EASE OF READING Global CurrentRate:Float=StartRate Global MaxRateReached:Int=False Global TotalTime:Float=0.0 Global TimeSinceLastCreated:Float=0.0 Function Update(DT:Float) TotalTime:+DT TimeSinceLastCreated:+DT If TimeSinceLastCreated>=CurrentRate Create() TimeSinceLastCreated=0 If MaxRateReached=False Local Mult:Float=TotalTime/LengthOfCycle If Mult>1.0 Mult=1.0 MaxRateReached=True End If CurrentRate=StartRate-((StartRate-EndRate)*Mult) End If End If End Function End Type Repeat Global LastTime:Int=MilliSecs() Global ThisTime:Int Delay(5) ThisTime=MilliSecs() Local DT:Float=ThisTime-LastTime LastTime=ThisTime Enemy.Update(DT*0.001) Until KeyHit(KEY_ESCAPE) |
| ||
Ah ok then, thanks guys! :) |