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 EntitySpawner which defines its behaviour. EntitySpawner's can be configured in the following ways. spawn time interval []

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