feat: pr suggestions

pull/160/head
Erick Zanardo 4 years ago
parent 338b26a50f
commit b47808b594

@ -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<void> onLoad() async {
await super.onLoad();
final gameZoom = gameRef.size.y / 16;
final backboardZoom = gameRef.size.y / 18;
/// {@endtemplate}
class CameraController extends ComponentController<FlameGame> {
/// {@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));
}
}

@ -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<GameBloc, GameState>, HasGameRef {
/// {@endtemplate}
class GameFlowController extends ComponentController<PinballGame>
with BlocComponent<GameBloc, GameState> {
/// {@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<Backboard>()?.gameOverMode();
gameRef.firstChild<CameraController>()?.focusOnBackboard();
component.firstChild<Backboard>()?.gameOverMode();
component.firstChild<CameraController>()?.focusOnBackboard();
}
/// Puts the game on a playing state
void start() {
gameRef.firstChild<Backboard>()?.waitingMode();
gameRef.firstChild<CameraController>()?.focusOnGame();
gameRef.overlays.remove(PinballGame.playButtonOverlay);
component.firstChild<Backboard>()?.waitingMode();
component.firstChild<CameraController>()?.focusOnGame();
component.overlays.remove(PinballGame.playButtonOverlay);
}
}

@ -38,8 +38,8 @@ class PinballGame extends Forge2DGame
Future<void> 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();

@ -14,7 +14,7 @@ void main() {
setUp(() async {
game = FlameGame()..onGameResize(Vector2(100, 200));
controller = CameraController();
controller = CameraController(game);
await game.ensureAdd(controller);
});

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

Loading…
Cancel
Save