revert: changed `CameraFocusingBehavior` logic (#451)

* revert: changed CameraFocusingBehavior logic

* refactor: removed unnecessary type

Co-authored-by: Erick <erickzanardoo@gmail.com>

* fix: coverage

Co-authored-by: Erick <erickzanardoo@gmail.com>
Co-authored-by: Tom Arra <tarra3@gmail.com>
pull/453/head
Alejandro Santiago 3 years ago committed by GitHub
parent 1c8813e039
commit ad77839d3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,5 +1,3 @@
import 'dart:math';
import 'package:flame/components.dart'; import 'package:flame/components.dart';
import 'package:flame/game.dart'; import 'package:flame/game.dart';
import 'package:flame_bloc/flame_bloc.dart'; import 'package:flame_bloc/flame_bloc.dart';
@ -43,30 +41,25 @@ class CameraFocusingBehavior extends Component
@override @override
void onGameResize(Vector2 size) { void onGameResize(Vector2 size) {
super.onGameResize(size); super.onGameResize(size);
if (size == _previousSize) { if (size == _previousSize) return;
return;
}
_previousSize.setFrom(size); _previousSize.setFrom(size);
final maxWidth = size.x / 90; _foci.addAll(
final maxHeight = size.y / 160; {
final scale = min(maxHeight, maxWidth);
_foci.addAll({
GameStatus.waiting: _FocusData( GameStatus.waiting: _FocusData(
zoom: scale + (maxWidth > maxHeight ? 0.3 : -0.5), zoom: size.y / 175,
position: Vector2(0, -112), position: _foci[GameStatus.waiting]?.position ?? Vector2(0, -112),
), ),
GameStatus.playing: _FocusData( GameStatus.playing: _FocusData(
zoom: scale + (maxWidth > maxHeight ? 0.5 : -0.2), zoom: size.y / 165,
position: Vector2(0, -7.8), position: _foci[GameStatus.playing]?.position ?? Vector2(0, -7.8),
), ),
GameStatus.gameOver: _FocusData( GameStatus.gameOver: _FocusData(
zoom: scale + (maxWidth > maxHeight ? 2.8 : 3.3), zoom: size.y / 100,
position: Vector2(0, -111), position: _foci[GameStatus.gameOver]?.position ?? Vector2(0, -111),
), ),
}); },
);
if (_activeFocus != null) { if (_activeFocus != null) {
_snap(_activeFocus!); _snap(_activeFocus!);

@ -34,13 +34,8 @@ void main() {
expect(game.descendants(), contains(behavior)); expect(game.descendants(), contains(behavior));
}); });
flameTester.test( flameTester.test('resizes and snaps', (game) async {
'changes focus when loaded',
(game) async {
final behavior = CameraFocusingBehavior(); final behavior = CameraFocusingBehavior();
final previousZoom = game.camera.zoom;
expect(game.camera.follow, isNull);
await game.ensureAdd( await game.ensureAdd(
FlameBlocProvider<GameBloc, GameState>.value( FlameBlocProvider<GameBloc, GameState>.value(
value: GameBloc(), value: GameBloc(),
@ -48,13 +43,16 @@ void main() {
), ),
); );
expect(game.camera.follow, isNotNull); behavior.onGameResize(Vector2.all(10));
expect(game.camera.zoom, isNot(equals(previousZoom))); expect(game.camera.zoom, greaterThan(0));
}, });
);
flameTester.test('sets zoom on resize', (game) async { flameTester.test(
'changes focus when loaded',
(game) async {
final behavior = CameraFocusingBehavior(); final behavior = CameraFocusingBehavior();
final previousZoom = game.camera.zoom;
expect(game.camera.follow, isNull);
await game.ensureAdd( await game.ensureAdd(
FlameBlocProvider<GameBloc, GameState>.value( FlameBlocProvider<GameBloc, GameState>.value(
@ -63,9 +61,10 @@ void main() {
), ),
); );
game.onGameResize(game.canvasSize * 2); expect(game.camera.follow, isNotNull);
expect(game.camera.zoom, equals(6.55)); expect(game.camera.zoom, isNot(equals(previousZoom)));
}); },
);
flameTester.test( flameTester.test(
'listenWhen only listens when status changes', 'listenWhen only listens when status changes',

Loading…
Cancel
Save