Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: Add removeWhere to Component #1878

Merged
merged 2 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/flame/lib/src/components/core/component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,15 @@ class Component {
components.forEach(remove);
}

/// Removes all the children for which the [test] function returns true.
void removeWhere(bool Function(Component component) test) {
children.forEach((component) {
if (test(component)) {
remove(component);
}
});
}

/// Remove the component from its parent in the next tick.
void removeFromParent() {
_parent?.remove(this);
Expand Down
26 changes: 26 additions & 0 deletions packages/flame/test/components/component_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ class _ParentOnPrepareComponent extends _OnPrepareComponent {
}
}

class _IdentifiableComponent extends Component {
final int id;

_IdentifiableComponent(this.id);
}

void main() {
final prepareGame = FlameTester(_PrepareGame.new);

Expand Down Expand Up @@ -344,6 +350,26 @@ void main() {
game.update(0);
},
);

testWithFlameGame(
'removeWhere removes the correct components',
(game) async {
final components = List.generate(
10,
_IdentifiableComponent.new,
);
game.addAll(components);
await game.ready();
expect(game.children.length, 10);
game.removeWhere((c) => (c as _IdentifiableComponent).id.isEven);
game.update(0);
expect(game.children.length, 5);
expect(
game.children.every((c) => (c as _IdentifiableComponent).id.isOdd),
true,
);
},
);
});

prepareGame.test(
Expand Down
3 changes: 1 addition & 2 deletions packages/flame_bloc/example/lib/src/game/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'package:flame/input.dart';
import 'package:flame_bloc/flame_bloc.dart';

import 'package:flame_bloc_example/src/game/components/enemy.dart';
import 'package:flame_bloc_example/src/game/components/enemy_creator.dart';
import 'package:flame_bloc_example/src/game/components/player.dart';
Expand All @@ -19,7 +18,7 @@ class GameStatsController extends Component with HasGameRef<SpaceShooterGame> {
newState.status == GameStatus.initial;
},
onNewState: (state) {
gameRef.children.removeWhere((element) => element is EnemyComponent);
gameRef.removeWhere((element) => element is EnemyComponent);
},
),
);
Expand Down