feat: refactor to CameraFocusingBehavior

pull/346/head
alestiago 3 years ago
parent 0595e82649
commit 9a7fa9b091

@ -1,2 +1,3 @@
export 'bumper_noisy_behavior.dart'; export 'bumper_noisy_behavior.dart';
export 'camera_focusing_behavior.dart';
export 'scoring_behavior.dart'; export 'scoring_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<FlameGame>, BlocComponent<GameBloc, GameState> {
final Map<String, FocusData> _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<void> 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);
}
}

@ -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<FlameGame> {
/// {@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));
}
}

@ -1,7 +1,6 @@
export 'android_acres/android_acres.dart'; export 'android_acres/android_acres.dart';
export 'backbox/backbox.dart'; export 'backbox/backbox.dart';
export 'bottom_group.dart'; export 'bottom_group.dart';
export 'camera_controller.dart';
export 'controlled_ball.dart'; export 'controlled_ball.dart';
export 'controlled_flipper.dart'; export 'controlled_flipper.dart';
export 'controlled_plunger.dart'; export 'controlled_plunger.dart';

@ -33,13 +33,11 @@ class GameFlowController extends ComponentController<PinballGame>
score: state?.displayScore ?? 0, score: state?.displayScore ?? 0,
characterIconPath: component.characterTheme.leaderboardIcon.keyName, characterIconPath: component.characterTheme.leaderboardIcon.keyName,
); );
component.firstChild<CameraController>()!.focusOnGameOverBackbox();
} }
/// Puts the game in the playing state. /// Puts the game in the playing state.
void start() { void start() {
component.audio.backgroundMusic(); component.audio.backgroundMusic();
component.firstChild<CameraController>()?.focusOnGame();
component.overlays.remove(PinballGame.playButtonOverlay); component.overlays.remove(PinballGame.playButtonOverlay);
} }
} }

@ -7,6 +7,7 @@ import 'package:flame/input.dart';
import 'package:flame_bloc/flame_bloc.dart'; import 'package:flame_bloc/flame_bloc.dart';
import 'package:flutter/gestures.dart'; import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:pinball/game/behaviors/behaviors.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
import 'package:pinball/l10n/l10n.dart'; import 'package:pinball/l10n/l10n.dart';
import 'package:pinball_audio/pinball_audio.dart'; import 'package:pinball_audio/pinball_audio.dart';
@ -43,11 +44,10 @@ class PinballGame extends PinballForge2DGame
late final GameFlowController gameFlowController; late final GameFlowController gameFlowController;
late final CameraFocusingBehavior cameraFocusingBehavior;
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
await add(gameFlowController = GameFlowController(this));
await add(CameraController(this));
final machine = [ final machine = [
BoardBackgroundSpriteComponent(), BoardBackgroundSpriteComponent(),
Boundaries(), Boundaries(),
@ -64,8 +64,10 @@ class PinballGame extends PinballForge2DGame
FlutterForest(), FlutterForest(),
SparkyScorch(), SparkyScorch(),
]; ];
await addAll(
await add( [
gameFlowController = GameFlowController(this),
CameraFocusingBehavior(),
ZCanvasComponent( ZCanvasComponent(
children: [ children: [
...machine, ...machine,
@ -76,6 +78,7 @@ class PinballGame extends PinballForge2DGame
Launcher(), Launcher(),
], ],
), ),
],
); );
await super.onLoad(); await super.onLoad();

Loading…
Cancel
Save