Skip to content

Commit

Permalink
feat: Adding ability for a SpawnComponent to not auto start (#2947)
Browse files Browse the repository at this point in the history
`SpawnComponent` didn't had the ability to not auto start. This PR adds
a new attribute in order to customize that.

Co-authored-by: Lukas Klingsbo <me@lukas.fyi>
  • Loading branch information
erickzanardo and spydon committed Dec 21, 2023
1 parent 6ee9ffe commit 37c7a07
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/flame/lib/src/components/spawn_component.dart
Expand Up @@ -25,6 +25,7 @@ class SpawnComponent extends Component {
this.area,
this.within = true,
this.selfPositioning = false,
this.autoStart = true,
Random? random,
super.key,
}) : assert(
Expand All @@ -46,6 +47,7 @@ class SpawnComponent extends Component {
this.area,
this.within = true,
this.selfPositioning = false,
this.autoStart = true,
Random? random,
super.key,
}) : assert(
Expand Down Expand Up @@ -102,6 +104,9 @@ class SpawnComponent extends Component {
/// The amount of spawned components.
int amount = 0;

/// Whether the timer automatically starts or not.
final bool autoStart;

@override
FutureOr<void> onLoad() async {
if (area == null && !selfPositioning) {
Expand Down Expand Up @@ -147,6 +152,7 @@ class SpawnComponent extends Component {
updatePeriod();
amount++;
},
autoStart: autoStart,
);
timer = timerComponent.timer;
add(timerComponent);
Expand Down
26 changes: 26 additions & 0 deletions packages/flame/test/components/spawn_component_test.dart
Expand Up @@ -140,5 +140,31 @@ void main() {
isTrue,
);
});

testWithFlameGame('Does not spawns when auto start is false', (game) async {
final random = Random(0);
final shape = Rectangle.fromCenter(
center: Vector2(100, 200),
size: Vector2.all(200),
);
final spawn = SpawnComponent(
factory: (_) => PositionComponent(),
period: 1,
area: shape,
random: random,
autoStart: false,
);
final world = game.world;
await world.ensureAdd(spawn);
game.update(1.5);
await game.ready();
expect(world.children.length, 1);

spawn.timer.start();

game.update(1);
await game.ready();
expect(world.children.length, 2);
});
});
}

0 comments on commit 37c7a07

Please sign in to comment.