-
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 EntitySpawners which defines its behaviour. The EntitySpawners' behaviour can be customised by configuring the parameters passed to class' constructor. An explanation of how this works follows.
- The time that the spawning will occur falls randomly in the interval [
spawnHour
,spawnHour
±randomRange
] - The initial number of entities spawned in a spawn cycle is
initialSpawnCount
and will grow linearly each spawn cycle bygrowthRate
until it reachesmaxSpawnCount
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
. This configuration spawns in initially 4 chickens every second day between 8am and 12pm. After each spawn, the number of chickens spawned per cycle will increase by 1 until it spawns 6 chickens per spawn.
//Spawning behaviour for passive animals
List<EntitySpawner> passiveSpawners = new ArrayList<>();
passiveSpawners.add(new EntitySpawner(6, NPCFactory::createChicken, player,
1, 4, 8, 4, 2));
passiveSpawner = new EntitiesSpawner(passiveSpawners);
passiveSpawner.setGameAreas(this);
//Initial spawns
passiveSpawner.spawnNow();
passiveSpawner.startPeriodicSpawning();