Merge branch 'main' into feat/spaceship-drop-tube

pull/79/head
RuiAlonso 4 years ago
commit 3e702e9d75

@ -30,8 +30,8 @@ void main() {
await game.ready(); await game.ready();
await game.ensureAdd(board); await game.ensureAdd(board);
final leftFlippers = board.findNestedChildren<Flipper>( final leftFlippers = board.descendants().whereType<Flipper>().where(
condition: (flipper) => flipper.side.isLeft, (flipper) => flipper.side.isLeft,
); );
expect(leftFlippers.length, equals(1)); expect(leftFlippers.length, equals(1));
}, },
@ -43,9 +43,8 @@ void main() {
final board = Board(); final board = Board();
await game.ready(); await game.ready();
await game.ensureAdd(board); await game.ensureAdd(board);
final rightFlippers = board.descendants().whereType<Flipper>().where(
final rightFlippers = board.findNestedChildren<Flipper>( (flipper) => flipper.side.isRight,
condition: (flipper) => flipper.side.isRight,
); );
expect(rightFlippers.length, equals(1)); expect(rightFlippers.length, equals(1));
}, },
@ -58,7 +57,7 @@ void main() {
await game.ready(); await game.ready();
await game.ensureAdd(board); await game.ensureAdd(board);
final baseboards = board.findNestedChildren<Baseboard>(); final baseboards = board.descendants().whereType<Baseboard>();
expect(baseboards.length, equals(2)); expect(baseboards.length, equals(2));
}, },
); );
@ -70,7 +69,7 @@ void main() {
await game.ready(); await game.ready();
await game.ensureAdd(board); await game.ensureAdd(board);
final kickers = board.findNestedChildren<Kicker>(); final kickers = board.descendants().whereType<Kicker>();
expect(kickers.length, equals(2)); expect(kickers.length, equals(2));
}, },
); );
@ -83,7 +82,7 @@ void main() {
await game.ready(); await game.ready();
await game.ensureAdd(board); await game.ensureAdd(board);
final roundBumpers = board.findNestedChildren<RoundBumper>(); final roundBumpers = board.descendants().whereType<RoundBumper>();
expect(roundBumpers.length, equals(3)); expect(roundBumpers.length, equals(3));
}, },
); );

@ -1,4 +1,3 @@
import 'package:flame/components.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
import 'package:pinball_theme/pinball_theme.dart'; import 'package:pinball_theme/pinball_theme.dart';
@ -21,41 +20,3 @@ extension DebugPinballGameTest on DebugPinballGame {
), ),
); );
} }
extension ComponentX on Component {
T findNestedChild<T extends Component>({
bool Function(T)? condition,
}) {
T? nestedChild;
propagateToChildren<T>((child) {
final foundChild = (condition ?? (_) => true)(child);
if (foundChild) {
nestedChild = child;
}
return !foundChild;
});
if (nestedChild == null) {
throw Exception('No child of type $T found.');
} else {
return nestedChild!;
}
}
List<T> findNestedChildren<T extends Component>({
bool Function(T)? condition,
}) {
final nestedChildren = <T>[];
propagateToChildren<T>((child) {
final foundChild = (condition ?? (_) => true)(child);
if (foundChild) {
nestedChildren.add(child);
}
return true;
});
return nestedChildren;
}
}

Loading…
Cancel
Save