spawning enemies

Blitz3D Forums/Blitz3D Beginners Area/spawning enemies

stayne(Posted 2008) [#1]
At the moment I'm spawning small groups of enemies randomly via Rnd(1,5) every 4 seconds and that's the way I want to keep it, but I only want 40 to spawn. Any ideas on the math? Quick and dirty example of what I mean...

Const maxsprites = 40

If TimeToSpawn = True
tmp = Rnd(1,5)
SpawnSprites(tmp)
Endif


PowerPC603(Posted 2008) [#2]
Use a global to keep track of your created enemies.
Const maxsprites% = 40
Global TotalEnemies%

If TimeToSpawn = True Then
	; Generate a random number of enemies
	tmp = Rnd(1,5)

	; Check to see if these enemies can be added (current number of enemies + the ones to be added must be lower than 40)
	If (TotalEnemies + tmp) <= maxsprites Then
		; Spawn the enemies
		SpawnSprites(tmp)
		; Add the new number of enemies to the current number of enemies
		TotalEnemies = TotalEnemies + tmp
	Else
		; The generated number of enemies cannot be added (the total number would be bigger than 40)
		If TotalEnemies < maxsprites Then
			; Calculate the remaining number of enemies that can be spawned (until 40)
			tmp = maxsprites - TotalEnemies
			; Spawn them
			SpawnSprites(tmp)
			; Add them to the total number of enemies
			TotalEnemies = TotalEnemies + tmp
		EndIf
	EndIf
EndIf 


I haven't tested this, but it should work.

This code should spawn enemies until the maximum of 40 is reached.

Afterwards, when you kill an enemy, you should decrease TotalEnemies with 1, so the code could generate a new enemy to replace the one you just killed.


Mikel(Posted 2008) [#3]
This is very similar and just another way to do it. It should work but may need to be tweaked.

Global TotalSprites% = 0

Const maxsprites% = 40

IF TimeToSpawn = True and TotalSprites% < maxsprites% THEN

  IF TotalSprites% <= (maxsprites% - 5) THEN
  
    tmp% = Rnd(1,5)
    SpawnSprites(tmp%)
    TotalSprites% = TotalSprites% + tmp%

  ELSE

    tmp% = Rnd(1, maxsprites% - TotalSprites%)
    SpawnSprites(tmp%)
    TotalSprites% = TotalSprites% + tmp%

  ENDIF

ENDIF