From aba49660c208b2f0cc2fbb5ce00e0e1807b1d516 Mon Sep 17 00:00:00 2001 From: Alejandro Santiago Date: Thu, 17 Mar 2022 13:38:48 +0000 Subject: [PATCH] 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 --- lib/game/components/board.dart | 82 ++++++++++++++++++++++++++-- lib/game/pinball_game.dart | 20 +++---- test/game/components/board_test.dart | 45 +++++++++------ test/game/pinball_game_test.dart | 4 +- 4 files changed, 118 insertions(+), 33 deletions(-) diff --git a/lib/game/components/board.dart b/lib/game/components/board.dart index b9ea85ea..f7b80bd8 100644 --- a/lib/game/components/board.dart +++ b/lib/game/components/board.dart @@ -1,15 +1,87 @@ import 'package:flame/components.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 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 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} /// 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} // TODO(alestiago): Consider renaming once entire Board is defined. -class BottomGroup extends Component { +class _BottomGroup extends Component { /// {@macro bottom_group} - BottomGroup({ + _BottomGroup({ required this.position, required this.spacing, }); @@ -17,7 +89,7 @@ class BottomGroup extends Component { /// The amount of space between the line of symmetry. final double spacing; - /// The position of this [BottomGroup]. + /// The position of this [_BottomGroup]. final Vector2 position; @override @@ -37,7 +109,7 @@ class BottomGroup extends Component { } /// {@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. /// {@endtemplate} diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index 61ef17e0..c0017b33 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -49,19 +49,19 @@ class PinballGame extends Forge2DGame ); unawaited(_addBonusWord()); - unawaited( - add( - BottomGroup( - position: screenToWorld( - Vector2( - camera.viewport.effectiveSize.x / 2, - camera.viewport.effectiveSize.y / 1.25, - ), - ), - spacing: 2, + unawaited(_addBoard()); + } + + Future _addBoard() async { + final board = Board( + size: screenToWorld( + Vector2( + camera.viewport.effectiveSize.x, + camera.viewport.effectiveSize.y, ), ), ); + await add(board); } Future _addBonusWord() async { diff --git a/test/game/components/board_test.dart b/test/game/components/board_test.dart index 34a628a8..ccf599ec 100644 --- a/test/game/components/board_test.dart +++ b/test/game/components/board_test.dart @@ -11,15 +11,15 @@ void main() { TestWidgetsFlutterBinding.ensureInitialized(); final flameTester = FlameTester(Forge2DGame.new); - group('BottomGroup', () { + group('Board', () { flameTester.test( 'loads correctly', (game) async { - final bottomGroup = BottomGroup(position: Vector2.zero(), spacing: 0); + final board = Board(size: Vector2.all(500)); 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( 'has one left flipper', (game) async { - final bottomGroup = BottomGroup(position: Vector2.zero(), spacing: 0); + final board = Board(size: Vector2.all(500)); await game.ready(); - await game.ensureAdd(bottomGroup); + await game.ensureAdd(board); - final leftFlippers = bottomGroup.findNestedChildren( + final leftFlippers = board.findNestedChildren( condition: (flipper) => flipper.side.isLeft, ); expect(leftFlippers.length, equals(1)); @@ -41,11 +41,11 @@ void main() { flameTester.test( 'has one right flipper', (game) async { - final bottomGroup = BottomGroup(position: Vector2.zero(), spacing: 0); + final board = Board(size: Vector2.all(500)); await game.ready(); - await game.ensureAdd(bottomGroup); + await game.ensureAdd(board); - final rightFlippers = bottomGroup.findNestedChildren( + final rightFlippers = board.findNestedChildren( condition: (flipper) => flipper.side.isRight, ); expect(rightFlippers.length, equals(1)); @@ -55,11 +55,11 @@ void main() { flameTester.test( 'has two Baseboards', (game) async { - final bottomGroup = BottomGroup(position: Vector2.zero(), spacing: 0); + final board = Board(size: Vector2.all(500)); await game.ready(); - await game.ensureAdd(bottomGroup); + await game.ensureAdd(board); - final baseboards = bottomGroup.findNestedChildren(); + final baseboards = board.findNestedChildren(); expect(baseboards.length, equals(2)); }, ); @@ -67,14 +67,27 @@ void main() { flameTester.test( 'has two SlingShots', (game) async { - final bottomGroup = BottomGroup(position: Vector2.zero(), spacing: 0); + final board = Board(size: Vector2.all(500)); await game.ready(); - await game.ensureAdd(bottomGroup); + await game.ensureAdd(board); - final slingShots = bottomGroup.findNestedChildren(); + final slingShots = board.findNestedChildren(); 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(); + expect(roundBumpers.length, equals(3)); + }, + ); }); }); } diff --git a/test/game/pinball_game_test.dart b/test/game/pinball_game_test.dart index f7d0f7db..6b3d5a5f 100644 --- a/test/game/pinball_game_test.dart +++ b/test/game/pinball_game_test.dart @@ -54,10 +54,10 @@ void main() { }, ); - flameTester.test('has only one BottomGroup', (game) async { + flameTester.test('has one Board', (game) async { await game.ready(); expect( - game.children.whereType().length, + game.children.whereType().length, equals(1), ); });