diff --git a/.github/workflows/pinball_components.yaml b/.github/workflows/pinball_components.yaml index cab60a54..bf1907f8 100644 --- a/.github/workflows/pinball_components.yaml +++ b/.github/workflows/pinball_components.yaml @@ -16,4 +16,4 @@ jobs: uses: VeryGoodOpenSource/very_good_workflows/.github/workflows/flutter_package.yml@v1 with: working_directory: packages/pinball_components - coverage_excludes: "lib/src/generated/*.dart" + coverage_excludes: "lib/gen/*.dart" diff --git a/lib/flame/blueprint.dart b/lib/flame/blueprint.dart index 9f2a68f6..d536d650 100644 --- a/lib/flame/blueprint.dart +++ b/lib/flame/blueprint.dart @@ -12,19 +12,19 @@ const _attachedErrorMessage = "Can't add to attached Blueprints"; /// A [Blueprint] is a virtual way of grouping [Component]s /// that are related, but they need to be added directly on /// the [FlameGame] level. -abstract class Blueprint { +abstract class Blueprint { final List _components = []; bool _isAttached = false; /// Called before the the [Component]s managed /// by this blueprint is added to the [FlameGame] - void build(); + void build(T gameRef); /// Attach the [Component]s built on [build] to the [game] /// instance @mustCallSuper - Future attach(FlameGame game) async { - build(); + Future attach(T game) async { + build(game); await game.addAll(_components); _isAttached = true; } @@ -47,7 +47,7 @@ abstract class Blueprint { /// A [Blueprint] that provides additional /// structures specific to flame_forge2d -abstract class Forge2DBlueprint extends Blueprint { +abstract class Forge2DBlueprint extends Blueprint { final List _callbacks = []; /// Adds a single [ContactCallback] to this blueprint @@ -63,13 +63,11 @@ abstract class Forge2DBlueprint extends Blueprint { } @override - Future attach(FlameGame game) async { + Future attach(Forge2DGame game) async { await super.attach(game); - assert(game is Forge2DGame, 'Forge2DBlueprint used outside a Forge2DGame'); - for (final callback in _callbacks) { - (game as Forge2DGame).addContactCallback(callback); + game.addContactCallback(callback); } } diff --git a/lib/game/components/ball.dart b/lib/game/components/ball.dart index def21929..756a8a97 100644 --- a/lib/game/components/ball.dart +++ b/lib/game/components/ball.dart @@ -1,61 +1,40 @@ import 'package:flame/components.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; +import 'package:pinball/flame/blueprint.dart'; import 'package:pinball/game/game.dart'; -import 'package:pinball/gen/assets.gen.dart'; +import 'package:pinball_components/pinball_components.dart'; -/// {@template ball} -/// A solid, [BodyType.dynamic] sphere that rolls and bounces along the -/// [PinballGame]. +/// {@template ball_blueprint} +/// [Blueprint] which cretes a ball game object /// {@endtemplate} -class Ball extends BodyComponent with InitialPosition, Layered { - /// {@macro ball} - Ball() { - // TODO(ruimiguel): while developing Ball can be launched by clicking mouse, - // and default layer is Layer.all. But on final game Ball will be always be - // be launched from Plunger and LauncherRamp will modify it to Layer.board. - // We need to see what happens if Ball appears from other place like nest - // bumper, it will need to explicit change layer to Layer.board then. - layer = Layer.board; - } - - /// The size of the [Ball] - final Vector2 size = Vector2.all(2); +class BallBlueprint extends Blueprint { + /// {@macro ball_blueprint} + BallBlueprint({required this.position}); - @override - Future onLoad() async { - await super.onLoad(); - final sprite = await gameRef.loadSprite(Assets.images.components.ball.path); - final tint = gameRef.theme.characterTheme.ballColor.withOpacity(0.5); - await add( - SpriteComponent( - sprite: sprite, - size: size, - anchor: Anchor.center, - )..tint(tint), - ); - } + /// The initial position of the [Ball] + final Vector2 position; @override - Body createBody() { - final shape = CircleShape()..radius = size.x / 2; - - final fixtureDef = FixtureDef(shape)..density = 1; + void build(PinballGame gameRef) { + final baseColor = gameRef.theme.characterTheme.ballColor; + final ball = Ball(baseColor: baseColor)..add(BallController()); - final bodyDef = BodyDef() - ..position = initialPosition - ..userData = this - ..type = BodyType.dynamic; - - return world.createBody(bodyDef)..createFixture(fixtureDef); + add(ball..initialPosition = position + Vector2(0, ball.size.y / 2)); } +} +/// {@template ball} +/// A solid, [BodyType.dynamic] sphere that rolls and bounces along the +/// [PinballGame]. +/// {@endtemplate} +class BallController extends Component with HasGameRef { /// Removes the [Ball] from a [PinballGame]; spawning a new [Ball] if /// any are left. /// /// Triggered by [BottomWallBallContactCallback] when the [Ball] falls into /// a [BottomWall]. void lost() { - shouldRemove = true; + parent?.shouldRemove = true; final bloc = gameRef.read()..add(const BallLost()); @@ -64,19 +43,13 @@ class Ball extends BodyComponent with InitialPosition, Layered { gameRef.spawnBall(); } } +} - /// Immediatly and completly [stop]s the ball. - /// - /// The [Ball] will no longer be affected by any forces, including it's - /// weight and those emitted from collisions. - void stop() { - body.setType(BodyType.static); - } - - /// Allows the [Ball] to be affected by forces. - /// - /// If previously [stop]ed, the previous ball's velocity is not kept. - void resume() { - body.setType(BodyType.dynamic); +/// Adds helper methods to the [Ball] +extension BallX on Ball { + /// Returns the controller instance of the ball + // TODO(erickzanardo): Remove the need of an extension. + BallController get controller { + return children.whereType().first; } } diff --git a/lib/game/components/baseboard.dart b/lib/game/components/baseboard.dart index 48d38497..60d0ebe7 100644 --- a/lib/game/components/baseboard.dart +++ b/lib/game/components/baseboard.dart @@ -2,6 +2,7 @@ import 'dart:math' as math; import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:pinball/game/game.dart'; +import 'package:pinball_components/pinball_components.dart'; /// {@template baseboard} /// Straight, angled board piece to corral the [Ball] towards the [Flipper]s. diff --git a/lib/game/components/board.dart b/lib/game/components/board.dart index af03efdd..6e895d6e 100644 --- a/lib/game/components/board.dart +++ b/lib/game/components/board.dart @@ -1,5 +1,6 @@ import 'package:flame/components.dart'; import 'package:pinball/game/game.dart'; +import 'package:pinball_components/pinball_components.dart'; /// {@template board} /// The main flat surface of the [PinballGame], where the [Flipper]s, @@ -127,7 +128,7 @@ class _BottomGroupSide extends Component { Future onLoad() async { final direction = _side.direction; - final flipper = Flipper.fromSide( + final flipper = Flipper( side: _side, )..initialPosition = _position; final baseboard = Baseboard(side: _side) diff --git a/lib/game/components/bonus_word.dart b/lib/game/components/bonus_word.dart index cc6391e8..d97a9bd1 100644 --- a/lib/game/components/bonus_word.dart +++ b/lib/game/components/bonus_word.dart @@ -8,6 +8,7 @@ import 'package:flame_bloc/flame_bloc.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flutter/material.dart'; import 'package:pinball/game/game.dart'; +import 'package:pinball_components/pinball_components.dart'; /// {@template bonus_word} /// Loads all [BonusLetter]s to compose a [BonusWord]. diff --git a/lib/game/components/components.dart b/lib/game/components/components.dart index 84d2833a..7ee555d8 100644 --- a/lib/game/components/components.dart +++ b/lib/game/components/components.dart @@ -4,12 +4,10 @@ export 'board.dart'; export 'board_side.dart'; export 'bonus_word.dart'; export 'flipper.dart'; -export 'initial_position.dart'; export 'jetpack_ramp.dart'; export 'joint_anchor.dart'; export 'kicker.dart'; export 'launcher_ramp.dart'; -export 'layer.dart'; export 'pathway.dart'; export 'plunger.dart'; export 'priority.dart'; diff --git a/lib/game/components/flipper.dart b/lib/game/components/flipper.dart index 92b2ddd4..6e64c781 100644 --- a/lib/game/components/flipper.dart +++ b/lib/game/components/flipper.dart @@ -3,10 +3,20 @@ import 'dart:math' as math; import 'package:flame/components.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; -import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:pinball/game/game.dart'; import 'package:pinball/gen/assets.gen.dart'; +import 'package:pinball_components/pinball_components.dart' hide Assets; + +const _leftFlipperKeys = [ + LogicalKeyboardKey.arrowLeft, + LogicalKeyboardKey.keyA, +]; + +const _rightFlipperKeys = [ + LogicalKeyboardKey.arrowRight, + LogicalKeyboardKey.keyD, +]; /// {@template flipper} /// A bat, typically found in pairs at the bottom of the board. @@ -15,43 +25,9 @@ import 'package:pinball/gen/assets.gen.dart'; /// {@endtemplate flipper} class Flipper extends BodyComponent with KeyboardHandler, InitialPosition { /// {@macro flipper} - Flipper._({ + Flipper({ required this.side, - required List keys, - }) : _keys = keys; - - Flipper._left() - : this._( - side: BoardSide.left, - keys: [ - LogicalKeyboardKey.arrowLeft, - LogicalKeyboardKey.keyA, - ], - ); - - Flipper._right() - : this._( - side: BoardSide.right, - keys: [ - LogicalKeyboardKey.arrowRight, - LogicalKeyboardKey.keyD, - ], - ); - - /// Constructs a [Flipper] from a [BoardSide]. - /// - /// A [Flipper._right] and [Flipper._left] besides being mirrored - /// horizontally, also have different [LogicalKeyboardKey]s that control them. - factory Flipper.fromSide({ - required BoardSide side, - }) { - switch (side) { - case BoardSide.left: - return Flipper._left(); - case BoardSide.right: - return Flipper._right(); - } - } + }) : _keys = side.isLeft ? _leftFlipperKeys : _rightFlipperKeys; /// The size of the [Flipper]. static final size = Vector2(12, 2.8); @@ -104,35 +80,29 @@ class Flipper extends BodyComponent with KeyboardHandler, InitialPosition { /// Anchors the [Flipper] to the [RevoluteJoint] that controls its arc motion. Future _anchorToJoint() async { - final anchor = FlipperAnchor(flipper: this); + final anchor = _FlipperAnchor(flipper: this); await add(anchor); - final jointDef = FlipperAnchorRevoluteJointDef( + final jointDef = _FlipperAnchorRevoluteJointDef( flipper: this, anchor: anchor, ); - // TODO(alestiago): Remove casting once the following is closed: - // https://github.com/flame-engine/forge2d/issues/36 - final joint = world.createJoint(jointDef) as RevoluteJoint; + final joint = _FlipperJoint(jointDef)..create(world); // FIXME(erickzanardo): when mounted the initial position is not fully // reached. unawaited( - mounted.whenComplete( - () => FlipperAnchorRevoluteJointDef.unlock(joint, side), - ), + mounted.whenComplete(joint.unlock), ); } List _createFixtureDefs() { final fixturesDef = []; - final isLeft = side.isLeft; + final direction = side.direction; final bigCircleShape = CircleShape()..radius = 1.75; bigCircleShape.position.setValues( - isLeft - ? -(size.x / 2) + bigCircleShape.radius - : (size.x / 2) - bigCircleShape.radius, + ((size.x / 2) * direction) + (bigCircleShape.radius * -direction), 0, ); final bigCircleFixtureDef = FixtureDef(bigCircleShape); @@ -140,15 +110,13 @@ class Flipper extends BodyComponent with KeyboardHandler, InitialPosition { final smallCircleShape = CircleShape()..radius = 0.9; smallCircleShape.position.setValues( - isLeft - ? (size.x / 2) - smallCircleShape.radius - : -(size.x / 2) + smallCircleShape.radius, + ((size.x / 2) * -direction) + (smallCircleShape.radius * direction), 0, ); final smallCircleFixtureDef = FixtureDef(smallCircleShape); fixturesDef.add(smallCircleFixtureDef); - final trapeziumVertices = isLeft + final trapeziumVertices = side.isLeft ? [ Vector2(bigCircleShape.position.x, bigCircleShape.radius), Vector2(smallCircleShape.position.x, smallCircleShape.radius), @@ -173,7 +141,8 @@ class Flipper extends BodyComponent with KeyboardHandler, InitialPosition { @override Future onLoad() async { await super.onLoad(); - paint = Paint()..color = Colors.transparent; + renderBody = false; + await Future.wait([ _loadSprite(), _anchorToJoint(), @@ -214,61 +183,66 @@ class Flipper extends BodyComponent with KeyboardHandler, InitialPosition { /// /// The end of a [Flipper] depends on its [Flipper.side]. /// {@endtemplate} -class FlipperAnchor extends JointAnchor { +class _FlipperAnchor extends JointAnchor { /// {@macro flipper_anchor} - FlipperAnchor({ + _FlipperAnchor({ required Flipper flipper, }) { initialPosition = Vector2( - flipper.side.isLeft - ? flipper.body.position.x - Flipper.size.x / 2 - : flipper.body.position.x + Flipper.size.x / 2, + flipper.body.position.x + ((Flipper.size.x * flipper.side.direction) / 2), flipper.body.position.y, ); } } /// {@template flipper_anchor_revolute_joint_def} -/// Hinges one end of [Flipper] to a [FlipperAnchor] to achieve an arc motion. +/// Hinges one end of [Flipper] to a [_FlipperAnchor] to achieve an arc motion. /// {@endtemplate} -class FlipperAnchorRevoluteJointDef extends RevoluteJointDef { +class _FlipperAnchorRevoluteJointDef extends RevoluteJointDef { /// {@macro flipper_anchor_revolute_joint_def} - FlipperAnchorRevoluteJointDef({ + _FlipperAnchorRevoluteJointDef({ required Flipper flipper, - required FlipperAnchor anchor, - }) { + required _FlipperAnchor anchor, + }) : side = flipper.side { initialize( flipper.body, anchor.body, anchor.body.position, ); - enableLimit = true; - final angle = (flipper.side.isLeft ? _sweepingAngle : -_sweepingAngle) / 2; + enableLimit = true; + final angle = (_sweepingAngle * -side.direction) / 2; lowerAngle = upperAngle = angle; } /// The total angle of the arc motion. static const _sweepingAngle = math.pi / 3.5; + final BoardSide side; +} + +class _FlipperJoint extends RevoluteJoint { + _FlipperJoint(_FlipperAnchorRevoluteJointDef def) + : side = def.side, + super(def); + + final BoardSide side; + + // TODO(alestiago): Remove once Forge2D supports custom joints. + void create(World world) { + world.joints.add(this); + bodyA.joints.add(this); + bodyB.joints.add(this); + } + /// Unlocks the [Flipper] from its resting position. /// /// The [Flipper] is locked when initialized in order to force it to be at /// its resting position. - // TODO(alestiago): consider refactor once the issue is solved: - // https://github.com/flame-engine/forge2d/issues/36 - static void unlock(RevoluteJoint joint, BoardSide side) { - late final double upperLimit, lowerLimit; - switch (side) { - case BoardSide.left: - lowerLimit = -joint.lowerLimit; - upperLimit = joint.upperLimit; - break; - case BoardSide.right: - lowerLimit = joint.lowerLimit; - upperLimit = -joint.upperLimit; - } - - joint.setLimits(lowerLimit, upperLimit); + void unlock() { + setLimits( + lowerLimit * side.direction, + -upperLimit * side.direction, + ); } } diff --git a/lib/game/components/jetpack_ramp.dart b/lib/game/components/jetpack_ramp.dart index 4afc36d1..a69b2111 100644 --- a/lib/game/components/jetpack_ramp.dart +++ b/lib/game/components/jetpack_ramp.dart @@ -4,6 +4,7 @@ import 'package:flame/components.dart'; import 'package:flame/extensions.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:pinball/game/game.dart'; +import 'package:pinball_components/pinball_components.dart'; /// {@template jetpack_ramp} /// Represents the upper left blue ramp of the [Board]. diff --git a/lib/game/components/joint_anchor.dart b/lib/game/components/joint_anchor.dart index e945bd72..98b80ece 100644 --- a/lib/game/components/joint_anchor.dart +++ b/lib/game/components/joint_anchor.dart @@ -1,5 +1,5 @@ import 'package:flame_forge2d/flame_forge2d.dart'; -import 'package:pinball/game/game.dart'; +import 'package:pinball_components/pinball_components.dart'; /// {@template joint_anchor} /// Non visual [BodyComponent] used to hold a [BodyType.dynamic] in [Joint]s diff --git a/lib/game/components/kicker.dart b/lib/game/components/kicker.dart index e4b2824d..dc55a52f 100644 --- a/lib/game/components/kicker.dart +++ b/lib/game/components/kicker.dart @@ -5,6 +5,7 @@ import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flutter/material.dart'; import 'package:geometry/geometry.dart' as geometry show centroid; import 'package:pinball/game/game.dart'; +import 'package:pinball_components/pinball_components.dart'; /// {@template kicker} /// Triangular [BodyType.static] body that propels the [Ball] towards the diff --git a/lib/game/components/launcher_ramp.dart b/lib/game/components/launcher_ramp.dart index 0b6e4dbf..aae9265f 100644 --- a/lib/game/components/launcher_ramp.dart +++ b/lib/game/components/launcher_ramp.dart @@ -4,6 +4,7 @@ import 'package:flame/components.dart'; import 'package:flame/extensions.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:pinball/game/game.dart'; +import 'package:pinball_components/pinball_components.dart'; /// {@template launcher_ramp} /// The yellow left ramp, where the [Ball] goes through when launched from the diff --git a/lib/game/components/pathway.dart b/lib/game/components/pathway.dart index 8604e0f3..54c81464 100644 --- a/lib/game/components/pathway.dart +++ b/lib/game/components/pathway.dart @@ -2,7 +2,7 @@ import 'package:flame/extensions.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flutter/material.dart'; import 'package:geometry/geometry.dart'; -import 'package:pinball/game/game.dart'; +import 'package:pinball_components/pinball_components.dart'; /// {@template pathway} /// [Pathway] creates lines of various shapes. @@ -144,6 +144,46 @@ class Pathway extends BodyComponent with InitialPosition, Layered { ); } + /// Creates an ellipse [Pathway]. + /// + /// Does so with two [ChainShape]s separated by a [width]. Can + /// be rotated by a given [rotation] in radians. + /// + /// If [singleWall] is true, just one [ChainShape] is created. + factory Pathway.ellipse({ + Color? color, + required Vector2 center, + required double width, + required double majorRadius, + required double minorRadius, + double rotation = 0, + bool singleWall = false, + }) { + final paths = >[]; + + // TODO(ruialonso): Refactor repetitive logic + final outerWall = calculateEllipse( + center: center, + majorRadius: majorRadius, + minorRadius: minorRadius, + ).map((vector) => vector..rotate(rotation)).toList(); + paths.add(outerWall); + + if (!singleWall) { + final innerWall = calculateEllipse( + center: center, + majorRadius: majorRadius - width, + minorRadius: minorRadius - width, + ).map((vector) => vector..rotate(rotation)).toList(); + paths.add(innerWall); + } + + return Pathway._( + color: color, + paths: paths, + ); + } + final List> _paths; /// Constructs different [ChainShape]s to form the [Pathway] shape. diff --git a/lib/game/components/plunger.dart b/lib/game/components/plunger.dart index 934cd8ac..9791ec66 100644 --- a/lib/game/components/plunger.dart +++ b/lib/game/components/plunger.dart @@ -2,6 +2,7 @@ import 'package:flame/components.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flutter/services.dart'; import 'package:pinball/game/game.dart'; +import 'package:pinball_components/pinball_components.dart'; /// {@template plunger} /// [Plunger] serves as a spring, that shoots the ball on the right side of the diff --git a/lib/game/components/ramp_opening.dart b/lib/game/components/ramp_opening.dart index 2d8454dd..d8ddcc35 100644 --- a/lib/game/components/ramp_opening.dart +++ b/lib/game/components/ramp_opening.dart @@ -2,6 +2,7 @@ import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:pinball/game/game.dart'; +import 'package:pinball_components/pinball_components.dart'; /// {@template ramp_orientation} /// Determines if a ramp is facing [up] or [down] on the [Board]. diff --git a/lib/game/components/round_bumper.dart b/lib/game/components/round_bumper.dart index 2f43a35b..969bddbe 100644 --- a/lib/game/components/round_bumper.dart +++ b/lib/game/components/round_bumper.dart @@ -1,5 +1,6 @@ import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:pinball/game/game.dart'; +import 'package:pinball_components/pinball_components.dart'; /// {@template round_bumper} /// Circular body that repels a [Ball] on contact, increasing the score. diff --git a/lib/game/components/score_points.dart b/lib/game/components/score_points.dart index da894652..12649137 100644 --- a/lib/game/components/score_points.dart +++ b/lib/game/components/score_points.dart @@ -2,11 +2,12 @@ import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:pinball/game/game.dart'; +import 'package:pinball_components/pinball_components.dart'; /// {@template score_points} /// Specifies the amount of points received on [Ball] collision. /// {@endtemplate} -mixin ScorePoints on BodyComponent { +mixin ScorePoints on BodyComponent { /// {@macro score_points} int get points; @@ -26,6 +27,8 @@ class BallScorePointsCallback extends ContactCallback { ScorePoints scorePoints, Contact _, ) { - ball.gameRef.read().add(Scored(points: scorePoints.points)); + ball.controller.gameRef.read().add( + Scored(points: scorePoints.points), + ); } } diff --git a/lib/game/components/spaceship.dart b/lib/game/components/spaceship.dart index e5427e78..854bb595 100644 --- a/lib/game/components/spaceship.dart +++ b/lib/game/components/spaceship.dart @@ -9,6 +9,7 @@ import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:pinball/flame/blueprint.dart'; import 'package:pinball/game/game.dart'; import 'package:pinball/gen/assets.gen.dart'; +import 'package:pinball_components/pinball_components.dart' hide Assets; /// A [Blueprint] which creates the spaceship feature. class Spaceship extends Forge2DBlueprint { @@ -16,7 +17,7 @@ class Spaceship extends Forge2DBlueprint { static const radius = 10.0; @override - void build() { + void build(_) { final position = Vector2( PinballGame.boardBounds.left + radius + 15, PinballGame.boardBounds.center.dy + 30, diff --git a/lib/game/components/wall.dart b/lib/game/components/wall.dart index 7475715e..0fb57a41 100644 --- a/lib/game/components/wall.dart +++ b/lib/game/components/wall.dart @@ -4,6 +4,7 @@ import 'package:flame/extensions.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:pinball/game/components/components.dart'; import 'package:pinball/game/pinball_game.dart'; +import 'package:pinball_components/pinball_components.dart'; /// {@template wall} /// A continuous generic and [BodyType.static] barrier that divides a game area. @@ -77,6 +78,6 @@ class BottomWall extends Wall { class BottomWallBallContactCallback extends ContactCallback { @override void begin(Ball ball, BottomWall wall, Contact contact) { - ball.lost(); + ball.controller.lost(); } } diff --git a/lib/game/game_assets.dart b/lib/game/game_assets.dart index d19ef177..fdc1f332 100644 --- a/lib/game/game_assets.dart +++ b/lib/game/game_assets.dart @@ -1,12 +1,13 @@ import 'package:pinball/game/game.dart'; import 'package:pinball/gen/assets.gen.dart'; +import 'package:pinball_components/pinball_components.dart' as components; /// Add methods to help loading and caching game assets. extension PinballGameAssetsX on PinballGame { /// Pre load the initial assets of the game. Future preLoadAssets() async { await Future.wait([ - images.load(Assets.images.components.ball.path), + images.load(components.Assets.images.ball.keyName), images.load(Assets.images.components.flipper.path), images.load(Assets.images.components.spaceship.androidTop.path), images.load(Assets.images.components.spaceship.androidBottom.path), diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index 44a7ec01..681a6431 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -1,5 +1,4 @@ // ignore_for_file: public_member_api_docs - import 'dart:async'; import 'package:flame/extensions.dart'; import 'package:flame/input.dart'; @@ -103,11 +102,7 @@ class PinballGame extends Forge2DGame } void spawnBall() { - final ball = Ball(); - add( - ball - ..initialPosition = plunger.body.position + Vector2(0, ball.size.y / 2), - ); + addFromBlueprint(BallBlueprint(position: plunger.body.position)); } } @@ -116,8 +111,6 @@ class DebugPinballGame extends PinballGame with TapDetector { @override void onTapUp(TapUpInfo info) { - add( - Ball()..initialPosition = info.eventPosition.game, - ); + addFromBlueprint(BallBlueprint(position: info.eventPosition.game)); } } diff --git a/lib/gen/assets.gen.dart b/lib/gen/assets.gen.dart index fe4bad8b..ba75412b 100644 --- a/lib/gen/assets.gen.dart +++ b/lib/gen/assets.gen.dart @@ -15,8 +15,6 @@ class $AssetsImagesGen { class $AssetsImagesComponentsGen { const $AssetsImagesComponentsGen(); - AssetGenImage get ball => - const AssetGenImage('assets/images/components/ball.png'); AssetGenImage get flipper => const AssetGenImage('assets/images/components/flipper.png'); AssetGenImage get sauce => diff --git a/lib/leaderboard/bloc/leaderboard_bloc.dart b/lib/leaderboard/bloc/leaderboard_bloc.dart index 6542548d..49a35474 100644 --- a/lib/leaderboard/bloc/leaderboard_bloc.dart +++ b/lib/leaderboard/bloc/leaderboard_bloc.dart @@ -3,6 +3,7 @@ import 'dart:async'; import 'package:bloc/bloc.dart'; import 'package:equatable/equatable.dart'; import 'package:leaderboard_repository/leaderboard_repository.dart'; +import 'package:pinball/leaderboard/leaderboard.dart'; part 'leaderboard_event.dart'; part 'leaderboard_state.dart'; @@ -30,10 +31,16 @@ class LeaderboardBloc extends Bloc { try { final top10Leaderboard = await _leaderboardRepository.fetchTop10Leaderboard(); + + final leaderboardEntries = []; + top10Leaderboard.asMap().forEach( + (index, value) => leaderboardEntries.add(value.toEntry(index + 1)), + ); + emit( state.copyWith( status: LeaderboardStatus.success, - leaderboard: top10Leaderboard, + leaderboard: leaderboardEntries, ), ); } catch (error) { diff --git a/lib/leaderboard/bloc/leaderboard_event.dart b/lib/leaderboard/bloc/leaderboard_event.dart index 34152163..b9e6955a 100644 --- a/lib/leaderboard/bloc/leaderboard_event.dart +++ b/lib/leaderboard/bloc/leaderboard_event.dart @@ -9,7 +9,7 @@ abstract class LeaderboardEvent extends Equatable { } /// {@template top_10_fetched} -/// Request the top 10 [LeaderboardEntry]s. +/// Request the top 10 [LeaderboardEntryData]s. /// {endtemplate} class Top10Fetched extends LeaderboardEvent { /// {@macro top_10_fetched} @@ -20,7 +20,7 @@ class Top10Fetched extends LeaderboardEvent { } /// {@template leaderboard_entry_added} -/// Writes a new [LeaderboardEntry]. +/// Writes a new [LeaderboardEntryData]. /// /// Should be added when a player finishes a game. /// {endtemplate} @@ -28,8 +28,8 @@ class LeaderboardEntryAdded extends LeaderboardEvent { /// {@macro leaderboard_entry_added} const LeaderboardEntryAdded({required this.entry}); - /// [LeaderboardEntry] to be written to the remote storage. - final LeaderboardEntry entry; + /// [LeaderboardEntryData] to be written to the remote storage. + final LeaderboardEntryData entry; @override List get props => [entry]; diff --git a/lib/leaderboard/leaderboard.dart b/lib/leaderboard/leaderboard.dart index 13d71e40..156b7f78 100644 --- a/lib/leaderboard/leaderboard.dart +++ b/lib/leaderboard/leaderboard.dart @@ -1 +1,2 @@ export 'bloc/leaderboard_bloc.dart'; +export 'models/leader_board_entry.dart'; diff --git a/lib/leaderboard/models/leader_board_entry.dart b/lib/leaderboard/models/leader_board_entry.dart new file mode 100644 index 00000000..194f7cb6 --- /dev/null +++ b/lib/leaderboard/models/leader_board_entry.dart @@ -0,0 +1,80 @@ +import 'package:leaderboard_repository/leaderboard_repository.dart'; +import 'package:pinball_theme/pinball_theme.dart'; + +/// {@template leaderboard_entry} +/// A model representing a leaderboard entry containing the ranking position, +/// player's initials, score, and chosen character. +/// +/// {@endtemplate} +class LeaderboardEntry { + /// {@macro leaderboard_entry} + LeaderboardEntry({ + required this.rank, + required this.playerInitials, + required this.score, + required this.character, + }); + + /// Ranking position for [LeaderboardEntry]. + final String rank; + + /// Player's chosen initials for [LeaderboardEntry]. + final String playerInitials; + + /// Score for [LeaderboardEntry]. + final int score; + + /// [CharacterTheme] for [LeaderboardEntry]. + final AssetGenImage character; +} + +/// Converts [LeaderboardEntryData] from repository to [LeaderboardEntry]. +extension LeaderboardEntryDataX on LeaderboardEntryData { + /// Conversion method to [LeaderboardEntry] + LeaderboardEntry toEntry(int position) { + return LeaderboardEntry( + rank: position.toString(), + playerInitials: playerInitials, + score: score, + character: character.toTheme.characterAsset, + ); + } +} + +/// Converts [CharacterType] to [CharacterTheme] to show on UI character theme +/// from repository. +extension CharacterTypeX on CharacterType { + /// Conversion method to [CharacterTheme] + CharacterTheme get toTheme { + switch (this) { + case CharacterType.dash: + return const DashTheme(); + case CharacterType.sparky: + return const SparkyTheme(); + case CharacterType.android: + return const AndroidTheme(); + case CharacterType.dino: + return const DinoTheme(); + } + } +} + +/// Converts [CharacterTheme] to [CharacterType] to persist at repository the +/// character theme from UI. +extension CharacterThemeX on CharacterTheme { + /// Conversion method to [CharacterType] + CharacterType get toType { + switch (runtimeType) { + case DashTheme: + return CharacterType.dash; + case SparkyTheme: + return CharacterType.sparky; + case AndroidTheme: + return CharacterType.android; + case DinoTheme: + return CharacterType.dino; + default: + return CharacterType.dash; + } + } +} diff --git a/packages/geometry/lib/src/geometry.dart b/packages/geometry/lib/src/geometry.dart index 8574bc73..6975f8cb 100644 --- a/packages/geometry/lib/src/geometry.dart +++ b/packages/geometry/lib/src/geometry.dart @@ -23,10 +23,45 @@ List calculateArc({ final points = []; for (var i = 0; i < precision; i++) { - final xCoord = center.x + radius * math.cos((stepAngle * i) + offsetAngle); - final yCoord = center.y - radius * math.sin((stepAngle * i) + offsetAngle); + final x = center.x + radius * math.cos((stepAngle * i) + offsetAngle); + final y = center.y - radius * math.sin((stepAngle * i) + offsetAngle); - final point = Vector2(xCoord, yCoord); + final point = Vector2(x, y); + points.add(point); + } + + return points; +} + +/// Calculates all [Vector2]s of an ellipse. +/// +/// An ellipse can be achieved by specifying a [center], a [majorRadius] and a +/// [minorRadius]. +/// +/// The higher the [precision], the more [Vector2]s will be calculated; +/// achieving a more rounded ellipse. +/// +/// For more information read: https://en.wikipedia.org/wiki/Ellipse. +List calculateEllipse({ + required Vector2 center, + required double majorRadius, + required double minorRadius, + int precision = 100, +}) { + assert( + 0 < minorRadius && minorRadius <= majorRadius, + 'smallRadius ($minorRadius) and bigRadius ($majorRadius) must be in ' + 'range 0 < smallRadius <= bigRadius', + ); + + final stepAngle = 2 * math.pi / (precision - 1); + + final points = []; + for (var i = 0; i < precision; i++) { + final x = center.x + minorRadius * math.cos(stepAngle * i); + final y = center.y - majorRadius * math.sin(stepAngle * i); + + final point = Vector2(x, y); points.add(point); } @@ -63,17 +98,15 @@ List calculateBezierCurve({ final points = []; do { - var xCoord = 0.0; - var yCoord = 0.0; + var x = 0.0; + var y = 0.0; for (var i = 0; i <= n; i++) { final point = controlPoints[i]; - xCoord += - binomial(n, i) * math.pow(1 - t, n - i) * math.pow(t, i) * point.x; - yCoord += - binomial(n, i) * math.pow(1 - t, n - i) * math.pow(t, i) * point.y; + x += binomial(n, i) * math.pow(1 - t, n - i) * math.pow(t, i) * point.x; + y += binomial(n, i) * math.pow(1 - t, n - i) * math.pow(t, i) * point.y; } - points.add(Vector2(xCoord, yCoord)); + points.add(Vector2(x, y)); t = t + step; } while (t <= 1); diff --git a/packages/geometry/test/src/geometry_test.dart b/packages/geometry/test/src/geometry_test.dart index 5c33d70f..7a49b2b2 100644 --- a/packages/geometry/test/src/geometry_test.dart +++ b/packages/geometry/test/src/geometry_test.dart @@ -33,6 +33,46 @@ void main() { }); }); + group('calculateEllipse', () { + test('returns by default 100 points as indicated by precision', () { + final points = calculateEllipse( + center: Vector2.zero(), + majorRadius: 100, + minorRadius: 50, + ); + expect(points.length, 100); + }); + + test('returns as many points as indicated by precision', () { + final points = calculateEllipse( + center: Vector2.zero(), + majorRadius: 100, + minorRadius: 50, + precision: 50, + ); + expect(points.length, 50); + }); + + test('fails if radius not in range', () { + expect( + () => calculateEllipse( + center: Vector2.zero(), + majorRadius: 100, + minorRadius: 150, + ), + throwsA(isA()), + ); + expect( + () => calculateEllipse( + center: Vector2.zero(), + majorRadius: 100, + minorRadius: 0, + ), + throwsA(isA()), + ); + }); + }); + group('calculateBezierCurve', () { test('fails if step not in range', () { expect( diff --git a/packages/leaderboard_repository/lib/src/leaderboard_repository.dart b/packages/leaderboard_repository/lib/src/leaderboard_repository.dart index 5a5fa42c..d75a88b3 100644 --- a/packages/leaderboard_repository/lib/src/leaderboard_repository.dart +++ b/packages/leaderboard_repository/lib/src/leaderboard_repository.dart @@ -83,9 +83,9 @@ class LeaderboardRepository { final FirebaseFirestore _firebaseFirestore; - /// Acquires top 10 [LeaderboardEntry]s. - Future> fetchTop10Leaderboard() async { - final leaderboardEntries = []; + /// Acquires top 10 [LeaderboardEntryData]s. + Future> fetchTop10Leaderboard() async { + final leaderboardEntries = []; late List documents; try { @@ -103,7 +103,7 @@ class LeaderboardRepository { final data = document.data() as Map?; if (data != null) { try { - leaderboardEntries.add(LeaderboardEntry.fromJson(data)); + leaderboardEntries.add(LeaderboardEntryData.fromJson(data)); } catch (error, stackTrace) { throw LeaderboardDeserializationException(error, stackTrace); } @@ -115,7 +115,9 @@ class LeaderboardRepository { /// Adds player's score entry to the leaderboard and gets their /// [LeaderboardRanking]. - Future addLeaderboardEntry(LeaderboardEntry entry) async { + Future addLeaderboardEntry( + LeaderboardEntryData entry, + ) async { late DocumentReference entryReference; try { entryReference = await _firebaseFirestore diff --git a/packages/leaderboard_repository/lib/src/models/leaderboard_entry.dart b/packages/leaderboard_repository/lib/src/models/leaderboard_entry_data.dart similarity index 64% rename from packages/leaderboard_repository/lib/src/models/leaderboard_entry.dart rename to packages/leaderboard_repository/lib/src/models/leaderboard_entry_data.dart index 86cb2464..c8137520 100644 --- a/packages/leaderboard_repository/lib/src/models/leaderboard_entry.dart +++ b/packages/leaderboard_repository/lib/src/models/leaderboard_entry_data.dart @@ -1,9 +1,9 @@ import 'package:equatable/equatable.dart'; import 'package:json_annotation/json_annotation.dart'; -part 'leaderboard_entry.g.dart'; +part 'leaderboard_entry_data.g.dart'; -/// Google character type associated with a [LeaderboardEntry]. +/// Google character type associated with a [LeaderboardEntryData]. enum CharacterType { /// Dash character. dash, @@ -18,7 +18,7 @@ enum CharacterType { dino, } -/// {@template leaderboard_entry} +/// {@template leaderboard_entry_data} /// A model representing a leaderboard entry containing the player's initials, /// score, and chosen character. /// @@ -34,42 +34,42 @@ enum CharacterType { /// ``` /// {@endtemplate} @JsonSerializable() -class LeaderboardEntry extends Equatable { - /// {@macro leaderboard_entry} - const LeaderboardEntry({ +class LeaderboardEntryData extends Equatable { + /// {@macro leaderboard_entry_data} + const LeaderboardEntryData({ required this.playerInitials, required this.score, required this.character, }); - /// Factory which converts a [Map] into a [LeaderboardEntry]. - factory LeaderboardEntry.fromJson(Map json) { + /// Factory which converts a [Map] into a [LeaderboardEntryData]. + factory LeaderboardEntryData.fromJson(Map json) { return _$LeaderboardEntryFromJson(json); } - /// Converts the [LeaderboardEntry] to [Map]. + /// Converts the [LeaderboardEntryData] to [Map]. Map toJson() => _$LeaderboardEntryToJson(this); - /// Player's chosen initials for [LeaderboardEntry]. + /// Player's chosen initials for [LeaderboardEntryData]. /// /// Example: 'ABC'. @JsonKey(name: 'playerInitials') final String playerInitials; - /// Score for [LeaderboardEntry]. + /// Score for [LeaderboardEntryData]. /// /// Example: 1500. @JsonKey(name: 'score') final int score; - /// [CharacterType] for [LeaderboardEntry]. + /// [CharacterType] for [LeaderboardEntryData]. /// /// Example: [CharacterType.dash]. @JsonKey(name: 'character') final CharacterType character; - /// An empty [LeaderboardEntry] object. - static const empty = LeaderboardEntry( + /// An empty [LeaderboardEntryData] object. + static const empty = LeaderboardEntryData( playerInitials: '', score: 0, character: CharacterType.dash, diff --git a/packages/leaderboard_repository/lib/src/models/leaderboard_entry.g.dart b/packages/leaderboard_repository/lib/src/models/leaderboard_entry_data.g.dart similarity index 76% rename from packages/leaderboard_repository/lib/src/models/leaderboard_entry.g.dart rename to packages/leaderboard_repository/lib/src/models/leaderboard_entry_data.g.dart index fc685220..e57e43b8 100644 --- a/packages/leaderboard_repository/lib/src/models/leaderboard_entry.g.dart +++ b/packages/leaderboard_repository/lib/src/models/leaderboard_entry_data.g.dart @@ -1,19 +1,19 @@ // GENERATED CODE - DO NOT MODIFY BY HAND -part of 'leaderboard_entry.dart'; +part of 'leaderboard_entry_data.dart'; // ************************************************************************** // JsonSerializableGenerator // ************************************************************************** -LeaderboardEntry _$LeaderboardEntryFromJson(Map json) => - LeaderboardEntry( +LeaderboardEntryData _$LeaderboardEntryFromJson(Map json) => + LeaderboardEntryData( playerInitials: json['playerInitials'] as String, score: json['score'] as int, character: $enumDecode(_$CharacterTypeEnumMap, json['character']), ); -Map _$LeaderboardEntryToJson(LeaderboardEntry instance) => +Map _$LeaderboardEntryToJson(LeaderboardEntryData instance) => { 'playerInitials': instance.playerInitials, 'score': instance.score, diff --git a/packages/leaderboard_repository/lib/src/models/leaderboard_ranking.dart b/packages/leaderboard_repository/lib/src/models/leaderboard_ranking.dart index 7ec90ef4..4a322e00 100644 --- a/packages/leaderboard_repository/lib/src/models/leaderboard_ranking.dart +++ b/packages/leaderboard_repository/lib/src/models/leaderboard_ranking.dart @@ -2,17 +2,17 @@ import 'package:equatable/equatable.dart'; import 'package:leaderboard_repository/leaderboard_repository.dart'; /// {@template leaderboard_ranking} -/// Contains [ranking] for a single [LeaderboardEntry] and the number of players -/// the [ranking] is [outOf]. +/// Contains [ranking] for a single [LeaderboardEntryData] and the number of +/// players the [ranking] is [outOf]. /// {@endtemplate} class LeaderboardRanking extends Equatable { /// {@macro leaderboard_ranking} const LeaderboardRanking({required this.ranking, required this.outOf}); - /// Place ranking by score for a [LeaderboardEntry]. + /// Place ranking by score for a [LeaderboardEntryData]. final int ranking; - /// Number of [LeaderboardEntry]s at the time of score entry. + /// Number of [LeaderboardEntryData]s at the time of score entry. final int outOf; @override diff --git a/packages/leaderboard_repository/lib/src/models/models.dart b/packages/leaderboard_repository/lib/src/models/models.dart index 3dabe2bf..e10a743b 100644 --- a/packages/leaderboard_repository/lib/src/models/models.dart +++ b/packages/leaderboard_repository/lib/src/models/models.dart @@ -1,2 +1,2 @@ -export 'leaderboard_entry.dart'; +export 'leaderboard_entry_data.dart'; export 'leaderboard_ranking.dart'; diff --git a/packages/leaderboard_repository/test/src/leaderboard_repository_test.dart b/packages/leaderboard_repository/test/src/leaderboard_repository_test.dart index cd632638..592425ec 100644 --- a/packages/leaderboard_repository/test/src/leaderboard_repository_test.dart +++ b/packages/leaderboard_repository/test/src/leaderboard_repository_test.dart @@ -57,7 +57,7 @@ void main() { final top10Leaderboard = top10Scores .map( - (score) => LeaderboardEntry( + (score) => LeaderboardEntryData( playerInitials: 'user$score', score: score, character: CharacterType.dash, @@ -144,7 +144,7 @@ void main() { entryScore, 1000, ]; - final leaderboardEntry = LeaderboardEntry( + final leaderboardEntry = LeaderboardEntryData( playerInitials: 'ABC', score: entryScore, character: CharacterType.dash, diff --git a/packages/leaderboard_repository/test/src/models/leaderboard_entry_test.dart b/packages/leaderboard_repository/test/src/models/leaderboard_entry_data_test.dart similarity index 72% rename from packages/leaderboard_repository/test/src/models/leaderboard_entry_test.dart rename to packages/leaderboard_repository/test/src/models/leaderboard_entry_data_test.dart index 21056529..f6e27e8a 100644 --- a/packages/leaderboard_repository/test/src/models/leaderboard_entry_test.dart +++ b/packages/leaderboard_repository/test/src/models/leaderboard_entry_data_test.dart @@ -9,21 +9,21 @@ void main() { 'character': 'dash', }; - const leaderboardEntry = LeaderboardEntry( + const leaderboardEntry = LeaderboardEntryData( playerInitials: 'ABC', score: 1500, character: CharacterType.dash, ); test('can be instantiated', () { - const leaderboardEntry = LeaderboardEntry.empty; + const leaderboardEntry = LeaderboardEntryData.empty; expect(leaderboardEntry, isNotNull); }); test('supports value equality.', () { - const leaderboardEntry = LeaderboardEntry.empty; - const leaderboardEntry2 = LeaderboardEntry.empty; + const leaderboardEntry = LeaderboardEntryData.empty; + const leaderboardEntry2 = LeaderboardEntryData.empty; expect(leaderboardEntry, equals(leaderboardEntry2)); }); @@ -33,7 +33,7 @@ void main() { }); test('can be obtained from json', () { - final leaderboardEntryFrom = LeaderboardEntry.fromJson(data); + final leaderboardEntryFrom = LeaderboardEntryData.fromJson(data); expect(leaderboardEntry, equals(leaderboardEntryFrom)); }); diff --git a/packages/pinball_components/analysis_options.yaml b/packages/pinball_components/analysis_options.yaml index 3742fc3d..f8155aa6 100644 --- a/packages/pinball_components/analysis_options.yaml +++ b/packages/pinball_components/analysis_options.yaml @@ -1 +1,4 @@ -include: package:very_good_analysis/analysis_options.2.4.0.yaml \ No newline at end of file +include: package:very_good_analysis/analysis_options.2.4.0.yaml +analyzer: + exclude: + - lib/**/*.gen.dart diff --git a/assets/images/components/ball.png b/packages/pinball_components/assets/images/ball.png similarity index 100% rename from assets/images/components/ball.png rename to packages/pinball_components/assets/images/ball.png diff --git a/packages/pinball_components/lib/gen/assets.gen.dart b/packages/pinball_components/lib/gen/assets.gen.dart new file mode 100644 index 00000000..c4ed6ca0 --- /dev/null +++ b/packages/pinball_components/lib/gen/assets.gen.dart @@ -0,0 +1,68 @@ +/// GENERATED CODE - DO NOT MODIFY BY HAND +/// ***************************************************** +/// FlutterGen +/// ***************************************************** + +import 'package:flutter/widgets.dart'; + +class $AssetsImagesGen { + const $AssetsImagesGen(); + + AssetGenImage get ball => const AssetGenImage('assets/images/ball.png'); +} + +class Assets { + Assets._(); + + static const $AssetsImagesGen images = $AssetsImagesGen(); +} + +class AssetGenImage extends AssetImage { + const AssetGenImage(String assetName) + : super(assetName, package: 'pinball_components'); + + Image image({ + Key? key, + ImageFrameBuilder? frameBuilder, + ImageLoadingBuilder? loadingBuilder, + ImageErrorWidgetBuilder? errorBuilder, + String? semanticLabel, + bool excludeFromSemantics = false, + double? width, + double? height, + Color? color, + BlendMode? colorBlendMode, + BoxFit? fit, + AlignmentGeometry alignment = Alignment.center, + ImageRepeat repeat = ImageRepeat.noRepeat, + Rect? centerSlice, + bool matchTextDirection = false, + bool gaplessPlayback = false, + bool isAntiAlias = false, + FilterQuality filterQuality = FilterQuality.low, + }) { + return Image( + key: key, + image: this, + frameBuilder: frameBuilder, + loadingBuilder: loadingBuilder, + errorBuilder: errorBuilder, + semanticLabel: semanticLabel, + excludeFromSemantics: excludeFromSemantics, + width: width, + height: height, + color: color, + colorBlendMode: colorBlendMode, + fit: fit, + alignment: alignment, + repeat: repeat, + centerSlice: centerSlice, + matchTextDirection: matchTextDirection, + gaplessPlayback: gaplessPlayback, + isAntiAlias: isAntiAlias, + filterQuality: filterQuality, + ); + } + + String get path => assetName; +} diff --git a/packages/pinball_components/lib/pinball_components.dart b/packages/pinball_components/lib/pinball_components.dart index a08579e5..b00b9d5b 100644 --- a/packages/pinball_components/lib/pinball_components.dart +++ b/packages/pinball_components/lib/pinball_components.dart @@ -1,3 +1,4 @@ library pinball_components; +export 'gen/assets.gen.dart'; export 'src/pinball_components.dart'; diff --git a/packages/pinball_components/lib/src/components/ball.dart b/packages/pinball_components/lib/src/components/ball.dart new file mode 100644 index 00000000..ec51cb47 --- /dev/null +++ b/packages/pinball_components/lib/src/components/ball.dart @@ -0,0 +1,72 @@ +import 'dart:ui'; + +import 'package:flame/components.dart'; +import 'package:flame_forge2d/flame_forge2d.dart'; +import 'package:pinball_components/pinball_components.dart'; + +/// {@template ball} +/// A solid, [BodyType.dynamic] sphere that rolls and bounces around +/// {@endtemplate} +class Ball extends BodyComponent + with Layered, InitialPosition { + /// {@macro ball_body} + Ball({ + required this.baseColor, + }) { + // TODO(ruimiguel): while developing Ball can be launched by clicking mouse, + // and default layer is Layer.all. But on final game Ball will be always be + // be launched from Plunger and LauncherRamp will modify it to Layer.board. + // We need to see what happens if Ball appears from other place like nest + // bumper, it will need to explicit change layer to Layer.board then. + layer = Layer.board; + } + + /// The size of the [Ball] + final Vector2 size = Vector2.all(2); + + /// The base [Color] used to tint this [Ball] + final Color baseColor; + + @override + Future onLoad() async { + await super.onLoad(); + final sprite = await gameRef.loadSprite(Assets.images.ball.keyName); + final tint = baseColor.withOpacity(0.5); + await add( + SpriteComponent( + sprite: sprite, + size: size, + anchor: Anchor.center, + )..tint(tint), + ); + } + + @override + Body createBody() { + final shape = CircleShape()..radius = size.x / 2; + + final fixtureDef = FixtureDef(shape)..density = 1; + + final bodyDef = BodyDef() + ..position = initialPosition + ..userData = this + ..type = BodyType.dynamic; + + return world.createBody(bodyDef)..createFixture(fixtureDef); + } + + /// Immediatly and completly [stop]s the ball. + /// + /// The [Ball] will no longer be affected by any forces, including it's + /// weight and those emitted from collisions. + void stop() { + body.setType(BodyType.static); + } + + /// Allows the [Ball] to be affected by forces. + /// + /// If previously [stop]ed, the previous ball's velocity is not kept. + void resume() { + body.setType(BodyType.dynamic); + } +} diff --git a/packages/pinball_components/lib/src/components/components.dart b/packages/pinball_components/lib/src/components/components.dart new file mode 100644 index 00000000..677bbd0c --- /dev/null +++ b/packages/pinball_components/lib/src/components/components.dart @@ -0,0 +1,3 @@ +export 'ball.dart'; +export 'initial_position.dart'; +export 'layer.dart'; diff --git a/lib/game/components/initial_position.dart b/packages/pinball_components/lib/src/components/initial_position.dart similarity index 100% rename from lib/game/components/initial_position.dart rename to packages/pinball_components/lib/src/components/initial_position.dart diff --git a/lib/game/components/layer.dart b/packages/pinball_components/lib/src/components/layer.dart similarity index 100% rename from lib/game/components/layer.dart rename to packages/pinball_components/lib/src/components/layer.dart diff --git a/packages/pinball_components/lib/src/pinball_components.dart b/packages/pinball_components/lib/src/pinball_components.dart index dbc4d6fd..003c1c49 100644 --- a/packages/pinball_components/lib/src/pinball_components.dart +++ b/packages/pinball_components/lib/src/pinball_components.dart @@ -1,7 +1 @@ -/// {@template pinball_components} -/// Package with the UI game components for the Pinball Game -/// {@endtemplate} -class PinballComponents { - /// {@macro pinball_components} - const PinballComponents(); -} +export 'components/components.dart'; diff --git a/packages/pinball_components/pubspec.yaml b/packages/pinball_components/pubspec.yaml index a3a990a0..56645a30 100644 --- a/packages/pinball_components/pubspec.yaml +++ b/packages/pinball_components/pubspec.yaml @@ -7,10 +7,24 @@ environment: sdk: ">=2.16.0 <3.0.0" dependencies: + flame: ^1.1.0-releasecandidate.6 + flame_forge2d: ^0.9.0-releasecandidate.6 flutter: sdk: flutter dev_dependencies: + flame_test: ^1.1.0 flutter_test: sdk: flutter - very_good_analysis: ^2.4.0 \ No newline at end of file + mocktail: ^0.2.0 + very_good_analysis: ^2.4.0 + +flutter: + generate: true + assets: + - assets/images/ + +flutter_gen: + line_length: 80 + assets: + package_parameter_enabled: true diff --git a/packages/pinball_components/test/helpers/helpers.dart b/packages/pinball_components/test/helpers/helpers.dart new file mode 100644 index 00000000..a8b9f7ff --- /dev/null +++ b/packages/pinball_components/test/helpers/helpers.dart @@ -0,0 +1 @@ +export 'test_game.dart'; diff --git a/packages/pinball_components/test/helpers/test_game.dart b/packages/pinball_components/test/helpers/test_game.dart new file mode 100644 index 00000000..a1219868 --- /dev/null +++ b/packages/pinball_components/test/helpers/test_game.dart @@ -0,0 +1,7 @@ +import 'package:flame_forge2d/flame_forge2d.dart'; + +class TestGame extends Forge2DGame { + TestGame() { + images.prefix = ''; + } +} diff --git a/packages/pinball_components/test/src/components/ball_test.dart b/packages/pinball_components/test/src/components/ball_test.dart new file mode 100644 index 00000000..682f1f73 --- /dev/null +++ b/packages/pinball_components/test/src/components/ball_test.dart @@ -0,0 +1,162 @@ +// ignore_for_file: cascade_invocations + +import 'package:flame_forge2d/flame_forge2d.dart'; +import 'package:flame_test/flame_test.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pinball_components/pinball_components.dart'; + +import '../../helpers/helpers.dart'; + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + final flameTester = FlameTester(TestGame.new); + + group('Ball', () { + flameTester.test( + 'loads correctly', + (game) async { + final ball = Ball(baseColor: Colors.blue); + await game.ready(); + await game.ensureAdd(ball); + + expect(game.contains(ball), isTrue); + }, + ); + + group('body', () { + flameTester.test( + 'is dynamic', + (game) async { + final ball = Ball(baseColor: Colors.blue); + await game.ensureAdd(ball); + + expect(ball.body.bodyType, equals(BodyType.dynamic)); + }, + ); + + group('can be moved', () { + flameTester.test('by its weight', (game) async { + final ball = Ball(baseColor: Colors.blue); + await game.ensureAdd(ball); + + game.update(1); + expect(ball.body.position, isNot(equals(ball.initialPosition))); + }); + + flameTester.test('by applying velocity', (game) async { + final ball = Ball(baseColor: Colors.blue); + await game.ensureAdd(ball); + + ball.body.gravityScale = 0; + ball.body.linearVelocity.setValues(10, 10); + game.update(1); + expect(ball.body.position, isNot(equals(ball.initialPosition))); + }); + }); + }); + + group('fixture', () { + flameTester.test( + 'exists', + (game) async { + final ball = Ball(baseColor: Colors.blue); + await game.ensureAdd(ball); + + expect(ball.body.fixtures[0], isA()); + }, + ); + + flameTester.test( + 'is dense', + (game) async { + final ball = Ball(baseColor: Colors.blue); + await game.ensureAdd(ball); + + final fixture = ball.body.fixtures[0]; + expect(fixture.density, greaterThan(0)); + }, + ); + + flameTester.test( + 'shape is circular', + (game) async { + final ball = Ball(baseColor: Colors.blue); + await game.ensureAdd(ball); + + final fixture = ball.body.fixtures[0]; + expect(fixture.shape.shapeType, equals(ShapeType.circle)); + expect(fixture.shape.radius, equals(1)); + }, + ); + + flameTester.test( + 'has Layer.all as default filter maskBits', + (game) async { + final ball = Ball(baseColor: Colors.blue); + await game.ready(); + await game.ensureAdd(ball); + await game.ready(); + + final fixture = ball.body.fixtures[0]; + expect(fixture.filterData.maskBits, equals(Layer.board.maskBits)); + }, + ); + }); + + group('stop', () { + group("can't be moved", () { + flameTester.test('by its weight', (game) async { + final ball = Ball(baseColor: Colors.blue); + await game.ensureAdd(ball); + ball.stop(); + + game.update(1); + expect(ball.body.position, equals(ball.initialPosition)); + }); + }); + + flameTester.test('by applying velocity', (game) async { + final ball = Ball(baseColor: Colors.blue); + await game.ensureAdd(ball); + ball.stop(); + + ball.body.linearVelocity.setValues(10, 10); + game.update(1); + expect(ball.body.position, equals(ball.initialPosition)); + }); + }); + + group('resume', () { + group('can move', () { + flameTester.test( + 'by its weight when previously stopped', + (game) async { + final ball = Ball(baseColor: Colors.blue); + await game.ensureAdd(ball); + ball.stop(); + ball.resume(); + + game.update(1); + expect(ball.body.position, isNot(equals(ball.initialPosition))); + }, + ); + + flameTester.test( + 'by applying velocity when previously stopped', + (game) async { + final ball = Ball(baseColor: Colors.blue); + await game.ensureAdd(ball); + ball.stop(); + ball.resume(); + + ball.body.gravityScale = 0; + ball.body.linearVelocity.setValues(10, 10); + game.update(1); + expect(ball.body.position, isNot(equals(ball.initialPosition))); + }, + ); + }); + }); + }); +} diff --git a/test/game/components/initial_position_test.dart b/packages/pinball_components/test/src/components/initial_position_test.dart similarity index 97% rename from test/game/components/initial_position_test.dart rename to packages/pinball_components/test/src/components/initial_position_test.dart index ece083cc..9f015150 100644 --- a/test/game/components/initial_position_test.dart +++ b/packages/pinball_components/test/src/components/initial_position_test.dart @@ -3,7 +3,7 @@ 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 'package:pinball_components/pinball_components.dart'; class TestBodyComponent extends BodyComponent with InitialPosition { @override diff --git a/test/game/components/layer_test.dart b/packages/pinball_components/test/src/components/layer_test.dart similarity index 98% rename from test/game/components/layer_test.dart rename to packages/pinball_components/test/src/components/layer_test.dart index 3aacdb49..1eaa9135 100644 --- a/test/game/components/layer_test.dart +++ b/packages/pinball_components/test/src/components/layer_test.dart @@ -4,7 +4,7 @@ import 'dart:math' as math; 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 'package:pinball_components/pinball_components.dart'; class TestBodyComponent extends BodyComponent with Layered { @override diff --git a/packages/pinball_components/test/src/pinball_components_test.dart b/packages/pinball_components/test/src/pinball_components_test.dart deleted file mode 100644 index 7359ddcb..00000000 --- a/packages/pinball_components/test/src/pinball_components_test.dart +++ /dev/null @@ -1,11 +0,0 @@ -// ignore_for_file: prefer_const_constructors -import 'package:flutter_test/flutter_test.dart'; -import 'package:pinball_components/pinball_components.dart'; - -void main() { - group('PinballComponents', () { - test('can be instantiated', () { - expect(PinballComponents(), isNotNull); - }); - }); -} diff --git a/pubspec.lock b/pubspec.lock index ac5fa36d..067559c4 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -392,6 +392,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.8.0" + pinball_components: + dependency: "direct main" + description: + path: "packages/pinball_components" + relative: true + source: path + version: "1.0.0+1" pinball_theme: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 838dd9ed..1efe9281 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -23,6 +23,8 @@ dependencies: intl: ^0.17.0 leaderboard_repository: path: packages/leaderboard_repository + pinball_components: + path: packages/pinball_components pinball_theme: path: packages/pinball_theme diff --git a/test/flame/blueprint_test.dart b/test/flame/blueprint_test.dart index e521a83c..3a9f5ed3 100644 --- a/test/flame/blueprint_test.dart +++ b/test/flame/blueprint_test.dart @@ -1,5 +1,4 @@ import 'package:flame/components.dart'; -import 'package:flame/game.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; import 'package:pinball/flame/blueprint.dart'; @@ -9,7 +8,7 @@ import '../helpers/helpers.dart'; class MyBlueprint extends Blueprint { @override - void build() { + void build(_) { add(Component()); addAll([Component(), Component()]); } @@ -17,7 +16,7 @@ class MyBlueprint extends Blueprint { class MyForge2dBlueprint extends Forge2DBlueprint { @override - void build() { + void build(_) { addContactCallback(MockContactCallback()); addAllContactCallback([MockContactCallback(), MockContactCallback()]); } @@ -26,7 +25,7 @@ class MyForge2dBlueprint extends Forge2DBlueprint { void main() { group('Blueprint', () { test('components can be added to it', () { - final blueprint = MyBlueprint()..build(); + final blueprint = MyBlueprint()..build(MockPinballGame()); expect(blueprint.components.length, equals(3)); }); @@ -59,7 +58,7 @@ void main() { }); test('callbacks can be added to it', () { - final blueprint = MyForge2dBlueprint()..build(); + final blueprint = MyForge2dBlueprint()..build(MockPinballGame()); expect(blueprint.callbacks.length, equals(3)); }); @@ -92,12 +91,5 @@ void main() { ); }, ); - - test('throws assertion error when used on a non Forge2dGame', () { - expect( - () => MyForge2dBlueprint().attach(FlameGame()), - throwsAssertionError, - ); - }); }); } diff --git a/test/game/components/ball_test.dart b/test/game/components/ball_test.dart index f94c1526..6419eef2 100644 --- a/test/game/components/ball_test.dart +++ b/test/game/components/ball_test.dart @@ -1,110 +1,17 @@ // ignore_for_file: cascade_invocations import 'package:bloc_test/bloc_test.dart'; -import 'package:flame_forge2d/flame_forge2d.dart'; -import 'package:flame_test/flame_test.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; import 'package:pinball/game/game.dart'; +import 'package:pinball_components/pinball_components.dart'; import '../../helpers/helpers.dart'; void main() { TestWidgetsFlutterBinding.ensureInitialized(); - final flameTester = FlameTester(PinballGameTest.create); group('Ball', () { - flameTester.test( - 'loads correctly', - (game) async { - final ball = Ball(); - await game.ready(); - await game.ensureAdd(ball); - - expect(game.contains(ball), isTrue); - }, - ); - - group('body', () { - flameTester.test( - 'is dynamic', - (game) async { - final ball = Ball(); - await game.ensureAdd(ball); - - expect(ball.body.bodyType, equals(BodyType.dynamic)); - }, - ); - - group('can be moved', () { - flameTester.test('by its weight', (game) async { - final ball = Ball(); - await game.ensureAdd(ball); - - game.update(1); - expect(ball.body.position, isNot(equals(ball.initialPosition))); - }); - - flameTester.test('by applying velocity', (game) async { - final ball = Ball(); - await game.ensureAdd(ball); - - ball.body.gravityScale = 0; - ball.body.linearVelocity.setValues(10, 10); - game.update(1); - expect(ball.body.position, isNot(equals(ball.initialPosition))); - }); - }); - }); - - group('fixture', () { - flameTester.test( - 'exists', - (game) async { - final ball = Ball(); - await game.ensureAdd(ball); - - expect(ball.body.fixtures[0], isA()); - }, - ); - - flameTester.test( - 'is dense', - (game) async { - final ball = Ball(); - await game.ensureAdd(ball); - - final fixture = ball.body.fixtures[0]; - expect(fixture.density, greaterThan(0)); - }, - ); - - flameTester.test( - 'shape is circular', - (game) async { - final ball = Ball(); - await game.ensureAdd(ball); - - final fixture = ball.body.fixtures[0]; - expect(fixture.shape.shapeType, equals(ShapeType.circle)); - expect(fixture.shape.radius, equals(1)); - }, - ); - - flameTester.test( - 'has Layer.all as default filter maskBits', - (game) async { - final ball = Ball(); - await game.ready(); - await game.ensureAdd(ball); - await game.ready(); - - final fixture = ball.body.fixtures[0]; - expect(fixture.filterData.maskBits, equals(Layer.board.maskBits)); - }, - ); - }); - group('lost', () { late GameBloc gameBloc; @@ -124,7 +31,7 @@ void main() { (game, tester) async { await game.ready(); - game.children.whereType().first.lost(); + game.children.whereType().first.controller.lost(); await tester.pump(); verify(() => gameBloc.add(const BallLost())).called(1); @@ -136,7 +43,7 @@ void main() { (game, tester) async { await game.ready(); - game.children.whereType().first.lost(); + game.children.whereType().first.controller.lost(); await game.ready(); // Making sure that all additions are done expect( @@ -162,7 +69,7 @@ void main() { ); await game.ready(); - game.children.whereType().first.lost(); + game.children.whereType().first.controller.lost(); await tester.pump(); expect( @@ -172,60 +79,5 @@ void main() { }, ); }); - - group('stop', () { - group("can't be moved", () { - flameTester.test('by its weight', (game) async { - final ball = Ball(); - await game.ensureAdd(ball); - ball.stop(); - - game.update(1); - expect(ball.body.position, equals(ball.initialPosition)); - }); - }); - - flameTester.test('by applying velocity', (game) async { - final ball = Ball(); - await game.ensureAdd(ball); - ball.stop(); - - ball.body.linearVelocity.setValues(10, 10); - game.update(1); - expect(ball.body.position, equals(ball.initialPosition)); - }); - }); - - group('resume', () { - group('can move', () { - flameTester.test( - 'by its weight when previously stopped', - (game) async { - final ball = Ball(); - await game.ensureAdd(ball); - ball.stop(); - ball.resume(); - - game.update(1); - expect(ball.body.position, isNot(equals(ball.initialPosition))); - }, - ); - - flameTester.test( - 'by applying velocity when previously stopped', - (game) async { - final ball = Ball(); - await game.ensureAdd(ball); - ball.stop(); - ball.resume(); - - ball.body.gravityScale = 0; - ball.body.linearVelocity.setValues(10, 10); - game.update(1); - expect(ball.body.position, isNot(equals(ball.initialPosition))); - }, - ); - }); - }); }); } diff --git a/test/game/components/flipper_test.dart b/test/game/components/flipper_test.dart index 64d2f77b..3e6429df 100644 --- a/test/game/components/flipper_test.dart +++ b/test/game/components/flipper_test.dart @@ -4,9 +4,11 @@ import 'dart:collection'; import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_test/flame_test.dart'; +import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pinball/game/game.dart'; +import 'package:pinball_components/pinball_components.dart'; import '../../helpers/helpers.dart'; @@ -17,13 +19,14 @@ void main() { group( 'Flipper', () { + // TODO(alestiago): Add golden tests. flameTester.test( 'loads correctly', (game) async { - final leftFlipper = Flipper.fromSide( + final leftFlipper = Flipper( side: BoardSide.left, ); - final rightFlipper = Flipper.fromSide( + final rightFlipper = Flipper( side: BoardSide.right, ); await game.ready(); @@ -36,13 +39,13 @@ void main() { group('constructor', () { test('sets BoardSide', () { - final leftFlipper = Flipper.fromSide( + final leftFlipper = Flipper( side: BoardSide.left, ); expect(leftFlipper.side, equals(leftFlipper.side)); - final rightFlipper = Flipper.fromSide( + final rightFlipper = Flipper( side: BoardSide.right, ); expect(rightFlipper.side, equals(rightFlipper.side)); @@ -53,7 +56,7 @@ void main() { flameTester.test( 'is dynamic', (game) async { - final flipper = Flipper.fromSide( + final flipper = Flipper( side: BoardSide.left, ); await game.ensureAdd(flipper); @@ -65,7 +68,7 @@ void main() { flameTester.test( 'ignores gravity', (game) async { - final flipper = Flipper.fromSide( + final flipper = Flipper( side: BoardSide.left, ); await game.ensureAdd(flipper); @@ -77,10 +80,10 @@ void main() { flameTester.test( 'has greater mass than Ball', (game) async { - final flipper = Flipper.fromSide( + final flipper = Flipper( side: BoardSide.left, ); - final ball = Ball(); + final ball = Ball(baseColor: Colors.white); await game.ready(); await game.ensureAddAll([flipper, ball]); @@ -97,7 +100,7 @@ void main() { flameTester.test( 'has three', (game) async { - final flipper = Flipper.fromSide( + final flipper = Flipper( side: BoardSide.left, ); await game.ensureAdd(flipper); @@ -109,7 +112,7 @@ void main() { flameTester.test( 'has density', (game) async { - final flipper = Flipper.fromSide( + final flipper = Flipper( side: BoardSide.left, ); await game.ensureAdd(flipper); @@ -139,7 +142,7 @@ void main() { late Flipper flipper; setUp(() { - flipper = Flipper.fromSide( + flipper = Flipper( side: BoardSide.left, ); }); @@ -205,7 +208,7 @@ void main() { late Flipper flipper; setUp(() { - flipper = Flipper.fromSide( + flipper = Flipper( side: BoardSide.right, ); }); @@ -269,159 +272,4 @@ void main() { }); }, ); - - group('FlipperAnchor', () { - flameTester.test( - 'position is at the left of the left Flipper', - (game) async { - final flipper = Flipper.fromSide( - side: BoardSide.left, - ); - await game.ensureAdd(flipper); - - final flipperAnchor = FlipperAnchor(flipper: flipper); - await game.ensureAdd(flipperAnchor); - - expect(flipperAnchor.body.position.x, equals(-Flipper.size.x / 2)); - }, - ); - - flameTester.test( - 'position is at the right of the right Flipper', - (game) async { - final flipper = Flipper.fromSide( - side: BoardSide.right, - ); - await game.ensureAdd(flipper); - - final flipperAnchor = FlipperAnchor(flipper: flipper); - await game.ensureAdd(flipperAnchor); - - expect(flipperAnchor.body.position.x, equals(Flipper.size.x / 2)); - }, - ); - }); - - group('FlipperAnchorRevoluteJointDef', () { - group('initializes with', () { - flameTester.test( - 'limits enabled', - (game) async { - final flipper = Flipper.fromSide( - side: BoardSide.left, - ); - await game.ensureAdd(flipper); - - final flipperAnchor = FlipperAnchor(flipper: flipper); - await game.ensureAdd(flipperAnchor); - - final jointDef = FlipperAnchorRevoluteJointDef( - flipper: flipper, - anchor: flipperAnchor, - ); - - expect(jointDef.enableLimit, isTrue); - }, - ); - - group('equal upper and lower limits', () { - flameTester.test( - 'when Flipper is left', - (game) async { - final flipper = Flipper.fromSide( - side: BoardSide.left, - ); - await game.ensureAdd(flipper); - - final flipperAnchor = FlipperAnchor(flipper: flipper); - await game.ensureAdd(flipperAnchor); - - final jointDef = FlipperAnchorRevoluteJointDef( - flipper: flipper, - anchor: flipperAnchor, - ); - - expect(jointDef.lowerAngle, equals(jointDef.upperAngle)); - }, - ); - - flameTester.test( - 'when Flipper is right', - (game) async { - final flipper = Flipper.fromSide( - side: BoardSide.right, - ); - await game.ensureAdd(flipper); - - final flipperAnchor = FlipperAnchor(flipper: flipper); - await game.ensureAdd(flipperAnchor); - - final jointDef = FlipperAnchorRevoluteJointDef( - flipper: flipper, - anchor: flipperAnchor, - ); - - expect(jointDef.lowerAngle, equals(jointDef.upperAngle)); - }, - ); - }); - }); - - group( - 'unlocks', - () { - flameTester.test( - 'when Flipper is left', - (game) async { - final flipper = Flipper.fromSide( - side: BoardSide.left, - ); - await game.ensureAdd(flipper); - - final flipperAnchor = FlipperAnchor(flipper: flipper); - await game.ensureAdd(flipperAnchor); - - final jointDef = FlipperAnchorRevoluteJointDef( - flipper: flipper, - anchor: flipperAnchor, - ); - final joint = game.world.createJoint(jointDef) as RevoluteJoint; - - FlipperAnchorRevoluteJointDef.unlock(joint, flipper.side); - - expect( - joint.upperLimit, - isNot(equals(joint.lowerLimit)), - ); - }, - ); - - flameTester.test( - 'when Flipper is right', - (game) async { - final flipper = Flipper.fromSide( - side: BoardSide.right, - ); - await game.ensureAdd(flipper); - - final flipperAnchor = FlipperAnchor(flipper: flipper); - await game.ensureAdd(flipperAnchor); - - final jointDef = FlipperAnchorRevoluteJointDef( - flipper: flipper, - anchor: flipperAnchor, - ); - final joint = game.world.createJoint(jointDef) as RevoluteJoint; - - FlipperAnchorRevoluteJointDef.unlock(joint, flipper.side); - - expect( - joint.upperLimit, - isNot(equals(joint.lowerLimit)), - ); - }, - ); - }, - ); - }); } diff --git a/test/game/components/pathway_test.dart b/test/game/components/pathway_test.dart index 03b67c62..63e74d4d 100644 --- a/test/game/components/pathway_test.dart +++ b/test/game/components/pathway_test.dart @@ -165,6 +165,42 @@ void main() { }); }); + group('ellipse', () { + flameTester.test( + 'loads correctly', + (game) async { + final pathway = Pathway.ellipse( + center: Vector2.zero(), + width: width, + majorRadius: 150, + minorRadius: 70, + ); + await game.ready(); + await game.ensureAdd(pathway); + + expect(game.contains(pathway), isTrue); + }, + ); + + group('body', () { + flameTester.test( + 'is static', + (game) async { + final pathway = Pathway.ellipse( + center: Vector2.zero(), + width: width, + majorRadius: 150, + minorRadius: 70, + ); + await game.ready(); + await game.ensureAdd(pathway); + + expect(pathway.body.bodyType, equals(BodyType.static)); + }, + ); + }); + }); + group('bezier curve', () { final controlPoints = [ Vector2(0, 0), diff --git a/test/game/components/ramp_opening_test.dart b/test/game/components/ramp_opening_test.dart index 1876f8ae..11cf8ddc 100644 --- a/test/game/components/ramp_opening_test.dart +++ b/test/game/components/ramp_opening_test.dart @@ -4,6 +4,7 @@ import 'package:flame_test/flame_test.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mockingjay/mockingjay.dart'; import 'package:pinball/game/game.dart'; +import 'package:pinball_components/pinball_components.dart'; import '../../helpers/helpers.dart'; diff --git a/test/game/components/score_points_test.dart b/test/game/components/score_points_test.dart index 9a93c078..30ec70db 100644 --- a/test/game/components/score_points_test.dart +++ b/test/game/components/score_points_test.dart @@ -1,9 +1,11 @@ +import 'package:flame/components.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; import 'package:pinball/game/game.dart'; +import 'package:pinball_components/pinball_components.dart'; -class MockBall extends Mock implements Ball {} +import '../../helpers/helpers.dart'; class MockGameBloc extends Mock implements GameBloc {} @@ -28,12 +30,16 @@ void main() { late PinballGame game; late GameBloc bloc; late Ball ball; + late ComponentSet componentSet; + late BallController ballController; late FakeScorePoints fakeScorePoints; setUp(() { game = MockPinballGame(); bloc = MockGameBloc(); ball = MockBall(); + componentSet = MockComponentSet(); + ballController = MockBallController(); fakeScorePoints = FakeScorePoints(); }); @@ -45,7 +51,10 @@ void main() { test( 'emits Scored event with points', () { - when(() => ball.gameRef).thenReturn(game); + when(() => componentSet.whereType()) + .thenReturn([ballController]); + when(() => ball.children).thenReturn(componentSet); + when(() => ballController.gameRef).thenReturn(game); when(game.read).thenReturn(bloc); BallScorePointsCallback().begin( diff --git a/test/game/components/spaceship_test.dart b/test/game/components/spaceship_test.dart index 43b9fa1f..bad20bdf 100644 --- a/test/game/components/spaceship_test.dart +++ b/test/game/components/spaceship_test.dart @@ -2,6 +2,7 @@ import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; import 'package:pinball/game/game.dart'; +import 'package:pinball_components/pinball_components.dart'; import '../../helpers/helpers.dart'; diff --git a/test/game/components/wall_test.dart b/test/game/components/wall_test.dart index 53d387fa..18c7ea5b 100644 --- a/test/game/components/wall_test.dart +++ b/test/game/components/wall_test.dart @@ -17,11 +17,14 @@ void main() { test( 'removes the ball on begin contact when the wall is a bottom one', () { - final game = MockPinballGame(); final wall = MockBottomWall(); + final ballController = MockBallController(); final ball = MockBall(); + final componentSet = MockComponentSet(); - when(() => ball.gameRef).thenReturn(game); + when(() => componentSet.whereType()) + .thenReturn([ballController]); + when(() => ball.children).thenReturn(componentSet); BottomWallBallContactCallback() // Remove once https://github.com/flame-engine/flame/pull/1415 @@ -29,7 +32,7 @@ void main() { ..end(MockBall(), MockBottomWall(), MockContact()) ..begin(ball, wall, MockContact()); - verify(ball.lost).called(1); + verify(ballController.lost).called(1); }, ); }); diff --git a/test/game/pinball_game_test.dart b/test/game/pinball_game_test.dart index b9e0ac7d..65732608 100644 --- a/test/game/pinball_game_test.dart +++ b/test/game/pinball_game_test.dart @@ -5,6 +5,7 @@ import 'package:flame_test/flame_test.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mocktail/mocktail.dart'; import 'package:pinball/game/game.dart'; +import 'package:pinball_components/pinball_components.dart'; import '../helpers/helpers.dart'; diff --git a/test/helpers/mocks.dart b/test/helpers/mocks.dart index 35688f30..3e77cdbc 100644 --- a/test/helpers/mocks.dart +++ b/test/helpers/mocks.dart @@ -3,9 +3,11 @@ import 'package:flame/input.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; +import 'package:leaderboard_repository/leaderboard_repository.dart'; import 'package:mocktail/mocktail.dart'; import 'package:pinball/game/game.dart'; import 'package:pinball/theme/theme.dart'; +import 'package:pinball_components/pinball_components.dart'; class MockPinballGame extends Mock implements PinballGame {} @@ -17,6 +19,8 @@ class MockBody extends Mock implements Body {} class MockBall extends Mock implements Ball {} +class MockBallController extends Mock implements BallController {} + class MockContact extends Mock implements Contact {} class MockContactCallback extends Mock @@ -33,6 +37,8 @@ class MockGameState extends Mock implements GameState {} class MockThemeCubit extends Mock implements ThemeCubit {} +class MockLeaderboardRepository extends Mock implements LeaderboardRepository {} + class MockRawKeyDownEvent extends Mock implements RawKeyDownEvent { @override String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) { @@ -62,3 +68,5 @@ class MockSpaceshipEntrance extends Mock implements SpaceshipEntrance {} class MockSpaceshipHole extends Mock implements SpaceshipHole {} class MockComponent extends Mock implements Component {} + +class MockComponentSet extends Mock implements ComponentSet {} diff --git a/test/leaderboard/bloc/leaderboard_bloc_test.dart b/test/leaderboard/bloc/leaderboard_bloc_test.dart index c44f7d3a..2b217704 100644 --- a/test/leaderboard/bloc/leaderboard_bloc_test.dart +++ b/test/leaderboard/bloc/leaderboard_bloc_test.dart @@ -5,8 +5,9 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:leaderboard_repository/leaderboard_repository.dart'; import 'package:mocktail/mocktail.dart'; import 'package:pinball/leaderboard/leaderboard.dart'; +import 'package:pinball_theme/pinball_theme.dart'; -class MockLeaderboardRepository extends Mock implements LeaderboardRepository {} +import '../../helpers/helpers.dart'; void main() { group('LeaderboardBloc', () { @@ -42,7 +43,7 @@ void main() { final top10Leaderboard = top10Scores .map( - (score) => LeaderboardEntry( + (score) => LeaderboardEntryData( playerInitials: 'user$score', score: score, character: CharacterType.dash, @@ -101,7 +102,7 @@ void main() { }); group('LeaderboardEntryAdded', () { - final leaderboardEntry = LeaderboardEntry( + final leaderboardEntry = LeaderboardEntryData( playerInitials: 'ABC', score: 1500, character: CharacterType.dash, @@ -163,4 +164,40 @@ void main() { ); }); }); + + group('CharacterTypeX', () { + test('converts CharacterType.android to AndroidTheme', () { + expect(CharacterType.android.toTheme, equals(AndroidTheme())); + }); + + test('converts CharacterType.dash to DashTheme', () { + expect(CharacterType.dash.toTheme, equals(DashTheme())); + }); + + test('converts CharacterType.dino to DinoTheme', () { + expect(CharacterType.dino.toTheme, equals(DinoTheme())); + }); + + test('converts CharacterType.sparky to SparkyTheme', () { + expect(CharacterType.sparky.toTheme, equals(SparkyTheme())); + }); + }); + + group('CharacterThemeX', () { + test('converts AndroidTheme to CharacterType.android', () { + expect(AndroidTheme().toType, equals(CharacterType.android)); + }); + + test('converts DashTheme to CharacterType.dash', () { + expect(DashTheme().toType, equals(CharacterType.dash)); + }); + + test('converts DinoTheme to CharacterType.dino', () { + expect(DinoTheme().toType, equals(CharacterType.dino)); + }); + + test('converts SparkyTheme to CharacterType.sparky', () { + expect(SparkyTheme().toType, equals(CharacterType.sparky)); + }); + }); } diff --git a/test/leaderboard/bloc/leaderboard_event_test.dart b/test/leaderboard/bloc/leaderboard_event_test.dart index f74296af..33199ca1 100644 --- a/test/leaderboard/bloc/leaderboard_event_test.dart +++ b/test/leaderboard/bloc/leaderboard_event_test.dart @@ -20,7 +20,7 @@ void main() { }); group('LeaderboardEntryAdded', () { - const leaderboardEntry = LeaderboardEntry( + const leaderboardEntry = LeaderboardEntryData( playerInitials: 'ABC', score: 1500, character: CharacterType.dash, diff --git a/test/leaderboard/bloc/leaderboard_state_test.dart b/test/leaderboard/bloc/leaderboard_state_test.dart index 6ff5df13..a40a1cdb 100644 --- a/test/leaderboard/bloc/leaderboard_state_test.dart +++ b/test/leaderboard/bloc/leaderboard_state_test.dart @@ -3,6 +3,7 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:leaderboard_repository/leaderboard_repository.dart'; import 'package:pinball/leaderboard/leaderboard.dart'; +import 'package:pinball_theme/pinball_theme.dart'; void main() { group('LeaderboardState', () { @@ -25,10 +26,11 @@ void main() { }); group('copyWith', () { - const leaderboardEntry = LeaderboardEntry( + final leaderboardEntry = LeaderboardEntry( + rank: '1', playerInitials: 'ABC', score: 1500, - character: CharacterType.dash, + character: DashTheme().characterAsset, ); test( @@ -51,7 +53,7 @@ void main() { final otherLeaderboardState = LeaderboardState( status: LeaderboardStatus.success, ranking: LeaderboardRanking(ranking: 0, outOf: 0), - leaderboard: const [leaderboardEntry], + leaderboard: [leaderboardEntry], ); expect(leaderboardState, isNot(equals(otherLeaderboardState)));