-
Notifications
You must be signed in to change notification settings - Fork 7
EntitiesSpawner
The entities spawner handles periodic spawning of multiple entities within the GameArea
. The spawner can be configured in various ways to customise the quantity of entities spawned and the frequency of their spawning.
The EntitiesSpawner
class takes a list of EntitySpawner which defines its behaviour.
To set up the EntitiesSpawner
, pass it a List<EntitySpawner>
containing all the entities that you want to spawned - configure per above section. From there, call setGameAreas(<GameArea to spawn entities on>)
on the EntitiesSpawner
. The spawner is now set up and can be used to spawn entities instantly or start periodic spawning. To start periodic spawning, call startPeriodicSpawning
. To spawn entities without waiting (which you may want to do for the initial spawning when setting up the game) call spawnNow
.
This example code takes place in create()
of SpaceGameArea
.
//Spawning behaviour for passive animals
List<EntitySpawner> passiveSpawners = new ArrayList<>();
passiveSpawners.add(new EntitySpawner(1, NPCFactory::createAstrolotl, player,
0, 1, 0, 0, 10));
passiveSpawners.add(new EntitySpawner(6, NPCFactory::createChicken, player,
1, 4, 8, 4, 2));
passiveSpawners.add(new EntitySpawner(5, NPCFactory::createCow, player,
1, 3, 12, 4, 1));
passiveSpawner = new EntitiesSpawner(passiveSpawners);
passiveSpawner.setGameAreas(this);
//Initial spawns
passiveSpawner.spawnNow();
passiveSpawner.startPeriodicSpawning();
java