flame-engine/flame

SpawnComponent enhancements

Closed this issue · 1 comments

Problem to solve

I often find the SpawnComponent not flexible enough for most of my game's requirements. Here are some problems I've encountered that the SpawnComponent is not functionally qualified for:

  1. Spawn count - I always have to create an additional TimerComponent to auto-dispose it.
  2. Custom target - I don't always want the spawner to spawn to its parent, but I do like to have it attached to a parent that will automatically remove the spawner when the parent is removed. For example, an enemy ship that spawns weapons should stop spawning weapons when it's destroyed, and the weapon spawner should not spawn to the enemy ship, but to the game world.
  3. Variable spawn times - I'd love to feed in a list of spawn times to the spawner of when to spawn. My game uses variable timed spawns quite often and I've had to build my own component to accommodate this.
  4. Reference to the parent in the factory callback - I need to be able to grab parent-related data when spawning children. Since I can't have access to the parent in the factory callback, I can't add the SpawnComponent into its parent's children constructor. This is mainly a developer experience thing that could be improved.

Proposal

New parameters:

  1. spawnCount - how many times to spawn before removing itself
  2. target - define the target you wish to spawn to. SpawnComponent should automatically spawn to parent, but can be configured to spawn to a different parent.
  3. spawnTimes - a list of doubles that denote when to spawn the components. The spawner would spawn new components based off of the passed-in ordered list of spawn times, the update loop would check if the accumulated delta time has reached or exceeded the time, and spawn the new component. A timer should be added onMount that will tick when the last spawn has occurred and remove the spawner automatically.
  4. New factory method callback param: 'owner'. In addition to passing the iterator, passing the parent back into the factory callback would allow for more elegant flexibility of spawning children. I'm not sure I'm using the correct nomenclature here so to be extra clear, here is an example:
SpawnComponent(
  period: .4,
  target: game.world,
  spawnCount: 12,
  factory: (owner, index) => NewPositionComponent(position: owner.absolutePosition),
);

More information

No response

Other

  • Are you interested in working on a PR for this?

I think 1-3 makes sense!
If owner should be something different than target, then I think that one should just take that out of the closure of the callback instead, then it'll be more dynamic.

final otherComponent = ...;

SpawnComponent(
  period: .4,
  target: game.world,
  spawnCount: 12,
  factory: (_) => NewPositionComponent(position: otherComponent.absolutePosition),
);