From 482215d36ba729262ec8ae2d808762b2c56008c1 Mon Sep 17 00:00:00 2001 From: Allison Ryan Date: Mon, 18 Apr 2022 11:59:06 -0500 Subject: [PATCH] refactor: pr suggestions --- .../lib/src/components/ball.dart | 19 ++++++++----------- .../test/src/components/ball_test.dart | 15 ++++++++------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/packages/pinball_components/lib/src/components/ball.dart b/packages/pinball_components/lib/src/components/ball.dart index 034cf8d5..7aaf4b9c 100644 --- a/packages/pinball_components/lib/src/components/ball.dart +++ b/packages/pinball_components/lib/src/components/ball.dart @@ -4,6 +4,7 @@ import 'dart:ui'; import 'package:flame/components.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; +import 'package:flutter/widgets.dart'; import 'package:pinball_components/pinball_components.dart'; /// {@template ball} @@ -84,10 +85,10 @@ class Ball extends BodyComponent body.gravityScale = Vector2(0, 1); } - /// Applies a boost and [_FlameEffect] on this [Ball]. + /// Applies a boost and [_TurboChargeSpriteAnimation] on this [Ball]. Future boost(Vector2 impulse) async { body.linearVelocity = impulse; - await add(_FlameEffect()); + await add(_TurboChargeSpriteAnimation()); } @override @@ -146,14 +147,15 @@ class _BallSpriteComponent extends SpriteComponent with HasGameRef { } } -class _FlameEffect extends SpriteAnimationComponent with HasGameRef { - _FlameEffect() +class _TurboChargeSpriteAnimation extends SpriteAnimationComponent + with HasGameRef { + _TurboChargeSpriteAnimation() : super( anchor: const Anchor(0.53, 0.72), priority: Ball.boardPriority + 1, + removeOnFinish: true, ); - var _duration = 2.0; late final Vector2 _textureSize; @override @@ -178,6 +180,7 @@ class _FlameEffect extends SpriteAnimationComponent with HasGameRef { amountPerRow: amountPerRow, stepTime: 1 / 24, textureSize: _textureSize, + loop: false, ), ); } @@ -192,11 +195,5 @@ class _FlameEffect extends SpriteAnimationComponent with HasGameRef { angle = math.atan2(direction.x, -direction.y); size = (_textureSize / 45) * body.fixtures.first.shape.radius; } - - _duration -= dt; - - if (_duration < 0) { - removeFromParent(); - } } } diff --git a/packages/pinball_components/test/src/components/ball_test.dart b/packages/pinball_components/test/src/components/ball_test.dart index 376abebb..26a03886 100644 --- a/packages/pinball_components/test/src/components/ball_test.dart +++ b/packages/pinball_components/test/src/components/ball_test.dart @@ -175,7 +175,7 @@ void main() { expect(ball.body.linearVelocity.y, greaterThan(0)); }); - flameTester.test('adds FlameEffect', (game) async { + flameTester.test('adds TurboChargeSpriteAnimation', (game) async { final ball = Ball(baseColor: Colors.blue); await game.ensureAdd(ball); @@ -188,22 +188,23 @@ void main() { ); }); - flameTester.test('removes FlameEffect after a duration', (game) async { + flameTester.test('removes TurboChargeSpriteAnimation after it finishes', + (game) async { final ball = Ball(baseColor: Colors.blue); await game.ensureAdd(ball); await ball.boost(Vector2.all(10)); game.update(0); - final flameEffect = + final turboChargeSpriteAnimation = ball.children.whereType().single; - expect(ball.contains(flameEffect), isTrue); + expect(ball.contains(turboChargeSpriteAnimation), isTrue); - game.update(3); - await game.ready(); + game.update(turboChargeSpriteAnimation.animation!.totalDuration()); + game.update(0.1); - expect(ball.contains(flameEffect), isFalse); + expect(ball.contains(turboChargeSpriteAnimation), isFalse); }); }); });