refactor: removed arrow value from spaceship ramp state to sprite logic

pull/296/head
RuiAlonso 3 years ago
parent 0536d654a9
commit 2d5e6911f5

@ -9,15 +9,8 @@ class SpaceshipRampCubit extends Cubit<SpaceshipRampState> {
SpaceshipRampCubit() : super(const SpaceshipRampState.initial()); SpaceshipRampCubit() : super(const SpaceshipRampState.initial());
void onBallInside() { void onBallInside() {
final index =
SpaceshipRampArrowSpriteState.values.indexOf(state.arrowState);
emit( emit(
state.copyWith( state.copyWith(hits: state.hits + 1),
hits: state.hits + 1,
arrowState: SpaceshipRampArrowSpriteState
.values[(index + 1) % SpaceshipRampArrowSpriteState.values.length],
),
); );
} }
} }

@ -2,51 +2,23 @@
part of 'spaceship_ramp_cubit.dart'; 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 { class SpaceshipRampState extends Equatable {
const SpaceshipRampState({ const SpaceshipRampState({
required this.hits, required this.hits,
required this.arrowState,
}) : assert(hits >= 0, "Hits can't be negative"); }) : assert(hits >= 0, "Hits can't be negative");
const SpaceshipRampState.initial() const SpaceshipRampState.initial() : this(hits: 0);
: this(
hits: 0,
arrowState: SpaceshipRampArrowSpriteState.inactive,
);
final int hits; final int hits;
final SpaceshipRampArrowSpriteState arrowState;
SpaceshipRampState copyWith({ SpaceshipRampState copyWith({
int? hits, int? hits,
SpaceshipRampArrowSpriteState? arrowState,
}) { }) {
return SpaceshipRampState( return SpaceshipRampState(
hits: hits ?? this.hits, hits: hits ?? this.hits,
arrowState: arrowState ?? this.arrowState,
); );
} }
@override @override
List<Object?> get props => [hits, arrowState]; List<Object?> get props => [hits];
} }

@ -54,7 +54,7 @@ class SpaceshipRamp extends Component {
_SpaceshipRampBase()..initialPosition = Vector2(1.7, -20), _SpaceshipRampBase()..initialPosition = Vector2(1.7, -20),
_SpaceshipRampBackgroundRailingSpriteComponent(), _SpaceshipRampBackgroundRailingSpriteComponent(),
_SpaceshipRampArrowSpriteComponent( _SpaceshipRampArrowSpriteComponent(
current: bloc.state.arrowState, current: bloc.state.hits,
), ),
...?children, ...?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 class _SpaceshipRampBackground extends BodyComponent
with InitialPosition, Layered, ZIndex { with InitialPosition, Layered, ZIndex {
_SpaceshipRampBackground() _SpaceshipRampBackground()
@ -202,12 +183,11 @@ class _SpaceshipRampBackgroundRampSpriteComponent extends SpriteComponent
/// ///
/// Lights progressively whenever a [Ball] gets into [SpaceshipRamp]. /// Lights progressively whenever a [Ball] gets into [SpaceshipRamp].
/// {@endtemplate} /// {@endtemplate}
class _SpaceshipRampArrowSpriteComponent class _SpaceshipRampArrowSpriteComponent extends SpriteGroupComponent<int>
extends SpriteGroupComponent<SpaceshipRampArrowSpriteState>
with HasGameRef, ParentIsA<SpaceshipRamp>, ZIndex { with HasGameRef, ParentIsA<SpaceshipRamp>, ZIndex {
/// {@macro spaceship_ramp_arrow_sprite_component} /// {@macro spaceship_ramp_arrow_sprite_component}
_SpaceshipRampArrowSpriteComponent({ _SpaceshipRampArrowSpriteComponent({
required SpaceshipRampArrowSpriteState current, required int current,
}) : super( }) : super(
anchor: Anchor.center, anchor: Anchor.center,
position: Vector2(-3.9, -56.5), position: Vector2(-3.9, -56.5),
@ -219,21 +199,64 @@ class _SpaceshipRampArrowSpriteComponent
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
await super.onLoad(); await super.onLoad();
parent.bloc.stream.listen((state) => current = state.arrowState); parent.bloc.stream.listen((state) {
current = state.hits % SpaceshipRampArrowSpriteState.values.length;
});
final sprites = <SpaceshipRampArrowSpriteState, Sprite>{}; final sprites = <int, Sprite>{};
this.sprites = sprites; this.sprites = sprites;
for (final spriteState in SpaceshipRampArrowSpriteState.values) { for (final spriteState in SpaceshipRampArrowSpriteState.values) {
sprites[spriteState] = Sprite( sprites[spriteState.index] = Sprite(
gameRef.images.fromCache(spriteState.path), gameRef.images.fromCache(spriteState.path),
); );
} }
current = SpaceshipRampArrowSpriteState.inactive; current = 0;
size = sprites[current]!.originalSize / 10; 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 class _SpaceshipRampBoardOpeningSpriteComponent extends SpriteComponent
with HasGameRef, ZIndex { with HasGameRef, ZIndex {
_SpaceshipRampBoardOpeningSpriteComponent() : super(anchor: Anchor.center) { _SpaceshipRampBoardOpeningSpriteComponent() : super(anchor: Anchor.center) {

@ -15,83 +15,11 @@ void main() {
..onBallInside() ..onBallInside()
..onBallInside(), ..onBallInside(),
expect: () => [ expect: () => [
SpaceshipRampState( SpaceshipRampState(hits: 1),
hits: 1, SpaceshipRampState(hits: 2),
arrowState: SpaceshipRampArrowSpriteState.active1, SpaceshipRampState(hits: 3),
),
SpaceshipRampState(
hits: 2,
arrowState: SpaceshipRampArrowSpriteState.active2,
),
SpaceshipRampState(
hits: 3,
arrowState: SpaceshipRampArrowSpriteState.active3,
),
], ],
); );
group('arrowState', () {
blocTest<SpaceshipRampCubit, SpaceshipRampState>(
'goes to the next value while less than max',
build: SpaceshipRampCubit.new,
act: (bloc) => bloc
..onBallInside()
..onBallInside()
..onBallInside()
..onBallInside()
..onBallInside(),
expect: () => [
isA<SpaceshipRampState>()
..having(
(state) => state.arrowState,
'arrowState',
SpaceshipRampArrowSpriteState.active1,
),
isA<SpaceshipRampState>()
..having(
(state) => state.arrowState,
'arrowState',
SpaceshipRampArrowSpriteState.active2,
),
isA<SpaceshipRampState>()
..having(
(state) => state.arrowState,
'arrowState',
SpaceshipRampArrowSpriteState.active3,
),
isA<SpaceshipRampState>()
..having(
(state) => state.arrowState,
'arrowState',
SpaceshipRampArrowSpriteState.active4,
),
isA<SpaceshipRampState>()
..having(
(state) => state.arrowState,
'arrowState',
SpaceshipRampArrowSpriteState.active5,
),
],
);
blocTest<SpaceshipRampCubit, SpaceshipRampState>(
'goes to the initial value when is max',
build: SpaceshipRampCubit.new,
seed: () => SpaceshipRampState(
hits: 5,
arrowState: SpaceshipRampArrowSpriteState.active5,
),
act: (bloc) => bloc..onBallInside(),
expect: () => [
isA<SpaceshipRampState>()
..having(
(state) => state.arrowState,
'arrowState',
SpaceshipRampArrowSpriteState.inactive,
),
],
);
});
}); });
}); });
} }

@ -7,15 +7,9 @@ void main() {
group('SpaceshipRampState', () { group('SpaceshipRampState', () {
test('supports value equality', () { test('supports value equality', () {
expect( expect(
SpaceshipRampState( SpaceshipRampState(hits: 0),
hits: 0,
arrowState: SpaceshipRampArrowSpriteState.inactive,
),
equals( equals(
SpaceshipRampState( SpaceshipRampState(hits: 0),
hits: 0,
arrowState: SpaceshipRampArrowSpriteState.inactive,
),
), ),
); );
}); });
@ -23,10 +17,7 @@ void main() {
group('constructor', () { group('constructor', () {
test('can be instantiated', () { test('can be instantiated', () {
expect( expect(
SpaceshipRampState( SpaceshipRampState(hits: 0),
hits: 0,
arrowState: SpaceshipRampArrowSpriteState.inactive,
),
isNotNull, isNotNull,
); );
}); });
@ -37,10 +28,7 @@ void main() {
'when hits is negative', 'when hits is negative',
() { () {
expect( expect(
() => SpaceshipRampState( () => SpaceshipRampState(hits: -1),
hits: -1,
arrowState: SpaceshipRampArrowSpriteState.inactive,
),
throwsAssertionError, throwsAssertionError,
); );
}, },
@ -51,10 +39,7 @@ void main() {
'throws AssertionError ' 'throws AssertionError '
'when hits is decreased', 'when hits is decreased',
() { () {
const rampState = SpaceshipRampState( const rampState = SpaceshipRampState(hits: 0);
hits: 0,
arrowState: SpaceshipRampArrowSpriteState.inactive,
);
expect( expect(
() => rampState.copyWith(hits: rampState.hits - 1), () => rampState.copyWith(hits: rampState.hits - 1),
throwsAssertionError, throwsAssertionError,
@ -66,10 +51,7 @@ void main() {
'copies correctly ' 'copies correctly '
'when no argument specified', 'when no argument specified',
() { () {
const rampState = SpaceshipRampState( const rampState = SpaceshipRampState(hits: 0);
hits: 0,
arrowState: SpaceshipRampArrowSpriteState.inactive,
);
expect( expect(
rampState.copyWith(), rampState.copyWith(),
equals(rampState), equals(rampState),
@ -81,21 +63,12 @@ void main() {
'copies correctly ' 'copies correctly '
'when all arguments specified', 'when all arguments specified',
() { () {
const rampState = SpaceshipRampState( const rampState = SpaceshipRampState(hits: 0);
hits: 0, final otherRampState = SpaceshipRampState(hits: rampState.hits + 1);
arrowState: SpaceshipRampArrowSpriteState.inactive,
);
final otherRampState = SpaceshipRampState(
hits: rampState.hits + 1,
arrowState: SpaceshipRampArrowSpriteState.active1,
);
expect(rampState, isNot(equals(otherRampState))); expect(rampState, isNot(equals(otherRampState)));
expect( expect(
rampState.copyWith( rampState.copyWith(hits: rampState.hits + 1),
hits: rampState.hits + 1,
arrowState: SpaceshipRampArrowSpriteState.active1,
),
equals(otherRampState), equals(otherRampState),
); );
}, },

@ -106,7 +106,9 @@ void main() {
await game.ensureAdd(parent); await game.ensureAdd(parent);
await parent.ensureAdd(behavior); await parent.ensureAdd(behavior);
streamController.add(SpaceshipRampState(hits: 1)); streamController.add(
SpaceshipRampState(hits: 1),
);
final scores = game.descendants().whereType<ScoreComponent>(); final scores = game.descendants().whereType<ScoreComponent>();
await game.ready(); await game.ready();

Loading…
Cancel
Save