Basics

Guides

API Reference

Menu

Basics

Guides

API Reference

class: EntityFactory

[43:7] extends: FxObj

Wraps FXGL's EntityFactory marker interface (com.almasb.fxgl.entity.EntityFactory). The upstream API is annotation-driven (@Spawns("name") on each spawner method); Aussom code cannot synthesize Java annotations, so the wrapper takes a different path:

  1. Construct via fxgl.entityFactory() — produces an empty AussomEntityFactory marker.
  2. Register per-name spawners with addSpawner(name, callback). The callback receives a SpawnData wrapper and must return an Entity wrapper.
  3. Attach to the running GameWorld with attachTo(world). The factory registers itself so GameWorld.create's entityFactories.isNotEmpty() check passes; AussomEntityFactoryHelper then installs each spawner directly into the GameWorld's spawner map. Spawners added after attachTo are installed immediately. Spawners added before are buffered and flushed during attachTo. Wiring: ef = fxgl.entityFactory(); ef.addSpawner("player", ::makePlayer); ef.addSpawner("enemy", ::makeEnemy); ef.attachTo(fxgl.getGameWorld()); ... fxgl.getGameWorld().spawn("player"); // calls makePlayer where makePlayer(spawnData) returns an Entity wrapper.

Members

  • pendingNames

    Buffered spawn names, flushed during attachTo.

  • pendingCbs

    Buffered spawn callbacks, paired with pendingNames.

  • world

    GameWorld wrapper this factory is attached to (null when detached).

Methods

  • EntityFactory (Ajo = null)

    Wraps an existing AussomEntityFactory AJO (the usual entry, since fxgl.entityFactory() returns a wrapper with the AJO already populated). Initialises empty buffers; spawners registered before attachTo are held here and flushed on first attach.

    • @p Ajo is an optional AussomJavaObject around an AussomEntityFactory.
  • adopt (object Ajo)

    Wraps an existing EntityFactory AussomJavaObject.

  • addSpawner (string Name, callback Cb)

    Registers a spawner under the given name. The callback receives a SpawnData wrapper and must return an Entity wrapper. When the factory is already attached to a GameWorld, the spawner is installed immediately; otherwise it is buffered and flushed during attachTo. Registering the same name twice replaces the prior callback (matches upstream's last-writer-wins semantics when a single factory is rescanned).

    • @p Name is the spawn name (use the same string in GameWorld.spawn(name, ...)).
    • @p Cb is a callback (spawnData) -> Entity wrapper.
    • @r this object
  • removeSpawner (string Name)

    Removes a previously registered spawner. No-op when the name was never registered or has already been removed. Works whether or not the factory is attached.

    • @p Name is the spawn name to remove.
    • @r this object
  • attachTo (object WorldObj)

    Attaches this factory to a GameWorld so spawn names resolve through it. Registers the underlying AussomEntityFactory with the world (satisfies the entityFactories.isNotEmpty() check inside GameWorld.create) and flushes every buffered spawner. Calling attachTo a second time against the same world is harmless; calling it against a different world re-buffers the existing spawners onto the new world (the old world still has them — call detach() first to clean up).

    • @p WorldObj is a GameWorld wrapper.
    • @r this object
  • detach ()

    Removes every spawner this factory installed from the attached GameWorld and deregisters the factory itself. Spawners are NOT re-buffered — call addSpawner again before the next attach.

    • @r this object
  • hasSpawner (string Name)

    Returns whether a spawner is registered under the given name on the attached GameWorld. Returns false when the factory is not attached.

    • @p Name is the spawn name.
    • @r A bool.