Spawner

local Spawner = require("encompass").Spawner

A Spawner is an Engine which is responsible for reading a SpawnMessage to create an Entity.

Spawners are defined by the Message prototype they track, and the spawn function they implement.

It is an anti-pattern to read Entities or Components from inside a Spawner. Do not do this.

Function Reference

Spawner.define(name, message_type)
Arguments:
  • name (string) – The name of the Spawner.
  • message_type (prototype) – A Message prototype that should be tracked by the Spawner.

Defines a Spawner that will track the given Message prototype.

Spawner:initialize(...)
Arguments:
  • ... (args) – An arbitrary list of arguments.

This callback is triggered when the Spawner is added to the World. Useful for Spawners that need to deal with side effects, such as physics worlds or particle systems.

Spawner:create_entity()
Returns:Entity An instantiated entity.
Spawner:spawn(message)
Arguments:
  • message (SpawnMessage) – A reference to a message that has been tracked by the Spawner.

This callback is triggered when a Message of the specified prototype is produced by a Detecter. The programmer must override this function or an error will be thrown.

Example

local SoundComponent = require('game.components.sound')
local SoundSpawnMessage = require('game.messages.spawners.sound')

local Spawner = require('encompass').Spawner
local SoundSpawner = Spawner.define('SoundSpawner', SoundSpawnMessage)

function SoundSpawner:spawn(sound_spawn_message)
    local source = sound_spawn_message.source

    local sound_entity = self:create_entity()
    sound_entity:add_component(SoundComponent, {
        source = source
    })

    source:play()
end

return SoundSpawner