mirror of https://github.com/flutter/pinball.git
feat: group Baseboards and Flipper (#47)
* feat: connected baseboards to flipper * chore: removed FlipperGroup * refactor: simplified Baseboard constructor * refactor: simplified BottomGroup * refactor: changed constructors * refactor: removed unecessary import * refactor: modified Flipper constructor * docs: updated doc comment * docs: used macro for BottomGroupSide * docs: improved doc comment * refactor: renamed magnitude to direction * refactor: renamed bumper to baseboard * feat: included board tests * docs: improved doc comment * feat: include boardSide.direction (#46) * feat: implemented BoardSide.direction * docs: included doc comment * refactor: used _side.direction * refactor: used ensureAdd * chore: removed old test * docs: improved commentspull/48/head
parent
413900e89f
commit
7a8f808822
@ -0,0 +1,76 @@
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
|
||||
/// {@template bottom_group}
|
||||
/// Grouping of the board's bottom [Component]s.
|
||||
///
|
||||
/// The bottom [Component]s are the [Flipper]s and the [Baseboard]s.
|
||||
/// {@endtemplate}
|
||||
// TODO(alestiago): Add [SlingShot] once provided.
|
||||
// TODO(alestiago): Consider renaming once entire Board is defined.
|
||||
class BottomGroup extends Component {
|
||||
/// {@macro bottom_group}
|
||||
BottomGroup({
|
||||
required this.position,
|
||||
required this.spacing,
|
||||
});
|
||||
|
||||
/// The amount of space between the line of symmetry.
|
||||
final double spacing;
|
||||
|
||||
/// The position of this [BottomGroup].
|
||||
final Vector2 position;
|
||||
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
final spacing = this.spacing + Flipper.width / 2;
|
||||
final rightSide = _BottomGroupSide(
|
||||
side: BoardSide.right,
|
||||
position: position + Vector2(spacing, 0),
|
||||
);
|
||||
final leftSide = _BottomGroupSide(
|
||||
side: BoardSide.left,
|
||||
position: position + Vector2(-spacing, 0),
|
||||
);
|
||||
|
||||
await addAll([rightSide, leftSide]);
|
||||
}
|
||||
}
|
||||
|
||||
/// {@template bottom_group_side}
|
||||
/// Group with one side of [BottomGroup]'s symmetric [Component]s.
|
||||
///
|
||||
/// For example, [Flipper]s are symmetric components.
|
||||
/// {@endtemplate}
|
||||
class _BottomGroupSide extends Component {
|
||||
/// {@macro bottom_group_side}
|
||||
_BottomGroupSide({
|
||||
required BoardSide side,
|
||||
required Vector2 position,
|
||||
}) : _side = side,
|
||||
_position = position;
|
||||
|
||||
final BoardSide _side;
|
||||
|
||||
final Vector2 _position;
|
||||
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
final direction = _side.direction;
|
||||
|
||||
final flipper = Flipper.fromSide(
|
||||
side: _side,
|
||||
position: _position,
|
||||
);
|
||||
final baseboard = Baseboard(
|
||||
side: _side,
|
||||
position: _position +
|
||||
Vector2(
|
||||
(Flipper.width * direction) - direction,
|
||||
Flipper.height,
|
||||
),
|
||||
);
|
||||
|
||||
await addAll([flipper, baseboard]);
|
||||
}
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
|
||||
import 'package:flame_forge2d/flame_forge2d.dart';
|
||||
import 'package:flame_test/flame_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
|
||||
import '../../helpers/helpers.dart';
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
final flameTester = FlameTester(Forge2DGame.new);
|
||||
|
||||
group('BottomGroup', () {
|
||||
flameTester.test(
|
||||
'loads correctly',
|
||||
(game) async {
|
||||
final bottomGroup = BottomGroup(position: Vector2.zero(), spacing: 0);
|
||||
await game.ready();
|
||||
await game.ensureAdd(bottomGroup);
|
||||
|
||||
expect(game.contains(bottomGroup), isTrue);
|
||||
},
|
||||
);
|
||||
|
||||
group('children', () {
|
||||
flameTester.test(
|
||||
'has one left flipper',
|
||||
(game) async {
|
||||
final bottomGroup = BottomGroup(position: Vector2.zero(), spacing: 0);
|
||||
await game.ready();
|
||||
await game.ensureAdd(bottomGroup);
|
||||
|
||||
final leftFlippers = bottomGroup.findNestedChildren<Flipper>(
|
||||
condition: (flipper) => flipper.side.isLeft,
|
||||
);
|
||||
expect(leftFlippers.length, equals(1));
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'has one right flipper',
|
||||
(game) async {
|
||||
final bottomGroup = BottomGroup(position: Vector2.zero(), spacing: 0);
|
||||
await game.ready();
|
||||
await game.ensureAdd(bottomGroup);
|
||||
|
||||
final leftFlippers = bottomGroup.findNestedChildren<Flipper>(
|
||||
condition: (flipper) => flipper.side.isRight,
|
||||
);
|
||||
expect(leftFlippers.length, equals(1));
|
||||
},
|
||||
);
|
||||
|
||||
flameTester.test(
|
||||
'has two Baseboards',
|
||||
(game) async {
|
||||
final bottomGroup = BottomGroup(position: Vector2.zero(), spacing: 0);
|
||||
await game.ready();
|
||||
await game.ensureAdd(bottomGroup);
|
||||
|
||||
final leftFlippers = bottomGroup.findNestedChildren<Baseboard>();
|
||||
expect(leftFlippers.length, equals(2));
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
Loading…
Reference in new issue