diff --git a/lib/game/components/camera_controller.dart b/lib/game/components/camera_controller.dart index e2086cb5..aa963e9a 100644 --- a/lib/game/components/camera_controller.dart +++ b/lib/game/components/camera_controller.dart @@ -1,7 +1,6 @@ -import 'dart:async'; - import 'package:flame/components.dart'; import 'package:flame/game.dart'; +import 'package:pinball/flame/flame.dart'; import 'package:pinball_components/pinball_components.dart'; /// Adds helpers methods to Flame's [Camera] @@ -39,20 +38,14 @@ class FocusData { final Vector2 position; } +/// {@template camera_controller} /// A [Component] that controls its game camera focus -class CameraController extends Component with HasGameRef, KeyboardHandler { - /// Holds the data for the game focus point - late final FocusData gameFocus; - - /// Holds the data for the backboard focus point - late final FocusData backboardFocus; - - @override - Future onLoad() async { - await super.onLoad(); - - final gameZoom = gameRef.size.y / 16; - final backboardZoom = gameRef.size.y / 18; +/// {@endtemplate} +class CameraController extends ComponentController { + /// {@macro camera_controller} + CameraController(FlameGame component) : super(component) { + final gameZoom = component.size.y / 16; + final backboardZoom = component.size.y / 18; gameFocus = FocusData( zoom: gameZoom, @@ -64,18 +57,24 @@ class CameraController extends Component with HasGameRef, KeyboardHandler { ); // Game starts with the camera focused on the panel - gameRef.camera + component.camera ..speed = 100 ..snapToFocus(backboardFocus); } + /// Holds the data for the game focus point + late final FocusData gameFocus; + + /// Holds the data for the backboard focus point + late final FocusData backboardFocus; + /// Move the camera focus to the game board void focusOnGame() { - gameRef.add(gameRef.camera.focusToCameraZoom(gameFocus)); + component.add(component.camera.focusToCameraZoom(gameFocus)); } /// Move the camera focus to the backboard void focusOnBackboard() { - gameRef.add(gameRef.camera.focusToCameraZoom(backboardFocus)); + component.add(component.camera.focusToCameraZoom(backboardFocus)); } } diff --git a/lib/game/components/game_flow_controller.dart b/lib/game/components/game_flow_controller.dart index 5a2563ba..b0f6f514 100644 --- a/lib/game/components/game_flow_controller.dart +++ b/lib/game/components/game_flow_controller.dart @@ -1,11 +1,17 @@ import 'package:flame/components.dart'; import 'package:flame_bloc/flame_bloc.dart'; +import 'package:pinball/flame/flame.dart'; import 'package:pinball/game/game.dart'; import 'package:pinball_components/pinball_components.dart'; +/// {@template game_flow_controller} /// A [Component] that controls the game over and game restart logic -class GameFlowController extends Component - with BlocComponent, HasGameRef { +/// {@endtemplate} +class GameFlowController extends ComponentController + with BlocComponent { + /// {@macro game_flow_controller} + GameFlowController(PinballGame component) : super(component); + @override bool listenWhen(GameState? previousState, GameState newState) { return previousState?.isGameOver != newState.isGameOver; @@ -22,14 +28,14 @@ class GameFlowController extends Component /// Puts the game on a game over state void gameOver() { - gameRef.firstChild()?.gameOverMode(); - gameRef.firstChild()?.focusOnBackboard(); + component.firstChild()?.gameOverMode(); + component.firstChild()?.focusOnBackboard(); } /// Puts the game on a playing state void start() { - gameRef.firstChild()?.waitingMode(); - gameRef.firstChild()?.focusOnGame(); - gameRef.overlays.remove(PinballGame.playButtonOverlay); + component.firstChild()?.waitingMode(); + component.firstChild()?.focusOnGame(); + component.overlays.remove(PinballGame.playButtonOverlay); } } diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index 82f70939..4ccad9db 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -38,8 +38,8 @@ class PinballGame extends Forge2DGame Future onLoad() async { _addContactCallbacks(); - unawaited(add(gameFlowController = GameFlowController())); - unawaited(add(CameraController())); + unawaited(add(gameFlowController = GameFlowController(this))); + unawaited(add(CameraController(this))); unawaited(add(Backboard(position: Vector2(0, -88)))); await _addGameBoundaries(); diff --git a/test/game/components/camera_controller_test.dart b/test/game/components/camera_controller_test.dart index 16d92379..6af3f594 100644 --- a/test/game/components/camera_controller_test.dart +++ b/test/game/components/camera_controller_test.dart @@ -14,7 +14,7 @@ void main() { setUp(() async { game = FlameGame()..onGameResize(Vector2(100, 200)); - controller = CameraController(); + controller = CameraController(game); await game.ensureAdd(controller); }); diff --git a/test/game/components/game_flow_controller_test.dart b/test/game/components/game_flow_controller_test.dart index 1473d1f4..dc1d9ab8 100644 --- a/test/game/components/game_flow_controller_test.dart +++ b/test/game/components/game_flow_controller_test.dart @@ -8,17 +8,6 @@ import 'package:pinball_components/pinball_components.dart'; import '../../helpers/helpers.dart'; -// TODO(erickzanardo): This will not be needed anymore when -// this issue is merged: https://github.com/flame-engine/flame/issues/1513 -class WrappedGameFlowController extends GameFlowController { - WrappedGameFlowController(this._gameRef); - - final PinballGame _gameRef; - - @override - PinballGame get gameRef => _gameRef; -} - void main() { group('GameFlowController', () { group('listenWhen', () { @@ -33,7 +22,7 @@ void main() { final previous = GameState.initial(); expect( - GameFlowController().listenWhen(previous, state), + GameFlowController(MockPinballGame()).listenWhen(previous, state), isTrue, ); }); @@ -50,7 +39,7 @@ void main() { game = MockPinballGame(); backboard = MockBackboard(); cameraController = MockCameraController(); - gameFlowController = WrappedGameFlowController(game); + gameFlowController = GameFlowController(game); overlays = MockActiveOverlaysNotifier(); when(backboard.gameOverMode).thenAnswer((_) async {});