refactor: plunger with SpriteAnimationGroupComponent

pull/200/head
RuiAlonso 3 years ago
parent adff493c98
commit f7719bba2e

@ -1,3 +1,5 @@
import 'dart:ui';
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:pinball_components/pinball_components.dart'; import 'package:pinball_components/pinball_components.dart';
@ -16,12 +18,13 @@ class Plunger extends BodyComponent with InitialPosition, Layered {
// are fixed. // are fixed.
}) : super(priority: 0) { }) : super(priority: 0) {
layer = Layer.launcher; layer = Layer.launcher;
renderBody = false;
} }
/// Distance the plunger can lower. /// Distance the plunger can lower.
final double compressionDistance; final double compressionDistance;
final _PlungerSpriteComponent _spriteComponent = _PlungerSpriteComponent(); late final _PlungerSpriteAnimationGroupComponent _spriteComponent;
List<FixtureDef> _createFixtureDefs() { List<FixtureDef> _createFixtureDefs() {
final fixturesDef = <FixtureDef>[]; final fixturesDef = <FixtureDef>[];
@ -104,43 +107,32 @@ class Plunger extends BodyComponent with InitialPosition, Layered {
Future<void> onLoad() async { Future<void> onLoad() async {
await super.onLoad(); await super.onLoad();
await _anchorToJoint(); await _anchorToJoint();
renderBody = false; await _loadSprite();
await add(_spriteComponent);
}
}
class _PlungerSpriteComponent extends SpriteAnimationComponent with HasGameRef {
_PlungerSpriteComponent()
: super(
anchor: Anchor.center,
playing: false,
);
late final SpriteAnimation _pullAnimation;
late final SpriteAnimation _releaseAnimation;
void pull() {
if (animation != _pullAnimation) {
animation = _pullAnimation;
}
playing = true;
}
void release() {
if (animation != _releaseAnimation) {
animation = _releaseAnimation;
playing = true;
}
} }
@override Future<void> _loadSprite() async {
Future<void> onLoad() async {
await super.onLoad();
final spriteSheet = await gameRef.images.load( final spriteSheet = await gameRef.images.load(
Assets.images.plunger.plunger.keyName, Assets.images.plunger.plunger.keyName,
); );
_spriteComponent = _PlungerSpriteAnimationGroupComponent(spriteSheet);
await add(_spriteComponent);
}
}
/// Animation states associated with a [Plunger].
enum _PlungerAnimationState {
/// Pull state.
pull,
/// Release state.
release,
}
class _PlungerSpriteAnimationGroupComponent
extends SpriteAnimationGroupComponent<_PlungerAnimationState>
with HasGameRef {
_PlungerSpriteAnimationGroupComponent(Image spriteSheet)
: super(anchor: Anchor.center) {
const amountPerRow = 20; const amountPerRow = 20;
const amountPerColumn = 1; const amountPerColumn = 1;
@ -150,11 +142,14 @@ class _PlungerSpriteComponent extends SpriteAnimationComponent with HasGameRef {
); );
size = textureSize / 10; size = textureSize / 10;
position = Vector2(1.87, 15.5);
// TODO(ruimiguel): we only need plunger pull animation, and release is just // TODO(ruimiguel): we only need plunger pull animation, and release is just
// to reverse it, so we need to divide by 2 while we don't have only half of // to reverse it, so we need to divide by 2 while we don't have only half of
// the animation (but amountPerRow and amountPerColumn needs to be correct // the animation (but amountPerRow and amountPerColumn needs to be correct
// in order of calculate textureSize correctly). // in order of calculate textureSize correctly).
_pullAnimation = SpriteAnimation.fromFrameData(
final pullAnimation = SpriteAnimation.fromFrameData(
spriteSheet, spriteSheet,
SpriteAnimationData.sequenced( SpriteAnimationData.sequenced(
amount: amountPerRow * amountPerColumn ~/ 2, amount: amountPerRow * amountPerColumn ~/ 2,
@ -164,27 +159,21 @@ class _PlungerSpriteComponent extends SpriteAnimationComponent with HasGameRef {
texturePosition: Vector2.zero(), texturePosition: Vector2.zero(),
loop: false, loop: false,
), ),
)..onComplete = () { );
playing = false;
};
_releaseAnimation = _pullAnimation.reversed() animations = {
..onComplete = () { _PlungerAnimationState.release: pullAnimation.reversed(),
playing = false; _PlungerAnimationState.pull: pullAnimation,
}; };
current = _PlungerAnimationState.release;
}
animation = _pullAnimation; void pull() {
position = Vector2(1.87, 15.5); current = _PlungerAnimationState.pull;
} }
@override void release() {
void update(double dt) { current = _PlungerAnimationState.release;
super.update(dt);
if (animation != null) {
if (animation!.isLastFrame) {
playing = false;
}
}
} }
} }

Loading…
Cancel
Save