Skip to content

EntitiesSpawner

27abdullah edited this page Oct 3, 2023 · 13 revisions

Introduction

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.

Configuring spawner

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 by growthRate until it reaches maxSpawnCount

Usage

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.

Example

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();
Clone this wiki locally