feat: made SignpostSpriteState visibleForTesting

pull/205/head
alestiago 3 years ago
parent 393f086eba
commit 6e5d1f1d1b

@ -1,31 +1,41 @@
import 'package:flame/components.dart'; import 'package:flame/components.dart';
import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/material.dart';
import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_components/pinball_components.dart';
enum _SignPostSpriteState { /// Represents the [SignPost]'s current [Sprite] state.
@visibleForTesting
enum SignPostSpriteState {
/// Signpost with no active dashes.
inactive, inactive,
/// Signpost with a single sign of active dashes.
active1, active1,
/// Signpost with two signs of active dashes.
active2, active2,
/// Signpost with all signs of active dashes.
active3, active3,
} }
extension on _SignPostSpriteState { extension on SignPostSpriteState {
String get path { String get path {
switch (this) { switch (this) {
case _SignPostSpriteState.inactive: case SignPostSpriteState.inactive:
return Assets.images.signPost.inactive.keyName; return Assets.images.signPost.inactive.keyName;
case _SignPostSpriteState.active1: case SignPostSpriteState.active1:
return Assets.images.signPost.active1.keyName; return Assets.images.signPost.active1.keyName;
case _SignPostSpriteState.active2: case SignPostSpriteState.active2:
return Assets.images.signPost.active2.keyName; return Assets.images.signPost.active2.keyName;
case _SignPostSpriteState.active3: case SignPostSpriteState.active3:
return Assets.images.signPost.active3.keyName; return Assets.images.signPost.active3.keyName;
} }
} }
_SignPostSpriteState get next { SignPostSpriteState get next {
return _SignPostSpriteState return SignPostSpriteState
.values[(index + 1) % _SignPostSpriteState.values.length]; .values[(index + 1) % SignPostSpriteState.values.length];
} }
} }
@ -42,7 +52,7 @@ class SignPost extends BodyComponent with InitialPosition {
renderBody = false; renderBody = false;
} }
/// Forwards the sprite to the next [_SignPostSpriteState]. /// Forwards the sprite to the next [SignPostSpriteState].
/// ///
/// If the current state is the last one it goes back to the initial state. /// If the current state is the last one it goes back to the initial state.
void progress() => firstChild<_SignPostSpriteComponent>()!.progress(); void progress() => firstChild<_SignPostSpriteComponent>()!.progress();
@ -59,8 +69,8 @@ class SignPost extends BodyComponent with InitialPosition {
} }
} }
class _SignPostSpriteComponent class _SignPostSpriteComponent extends SpriteGroupComponent<SignPostSpriteState>
extends SpriteGroupComponent<_SignPostSpriteState> with HasGameRef { with HasGameRef {
_SignPostSpriteComponent() _SignPostSpriteComponent()
: super( : super(
anchor: Anchor.bottomCenter, anchor: Anchor.bottomCenter,
@ -73,9 +83,9 @@ class _SignPostSpriteComponent
Future<void> onLoad() async { Future<void> onLoad() async {
await super.onLoad(); await super.onLoad();
final sprites = <_SignPostSpriteState, Sprite>{}; final sprites = <SignPostSpriteState, Sprite>{};
this.sprites = sprites; this.sprites = sprites;
for (final spriteState in _SignPostSpriteState.values) { for (final spriteState in SignPostSpriteState.values) {
// TODO(allisonryan0002): Support caching // TODO(allisonryan0002): Support caching
// https://github.com/VGVentures/pinball/pull/204 // https://github.com/VGVentures/pinball/pull/204
// sprites[spriteState] = Sprite( // sprites[spriteState] = Sprite(
@ -84,7 +94,7 @@ class _SignPostSpriteComponent
sprites[spriteState] = await gameRef.loadSprite(spriteState.path); sprites[spriteState] = await gameRef.loadSprite(spriteState.path);
} }
current = _SignPostSpriteState.inactive; current = SignPostSpriteState.inactive;
size = sprites[current]!.originalSize / 10; size = sprites[current]!.originalSize / 10;
} }
} }

@ -27,7 +27,14 @@ void main() {
flameTester.testGameWidget( flameTester.testGameWidget(
'inactive sprite', 'inactive sprite',
setUp: (game, tester) async { setUp: (game, tester) async {
await game.ensureAdd(SignPost()); final signPost = SignPost();
await game.ensureAdd(signPost);
expect(
signPost.firstChild<SpriteGroupComponent>()!.current,
SignPostSpriteState.inactive,
);
game.camera.followVector2(Vector2.zero()); game.camera.followVector2(Vector2.zero());
}, },
verify: (game, tester) async { verify: (game, tester) async {
@ -45,6 +52,11 @@ void main() {
await game.ensureAdd(signPost); await game.ensureAdd(signPost);
signPost.progress(); signPost.progress();
expect(
signPost.firstChild<SpriteGroupComponent>()!.current,
SignPostSpriteState.active1,
);
game.camera.followVector2(Vector2.zero()); game.camera.followVector2(Vector2.zero());
}, },
verify: (game, tester) async { verify: (game, tester) async {
@ -64,6 +76,11 @@ void main() {
..progress() ..progress()
..progress(); ..progress();
expect(
signPost.firstChild<SpriteGroupComponent>()!.current,
SignPostSpriteState.active2,
);
game.camera.followVector2(Vector2.zero()); game.camera.followVector2(Vector2.zero());
}, },
verify: (game, tester) async { verify: (game, tester) async {
@ -84,6 +101,11 @@ void main() {
..progress() ..progress()
..progress(); ..progress();
expect(
signPost.firstChild<SpriteGroupComponent>()!.current,
SignPostSpriteState.active3,
);
game.camera.followVector2(Vector2.zero()); game.camera.followVector2(Vector2.zero());
}, },
verify: (game, tester) async { verify: (game, tester) async {
@ -103,14 +125,17 @@ void main() {
await game.ensureAdd(signPost); await game.ensureAdd(signPost);
final spriteComponent = signPost.firstChild<SpriteGroupComponent>()!; final spriteComponent = signPost.firstChild<SpriteGroupComponent>()!;
final sprites = <Sprite>{};
for (var i = 0; i < 4; i++) { for (var i = 0; i < 4; i++) {
sprites.add(spriteComponent.sprite!); expect(spriteComponent.current, SignPostSpriteState.inactive);
signPost.progress();
expect(spriteComponent.current, SignPostSpriteState.active1);
signPost.progress();
expect(spriteComponent.current, SignPostSpriteState.active2);
signPost.progress();
expect(spriteComponent.current, SignPostSpriteState.active3);
signPost.progress(); signPost.progress();
} }
expect(sprites.length, equals(4));
}, },
); );
}); });

Loading…
Cancel
Save