feat: include DashForest area (#49)

* feat: removed Wall friction

* feat: included SlingShot

* feat: adjusted SlingShot size

* feat: included tests

* refactor: fixed tests

* feat: included DashForest

* feat: included tests

* docs: used correct reference

* docs: fixed typo in comment

* docs: improved doc comments

* refactor: used initialPosition API
pull/56/head
Alejandro Santiago 2 years ago committed by GitHub
parent 9fd7078968
commit aba49660c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,15 +1,87 @@
import 'package:flame/components.dart'; import 'package:flame/components.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
/// {@template board}
/// The main flat surface of the [PinballGame], where the [Flipper]s,
/// [RoundBumper]s, [SlingShot]s are arranged.
/// {entemplate}
class Board extends Component {
/// {@macro board}
Board({required Vector2 size}) : _size = size;
final Vector2 _size;
@override
Future<void> onLoad() async {
// TODO(alestiago): adjust positioning once sprites are added.
final bottomGroup = _BottomGroup(
position: Vector2(
_size.x / 2,
_size.y / 1.25,
),
spacing: 2,
);
final dashForest = _FlutterForest(
position: Vector2(
_size.x / 1.25,
_size.y / 4.25,
),
);
await addAll([
bottomGroup,
dashForest,
]);
}
}
/// {@template flutter_forest}
/// Area positioned at the top right of the [Board] where the [Ball]
/// can bounce off [RoundBumper]s.
/// {@endtemplate}
class _FlutterForest extends Component {
/// {@macro flutter_forest}
_FlutterForest({
required this.position,
});
final Vector2 position;
@override
Future<void> onLoad() async {
// TODO(alestiago): adjust positioning once sprites are added.
// TODO(alestiago): Use [NestBumper] instead of [RoundBumper] once provided.
final smallLeftNest = RoundBumper(
radius: 1,
points: 10,
)..initialPosition = position + Vector2(-4.8, 2.8);
final smallRightNest = RoundBumper(
radius: 1,
points: 10,
)..initialPosition = position + Vector2(0.5, -5.5);
final bigNest = RoundBumper(
radius: 2,
points: 20,
)..initialPosition = position;
await addAll([
smallLeftNest,
smallRightNest,
bigNest,
]);
}
}
/// {@template bottom_group} /// {@template bottom_group}
/// Grouping of the board's bottom [Component]s. /// Grouping of the board's bottom [Component]s.
/// ///
/// The bottom [Component]s are the [Flipper]s and the [Baseboard]s. /// The [_BottomGroup] consists of[Flipper]s, [Baseboard]s and [SlingShot]s.
/// {@endtemplate} /// {@endtemplate}
// TODO(alestiago): Consider renaming once entire Board is defined. // TODO(alestiago): Consider renaming once entire Board is defined.
class BottomGroup extends Component { class _BottomGroup extends Component {
/// {@macro bottom_group} /// {@macro bottom_group}
BottomGroup({ _BottomGroup({
required this.position, required this.position,
required this.spacing, required this.spacing,
}); });
@ -17,7 +89,7 @@ class BottomGroup extends Component {
/// The amount of space between the line of symmetry. /// The amount of space between the line of symmetry.
final double spacing; final double spacing;
/// The position of this [BottomGroup]. /// The position of this [_BottomGroup].
final Vector2 position; final Vector2 position;
@override @override
@ -37,7 +109,7 @@ class BottomGroup extends Component {
} }
/// {@template bottom_group_side} /// {@template bottom_group_side}
/// Group with one side of [BottomGroup]'s symmetric [Component]s. /// Group with one side of [_BottomGroup]'s symmetric [Component]s.
/// ///
/// For example, [Flipper]s are symmetric components. /// For example, [Flipper]s are symmetric components.
/// {@endtemplate} /// {@endtemplate}

@ -49,19 +49,19 @@ class PinballGame extends Forge2DGame
); );
unawaited(_addBonusWord()); unawaited(_addBonusWord());
unawaited( unawaited(_addBoard());
add( }
BottomGroup(
position: screenToWorld( Future<void> _addBoard() async {
Vector2( final board = Board(
camera.viewport.effectiveSize.x / 2, size: screenToWorld(
camera.viewport.effectiveSize.y / 1.25, Vector2(
), camera.viewport.effectiveSize.x,
), camera.viewport.effectiveSize.y,
spacing: 2,
), ),
), ),
); );
await add(board);
} }
Future<void> _addBonusWord() async { Future<void> _addBonusWord() async {

@ -11,15 +11,15 @@ void main() {
TestWidgetsFlutterBinding.ensureInitialized(); TestWidgetsFlutterBinding.ensureInitialized();
final flameTester = FlameTester(Forge2DGame.new); final flameTester = FlameTester(Forge2DGame.new);
group('BottomGroup', () { group('Board', () {
flameTester.test( flameTester.test(
'loads correctly', 'loads correctly',
(game) async { (game) async {
final bottomGroup = BottomGroup(position: Vector2.zero(), spacing: 0); final board = Board(size: Vector2.all(500));
await game.ready(); await game.ready();
await game.ensureAdd(bottomGroup); await game.ensureAdd(board);
expect(game.contains(bottomGroup), isTrue); expect(game.contains(board), isTrue);
}, },
); );
@ -27,11 +27,11 @@ void main() {
flameTester.test( flameTester.test(
'has one left flipper', 'has one left flipper',
(game) async { (game) async {
final bottomGroup = BottomGroup(position: Vector2.zero(), spacing: 0); final board = Board(size: Vector2.all(500));
await game.ready(); await game.ready();
await game.ensureAdd(bottomGroup); await game.ensureAdd(board);
final leftFlippers = bottomGroup.findNestedChildren<Flipper>( final leftFlippers = board.findNestedChildren<Flipper>(
condition: (flipper) => flipper.side.isLeft, condition: (flipper) => flipper.side.isLeft,
); );
expect(leftFlippers.length, equals(1)); expect(leftFlippers.length, equals(1));
@ -41,11 +41,11 @@ void main() {
flameTester.test( flameTester.test(
'has one right flipper', 'has one right flipper',
(game) async { (game) async {
final bottomGroup = BottomGroup(position: Vector2.zero(), spacing: 0); final board = Board(size: Vector2.all(500));
await game.ready(); await game.ready();
await game.ensureAdd(bottomGroup); await game.ensureAdd(board);
final rightFlippers = bottomGroup.findNestedChildren<Flipper>( final rightFlippers = board.findNestedChildren<Flipper>(
condition: (flipper) => flipper.side.isRight, condition: (flipper) => flipper.side.isRight,
); );
expect(rightFlippers.length, equals(1)); expect(rightFlippers.length, equals(1));
@ -55,11 +55,11 @@ void main() {
flameTester.test( flameTester.test(
'has two Baseboards', 'has two Baseboards',
(game) async { (game) async {
final bottomGroup = BottomGroup(position: Vector2.zero(), spacing: 0); final board = Board(size: Vector2.all(500));
await game.ready(); await game.ready();
await game.ensureAdd(bottomGroup); await game.ensureAdd(board);
final baseboards = bottomGroup.findNestedChildren<Baseboard>(); final baseboards = board.findNestedChildren<Baseboard>();
expect(baseboards.length, equals(2)); expect(baseboards.length, equals(2));
}, },
); );
@ -67,14 +67,27 @@ void main() {
flameTester.test( flameTester.test(
'has two SlingShots', 'has two SlingShots',
(game) async { (game) async {
final bottomGroup = BottomGroup(position: Vector2.zero(), spacing: 0); final board = Board(size: Vector2.all(500));
await game.ready(); await game.ready();
await game.ensureAdd(bottomGroup); await game.ensureAdd(board);
final slingShots = bottomGroup.findNestedChildren<SlingShot>(); final slingShots = board.findNestedChildren<SlingShot>();
expect(slingShots.length, equals(2)); expect(slingShots.length, equals(2));
}, },
); );
flameTester.test(
'has three RoundBumpers',
(game) async {
// TODO(alestiago): change to [NestBumpers] once provided.
final board = Board(size: Vector2.all(500));
await game.ready();
await game.ensureAdd(board);
final roundBumpers = board.findNestedChildren<RoundBumper>();
expect(roundBumpers.length, equals(3));
},
);
}); });
}); });
} }

@ -54,10 +54,10 @@ void main() {
}, },
); );
flameTester.test('has only one BottomGroup', (game) async { flameTester.test('has one Board', (game) async {
await game.ready(); await game.ready();
expect( expect(
game.children.whereType<BottomGroup>().length, game.children.whereType<Board>().length,
equals(1), equals(1),
); );
}); });

Loading…
Cancel
Save