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 d40ffb1e..a9172416 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 @@ -9,15 +9,8 @@ class SpaceshipRampCubit extends Cubit { SpaceshipRampCubit() : super(const SpaceshipRampState.initial()); void onBallInside() { - final index = - SpaceshipRampArrowSpriteState.values.indexOf(state.arrowState); - emit( - state.copyWith( - hits: state.hits + 1, - arrowState: SpaceshipRampArrowSpriteState - .values[(index + 1) % SpaceshipRampArrowSpriteState.values.length], - ), + state.copyWith(hits: state.hits + 1), ); } } 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 3006f176..7fae894f 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 @@ -2,51 +2,23 @@ part of 'spaceship_ramp_cubit.dart'; -enum SpaceshipRampArrowSpriteState { - /// Arrow with no dashes lit up. - inactive, - - /// Arrow with 1 light lit up. - active1, - - /// Arrow with 2 lights lit up. - active2, - - /// Arrow with 3 lights lit up. - active3, - - /// Arrow with 4 lights lit up. - active4, - - /// Arrow with all 5 lights lit up. - active5, -} - class SpaceshipRampState extends Equatable { const SpaceshipRampState({ required this.hits, - required this.arrowState, }) : assert(hits >= 0, "Hits can't be negative"); - const SpaceshipRampState.initial() - : this( - hits: 0, - arrowState: SpaceshipRampArrowSpriteState.inactive, - ); + const SpaceshipRampState.initial() : this(hits: 0); final int hits; - final SpaceshipRampArrowSpriteState arrowState; SpaceshipRampState copyWith({ int? hits, - SpaceshipRampArrowSpriteState? arrowState, }) { return SpaceshipRampState( hits: hits ?? this.hits, - arrowState: arrowState ?? this.arrowState, ); } @override - List get props => [hits, arrowState]; + List get props => [hits]; } 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 6e6f6c2a..9f50eff5 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 @@ -54,7 +54,7 @@ class SpaceshipRamp extends Component { _SpaceshipRampBase()..initialPosition = Vector2(1.7, -20), _SpaceshipRampBackgroundRailingSpriteComponent(), _SpaceshipRampArrowSpriteComponent( - current: bloc.state.arrowState, + current: bloc.state.hits, ), ...?children, ], @@ -80,25 +80,6 @@ class SpaceshipRamp extends Component { } } -extension on SpaceshipRampArrowSpriteState { - String get path { - switch (this) { - case SpaceshipRampArrowSpriteState.inactive: - return Assets.images.android.ramp.arrow.inactive.keyName; - case SpaceshipRampArrowSpriteState.active1: - return Assets.images.android.ramp.arrow.active1.keyName; - case SpaceshipRampArrowSpriteState.active2: - return Assets.images.android.ramp.arrow.active2.keyName; - case SpaceshipRampArrowSpriteState.active3: - return Assets.images.android.ramp.arrow.active3.keyName; - case SpaceshipRampArrowSpriteState.active4: - return Assets.images.android.ramp.arrow.active4.keyName; - case SpaceshipRampArrowSpriteState.active5: - return Assets.images.android.ramp.arrow.active5.keyName; - } - } -} - class _SpaceshipRampBackground extends BodyComponent with InitialPosition, Layered, ZIndex { _SpaceshipRampBackground() @@ -202,12 +183,11 @@ class _SpaceshipRampBackgroundRampSpriteComponent extends SpriteComponent /// /// Lights progressively whenever a [Ball] gets into [SpaceshipRamp]. /// {@endtemplate} -class _SpaceshipRampArrowSpriteComponent - extends SpriteGroupComponent +class _SpaceshipRampArrowSpriteComponent extends SpriteGroupComponent with HasGameRef, ParentIsA, ZIndex { /// {@macro spaceship_ramp_arrow_sprite_component} _SpaceshipRampArrowSpriteComponent({ - required SpaceshipRampArrowSpriteState current, + required int current, }) : super( anchor: Anchor.center, position: Vector2(-3.9, -56.5), @@ -219,21 +199,64 @@ class _SpaceshipRampArrowSpriteComponent @override Future onLoad() async { await super.onLoad(); - parent.bloc.stream.listen((state) => current = state.arrowState); + parent.bloc.stream.listen((state) { + current = state.hits % SpaceshipRampArrowSpriteState.values.length; + }); - final sprites = {}; + final sprites = {}; this.sprites = sprites; for (final spriteState in SpaceshipRampArrowSpriteState.values) { - sprites[spriteState] = Sprite( + sprites[spriteState.index] = Sprite( gameRef.images.fromCache(spriteState.path), ); } - current = SpaceshipRampArrowSpriteState.inactive; + current = 0; size = sprites[current]!.originalSize / 10; } } +/// Indicates the state of the arrow on the [SpaceshipRamp]. +@visibleForTesting +enum SpaceshipRampArrowSpriteState { + /// Arrow with no dashes lit up. + inactive, + + /// Arrow with 1 light lit up. + active1, + + /// Arrow with 2 lights lit up. + active2, + + /// Arrow with 3 lights lit up. + active3, + + /// Arrow with 4 lights lit up. + active4, + + /// Arrow with all 5 lights lit up. + active5, +} + +extension on SpaceshipRampArrowSpriteState { + String get path { + switch (this) { + case SpaceshipRampArrowSpriteState.inactive: + return Assets.images.android.ramp.arrow.inactive.keyName; + case SpaceshipRampArrowSpriteState.active1: + return Assets.images.android.ramp.arrow.active1.keyName; + case SpaceshipRampArrowSpriteState.active2: + return Assets.images.android.ramp.arrow.active2.keyName; + case SpaceshipRampArrowSpriteState.active3: + return Assets.images.android.ramp.arrow.active3.keyName; + case SpaceshipRampArrowSpriteState.active4: + return Assets.images.android.ramp.arrow.active4.keyName; + case SpaceshipRampArrowSpriteState.active5: + return Assets.images.android.ramp.arrow.active5.keyName; + } + } +} + class _SpaceshipRampBoardOpeningSpriteComponent extends SpriteComponent with HasGameRef, ZIndex { _SpaceshipRampBoardOpeningSpriteComponent() : super(anchor: Anchor.center) { 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 85030d70..e0c9dd61 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 @@ -15,83 +15,11 @@ void main() { ..onBallInside() ..onBallInside(), expect: () => [ - SpaceshipRampState( - hits: 1, - arrowState: SpaceshipRampArrowSpriteState.active1, - ), - SpaceshipRampState( - hits: 2, - arrowState: SpaceshipRampArrowSpriteState.active2, - ), - SpaceshipRampState( - hits: 3, - arrowState: SpaceshipRampArrowSpriteState.active3, - ), + SpaceshipRampState(hits: 1), + SpaceshipRampState(hits: 2), + SpaceshipRampState(hits: 3), ], ); - - group('arrowState', () { - blocTest( - 'goes to the next value while less than max', - build: SpaceshipRampCubit.new, - act: (bloc) => bloc - ..onBallInside() - ..onBallInside() - ..onBallInside() - ..onBallInside() - ..onBallInside(), - expect: () => [ - isA() - ..having( - (state) => state.arrowState, - 'arrowState', - SpaceshipRampArrowSpriteState.active1, - ), - isA() - ..having( - (state) => state.arrowState, - 'arrowState', - SpaceshipRampArrowSpriteState.active2, - ), - isA() - ..having( - (state) => state.arrowState, - 'arrowState', - SpaceshipRampArrowSpriteState.active3, - ), - isA() - ..having( - (state) => state.arrowState, - 'arrowState', - SpaceshipRampArrowSpriteState.active4, - ), - isA() - ..having( - (state) => state.arrowState, - 'arrowState', - SpaceshipRampArrowSpriteState.active5, - ), - ], - ); - - blocTest( - 'goes to the initial value when is max', - build: SpaceshipRampCubit.new, - seed: () => SpaceshipRampState( - hits: 5, - arrowState: SpaceshipRampArrowSpriteState.active5, - ), - act: (bloc) => bloc..onBallInside(), - expect: () => [ - isA() - ..having( - (state) => state.arrowState, - 'arrowState', - SpaceshipRampArrowSpriteState.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 c168842e..536f4e8e 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 @@ -7,15 +7,9 @@ void main() { group('SpaceshipRampState', () { test('supports value equality', () { expect( - SpaceshipRampState( - hits: 0, - arrowState: SpaceshipRampArrowSpriteState.inactive, - ), + SpaceshipRampState(hits: 0), equals( - SpaceshipRampState( - hits: 0, - arrowState: SpaceshipRampArrowSpriteState.inactive, - ), + SpaceshipRampState(hits: 0), ), ); }); @@ -23,10 +17,7 @@ void main() { group('constructor', () { test('can be instantiated', () { expect( - SpaceshipRampState( - hits: 0, - arrowState: SpaceshipRampArrowSpriteState.inactive, - ), + SpaceshipRampState(hits: 0), isNotNull, ); }); @@ -37,10 +28,7 @@ void main() { 'when hits is negative', () { expect( - () => SpaceshipRampState( - hits: -1, - arrowState: SpaceshipRampArrowSpriteState.inactive, - ), + () => SpaceshipRampState(hits: -1), throwsAssertionError, ); }, @@ -51,10 +39,7 @@ void main() { 'throws AssertionError ' 'when hits is decreased', () { - const rampState = SpaceshipRampState( - hits: 0, - arrowState: SpaceshipRampArrowSpriteState.inactive, - ); + const rampState = SpaceshipRampState(hits: 0); expect( () => rampState.copyWith(hits: rampState.hits - 1), throwsAssertionError, @@ -66,10 +51,7 @@ void main() { 'copies correctly ' 'when no argument specified', () { - const rampState = SpaceshipRampState( - hits: 0, - arrowState: SpaceshipRampArrowSpriteState.inactive, - ); + const rampState = SpaceshipRampState(hits: 0); expect( rampState.copyWith(), equals(rampState), @@ -81,21 +63,12 @@ void main() { 'copies correctly ' 'when all arguments specified', () { - const rampState = SpaceshipRampState( - hits: 0, - arrowState: SpaceshipRampArrowSpriteState.inactive, - ); - final otherRampState = SpaceshipRampState( - hits: rampState.hits + 1, - arrowState: SpaceshipRampArrowSpriteState.active1, - ); + const rampState = SpaceshipRampState(hits: 0); + final otherRampState = SpaceshipRampState(hits: rampState.hits + 1); expect(rampState, isNot(equals(otherRampState))); expect( - rampState.copyWith( - hits: rampState.hits + 1, - arrowState: SpaceshipRampArrowSpriteState.active1, - ), + rampState.copyWith(hits: rampState.hits + 1), equals(otherRampState), ); }, diff --git a/test/game/components/android_acres/behaviors/ramp_bonus_behavior_test.dart b/test/game/components/android_acres/behaviors/ramp_bonus_behavior_test.dart index cbbdbb0f..3eab404c 100644 --- a/test/game/components/android_acres/behaviors/ramp_bonus_behavior_test.dart +++ b/test/game/components/android_acres/behaviors/ramp_bonus_behavior_test.dart @@ -106,7 +106,9 @@ void main() { await game.ensureAdd(parent); await parent.ensureAdd(behavior); - streamController.add(SpaceshipRampState(hits: 1)); + streamController.add( + SpaceshipRampState(hits: 1), + ); final scores = game.descendants().whereType(); await game.ready();