-
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 the 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();
![Screenshot 2023-10-16 at 1 14 38 pm](https://private-user-images.githubusercontent.com/140485817/275382081-b6a189a1-884d-4243-aaaf-2ca24396bbf7.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzQ1NTQ1OTAsIm5iZiI6MTczNDU1NDI5MCwicGF0aCI6Ii8xNDA0ODU4MTcvMjc1MzgyMDgxLWI2YTE4OWExLTg4NGQtNDI0My1hYWFmLTJjYTI0Mzk2YmJmNy5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQxMjE4JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MTIxOFQyMDM4MTBaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT00MzA2ZDZhYmNhZDQ3MmZkZTFhMDg0NjQyYzBlZmMxZjk3NzI0YWQyNzE5NTk3YmI5ZDAyY2M5MGFmYTQzNTY5JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.Akya02PLwVj3Df-nQEER1H3ZIkUCWMtP4Sc5A9gi86w)