From fd730e37d709ddf73283d64b9dba670df4b2c85a Mon Sep 17 00:00:00 2001 From: alestiago Date: Tue, 29 Mar 2022 12:07:37 +0100 Subject: [PATCH] feat: implemented BallType --- lib/game/components/ball.dart | 48 ++++++++++++++++++++----- lib/game/components/flutter_forest.dart | 1 + lib/game/pinball_game.dart | 14 ++++++-- 3 files changed, 52 insertions(+), 11 deletions(-) diff --git a/lib/game/components/ball.dart b/lib/game/components/ball.dart index 756a8a97..4e829c71 100644 --- a/lib/game/components/ball.dart +++ b/lib/game/components/ball.dart @@ -1,33 +1,61 @@ 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_components/pinball_components.dart'; +/// {@template ball_type} +/// Specifies the type of [Ball]. +/// +/// Different [BallType]s have different game mechanics. +/// {@endtemplate} +enum BallType { + /// A [Ball] spawned from the [Plunger]. + /// + /// [normal] balls reduce the [GameState.balls] + normal, + + /// A [Ball] was spawned by Dash in the [FlutterForest]. + egg, +} + /// {@template ball_blueprint} -/// [Blueprint] which cretes a ball game object +/// [Blueprint] which cretes a ball game object. /// {@endtemplate} class BallBlueprint extends Blueprint { /// {@macro ball_blueprint} - BallBlueprint({required this.position}); + BallBlueprint({ + required this.position, + required this.type, + }); - /// The initial position of the [Ball] + /// The initial position of the [Ball]. final Vector2 position; + /// {@macro ball_type} + final BallType type; + @override void build(PinballGame gameRef) { final baseColor = gameRef.theme.characterTheme.ballColor; - final ball = Ball(baseColor: baseColor)..add(BallController()); + final ball = Ball(baseColor: baseColor) + ..add( + BallController(type: type), + ); add(ball..initialPosition = position + Vector2(0, ball.size.y / 2)); } } -/// {@template ball} -/// A solid, [BodyType.dynamic] sphere that rolls and bounces along the -/// [PinballGame]. +/// {@template ball_controller} +/// Controller attached to a [Ball] that handles its game related logic. /// {@endtemplate} class BallController extends Component with HasGameRef { + /// {@macro ball_controller} + BallController({required this.type}); + + /// {@macro ball_type} + final BallType type; + /// Removes the [Ball] from a [PinballGame]; spawning a new [Ball] if /// any are left. /// @@ -35,9 +63,11 @@ class BallController extends Component with HasGameRef { /// a [BottomWall]. void lost() { parent?.shouldRemove = true; + // TODO(alestiago): Consider adding test for this logic once we remove the + // BallX extension. + if (type != BallType.normal) return; final bloc = gameRef.read()..add(const BallLost()); - final shouldBallRespwan = !bloc.state.isLastBall && !bloc.state.isGameOver; if (shouldBallRespwan) { gameRef.spawnBall(); diff --git a/lib/game/components/flutter_forest.dart b/lib/game/components/flutter_forest.dart index 31ea8c2b..28f895bf 100644 --- a/lib/game/components/flutter_forest.dart +++ b/lib/game/components/flutter_forest.dart @@ -35,6 +35,7 @@ class FlutterForest extends Component gameRef.addFromBlueprint( BallBlueprint( position: Vector2(17.2, 52.7), + type: BallType.egg, ), ); } diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index 2a8ddd42..e9a9728f 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -86,7 +86,12 @@ class PinballGame extends Forge2DGame } void spawnBall() { - addFromBlueprint(BallBlueprint(position: plunger.body.position)); + addFromBlueprint( + BallBlueprint( + position: plunger.body.position, + type: BallType.normal, + ), + ); } } @@ -95,6 +100,11 @@ class DebugPinballGame extends PinballGame with TapDetector { @override void onTapUp(TapUpInfo info) { - addFromBlueprint(BallBlueprint(position: info.eventPosition.game)); + addFromBlueprint( + BallBlueprint( + position: info.eventPosition.game, + type: BallType.egg, + ), + ); } }