Skip to content

Commit

Permalink
feat: adding FlameBloc mixin to allow its usage with enhanced FlameGa…
Browse files Browse the repository at this point in the history
…me 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 <me@lukas.fyi>
Co-authored-by: Pasha Stetsenko <stpasha@google.com>
Co-authored-by: Luan Nico <luanpotter27@gmail.com>

Co-authored-by: Lukas Klingsbo <me@lukas.fyi>
Co-authored-by: Pasha Stetsenko <stpasha@google.com>
Co-authored-by: Luan Nico <luanpotter27@gmail.com>
  • Loading branch information
4 people authored Feb 28, 2022
1 parent 2935a6a commit 78aab42
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
5 changes: 5 additions & 0 deletions packages/flame_bloc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ BlocProvider<ExampleGame>(
)
```

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
Expand Down
5 changes: 3 additions & 2 deletions packages/flame_bloc/example/lib/src/game/game.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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
Expand Down
23 changes: 13 additions & 10 deletions packages/flame_bloc/lib/src/flame_bloc_game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mixin BlocComponent<B extends BlocBase<S>, 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<B>();
_state = _bloc.state;

Expand Down Expand Up @@ -60,10 +60,10 @@ mixin BlocComponent<B extends BlocBase<S>, 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 {
Expand All @@ -79,13 +79,9 @@ mixin BlocComponent<B extends BlocBase<S>, 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
Expand Down Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion packages/flame_bloc/test/flame_bloc_game_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class MockInventoryComponent extends Component
int numSubscribeCalls = 0;

@override
void subscribe(FlameBlocGame game) {
void subscribe(FlameBloc game) {
numSubscribeCalls++;
}
}
Expand Down

0 comments on commit 78aab42

Please sign in to comment.