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/components.dart';
import 'package:flame/game.dart'; import 'package:flame/game.dart';
import 'package:pinball/flame/flame.dart';
import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_components/pinball_components.dart';
/// Adds helpers methods to Flame's [Camera] /// Adds helpers methods to Flame's [Camera]
@ -39,20 +38,14 @@ class FocusData {
final Vector2 position; final Vector2 position;
} }
/// {@template camera_controller}
/// A [Component] that controls its game camera focus /// A [Component] that controls its game camera focus
class CameraController extends Component with HasGameRef, KeyboardHandler { /// {@endtemplate}
/// Holds the data for the game focus point class CameraController extends ComponentController<FlameGame> {
late final FocusData gameFocus; /// {@macro camera_controller}
CameraController(FlameGame component) : super(component) {
/// Holds the data for the backboard focus point final gameZoom = component.size.y / 16;
late final FocusData backboardFocus; final backboardZoom = component.size.y / 18;
@override
Future<void> onLoad() async {
await super.onLoad();
final gameZoom = gameRef.size.y / 16;
final backboardZoom = gameRef.size.y / 18;
gameFocus = FocusData( gameFocus = FocusData(
zoom: gameZoom, zoom: gameZoom,
@ -64,18 +57,24 @@ class CameraController extends Component with HasGameRef, KeyboardHandler {
); );
// Game starts with the camera focused on the panel // Game starts with the camera focused on the panel
gameRef.camera component.camera
..speed = 100 ..speed = 100
..snapToFocus(backboardFocus); ..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 /// Move the camera focus to the game board
void focusOnGame() { void focusOnGame() {
gameRef.add(gameRef.camera.focusToCameraZoom(gameFocus)); component.add(component.camera.focusToCameraZoom(gameFocus));
} }
/// Move the camera focus to the backboard /// Move the camera focus to the backboard
void focusOnBackboard() { 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/components.dart';
import 'package:flame_bloc/flame_bloc.dart'; import 'package:flame_bloc/flame_bloc.dart';
import 'package:pinball/flame/flame.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_components/pinball_components.dart';
/// {@template game_flow_controller}
/// A [Component] that controls the game over and game restart logic /// A [Component] that controls the game over and game restart logic
class GameFlowController extends Component /// {@endtemplate}
with BlocComponent<GameBloc, GameState>, HasGameRef { class GameFlowController extends ComponentController<PinballGame>
with BlocComponent<GameBloc, GameState> {
/// {@macro game_flow_controller}
GameFlowController(PinballGame component) : super(component);
@override @override
bool listenWhen(GameState? previousState, GameState newState) { bool listenWhen(GameState? previousState, GameState newState) {
return previousState?.isGameOver != newState.isGameOver; return previousState?.isGameOver != newState.isGameOver;
@ -22,14 +28,14 @@ class GameFlowController extends Component
/// Puts the game on a game over state /// Puts the game on a game over state
void gameOver() { void gameOver() {
gameRef.firstChild<Backboard>()?.gameOverMode(); component.firstChild<Backboard>()?.gameOverMode();
gameRef.firstChild<CameraController>()?.focusOnBackboard(); component.firstChild<CameraController>()?.focusOnBackboard();
} }
/// Puts the game on a playing state /// Puts the game on a playing state
void start() { void start() {
gameRef.firstChild<Backboard>()?.waitingMode(); component.firstChild<Backboard>()?.waitingMode();
gameRef.firstChild<CameraController>()?.focusOnGame(); component.firstChild<CameraController>()?.focusOnGame();
gameRef.overlays.remove(PinballGame.playButtonOverlay); component.overlays.remove(PinballGame.playButtonOverlay);
} }
} }

@ -38,8 +38,8 @@ class PinballGame extends Forge2DGame
Future<void> onLoad() async { Future<void> onLoad() async {
_addContactCallbacks(); _addContactCallbacks();
unawaited(add(gameFlowController = GameFlowController())); unawaited(add(gameFlowController = GameFlowController(this)));
unawaited(add(CameraController())); unawaited(add(CameraController(this)));
unawaited(add(Backboard(position: Vector2(0, -88)))); unawaited(add(Backboard(position: Vector2(0, -88))));
await _addGameBoundaries(); await _addGameBoundaries();

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

@ -8,17 +8,6 @@ import 'package:pinball_components/pinball_components.dart';
import '../../helpers/helpers.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() { void main() {
group('GameFlowController', () { group('GameFlowController', () {
group('listenWhen', () { group('listenWhen', () {
@ -33,7 +22,7 @@ void main() {
final previous = GameState.initial(); final previous = GameState.initial();
expect( expect(
GameFlowController().listenWhen(previous, state), GameFlowController(MockPinballGame()).listenWhen(previous, state),
isTrue, isTrue,
); );
}); });
@ -50,7 +39,7 @@ void main() {
game = MockPinballGame(); game = MockPinballGame();
backboard = MockBackboard(); backboard = MockBackboard();
cameraController = MockCameraController(); cameraController = MockCameraController();
gameFlowController = WrappedGameFlowController(game); gameFlowController = GameFlowController(game);
overlays = MockActiveOverlaysNotifier(); overlays = MockActiveOverlaysNotifier();
when(backboard.gameOverMode).thenAnswer((_) async {}); when(backboard.gameOverMode).thenAnswer((_) async {});

Loading…
Cancel
Save