From f5879116512244e49224e251652096c366efee13 Mon Sep 17 00:00:00 2001 From: RuiAlonso Date: Mon, 9 May 2022 08:54:40 +0200 Subject: [PATCH] refactor: remove arrow blinking from this pr --- .../behaviors/ramp_progress_behavior.dart | 2 +- .../spaceship_ramp/behavior/behavior.dart | 1 - .../ramp_arrow_blinking_behavior.dart | 83 ------- .../cubit/spaceship_ramp_cubit.dart | 52 ----- .../cubit/spaceship_ramp_state.dart | 17 +- .../spaceship_ramp/spaceship_ramp.dart | 1 - .../ramp_arrow_blinking_behavior_test.dart | 219 ------------------ .../cubit/spaceship_ramp_cubit_test.dart | 103 -------- .../cubit/spaceship_ramp_state_test.dart | 9 - .../spaceship_ramp/spaceship_ramp_test.dart | 22 -- .../ramp_progress_behavior_test.dart | 12 +- 11 files changed, 9 insertions(+), 512 deletions(-) delete mode 100644 packages/pinball_components/lib/src/components/spaceship_ramp/behavior/ramp_arrow_blinking_behavior.dart delete mode 100644 packages/pinball_components/test/src/components/spaceship_ramp/behavior/ramp_arrow_blinking_behavior_test.dart diff --git a/lib/game/components/android_acres/behaviors/ramp_progress_behavior.dart b/lib/game/components/android_acres/behaviors/ramp_progress_behavior.dart index 48738b7c..c9d396c2 100644 --- a/lib/game/components/android_acres/behaviors/ramp_progress_behavior.dart +++ b/lib/game/components/android_acres/behaviors/ramp_progress_behavior.dart @@ -34,7 +34,7 @@ class RampProgressBehavior extends Component with ParentIsA { if (spaceshipCubit.state.fullArrowLit && !gameBloc.state.isMaxMultiplier) { - spaceshipCubit.onAnimate(); + spaceshipCubit.onProgressed(); } }, ), diff --git a/packages/pinball_components/lib/src/components/spaceship_ramp/behavior/behavior.dart b/packages/pinball_components/lib/src/components/spaceship_ramp/behavior/behavior.dart index 9eae19f0..1f9b6284 100644 --- a/packages/pinball_components/lib/src/components/spaceship_ramp/behavior/behavior.dart +++ b/packages/pinball_components/lib/src/components/spaceship_ramp/behavior/behavior.dart @@ -1,2 +1 @@ -export 'ramp_arrow_blinking_behavior.dart'; export 'ramp_ball_ascending_contact_behavior.dart'; diff --git a/packages/pinball_components/lib/src/components/spaceship_ramp/behavior/ramp_arrow_blinking_behavior.dart b/packages/pinball_components/lib/src/components/spaceship_ramp/behavior/ramp_arrow_blinking_behavior.dart deleted file mode 100644 index 35b3f81f..00000000 --- a/packages/pinball_components/lib/src/components/spaceship_ramp/behavior/ramp_arrow_blinking_behavior.dart +++ /dev/null @@ -1,83 +0,0 @@ -import 'package:flame/components.dart'; -import 'package:flame_bloc/flame_bloc.dart'; -import 'package:pinball_components/pinball_components.dart'; -import 'package:pinball_flame/pinball_flame.dart'; - -/// {@template ramp_arrow_blinking_behavior} -/// Makes a [SpaceshipRampArrowSpriteComponent] blink between -/// [ArrowLightState.values]. -/// {@endtemplate} -class RampArrowBlinkingBehavior extends TimerComponent - with ParentIsA { - /// {@macro ramp_arrow_blinking_behavior} - RampArrowBlinkingBehavior() : super(period: 0.05); - - final _maxBlinks = 20; - - int _blinksCounter = 0; - - bool _isAnimating = false; - - void _onNewState(SpaceshipRampState state) { - final animationEnabled = - state.animationState == ArrowAnimationState.blinking; - final canBlink = _blinksCounter < _maxBlinks; - - if (animationEnabled && canBlink) { - _start(); - } else { - _stop(); - } - } - - void _start() { - if (!_isAnimating) { - _isAnimating = true; - timer - ..reset() - ..start(); - _animate(); - } - } - - void _animate() { - readBloc().onBlink(); - _blinksCounter++; - } - - void _stop() { - if (_isAnimating) { - _isAnimating = false; - timer.stop(); - _blinksCounter = 0; - readBloc().onStop(); - } - } - - @override - Future onLoad() async { - await super.onLoad(); - await add( - FlameBlocListener( - onNewState: _onNewState, - ), - ); - } - - @override - void onTick() { - super.onTick(); - if (!_isAnimating) { - timer.stop(); - } else { - if (_blinksCounter < _maxBlinks) { - _animate(); - timer - ..reset() - ..start(); - } else { - timer.stop(); - } - } - } -} diff --git a/packages/pinball_components/lib/src/components/spaceship_ramp/cubit/spaceship_ramp_cubit.dart b/packages/pinball_components/lib/src/components/spaceship_ramp/cubit/spaceship_ramp_cubit.dart index f6d230e4..a117d2e4 100644 --- a/packages/pinball_components/lib/src/components/spaceship_ramp/cubit/spaceship_ramp_cubit.dart +++ b/packages/pinball_components/lib/src/components/spaceship_ramp/cubit/spaceship_ramp_cubit.dart @@ -19,7 +19,6 @@ class SpaceshipRampCubit extends Cubit { state.copyWith( lightState: ArrowLightState.values[(index + 1) % ArrowLightState.values.length], - animationState: ArrowAnimationState.idle, ), ); } @@ -29,58 +28,7 @@ class SpaceshipRampCubit extends Cubit { const SpaceshipRampState( hits: 0, lightState: ArrowLightState.inactive, - animationState: ArrowAnimationState.idle, ), ); } - - void onAnimate() { - emit( - state.copyWith(animationState: ArrowAnimationState.blinking), - ); - } - - void onStop() { - emit( - state.copyWith( - lightState: ArrowLightState.inactive, - animationState: ArrowAnimationState.idle, - ), - ); - } - - void onBlink() { - switch (state.lightState) { - case ArrowLightState.inactive: - emit( - state.copyWith(lightState: ArrowLightState.active1), - ); - break; - case ArrowLightState.active1: - emit( - state.copyWith(lightState: ArrowLightState.active2), - ); - break; - case ArrowLightState.active2: - emit( - state.copyWith(lightState: ArrowLightState.active3), - ); - break; - case ArrowLightState.active3: - emit( - state.copyWith(lightState: ArrowLightState.active4), - ); - break; - case ArrowLightState.active4: - emit( - state.copyWith(lightState: ArrowLightState.active5), - ); - break; - case ArrowLightState.active5: - emit( - state.copyWith(lightState: ArrowLightState.inactive), - ); - break; - } - } } diff --git a/packages/pinball_components/lib/src/components/spaceship_ramp/cubit/spaceship_ramp_state.dart b/packages/pinball_components/lib/src/components/spaceship_ramp/cubit/spaceship_ramp_state.dart index 2a8678f5..dbeb35b3 100644 --- a/packages/pinball_components/lib/src/components/spaceship_ramp/cubit/spaceship_ramp_state.dart +++ b/packages/pinball_components/lib/src/components/spaceship_ramp/cubit/spaceship_ramp_state.dart @@ -6,38 +6,31 @@ class SpaceshipRampState extends Equatable { const SpaceshipRampState({ required this.hits, required this.lightState, - required this.animationState, }) : assert(hits >= 0, "Hits can't be negative"); const SpaceshipRampState.initial() : this( hits: 0, lightState: ArrowLightState.inactive, - animationState: ArrowAnimationState.idle, ); final int hits; final ArrowLightState lightState; - final ArrowAnimationState animationState; - bool get fullArrowLit => - lightState == ArrowLightState.active5 && - animationState == ArrowAnimationState.idle; + bool get fullArrowLit => lightState == ArrowLightState.active5; SpaceshipRampState copyWith({ int? hits, ArrowLightState? lightState, - ArrowAnimationState? animationState, }) { return SpaceshipRampState( hits: hits ?? this.hits, lightState: lightState ?? this.lightState, - animationState: animationState ?? this.animationState, ); } @override - List get props => [hits, lightState, animationState]; + List get props => [hits, lightState]; } /// Indicates the state of the arrow on the [SpaceshipRamp]. @@ -60,9 +53,3 @@ enum ArrowLightState { /// Arrow with all 5 lights lit up. active5, } - -// Indicates if the blinking animation is running. -enum ArrowAnimationState { - idle, - blinking, -} diff --git a/packages/pinball_components/lib/src/components/spaceship_ramp/spaceship_ramp.dart b/packages/pinball_components/lib/src/components/spaceship_ramp/spaceship_ramp.dart index 1ce14ec1..32227472 100644 --- a/packages/pinball_components/lib/src/components/spaceship_ramp/spaceship_ramp.dart +++ b/packages/pinball_components/lib/src/components/spaceship_ramp/spaceship_ramp.dart @@ -39,7 +39,6 @@ class SpaceshipRamp extends Component { SpaceshipRampBase()..initialPosition = Vector2(3.4, -42.5), _SpaceshipRampBackgroundRailingSpriteComponent(), SpaceshipRampArrowSpriteComponent(), - RampArrowBlinkingBehavior(), ...?children, ], ); diff --git a/packages/pinball_components/test/src/components/spaceship_ramp/behavior/ramp_arrow_blinking_behavior_test.dart b/packages/pinball_components/test/src/components/spaceship_ramp/behavior/ramp_arrow_blinking_behavior_test.dart deleted file mode 100644 index cd225212..00000000 --- a/packages/pinball_components/test/src/components/spaceship_ramp/behavior/ramp_arrow_blinking_behavior_test.dart +++ /dev/null @@ -1,219 +0,0 @@ -// ignore_for_file: prefer_const_constructors, cascade_invocations - -import 'dart:async'; - -import 'package:bloc_test/bloc_test.dart'; -import 'package:flame_bloc/flame_bloc.dart'; -import 'package:flame_forge2d/flame_forge2d.dart'; -import 'package:flame_test/flame_test.dart'; -import 'package:flutter_test/flutter_test.dart'; -import 'package:mocktail/mocktail.dart'; -import 'package:pinball_components/pinball_components.dart'; -import 'package:pinball_components/src/components/spaceship_ramp/behavior/ramp_arrow_blinking_behavior.dart'; -import 'package:pinball_flame/pinball_flame.dart'; - -class _TestGame extends Forge2DGame { - @override - Future onLoad() async { - images.prefix = ''; - await images.loadAll([ - Assets.images.multiball.lit.keyName, - Assets.images.android.ramp.railingForeground.keyName, - Assets.images.android.ramp.railingBackground.keyName, - Assets.images.android.ramp.main.keyName, - Assets.images.android.ramp.arrow.inactive.keyName, - Assets.images.android.ramp.arrow.active1.keyName, - Assets.images.android.ramp.arrow.active2.keyName, - Assets.images.android.ramp.arrow.active3.keyName, - Assets.images.android.ramp.arrow.active4.keyName, - Assets.images.android.ramp.arrow.active5.keyName, - ]); - } - - Future pump( - SpaceshipRamp child, { - required SpaceshipRampCubit spaceshipRampCubit, - }) async { - await ensureAdd( - FlameBlocProvider.value( - value: spaceshipRampCubit, - children: [ - ZCanvasComponent(children: [child]), - ], - ), - ); - } -} - -class _MockSpaceshipRampCubit extends Mock implements SpaceshipRampCubit {} - -void main() { - TestWidgetsFlutterBinding.ensureInitialized(); - - final flameTester = FlameTester(_TestGame.new); - - group( - 'RampArrowBlinkingBehavior', - () { - flameTester.testGameWidget( - 'calls onBlink every 0.05 seconds when animation state is animated', - setUp: (game, tester) async { - final behavior = RampArrowBlinkingBehavior(); - final bloc = _MockSpaceshipRampCubit(); - final streamController = StreamController(); - whenListen( - bloc, - streamController.stream, - initialState: SpaceshipRampState.initial(), - ); - - final spaceshipRamp = SpaceshipRamp.test(); - await game.pump( - spaceshipRamp, - spaceshipRampCubit: bloc, - ); - await spaceshipRamp.add(behavior); - - streamController.add( - SpaceshipRampState( - hits: 1, - animationState: ArrowAnimationState.blinking, - lightState: ArrowLightState.active1, - ), - ); - await tester.pump(); - game.update(0); - - verify(bloc.onBlink).called(1); - - await tester.pump(); - game.update(0.05); - - await streamController.close(); - verify(bloc.onBlink).called(1); - }, - ); - - flameTester.testGameWidget( - 'calls onStop when animation state is stopped', - setUp: (game, tester) async { - final behavior = RampArrowBlinkingBehavior(); - final bloc = _MockSpaceshipRampCubit(); - final streamController = StreamController(); - whenListen( - bloc, - streamController.stream, - initialState: SpaceshipRampState.initial(), - ); - when(bloc.onBlink).thenAnswer((_) async {}); - - final spaceshipRamp = SpaceshipRamp.test(); - await game.pump( - spaceshipRamp, - spaceshipRampCubit: bloc, - ); - await spaceshipRamp.add(behavior); - - streamController.add( - SpaceshipRampState( - hits: 1, - animationState: ArrowAnimationState.blinking, - lightState: ArrowLightState.active1, - ), - ); - await tester.pump(); - - streamController.add( - SpaceshipRampState( - hits: 1, - animationState: ArrowAnimationState.idle, - lightState: ArrowLightState.active1, - ), - ); - - await streamController.close(); - - await game.ready(); - - verify(bloc.onStop).called(1); - }, - ); - - flameTester.testGameWidget( - 'onTick stops when there is no animation', - setUp: (game, tester) async { - final behavior = RampArrowBlinkingBehavior(); - final bloc = _MockSpaceshipRampCubit(); - final streamController = StreamController(); - whenListen( - bloc, - streamController.stream, - initialState: SpaceshipRampState.initial(), - ); - when(bloc.onBlink).thenAnswer((_) async {}); - - final spaceshipRamp = SpaceshipRamp.test(); - await game.pump( - spaceshipRamp, - spaceshipRampCubit: bloc, - ); - await spaceshipRamp.add(behavior); - - streamController.add( - SpaceshipRampState( - hits: 1, - animationState: ArrowAnimationState.idle, - lightState: ArrowLightState.active1, - ), - ); - await tester.pump(); - - behavior.onTick(); - - await game.ready(); - - expect(behavior.timer.isRunning(), false); - }, - ); - - flameTester.testGameWidget( - 'onTick stops after 10 blinks repetitions', - setUp: (game, tester) async { - final behavior = RampArrowBlinkingBehavior(); - final bloc = _MockSpaceshipRampCubit(); - final streamController = StreamController(); - whenListen( - bloc, - streamController.stream, - initialState: SpaceshipRampState.initial(), - ); - when(bloc.onBlink).thenAnswer((_) async {}); - - final spaceshipRamp = SpaceshipRamp.test(); - await game.pump( - spaceshipRamp, - spaceshipRampCubit: bloc, - ); - await spaceshipRamp.add(behavior); - - streamController.add( - SpaceshipRampState( - hits: 1, - animationState: ArrowAnimationState.blinking, - lightState: ArrowLightState.inactive, - ), - ); - await tester.pump(); - - for (var i = 0; i < 10; i++) { - behavior.onTick(); - } - - await game.ready(); - - expect(behavior.timer.isRunning(), false); - }, - ); - }, - ); -} diff --git a/packages/pinball_components/test/src/components/spaceship_ramp/cubit/spaceship_ramp_cubit_test.dart b/packages/pinball_components/test/src/components/spaceship_ramp/cubit/spaceship_ramp_cubit_test.dart index 32826cd6..89d03af0 100644 --- a/packages/pinball_components/test/src/components/spaceship_ramp/cubit/spaceship_ramp_cubit_test.dart +++ b/packages/pinball_components/test/src/components/spaceship_ramp/cubit/spaceship_ramp_cubit_test.dart @@ -29,7 +29,6 @@ void main() { seed: () => SpaceshipRampState( hits: 100, lightState: ArrowLightState.active3, - animationState: ArrowAnimationState.blinking, ), act: (bloc) => bloc.onReset(), expect: () => [ @@ -39,111 +38,9 @@ void main() { (state) => state.lightState, 'lightState', ArrowLightState.inactive, - ) - ..having( - (state) => state.animationState, - 'animationState', - ArrowAnimationState.idle, ), ], ); }); - - group('onAnimate', () { - blocTest( - 'emits animationState blinking', - build: SpaceshipRampCubit.new, - seed: () => SpaceshipRampState( - hits: 100, - lightState: ArrowLightState.active3, - animationState: ArrowAnimationState.idle, - ), - act: (bloc) => bloc.onAnimate(), - expect: () => [ - isA().having( - (state) => state.animationState, - 'animationState', - ArrowAnimationState.blinking, - ), - ], - ); - }); - - group('onStop', () { - blocTest( - 'emits animationState idle and lightState inactive', - build: SpaceshipRampCubit.new, - seed: () => SpaceshipRampState( - hits: 100, - lightState: ArrowLightState.active3, - animationState: ArrowAnimationState.blinking, - ), - act: (bloc) => bloc.onStop(), - expect: () => [ - isA() - ..having( - (state) => state.lightState, - 'lightState', - ArrowLightState.inactive, - ) - ..having( - (state) => state.animationState, - 'animationState', - ArrowAnimationState.idle, - ), - ], - ); - }); - - group('onBlink', () { - blocTest( - 'emits next lit state at lightState', - build: SpaceshipRampCubit.new, - seed: () => SpaceshipRampState( - hits: 100, - lightState: ArrowLightState.inactive, - animationState: ArrowAnimationState.blinking, - ), - act: (bloc) => bloc - ..onBlink() - ..onBlink() - ..onBlink() - ..onBlink() - ..onBlink() - ..onBlink(), - expect: () => [ - isA().having( - (state) => state.lightState, - 'lightState', - ArrowLightState.active1, - ), - isA().having( - (state) => state.lightState, - 'lightState', - ArrowLightState.active2, - ), - isA().having( - (state) => state.lightState, - 'lightState', - ArrowLightState.active3, - ), - isA().having( - (state) => state.lightState, - 'lightState', - ArrowLightState.active4, - ), - isA().having( - (state) => state.lightState, - 'lightState', - ArrowLightState.active5, - ), - isA().having( - (state) => state.lightState, - 'lightState', - ArrowLightState.inactive, - ), - ], - ); - }); }); } diff --git a/packages/pinball_components/test/src/components/spaceship_ramp/cubit/spaceship_ramp_state_test.dart b/packages/pinball_components/test/src/components/spaceship_ramp/cubit/spaceship_ramp_state_test.dart index 3ad7d067..3cc982c3 100644 --- a/packages/pinball_components/test/src/components/spaceship_ramp/cubit/spaceship_ramp_state_test.dart +++ b/packages/pinball_components/test/src/components/spaceship_ramp/cubit/spaceship_ramp_state_test.dart @@ -10,13 +10,11 @@ void main() { SpaceshipRampState( hits: 0, lightState: ArrowLightState.inactive, - animationState: ArrowAnimationState.idle, ), equals( SpaceshipRampState( hits: 0, lightState: ArrowLightState.inactive, - animationState: ArrowAnimationState.idle, ), ), ); @@ -28,7 +26,6 @@ void main() { SpaceshipRampState( hits: 0, lightState: ArrowLightState.inactive, - animationState: ArrowAnimationState.idle, ), isNotNull, ); @@ -43,7 +40,6 @@ void main() { () => SpaceshipRampState( hits: -1, lightState: ArrowLightState.inactive, - animationState: ArrowAnimationState.idle, ), throwsAssertionError, ); @@ -58,7 +54,6 @@ void main() { const rampState = SpaceshipRampState( hits: 0, lightState: ArrowLightState.inactive, - animationState: ArrowAnimationState.idle, ); expect( () => rampState.copyWith(hits: rampState.hits - 1), @@ -74,7 +69,6 @@ void main() { const rampState = SpaceshipRampState( hits: 0, lightState: ArrowLightState.inactive, - animationState: ArrowAnimationState.idle, ); expect( rampState.copyWith(), @@ -90,12 +84,10 @@ void main() { const rampState = SpaceshipRampState( hits: 0, lightState: ArrowLightState.inactive, - animationState: ArrowAnimationState.idle, ); final otherRampState = SpaceshipRampState( hits: rampState.hits + 1, lightState: ArrowLightState.active1, - animationState: ArrowAnimationState.blinking, ); expect(rampState, isNot(equals(otherRampState))); @@ -103,7 +95,6 @@ void main() { rampState.copyWith( hits: otherRampState.hits, lightState: otherRampState.lightState, - animationState: otherRampState.animationState, ), equals(otherRampState), ); diff --git a/packages/pinball_components/test/src/components/spaceship_ramp/spaceship_ramp_test.dart b/packages/pinball_components/test/src/components/spaceship_ramp/spaceship_ramp_test.dart index 779e243f..015dd5aa 100644 --- a/packages/pinball_components/test/src/components/spaceship_ramp/spaceship_ramp_test.dart +++ b/packages/pinball_components/test/src/components/spaceship_ramp/spaceship_ramp_test.dart @@ -138,28 +138,6 @@ void main() { ); }, ); - - flameTester.test( - 'a RampArrowBlinkingBehavior', - (game) async { - final bloc = _MockSpaceshipRampCubit(); - final streamController = StreamController(); - whenListen( - bloc, - streamController.stream, - initialState: SpaceshipRampState.initial(), - ); - final ramp = SpaceshipRamp(); - await game.pump( - ramp, - spaceshipRampCubit: bloc, - ); - expect( - game.descendants().whereType().length, - equals(1), - ); - }, - ); }); group('renders correctly', () { diff --git a/test/game/components/android_acres/behaviors/ramp_progress_behavior_test.dart b/test/game/components/android_acres/behaviors/ramp_progress_behavior_test.dart index bdd5414b..e728d8ee 100644 --- a/test/game/components/android_acres/behaviors/ramp_progress_behavior_test.dart +++ b/test/game/components/android_acres/behaviors/ramp_progress_behavior_test.dart @@ -188,7 +188,7 @@ void main() { ); flameTester.test( - 'adds onAnimate ' + 'adds again onProgressed to dimmed all ' 'when arrow is full lit after hit and multiplier is less than 6', (game) async { final bloc = _MockSpaceshipRampCubit(); @@ -224,12 +224,12 @@ void main() { await game.ready(); - verify(bloc.onAnimate).called(1); + verify(bloc.onProgressed).called(2); }, ); flameTester.test( - "doesn't add onAnimate " + "doesn't add again onProgressed to dimmed all " 'when arrow is not full lit after hit', (game) async { final bloc = _MockSpaceshipRampCubit(); @@ -265,12 +265,12 @@ void main() { await game.ready(); - verifyNever(bloc.onAnimate); + verify(bloc.onProgressed).called(1); }, ); flameTester.test( - "doesn't add onAnimate " + "doesn't add again onProgressed to dimmed all " 'when multiplier is 6 after hit', (game) async { final bloc = _MockSpaceshipRampCubit(); @@ -303,7 +303,7 @@ void main() { await game.ready(); - verifyNever(bloc.onAnimate); + verify(bloc.onProgressed).called(1); }, ); });