From 8bb3c030a771ec68ffd81efd64fc10c3b6b4fb2c Mon Sep 17 00:00:00 2001 From: alestiago Date: Thu, 10 Mar 2022 16:15:31 +0000 Subject: [PATCH 01/15] feat: started implementing FlipperGroup --- lib/game/components/flipper.dart | 78 +++++++++++++++++++++++++++++++- lib/game/pinball_game.dart | 63 ++------------------------ 2 files changed, 81 insertions(+), 60 deletions(-) diff --git a/lib/game/components/flipper.dart b/lib/game/components/flipper.dart index 16754ed3..b73db0e9 100644 --- a/lib/game/components/flipper.dart +++ b/lib/game/components/flipper.dart @@ -1,12 +1,88 @@ import 'dart:async'; import 'dart:math' as math; -import 'package:flame/components.dart' show SpriteComponent; +import 'package:flame/components.dart' + show HasGameRef, PositionComponent, SpriteComponent; import 'package:flame/input.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flutter/services.dart'; import 'package:pinball/game/game.dart'; +/// {@template flipper_group} +/// +/// {@endtemplate} +class FlipperGroup extends PositionComponent with HasGameRef { + /// @macro {flipper_group} + FlipperGroup({ + required Vector2 position, + required this.spacing, + }) : super(position: position); + + /// The amount of space between the [Flipper.right] and [Flipper.left]. + final double spacing; + + @override + Future onLoad() async { + final leftFlipper = Flipper.left( + position: Vector2( + position.x - (Flipper.width / 2) - (spacing / 2), + position.y, + ), + ); + await add(leftFlipper); + final leftFlipperAnchor = FlipperAnchor(flipper: leftFlipper); + await add(leftFlipperAnchor); + + final leftFlipperRevoluteJointDef = FlipperAnchorRevoluteJointDef( + flipper: leftFlipper, + anchor: leftFlipperAnchor, + ); + // TODO(alestiago): Remove casting once the following is closed: + // https://github.com/flame-engine/forge2d/issues/36 + final leftFlipperRevoluteJoint = + gameRef.world.createJoint(leftFlipperRevoluteJointDef) as RevoluteJoint; + + final rightFlipper = Flipper.right( + position: Vector2( + position.x + (Flipper.width / 2) + (spacing / 2), + position.y, + ), + ); + await add(rightFlipper); + final rightFlipperAnchor = FlipperAnchor(flipper: rightFlipper); + await add(rightFlipperAnchor); + final rightFlipperRevoluteJointDef = FlipperAnchorRevoluteJointDef( + flipper: rightFlipper, + anchor: rightFlipperAnchor, + ); + // TODO(alestiago): Remove casting once the following is closed: + // https://github.com/flame-engine/forge2d/issues/36 + final rightFlipperRevoluteJoint = gameRef.world + .createJoint(rightFlipperRevoluteJointDef) as RevoluteJoint; + + // TODO(erickzanardo): Clean this once the issue is solved: + // https://github.com/flame-engine/flame/issues/1417 + // FIXME(erickzanardo): when mounted the initial position is not fully + // reached. + unawaited( + leftFlipper.hasMounted.future.whenComplete( + () => FlipperAnchorRevoluteJointDef.unlock( + leftFlipperRevoluteJoint, + leftFlipper.side, + ), + ), + ); + unawaited( + rightFlipper.hasMounted.future.whenComplete( + () => FlipperAnchorRevoluteJointDef.unlock( + rightFlipperRevoluteJoint, + rightFlipper.side, + ), + ), + ); + } +} + /// {@template flipper} /// A bat, typically found in pairs at the bottom of the board. /// diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index 33ea5eda..ee435aa6 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -37,65 +37,10 @@ class PinballGame extends Forge2DGame await add(BottomWall(this)); addContactCallback(BottomWallBallContactCallback()); - unawaited(_addFlippers()); - } - - Future _addFlippers() async { - const spaceBetweenFlippers = 2; - final leftFlipper = Flipper.left( - position: Vector2( - flippersPosition.x - (Flipper.width / 2) - (spaceBetweenFlippers / 2), - flippersPosition.y, - ), - ); - await add(leftFlipper); - final leftFlipperAnchor = FlipperAnchor(flipper: leftFlipper); - await add(leftFlipperAnchor); - final leftFlipperRevoluteJointDef = FlipperAnchorRevoluteJointDef( - flipper: leftFlipper, - anchor: leftFlipperAnchor, - ); - // TODO(alestiago): Remove casting once the following is closed: - // https://github.com/flame-engine/forge2d/issues/36 - final leftFlipperRevoluteJoint = - world.createJoint(leftFlipperRevoluteJointDef) as RevoluteJoint; - - final rightFlipper = Flipper.right( - position: Vector2( - flippersPosition.x + (Flipper.width / 2) + (spaceBetweenFlippers / 2), - flippersPosition.y, - ), - ); - await add(rightFlipper); - final rightFlipperAnchor = FlipperAnchor(flipper: rightFlipper); - await add(rightFlipperAnchor); - final rightFlipperRevoluteJointDef = FlipperAnchorRevoluteJointDef( - flipper: rightFlipper, - anchor: rightFlipperAnchor, - ); - // TODO(alestiago): Remove casting once the following is closed: - // https://github.com/flame-engine/forge2d/issues/36 - final rightFlipperRevoluteJoint = - world.createJoint(rightFlipperRevoluteJointDef) as RevoluteJoint; - - // TODO(erickzanardo): Clean this once the issue is solved: - // https://github.com/flame-engine/flame/issues/1417 - // FIXME(erickzanardo): when mounted the initial position is not fully - // reached. - unawaited( - leftFlipper.hasMounted.future.whenComplete( - () => FlipperAnchorRevoluteJointDef.unlock( - leftFlipperRevoluteJoint, - leftFlipper.side, - ), - ), - ); - unawaited( - rightFlipper.hasMounted.future.whenComplete( - () => FlipperAnchorRevoluteJointDef.unlock( - rightFlipperRevoluteJoint, - rightFlipper.side, - ), + await add( + FlipperGroup( + position: flippersPosition, + spacing: 2, ), ); } From 95f6636cdcfd5368bfbeb0cf2f98dad5d7152d1f Mon Sep 17 00:00:00 2001 From: alestiago Date: Thu, 10 Mar 2022 16:40:54 +0000 Subject: [PATCH 02/15] refactor: simplified Flipper logic --- lib/game/components/flipper.dart | 129 +++++++++++++------------------ 1 file changed, 54 insertions(+), 75 deletions(-) diff --git a/lib/game/components/flipper.dart b/lib/game/components/flipper.dart index b73db0e9..b6608afa 100644 --- a/lib/game/components/flipper.dart +++ b/lib/game/components/flipper.dart @@ -9,7 +9,7 @@ import 'package:flutter/services.dart'; import 'package:pinball/game/game.dart'; /// {@template flipper_group} -/// +/// Adds a [Flipper.right] and a [Flipper.left]. /// {@endtemplate} class FlipperGroup extends PositionComponent with HasGameRef { /// @macro {flipper_group} @@ -30,17 +30,6 @@ class FlipperGroup extends PositionComponent with HasGameRef { ), ); await add(leftFlipper); - final leftFlipperAnchor = FlipperAnchor(flipper: leftFlipper); - await add(leftFlipperAnchor); - - final leftFlipperRevoluteJointDef = FlipperAnchorRevoluteJointDef( - flipper: leftFlipper, - anchor: leftFlipperAnchor, - ); - // TODO(alestiago): Remove casting once the following is closed: - // https://github.com/flame-engine/forge2d/issues/36 - final leftFlipperRevoluteJoint = - gameRef.world.createJoint(leftFlipperRevoluteJointDef) as RevoluteJoint; final rightFlipper = Flipper.right( position: Vector2( @@ -49,37 +38,6 @@ class FlipperGroup extends PositionComponent with HasGameRef { ), ); await add(rightFlipper); - final rightFlipperAnchor = FlipperAnchor(flipper: rightFlipper); - await add(rightFlipperAnchor); - final rightFlipperRevoluteJointDef = FlipperAnchorRevoluteJointDef( - flipper: rightFlipper, - anchor: rightFlipperAnchor, - ); - // TODO(alestiago): Remove casting once the following is closed: - // https://github.com/flame-engine/forge2d/issues/36 - final rightFlipperRevoluteJoint = gameRef.world - .createJoint(rightFlipperRevoluteJointDef) as RevoluteJoint; - - // TODO(erickzanardo): Clean this once the issue is solved: - // https://github.com/flame-engine/flame/issues/1417 - // FIXME(erickzanardo): when mounted the initial position is not fully - // reached. - unawaited( - leftFlipper.hasMounted.future.whenComplete( - () => FlipperAnchorRevoluteJointDef.unlock( - leftFlipperRevoluteJoint, - leftFlipper.side, - ), - ), - ); - unawaited( - rightFlipper.hasMounted.future.whenComplete( - () => FlipperAnchorRevoluteJointDef.unlock( - rightFlipperRevoluteJoint, - rightFlipper.side, - ), - ), - ); } } @@ -152,9 +110,31 @@ class Flipper extends PositionBodyComponent with KeyboardHandler { /// [onKeyEvent] method listens to when one of these keys is pressed. final List _keys; + late final RevoluteJoint _joint; + + /// Applies downward linear velocity to the [Flipper], moving it to its + /// resting position. + void _moveDown() { + body.linearVelocity = Vector2(0, -_speed); + } + + /// Applies upward linear velocity to the [Flipper], moving it to its highest + /// position. + void _moveUp() { + body.linearVelocity = Vector2(0, _speed); + } + @override Future onLoad() async { await super.onLoad(); + await Future.wait([ + _loadSprite(), + _anchorToJoint(), + ]); + } + + /// Loads the sprite that renders with the [Flipper]. + Future _loadSprite() async { final sprite = await gameRef.loadSprite(spritePath); positionComponent = SpriteComponent( sprite: sprite, @@ -166,16 +146,39 @@ class Flipper extends PositionBodyComponent with KeyboardHandler { } } - /// Applies downward linear velocity to the [Flipper], moving it to its - /// resting position. - void _moveDown() { - body.linearVelocity = Vector2(0, -_speed); + /// Anchors the [Flipper] to the [RevoluteJoint] that controls its arc motion. + Future _anchorToJoint() async { + final anchor = FlipperAnchor(flipper: this); + await add(anchor); + + final jointDef = FlipperAnchorRevoluteJointDef( + flipper: this, + anchor: anchor, + ); + // TODO(alestiago): Remove casting once the following is closed: + // https://github.com/flame-engine/forge2d/issues/36 + _joint = world.createJoint(jointDef) as RevoluteJoint; + + // FIXME(erickzanardo): when mounted the initial position is not fully + // reached. + unawaited( + mounted.whenComplete( + () => FlipperAnchorRevoluteJointDef.unlock(_joint, side), + ), + ); } - /// Applies upward linear velocity to the [Flipper], moving it to its highest - /// position. - void _moveUp() { - body.linearVelocity = Vector2(0, _speed); + @override + Body createBody() { + final bodyDef = BodyDef() + ..gravityScale = 0 + ..type = BodyType.dynamic + ..position = _position; + + final body = world.createBody(bodyDef); + _createFixtureDefs().forEach(body.createFixture); + + return body; } List _createFixtureDefs() { @@ -224,30 +227,6 @@ class Flipper extends PositionBodyComponent with KeyboardHandler { return fixtures; } - @override - Body createBody() { - final bodyDef = BodyDef() - ..gravityScale = 0 - ..type = BodyType.dynamic - ..position = _position; - - final body = world.createBody(bodyDef); - _createFixtureDefs().forEach(body.createFixture); - - return body; - } - - // TODO(erickzanardo): Remove this once the issue is solved: - // https://github.com/flame-engine/flame/issues/1417 - // ignore: public_member_api_docs - final Completer hasMounted = Completer(); - - @override - void onMount() { - super.onMount(); - hasMounted.complete(); - } - @override bool onKeyEvent( RawKeyEvent event, From 5c004397dc4f26050e9e1d9327af61d6fb651bbb Mon Sep 17 00:00:00 2001 From: alestiago Date: Thu, 10 Mar 2022 16:43:34 +0000 Subject: [PATCH 03/15] refactor: used extension instead of condition --- lib/game/components/flipper.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/game/components/flipper.dart b/lib/game/components/flipper.dart index b6608afa..dd0119ae 100644 --- a/lib/game/components/flipper.dart +++ b/lib/game/components/flipper.dart @@ -141,7 +141,7 @@ class Flipper extends PositionBodyComponent with KeyboardHandler { size: size, ); - if (side == BoardSide.right) { + if (side.isRight) { positionComponent?.flipHorizontally(); } } From a3651b846a4f86070b42ae1d78f9467fabd655d9 Mon Sep 17 00:00:00 2001 From: alestiago Date: Thu, 10 Mar 2022 16:47:48 +0000 Subject: [PATCH 04/15] docs: used "loads" over "adds" --- lib/game/components/flipper.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/game/components/flipper.dart b/lib/game/components/flipper.dart index dd0119ae..e062f66d 100644 --- a/lib/game/components/flipper.dart +++ b/lib/game/components/flipper.dart @@ -9,7 +9,7 @@ import 'package:flutter/services.dart'; import 'package:pinball/game/game.dart'; /// {@template flipper_group} -/// Adds a [Flipper.right] and a [Flipper.left]. +/// Loads a [Flipper.right] and a [Flipper.left]. /// {@endtemplate} class FlipperGroup extends PositionComponent with HasGameRef { /// @macro {flipper_group} From 329288a8f94b71bdb474c18bd5ef7917b361eb66 Mon Sep 17 00:00:00 2001 From: alestiago Date: Thu, 10 Mar 2022 16:49:16 +0000 Subject: [PATCH 05/15] feat: used size rather than width and height --- lib/game/components/flipper.dart | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/game/components/flipper.dart b/lib/game/components/flipper.dart index e062f66d..a0bd627b 100644 --- a/lib/game/components/flipper.dart +++ b/lib/game/components/flipper.dart @@ -185,11 +185,11 @@ class Flipper extends PositionBodyComponent with KeyboardHandler { final fixtures = []; final isLeft = side.isLeft; - final bigCircleShape = CircleShape()..radius = height / 2; + final bigCircleShape = CircleShape()..radius = size.y / 2; bigCircleShape.position.setValues( isLeft - ? -(width / 2) + bigCircleShape.radius - : (width / 2) - bigCircleShape.radius, + ? -(size.x / 2) + bigCircleShape.radius + : (size.x / 2) - bigCircleShape.radius, 0, ); final bigCircleFixtureDef = FixtureDef(bigCircleShape); @@ -198,8 +198,8 @@ class Flipper extends PositionBodyComponent with KeyboardHandler { final smallCircleShape = CircleShape()..radius = bigCircleShape.radius / 2; smallCircleShape.position.setValues( isLeft - ? (width / 2) - smallCircleShape.radius - : -(width / 2) + smallCircleShape.radius, + ? (size.x / 2) - smallCircleShape.radius + : -(size.x / 2) + smallCircleShape.radius, 0, ); final smallCircleFixtureDef = FixtureDef(smallCircleShape); @@ -259,8 +259,8 @@ class FlipperAnchor extends Anchor { }) : super( position: Vector2( flipper.side.isLeft - ? flipper.body.position.x - Flipper.width / 2 - : flipper.body.position.x + Flipper.width / 2, + ? flipper.body.position.x - flipper.size.x / 2 + : flipper.body.position.x + flipper.size.x / 2, flipper.body.position.y, ), ); From d445d27e7e80a240e4183eec61794a326bdf8f76 Mon Sep 17 00:00:00 2001 From: alestiago Date: Thu, 10 Mar 2022 16:56:25 +0000 Subject: [PATCH 06/15] refactor: removed unecessary mixin --- lib/game/components/flipper.dart | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/game/components/flipper.dart b/lib/game/components/flipper.dart index a0bd627b..1f342c96 100644 --- a/lib/game/components/flipper.dart +++ b/lib/game/components/flipper.dart @@ -1,8 +1,7 @@ import 'dart:async'; import 'dart:math' as math; -import 'package:flame/components.dart' - show HasGameRef, PositionComponent, SpriteComponent; +import 'package:flame/components.dart' show PositionComponent, SpriteComponent; import 'package:flame/input.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flutter/services.dart'; @@ -11,7 +10,7 @@ import 'package:pinball/game/game.dart'; /// {@template flipper_group} /// Loads a [Flipper.right] and a [Flipper.left]. /// {@endtemplate} -class FlipperGroup extends PositionComponent with HasGameRef { +class FlipperGroup extends PositionComponent { /// @macro {flipper_group} FlipperGroup({ required Vector2 position, From 7a880c241eed5517d77383139222ba850d99fca1 Mon Sep 17 00:00:00 2001 From: alestiago Date: Thu, 10 Mar 2022 17:05:08 +0000 Subject: [PATCH 07/15] feat: reorder methods --- lib/game/components/flipper.dart | 44 ++++++++++++++++---------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/lib/game/components/flipper.dart b/lib/game/components/flipper.dart index 1f342c96..a0d4bd45 100644 --- a/lib/game/components/flipper.dart +++ b/lib/game/components/flipper.dart @@ -123,15 +123,6 @@ class Flipper extends PositionBodyComponent with KeyboardHandler { body.linearVelocity = Vector2(0, _speed); } - @override - Future onLoad() async { - await super.onLoad(); - await Future.wait([ - _loadSprite(), - _anchorToJoint(), - ]); - } - /// Loads the sprite that renders with the [Flipper]. Future _loadSprite() async { final sprite = await gameRef.loadSprite(spritePath); @@ -167,19 +158,6 @@ class Flipper extends PositionBodyComponent with KeyboardHandler { ); } - @override - Body createBody() { - final bodyDef = BodyDef() - ..gravityScale = 0 - ..type = BodyType.dynamic - ..position = _position; - - final body = world.createBody(bodyDef); - _createFixtureDefs().forEach(body.createFixture); - - return body; - } - List _createFixtureDefs() { final fixtures = []; final isLeft = side.isLeft; @@ -226,6 +204,28 @@ class Flipper extends PositionBodyComponent with KeyboardHandler { return fixtures; } + @override + Future onLoad() async { + await super.onLoad(); + await Future.wait([ + _loadSprite(), + _anchorToJoint(), + ]); + } + + @override + Body createBody() { + final bodyDef = BodyDef() + ..gravityScale = 0 + ..type = BodyType.dynamic + ..position = _position; + + final body = world.createBody(bodyDef); + _createFixtureDefs().forEach(body.createFixture); + + return body; + } + @override bool onKeyEvent( RawKeyEvent event, From 4a7e0954a7c52ce85ebf5caab9738607b8a6c11a Mon Sep 17 00:00:00 2001 From: alestiago Date: Thu, 10 Mar 2022 17:05:55 +0000 Subject: [PATCH 08/15] refactor: removed _joint over joint --- lib/game/components/flipper.dart | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/game/components/flipper.dart b/lib/game/components/flipper.dart index a0d4bd45..4d506c2f 100644 --- a/lib/game/components/flipper.dart +++ b/lib/game/components/flipper.dart @@ -109,8 +109,6 @@ class Flipper extends PositionBodyComponent with KeyboardHandler { /// [onKeyEvent] method listens to when one of these keys is pressed. final List _keys; - late final RevoluteJoint _joint; - /// Applies downward linear velocity to the [Flipper], moving it to its /// resting position. void _moveDown() { @@ -147,13 +145,13 @@ class Flipper extends PositionBodyComponent with KeyboardHandler { ); // TODO(alestiago): Remove casting once the following is closed: // https://github.com/flame-engine/forge2d/issues/36 - _joint = world.createJoint(jointDef) as RevoluteJoint; + final joint = world.createJoint(jointDef) as RevoluteJoint; // FIXME(erickzanardo): when mounted the initial position is not fully // reached. unawaited( mounted.whenComplete( - () => FlipperAnchorRevoluteJointDef.unlock(_joint, side), + () => FlipperAnchorRevoluteJointDef.unlock(joint, side), ), ); } From edba5b858f2576d749317f16250cb423d652598f Mon Sep 17 00:00:00 2001 From: alestiago Date: Thu, 10 Mar 2022 18:33:34 +0000 Subject: [PATCH 09/15] docs: fixed macro typo --- lib/game/components/flipper.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/game/components/flipper.dart b/lib/game/components/flipper.dart index 4d506c2f..85264c34 100644 --- a/lib/game/components/flipper.dart +++ b/lib/game/components/flipper.dart @@ -11,7 +11,7 @@ import 'package:pinball/game/game.dart'; /// Loads a [Flipper.right] and a [Flipper.left]. /// {@endtemplate} class FlipperGroup extends PositionComponent { - /// @macro {flipper_group} + /// {@macro flipper_group} FlipperGroup({ required Vector2 position, required this.spacing, From c26464bbcfc76825476ae7db9c9964b77122bd6e Mon Sep 17 00:00:00 2001 From: alestiago Date: Thu, 10 Mar 2022 18:33:45 +0000 Subject: [PATCH 10/15] feat: unawait add operation --- lib/game/pinball_game.dart | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index ee435aa6..948caa04 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -37,10 +37,12 @@ class PinballGame extends Forge2DGame await add(BottomWall(this)); addContactCallback(BottomWallBallContactCallback()); - await add( - FlipperGroup( - position: flippersPosition, - spacing: 2, + unawaited( + add( + FlipperGroup( + position: flippersPosition, + spacing: 2, + ), ), ); } From 5455c07f42c4b8b4a563bf9ee8d608c02161db81 Mon Sep 17 00:00:00 2001 From: alestiago Date: Thu, 10 Mar 2022 18:39:55 +0000 Subject: [PATCH 11/15] feat: included tests --- test/game/components/flipper_test.dart | 101 +++++++++++++++++++++++++ test/game/pinball_game_test.dart | 24 +----- 2 files changed, 105 insertions(+), 20 deletions(-) diff --git a/test/game/components/flipper_test.dart b/test/game/components/flipper_test.dart index b9894d9a..70fefa5d 100644 --- a/test/game/components/flipper_test.dart +++ b/test/game/components/flipper_test.dart @@ -2,6 +2,7 @@ import 'dart:collection'; +import 'package:flame/components.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_test/flame_test.dart'; import 'package:flutter/services.dart'; @@ -13,6 +14,104 @@ import '../../helpers/helpers.dart'; void main() { TestWidgetsFlutterBinding.ensureInitialized(); final flameTester = FlameTester(PinballGame.new); + + group('FlipperGroup', () { + flameTester.test( + 'loads correctly', + (game) async { + final flipperGroup = FlipperGroup( + position: Vector2.zero(), + spacing: 0, + ); + await game.ensureAdd(flipperGroup); + + expect(game.contains(flipperGroup), isTrue); + }, + ); + + group('constructor', () { + flameTester.test( + 'positions correctly', + (game) async { + final position = Vector2.all(10); + final flipperGroup = FlipperGroup( + position: position, + spacing: 0, + ); + await game.ensureAdd(flipperGroup); + + expect(flipperGroup.position, equals(position)); + }, + ); + }); + + group('children', () { + bool Function(Component) flipperSelector(BoardSide side) => + (component) => component is Flipper && component.side == side; + flameTester.test( + 'has only one left Flipper', + (game) async { + final flipperGroup = FlipperGroup( + position: Vector2.zero(), + spacing: 0, + ); + await game.ensureAdd(flipperGroup); + + expect( + () => flipperGroup.children.singleWhere( + flipperSelector(BoardSide.left), + ), + returnsNormally, + ); + }, + ); + + flameTester.test( + 'has only one right Flipper', + (game) async { + final flipperGroup = FlipperGroup( + position: Vector2.zero(), + spacing: 0, + ); + await game.ensureAdd(flipperGroup); + + expect( + () => flipperGroup.children.singleWhere( + flipperSelector(BoardSide.right), + ), + returnsNormally, + ); + }, + ); + + flameTester.test( + 'spaced correctly', + (game) async { + final flipperGroup = FlipperGroup( + position: Vector2.zero(), + spacing: 2, + ); + await game.ready(); + await game.ensureAdd(flipperGroup); + + final leftFlipper = flipperGroup.children.singleWhere( + flipperSelector(BoardSide.left), + ) as Flipper; + final rightFlipper = flipperGroup.children.singleWhere( + flipperSelector(BoardSide.right), + ) as Flipper; + + expect( + leftFlipper.body.position.x + + leftFlipper.size.x + + flipperGroup.spacing, + equals(rightFlipper.body.position.x), + ); + }, + ); + }); + }); + group( 'Flipper', () { @@ -21,9 +120,11 @@ void main() { (game) async { final leftFlipper = Flipper.left(position: Vector2.zero()); final rightFlipper = Flipper.right(position: Vector2.zero()); + await game.ready(); await game.ensureAddAll([leftFlipper, rightFlipper]); expect(game.contains(leftFlipper), isTrue); + expect(game.contains(rightFlipper), isTrue); }, ); diff --git a/test/game/pinball_game_test.dart b/test/game/pinball_game_test.dart index 4dc93b7f..d89e89e5 100644 --- a/test/game/pinball_game_test.dart +++ b/test/game/pinball_game_test.dart @@ -16,32 +16,16 @@ void main() { group( 'components', () { - group('Flippers', () { - bool Function(Component) flipperSelector(BoardSide side) => - (component) => component is Flipper && component.side == side; - - flameTester.test( - 'has only one left Flipper', - (game) async { - await game.ready(); - - expect( - () => game.children.singleWhere( - flipperSelector(BoardSide.left), - ), - returnsNormally, - ); - }, - ); - + bool Function(Component) componentSelector() => + (component) => component is T; + group('FlipperGroup', () { flameTester.test( 'has only one right Flipper', (game) async { await game.ready(); - expect( () => game.children.singleWhere( - flipperSelector(BoardSide.right), + componentSelector(), ), returnsNormally, ); From 5802448ad9a1c4b1fff24c986801e5829ec5d47e Mon Sep 17 00:00:00 2001 From: Alejandro Santiago Date: Fri, 11 Mar 2022 14:41:52 +0000 Subject: [PATCH 12/15] refactor: added missing white space Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com> --- test/game/components/flipper_test.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/test/game/components/flipper_test.dart b/test/game/components/flipper_test.dart index 70fefa5d..355484db 100644 --- a/test/game/components/flipper_test.dart +++ b/test/game/components/flipper_test.dart @@ -48,6 +48,7 @@ void main() { group('children', () { bool Function(Component) flipperSelector(BoardSide side) => (component) => component is Flipper && component.side == side; + flameTester.test( 'has only one left Flipper', (game) async { From 096042f6d7c3258702aaf86de424bf48177e2b7a Mon Sep 17 00:00:00 2001 From: Alejandro Santiago Date: Fri, 11 Mar 2022 14:42:05 +0000 Subject: [PATCH 13/15] refactor: adding missing white space Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com> --- test/game/pinball_game_test.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/test/game/pinball_game_test.dart b/test/game/pinball_game_test.dart index d89e89e5..6f3646e9 100644 --- a/test/game/pinball_game_test.dart +++ b/test/game/pinball_game_test.dart @@ -18,6 +18,7 @@ void main() { () { bool Function(Component) componentSelector() => (component) => component is T; + group('FlipperGroup', () { flameTester.test( 'has only one right Flipper', From 22c3c89546e5884bcf84059a39dd1e77726a5bd7 Mon Sep 17 00:00:00 2001 From: alestiago Date: Fri, 11 Mar 2022 14:43:02 +0000 Subject: [PATCH 14/15] refactor: renamed test --- test/game/pinball_game_test.dart | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/test/game/pinball_game_test.dart b/test/game/pinball_game_test.dart index 6f3646e9..969a8704 100644 --- a/test/game/pinball_game_test.dart +++ b/test/game/pinball_game_test.dart @@ -18,21 +18,19 @@ void main() { () { bool Function(Component) componentSelector() => (component) => component is T; - - group('FlipperGroup', () { - flameTester.test( - 'has only one right Flipper', - (game) async { - await game.ready(); - expect( - () => game.children.singleWhere( - componentSelector(), - ), - returnsNormally, - ); - }, - ); - }); + + flameTester.test( + 'has only one FlipperGroup', + (game) async { + await game.ready(); + expect( + () => game.children.singleWhere( + componentSelector(), + ), + returnsNormally, + ); + }, + ); }, ); }); From c6feb7973bbc687439b5acf7a3cd215b07eddd5a Mon Sep 17 00:00:00 2001 From: alestiago Date: Fri, 11 Mar 2022 14:43:42 +0000 Subject: [PATCH 15/15] refactor: used ! instead of ? --- lib/game/components/flipper.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/game/components/flipper.dart b/lib/game/components/flipper.dart index 85264c34..1138828b 100644 --- a/lib/game/components/flipper.dart +++ b/lib/game/components/flipper.dart @@ -130,7 +130,7 @@ class Flipper extends PositionBodyComponent with KeyboardHandler { ); if (side.isRight) { - positionComponent?.flipHorizontally(); + positionComponent!.flipHorizontally(); } }