Skip to main content

EntityFactory

EntityFactory is a protocol that all entity factories implement. It defines a single method for creating an entity in a given world.

public protocol EntityFactory {
@discardableResult
func make(in world: World) -> Entity
}

Each concrete factory is a struct that accepts its configuration via init, then creates and returns a fully wired entity when make(in: world) is called.

Concrete Factory's Usage

// 1. Construct the factory with configuration
let factory = EnemyEntityFactory(at: position, type: .mummy, baseScale: scale)

// 2. Call make to spawn the entity
let entity = factory.make(in: world)

The return value is @discardableResult — you can ignore it if you don't need a reference to the created entity.

Concrete Factories

FactoryCreates
PlayerEntityFactoryThe player character
EnemyEntityFactoryAn enemy of a given EnemyType
WeaponEntityFactoryA weapon attached to a player entity
ProjectileEntityFactoryA single projectile (e.g. bullet)
RoomEntityFactoryA room with bounds, doorways, and spawn points