From 78aab42694c66c8b9ea749ac11187f1ed1789a4c Mon Sep 17 00:00:00 2001 From: Erick Date: Mon, 28 Feb 2022 09:43:23 -0300 Subject: [PATCH] feat: adding FlameBloc mixin to allow its usage with enhanced FlameGame classes (#1399) * feat: adding FlameBloc mixin to allow its usage with enhanced FlameGame classes * fixing tests * Apply suggestions from code review Co-authored-by: Lukas Klingsbo Co-authored-by: Pasha Stetsenko Co-authored-by: Luan Nico Co-authored-by: Lukas Klingsbo Co-authored-by: Pasha Stetsenko Co-authored-by: Luan Nico --- packages/flame_bloc/README.md | 5 ++++ .../flame_bloc/example/lib/src/game/game.dart | 5 ++-- .../flame_bloc/lib/src/flame_bloc_game.dart | 23 +++++++++++-------- .../flame_bloc/test/flame_bloc_game_test.dart | 2 +- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/packages/flame_bloc/README.md b/packages/flame_bloc/README.md index caf0559d022..36be7290226 100644 --- a/packages/flame_bloc/README.md +++ b/packages/flame_bloc/README.md @@ -15,6 +15,11 @@ BlocProvider( ) ``` +To enable the features of `flame_bloc` in your game, you can make your game class inherit from +`FlameBlocGame`, or if you are already using an enhanced `FlameGame` class (like for example a +`Forge2DGame`), the `FlameBloc` mixin can be used instead. + + To access the bloc from inside your game, the `read` method can be used. ```dart diff --git a/packages/flame_bloc/example/lib/src/game/game.dart b/packages/flame_bloc/example/lib/src/game/game.dart index a52ec709019..0a17439327d 100644 --- a/packages/flame_bloc/example/lib/src/game/game.dart +++ b/packages/flame_bloc/example/lib/src/game/game.dart @@ -1,4 +1,5 @@ import 'package:flame/components.dart'; +import 'package:flame/game.dart'; import 'package:flame/input.dart'; import 'package:flame_bloc/flame_bloc.dart'; @@ -23,8 +24,8 @@ class GameStatsController extends Component } } -class SpaceShooterGame extends FlameBlocGame - with PanDetector, HasCollidables, HasKeyboardHandlerComponents { +class SpaceShooterGame extends FlameGame + with FlameBloc, PanDetector, HasCollidables, HasKeyboardHandlerComponents { late PlayerComponent player; @override diff --git a/packages/flame_bloc/lib/src/flame_bloc_game.dart b/packages/flame_bloc/lib/src/flame_bloc_game.dart index d0bd7f8b320..a06a67ec34a 100644 --- a/packages/flame_bloc/lib/src/flame_bloc_game.dart +++ b/packages/flame_bloc/lib/src/flame_bloc_game.dart @@ -20,7 +20,7 @@ mixin BlocComponent, S> on Component { /// Makes this component subscribe to the Bloc changes. /// Visible only for test purposes. @visibleForTesting - void subscribe(FlameBlocGame game) { + void subscribe(FlameBloc game) { final _bloc = game.read(); _state = _bloc.state; @@ -60,10 +60,10 @@ mixin BlocComponent, S> on Component { void onMount() { super.onMount(); assert( - findGame()! is FlameBlocGame, - 'BlocComponent can only be added to a FlameBlocGame', + findGame()! is FlameBloc, + 'BlocComponent can only be added to a FlameBloc game', ); - final game = findGame()! as FlameBlocGame; + final game = findGame()! as FlameBloc; if (game.isAttached) { subscribe(game); } else { @@ -79,13 +79,9 @@ mixin BlocComponent, S> on Component { } } -/// An enhanced [FlameGame] that has the capability to listen +/// A mixin that enhances a [FlameGame] enabling features to receive /// and emit changes to a [Bloc] state. -class FlameBlocGame extends FlameGame { - /// FlameBlocGame constructor with an optional [Camera] as a parameter to - /// FlameGame. - FlameBlocGame({Camera? camera}) : super(camera: camera); - +mixin FlameBloc on FlameGame { /// Contains a list of all of the [BlocComponent]s with an active /// subscription. Only visible for testing. @visibleForTesting @@ -132,3 +128,10 @@ class FlameBlocGame extends FlameGame { }); } } + +/// Provides a default, concrete implementation of a [FlameBloc] game. +class FlameBlocGame extends FlameGame with FlameBloc { + /// FlameBlocGame constructor with an optional [Camera] as a parameter to + /// FlameGame. + FlameBlocGame({Camera? camera}) : super(camera: camera); +} diff --git a/packages/flame_bloc/test/flame_bloc_game_test.dart b/packages/flame_bloc/test/flame_bloc_game_test.dart index 358b44d6667..656284a7695 100644 --- a/packages/flame_bloc/test/flame_bloc_game_test.dart +++ b/packages/flame_bloc/test/flame_bloc_game_test.dart @@ -31,7 +31,7 @@ class MockInventoryComponent extends Component int numSubscribeCalls = 0; @override - void subscribe(FlameBlocGame game) { + void subscribe(FlameBloc game) { numSubscribeCalls++; } }