refactor: bottom group blueprint

pull/224/head
Allison Ryan 3 years ago
parent a50a819af8
commit fbf880ecd2

@ -1,6 +1,7 @@
import 'package:flame/components.dart'; import 'package:flame/components.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_components/pinball_components.dart';
import 'package:pinball_flame/pinball_flame.dart';
/// {@template bottom_group} /// {@template bottom_group}
/// Grouping of the board's symmetrical bottom [Component]s. /// Grouping of the board's symmetrical bottom [Component]s.
@ -8,21 +9,15 @@ import 'package:pinball_components/pinball_components.dart';
/// The [BottomGroup] consists of [Flipper]s, [Baseboard]s and [Kicker]s. /// The [BottomGroup] consists of [Flipper]s, [Baseboard]s and [Kicker]s.
/// {@endtemplate} /// {@endtemplate}
// TODO(allisonryan0002): Consider renaming. // TODO(allisonryan0002): Consider renaming.
class BottomGroup extends Component { class BottomGroup extends Blueprint {
/// {@macro bottom_group} /// {@macro bottom_group}
BottomGroup() : super(priority: RenderPriority.bottomGroup); BottomGroup()
: super(
@override components: [
Future<void> onLoad() async { _BottomGroupSide(side: BoardSide.right),
final rightSide = _BottomGroupSide( _BottomGroupSide(side: BoardSide.left),
side: BoardSide.right, ],
); );
final leftSide = _BottomGroupSide(
side: BoardSide.left,
);
await addAll([rightSide, leftSide]);
}
} }
/// {@template bottom_group_side} /// {@template bottom_group_side}
@ -34,7 +29,8 @@ class _BottomGroupSide extends Component {
/// {@macro bottom_group_side} /// {@macro bottom_group_side}
_BottomGroupSide({ _BottomGroupSide({
required BoardSide side, required BoardSide side,
}) : _side = side; }) : _side = side,
super(priority: RenderPriority.bottomGroup);
final BoardSide _side; final BoardSide _side;

@ -47,7 +47,7 @@ class PinballGame extends Forge2DGame
unawaited(add(CameraController(this))); unawaited(add(CameraController(this)));
unawaited(add(Backboard.waiting(position: Vector2(0, -88)))); unawaited(add(Backboard.waiting(position: Vector2(0, -88))));
await add(Drain()); await add(Drain());
await add(BottomGroup()); await addFromBlueprint(BottomGroup());
unawaited(addFromBlueprint(Boundaries())); unawaited(addFromBlueprint(Boundaries()));
unawaited(addFromBlueprint(LaunchRamp())); unawaited(addFromBlueprint(LaunchRamp()));

@ -4,6 +4,7 @@ import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_components/pinball_components.dart';
import 'package:pinball_flame/pinball_flame.dart';
import '../../helpers/helpers.dart'; import '../../helpers/helpers.dart';
@ -21,10 +22,8 @@ void main() {
flameTester.test( flameTester.test(
'loads correctly', 'loads correctly',
(game) async { (game) async {
final bottomGroup = BottomGroup(); await game.addFromBlueprint(BottomGroup());
await game.ensureAdd(bottomGroup); await game.ready();
expect(game.contains(bottomGroup), isTrue);
}, },
); );
@ -32,13 +31,12 @@ void main() {
flameTester.test( flameTester.test(
'one left flipper', 'one left flipper',
(game) async { (game) async {
final bottomGroup = BottomGroup(); await game.addFromBlueprint(BottomGroup());
await game.ensureAdd(bottomGroup); await game.ready();
final leftFlippers = final leftFlippers = game.descendants().whereType<Flipper>().where(
bottomGroup.descendants().whereType<Flipper>().where( (flipper) => flipper.side.isLeft,
(flipper) => flipper.side.isLeft, );
);
expect(leftFlippers.length, equals(1)); expect(leftFlippers.length, equals(1));
}, },
); );
@ -46,12 +44,12 @@ void main() {
flameTester.test( flameTester.test(
'one right flipper', 'one right flipper',
(game) async { (game) async {
final bottomGroup = BottomGroup(); await game.addFromBlueprint(BottomGroup());
await game.ensureAdd(bottomGroup); await game.ready();
final rightFlippers =
bottomGroup.descendants().whereType<Flipper>().where( final rightFlippers = game.descendants().whereType<Flipper>().where(
(flipper) => flipper.side.isRight, (flipper) => flipper.side.isRight,
); );
expect(rightFlippers.length, equals(1)); expect(rightFlippers.length, equals(1));
}, },
); );
@ -59,11 +57,10 @@ void main() {
flameTester.test( flameTester.test(
'two Baseboards', 'two Baseboards',
(game) async { (game) async {
final bottomGroup = BottomGroup(); await game.addFromBlueprint(BottomGroup());
await game.ensureAdd(bottomGroup); await game.ready();
final basebottomGroups = final basebottomGroups = game.descendants().whereType<Baseboard>();
bottomGroup.descendants().whereType<Baseboard>();
expect(basebottomGroups.length, equals(2)); expect(basebottomGroups.length, equals(2));
}, },
); );
@ -71,10 +68,10 @@ void main() {
flameTester.test( flameTester.test(
'two Kickers', 'two Kickers',
(game) async { (game) async {
final bottomGroup = BottomGroup(); await game.addFromBlueprint(BottomGroup());
await game.ensureAdd(bottomGroup); await game.ready();
final kickers = bottomGroup.descendants().whereType<Kicker>(); final kickers = game.descendants().whereType<Kicker>();
expect(kickers.length, equals(2)); expect(kickers.length, equals(2));
}, },
); );

Loading…
Cancel
Save