From 9a7fa9b091406d572c8ef80ae6dd098a83bc4b3e Mon Sep 17 00:00:00 2001 From: alestiago Date: Thu, 5 May 2022 12:49:58 +0100 Subject: [PATCH] feat: refactor to CameraFocusingBehavior --- lib/game/behaviors/behaviors.dart | 1 + .../behaviors/camera_focusing_behavior.dart | 84 +++++++++++++++++ lib/game/components/camera_controller.dart | 93 ------------------- lib/game/components/components.dart | 1 - lib/game/components/game_flow_controller.dart | 2 - lib/game/pinball_game.dart | 33 ++++--- 6 files changed, 103 insertions(+), 111 deletions(-) create mode 100644 lib/game/behaviors/camera_focusing_behavior.dart delete mode 100644 lib/game/components/camera_controller.dart diff --git a/lib/game/behaviors/behaviors.dart b/lib/game/behaviors/behaviors.dart index ae51fc09..f87b4f10 100644 --- a/lib/game/behaviors/behaviors.dart +++ b/lib/game/behaviors/behaviors.dart @@ -1,2 +1,3 @@ export 'bumper_noisy_behavior.dart'; +export 'camera_focusing_behavior.dart'; export 'scoring_behavior.dart'; diff --git a/lib/game/behaviors/camera_focusing_behavior.dart b/lib/game/behaviors/camera_focusing_behavior.dart new file mode 100644 index 00000000..5f1148df --- /dev/null +++ b/lib/game/behaviors/camera_focusing_behavior.dart @@ -0,0 +1,84 @@ +import 'package:flame/components.dart'; +import 'package:flame/game.dart'; +import 'package:flame_bloc/flame_bloc.dart'; +import 'package:pinball/game/game.dart'; +import 'package:pinball_components/pinball_components.dart'; +import 'package:pinball_flame/pinball_flame.dart'; + +/// {@template focus_data} +/// Model class that defines a focus point of the camera. +/// {@endtemplate} +class FocusData { + /// {@template focus_data} + FocusData({ + required this.zoom, + required this.position, + }); + + /// The amount of zoom. + final double zoom; + + /// The position of the camera. + final Vector2 position; +} + +/// +class CameraFocusingBehavior extends Component + with ParentIsA, BlocComponent { + final Map _focuses = {}; + + // @override + // bool listenWhen(GameState? previousState, GameState newState) { + // print('listen'); + // return true; + // return previousState?.isGameOver != newState.isGameOver; + // } + + @override + void onNewState(GameState state) { + print(state); + if (state.isGameOver) { + _zoom(_focuses['backbox']!); + } else { + _zoom(_focuses['game']!); + } + } + + @override + void onGameResize(Vector2 size) { + super.onGameResize(size); + _focuses['game'] = FocusData( + zoom: parent.size.y / 16, + position: Vector2(0, -7.8), + ); + _focuses['waiting'] = FocusData( + zoom: parent.size.y / 18, + position: Vector2(0, -112), + ); + _focuses['backbox'] = FocusData( + zoom: parent.size.y / 10, + position: Vector2(0, -111), + ); + } + + @override + Future onLoad() async { + await super.onLoad(); + _snap(_focuses['waiting']!); + } + + void _snap(FocusData data) { + parent.camera + ..speed = 100 + ..followVector2(data.position) + ..zoom = data.zoom; + } + + void _zoom(FocusData data) { + final zoom = CameraZoom(value: data.zoom); + zoom.completed.then((_) { + parent.camera.moveTo(data.position); + }); + parent.add(zoom); + } +} diff --git a/lib/game/components/camera_controller.dart b/lib/game/components/camera_controller.dart deleted file mode 100644 index 083e5745..00000000 --- a/lib/game/components/camera_controller.dart +++ /dev/null @@ -1,93 +0,0 @@ -import 'package:flame/components.dart'; -import 'package:flame/game.dart'; -import 'package:pinball_components/pinball_components.dart'; -import 'package:pinball_flame/pinball_flame.dart'; - -/// Adds helpers methods to Flame's [Camera]. -extension CameraX on Camera { - /// Instantly apply the point of focus to the [Camera]. - void snapToFocus(FocusData data) { - followVector2(data.position); - zoom = data.zoom; - } - - /// Returns a [CameraZoom] that can be added to a [FlameGame]. - CameraZoom focusToCameraZoom(FocusData data) { - final zoom = CameraZoom(value: data.zoom); - zoom.completed.then((_) { - moveTo(data.position); - }); - return zoom; - } -} - -/// {@template focus_data} -/// Model class that defines a focus point of the camera. -/// {@endtemplate} -class FocusData { - /// {@template focus_data} - FocusData({ - required this.zoom, - required this.position, - }); - - /// The amount of zoom. - final double zoom; - - /// The position of the camera. - final Vector2 position; -} - -/// {@template camera_controller} -/// A [Component] that controls its game camera focus. -/// {@endtemplate} -class CameraController extends ComponentController { - /// {@macro camera_controller} - CameraController(FlameGame component) : super(component) { - final gameZoom = component.size.y / 16; - final waitingBackboxZoom = component.size.y / 18; - final gameOverBackboxZoom = component.size.y / 10; - - gameFocus = FocusData( - zoom: gameZoom, - position: Vector2(0, -7.8), - ); - waitingBackboxFocus = FocusData( - zoom: waitingBackboxZoom, - position: Vector2(0, -112), - ); - gameOverBackboxFocus = FocusData( - zoom: gameOverBackboxZoom, - position: Vector2(0, -111), - ); - - // Game starts with the camera focused on the [Backbox]. - component.camera - ..speed = 100 - ..snapToFocus(waitingBackboxFocus); - } - - /// Holds the data for the game focus point. - late final FocusData gameFocus; - - /// Holds the data for the waiting backbox focus point. - late final FocusData waitingBackboxFocus; - - /// Holds the data for the game over backbox focus point. - late final FocusData gameOverBackboxFocus; - - /// Move the camera focus to the game board. - void focusOnGame() { - component.add(component.camera.focusToCameraZoom(gameFocus)); - } - - /// Move the camera focus to the waiting backbox. - void focusOnWaitingBackbox() { - component.add(component.camera.focusToCameraZoom(waitingBackboxFocus)); - } - - /// Move the camera focus to the game over backbox. - void focusOnGameOverBackbox() { - component.add(component.camera.focusToCameraZoom(gameOverBackboxFocus)); - } -} diff --git a/lib/game/components/components.dart b/lib/game/components/components.dart index 2b132656..fa37fd58 100644 --- a/lib/game/components/components.dart +++ b/lib/game/components/components.dart @@ -1,7 +1,6 @@ export 'android_acres/android_acres.dart'; export 'backbox/backbox.dart'; export 'bottom_group.dart'; -export 'camera_controller.dart'; export 'controlled_ball.dart'; export 'controlled_flipper.dart'; export 'controlled_plunger.dart'; diff --git a/lib/game/components/game_flow_controller.dart b/lib/game/components/game_flow_controller.dart index 1299e6eb..1d3c9e6d 100644 --- a/lib/game/components/game_flow_controller.dart +++ b/lib/game/components/game_flow_controller.dart @@ -33,13 +33,11 @@ class GameFlowController extends ComponentController score: state?.displayScore ?? 0, characterIconPath: component.characterTheme.leaderboardIcon.keyName, ); - component.firstChild()!.focusOnGameOverBackbox(); } /// Puts the game in the playing state. void start() { component.audio.backgroundMusic(); - component.firstChild()?.focusOnGame(); component.overlays.remove(PinballGame.playButtonOverlay); } } diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index aa963a53..ac07a82b 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -7,6 +7,7 @@ import 'package:flame/input.dart'; import 'package:flame_bloc/flame_bloc.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; +import 'package:pinball/game/behaviors/behaviors.dart'; import 'package:pinball/game/game.dart'; import 'package:pinball/l10n/l10n.dart'; import 'package:pinball_audio/pinball_audio.dart'; @@ -43,11 +44,10 @@ class PinballGame extends PinballForge2DGame late final GameFlowController gameFlowController; + late final CameraFocusingBehavior cameraFocusingBehavior; + @override Future onLoad() async { - await add(gameFlowController = GameFlowController(this)); - await add(CameraController(this)); - final machine = [ BoardBackgroundSpriteComponent(), Boundaries(), @@ -64,18 +64,21 @@ class PinballGame extends PinballForge2DGame FlutterForest(), SparkyScorch(), ]; - - await add( - ZCanvasComponent( - children: [ - ...machine, - ...decals, - ...characterAreas, - Drain(), - BottomGroup(), - Launcher(), - ], - ), + await addAll( + [ + gameFlowController = GameFlowController(this), + CameraFocusingBehavior(), + ZCanvasComponent( + children: [ + ...machine, + ...decals, + ...characterAreas, + Drain(), + BottomGroup(), + Launcher(), + ], + ), + ], ); await super.onLoad();