From ad77839d3ad682873161d3b9b2340b057c953094 Mon Sep 17 00:00:00 2001 From: Alejandro Santiago Date: Tue, 10 May 2022 01:33:17 +0100 Subject: [PATCH] revert: changed `CameraFocusingBehavior` logic (#451) * revert: changed CameraFocusingBehavior logic * refactor: removed unnecessary type Co-authored-by: Erick * fix: coverage Co-authored-by: Erick Co-authored-by: Tom Arra --- .../behaviors/camera_focusing_behavior.dart | 41 ++++++++----------- .../camera_focusing_behavior_test.dart | 27 ++++++------ 2 files changed, 30 insertions(+), 38 deletions(-) diff --git a/lib/game/behaviors/camera_focusing_behavior.dart b/lib/game/behaviors/camera_focusing_behavior.dart index 1d454f02..ec3e918f 100644 --- a/lib/game/behaviors/camera_focusing_behavior.dart +++ b/lib/game/behaviors/camera_focusing_behavior.dart @@ -1,5 +1,3 @@ -import 'dart:math'; - import 'package:flame/components.dart'; import 'package:flame/game.dart'; import 'package:flame_bloc/flame_bloc.dart'; @@ -43,30 +41,25 @@ class CameraFocusingBehavior extends Component @override void onGameResize(Vector2 size) { super.onGameResize(size); - if (size == _previousSize) { - return; - } + if (size == _previousSize) return; _previousSize.setFrom(size); - final maxWidth = size.x / 90; - final maxHeight = size.y / 160; - - final scale = min(maxHeight, maxWidth); - - _foci.addAll({ - GameStatus.waiting: _FocusData( - zoom: scale + (maxWidth > maxHeight ? 0.3 : -0.5), - position: Vector2(0, -112), - ), - GameStatus.playing: _FocusData( - zoom: scale + (maxWidth > maxHeight ? 0.5 : -0.2), - position: Vector2(0, -7.8), - ), - GameStatus.gameOver: _FocusData( - zoom: scale + (maxWidth > maxHeight ? 2.8 : 3.3), - position: Vector2(0, -111), - ), - }); + _foci.addAll( + { + GameStatus.waiting: _FocusData( + zoom: size.y / 175, + position: _foci[GameStatus.waiting]?.position ?? Vector2(0, -112), + ), + GameStatus.playing: _FocusData( + zoom: size.y / 165, + position: _foci[GameStatus.playing]?.position ?? Vector2(0, -7.8), + ), + GameStatus.gameOver: _FocusData( + zoom: size.y / 100, + position: _foci[GameStatus.gameOver]?.position ?? Vector2(0, -111), + ), + }, + ); if (_activeFocus != null) { _snap(_activeFocus!); diff --git a/test/game/behaviors/camera_focusing_behavior_test.dart b/test/game/behaviors/camera_focusing_behavior_test.dart index 092f3efe..fb0d91c5 100644 --- a/test/game/behaviors/camera_focusing_behavior_test.dart +++ b/test/game/behaviors/camera_focusing_behavior_test.dart @@ -34,6 +34,19 @@ void main() { expect(game.descendants(), contains(behavior)); }); + flameTester.test('resizes and snaps', (game) async { + final behavior = CameraFocusingBehavior(); + await game.ensureAdd( + FlameBlocProvider.value( + value: GameBloc(), + children: [behavior], + ), + ); + + behavior.onGameResize(Vector2.all(10)); + expect(game.camera.zoom, greaterThan(0)); + }); + flameTester.test( 'changes focus when loaded', (game) async { @@ -53,20 +66,6 @@ void main() { }, ); - flameTester.test('sets zoom on resize', (game) async { - final behavior = CameraFocusingBehavior(); - - await game.ensureAdd( - FlameBlocProvider.value( - value: GameBloc(), - children: [behavior], - ), - ); - - game.onGameResize(game.canvasSize * 2); - expect(game.camera.zoom, equals(6.55)); - }); - flameTester.test( 'listenWhen only listens when status changes', (game) async {