feat: allowed progressing sprite

pull/205/head
alestiago 3 years ago
parent b56ae9c93d
commit 2130d5ea9d

@ -2,6 +2,33 @@ import 'package:flame/components.dart';
import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_components/pinball_components.dart';
enum _SignPostSpriteState {
inactive,
active1,
active2,
active3,
}
extension on _SignPostSpriteState {
String get path {
switch (this) {
case _SignPostSpriteState.inactive:
return Assets.images.signPost.inactive.keyName;
case _SignPostSpriteState.active1:
return Assets.images.signPost.active1.keyName;
case _SignPostSpriteState.active2:
return Assets.images.signPost.active2.keyName;
case _SignPostSpriteState.active3:
return Assets.images.signPost.active3.keyName;
}
}
_SignPostSpriteState get next {
return _SignPostSpriteState
.values[(index + 1) % _SignPostSpriteState.values.length];
}
}
/// {@template flutter_sign_post} /// {@template flutter_sign_post}
/// A sign, found in the Flutter Forest. /// A sign, found in the Flutter Forest.
/// {@endtemplate} /// {@endtemplate}
@ -15,6 +42,11 @@ class FlutterSignPost extends BodyComponent with InitialPosition {
renderBody = false; renderBody = false;
} }
/// Forwards the sprite to the next [_SignPostSpriteState].
///
/// If the current state is the last one it goes back to the initial state.
void progress() => firstChild<_FlutterSignPostSpriteComponent>()!.progress();
@override @override
Body createBody() { Body createBody() {
final shape = CircleShape()..radius = 0.25; final shape = CircleShape()..radius = 0.25;
@ -27,17 +59,29 @@ class FlutterSignPost extends BodyComponent with InitialPosition {
} }
} }
class _FlutterSignPostSpriteComponent extends SpriteComponent with HasGameRef { class _FlutterSignPostSpriteComponent
extends SpriteGroupComponent<_SignPostSpriteState> with HasGameRef {
_FlutterSignPostSpriteComponent()
: super(
anchor: Anchor.bottomCenter,
position: Vector2(0.65, 0.45),
);
void progress() => current = current?.next;
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
await super.onLoad(); await super.onLoad();
final sprite = await gameRef.loadSprite( final sprites = <_SignPostSpriteState, Sprite>{};
Assets.images.signPost.inactive.keyName, this.sprites = sprites;
for (final spriteState in _SignPostSpriteState.values) {
sprites[spriteState] = Sprite(
gameRef.images.fromCache(spriteState.path),
); );
this.sprite = sprite; }
size = sprite.originalSize / 10;
anchor = Anchor.bottomCenter; current = _SignPostSpriteState.inactive;
position = Vector2(0.65, 0.45); size = sprites[current]!.originalSize / 10;
} }
} }

Loading…
Cancel
Save