mirror of https://github.com/flutter/pinball.git
parent
0595e82649
commit
9a7fa9b091
@ -1,2 +1,3 @@
|
||||
export 'bumper_noisy_behavior.dart';
|
||||
export 'camera_focusing_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));
|
||||
}
|
||||
}
|
Loading…
Reference in new issue