From f7719bba2e45035413260f6e149b0e24c69963c5 Mon Sep 17 00:00:00 2001 From: RuiAlonso Date: Tue, 19 Apr 2022 13:06:34 +0200 Subject: [PATCH] refactor: plunger with SpriteAnimationGroupComponent --- .../lib/src/components/plunger.dart | 89 ++++++++----------- 1 file changed, 39 insertions(+), 50 deletions(-) diff --git a/packages/pinball_components/lib/src/components/plunger.dart b/packages/pinball_components/lib/src/components/plunger.dart index 4eda5ae6..60671973 100644 --- a/packages/pinball_components/lib/src/components/plunger.dart +++ b/packages/pinball_components/lib/src/components/plunger.dart @@ -1,3 +1,5 @@ +import 'dart:ui'; + import 'package:flame/components.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:pinball_components/pinball_components.dart'; @@ -16,12 +18,13 @@ class Plunger extends BodyComponent with InitialPosition, Layered { // are fixed. }) : super(priority: 0) { layer = Layer.launcher; + renderBody = false; } /// Distance the plunger can lower. final double compressionDistance; - final _PlungerSpriteComponent _spriteComponent = _PlungerSpriteComponent(); + late final _PlungerSpriteAnimationGroupComponent _spriteComponent; List _createFixtureDefs() { final fixturesDef = []; @@ -104,43 +107,32 @@ class Plunger extends BodyComponent with InitialPosition, Layered { Future onLoad() async { await super.onLoad(); await _anchorToJoint(); - renderBody = false; - 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; - } + await _loadSprite(); } - @override - Future onLoad() async { - await super.onLoad(); + Future _loadSprite() async { final spriteSheet = await gameRef.images.load( 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 amountPerColumn = 1; @@ -150,11 +142,14 @@ class _PlungerSpriteComponent extends SpriteAnimationComponent with HasGameRef { ); size = textureSize / 10; + position = Vector2(1.87, 15.5); + // 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 // the animation (but amountPerRow and amountPerColumn needs to be correct // in order of calculate textureSize correctly). - _pullAnimation = SpriteAnimation.fromFrameData( + + final pullAnimation = SpriteAnimation.fromFrameData( spriteSheet, SpriteAnimationData.sequenced( amount: amountPerRow * amountPerColumn ~/ 2, @@ -164,27 +159,21 @@ class _PlungerSpriteComponent extends SpriteAnimationComponent with HasGameRef { texturePosition: Vector2.zero(), loop: false, ), - )..onComplete = () { - playing = false; - }; + ); - _releaseAnimation = _pullAnimation.reversed() - ..onComplete = () { - playing = false; - }; + animations = { + _PlungerAnimationState.release: pullAnimation.reversed(), + _PlungerAnimationState.pull: pullAnimation, + }; + current = _PlungerAnimationState.release; + } - animation = _pullAnimation; - position = Vector2(1.87, 15.5); + void pull() { + current = _PlungerAnimationState.pull; } - @override - void update(double dt) { - super.update(dt); - if (animation != null) { - if (animation!.isLastFrame) { - playing = false; - } - } + void release() { + current = _PlungerAnimationState.release; } }