From 749b6f295ee934ad3fc70385ab0c005aa212f9b7 Mon Sep 17 00:00:00 2001 From: RuiAlonso Date: Wed, 6 Apr 2022 12:54:42 +0200 Subject: [PATCH] refactor: removed Bumper and added TODO for future refactor --- .../lib/src/components/sparky_bumper.dart | 105 ++++++++---------- 1 file changed, 45 insertions(+), 60 deletions(-) diff --git a/packages/pinball_components/lib/src/components/sparky_bumper.dart b/packages/pinball_components/lib/src/components/sparky_bumper.dart index 62bf4d07..dcd9c18f 100644 --- a/packages/pinball_components/lib/src/components/sparky_bumper.dart +++ b/packages/pinball_components/lib/src/components/sparky_bumper.dart @@ -4,74 +4,19 @@ import 'package:flame/components.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:pinball_components/pinball_components.dart'; -/// {@template bumper} -/// Generic Bumper. -/// {@endtemplate} -abstract class Bumper extends BodyComponent with InitialPosition { - /// {@macro bumper} - Bumper({ - required String activeAssetPath, - required String inactiveAssetPath, - required SpriteComponent spriteComponent, - }) : _activeAssetPath = activeAssetPath, - _inactiveAssetPath = inactiveAssetPath, - _spriteComponent = spriteComponent; - - final String _activeAssetPath; - late final Sprite _activeSprite; - final String _inactiveAssetPath; - late final Sprite _inactiveSprite; - final SpriteComponent _spriteComponent; - - Future _loadSprites() async { - // TODO(alestiago): I think ideally we would like to do: - // Sprite(path).load so we don't require to store the activeAssetPath and - // the inactive assetPath. - _inactiveSprite = await gameRef.loadSprite(_inactiveAssetPath); - _activeSprite = await gameRef.loadSprite(_activeAssetPath); - } - - /// Activates the [DashNestBumper]. - void activate() { - _spriteComponent - ..sprite = _activeSprite - ..size = _activeSprite.originalSize / 10; - } - - /// Deactivates the [DashNestBumper]. - void deactivate() { - _spriteComponent - ..sprite = _inactiveSprite - ..size = _inactiveSprite.originalSize / 10; - } - - @override - Future onLoad() async { - await super.onLoad(); - await _loadSprites(); - - // TODO(erickzanardo): Look into using onNewState instead. - // Currently doing: onNewState(gameRef.read()) will throw an - // `Exception: build context is not available yet` - deactivate(); - await add(_spriteComponent); - } -} - /// {@template sparky_bumper} /// Bumper for Sparky fire. /// {@endtemplate} -class SparkyBumper extends Bumper { +// TODO(ruimiguel): refactor later to unify with DashBumpers. +class SparkyBumper extends BodyComponent with InitialPosition { /// {@macro sparky_bumper} SparkyBumper._({ required String activeAssetPath, required String inactiveAssetPath, required SpriteComponent spriteComponent, - }) : super( - activeAssetPath: activeAssetPath, - inactiveAssetPath: inactiveAssetPath, - spriteComponent: spriteComponent, - ); + }) : _activeAssetPath = activeAssetPath, + _inactiveAssetPath = inactiveAssetPath, + _spriteComponent = spriteComponent; /// {@macro sparky_bumper} SparkyBumper.a() @@ -106,6 +51,24 @@ class SparkyBumper extends Bumper { ), ); + final String _activeAssetPath; + late final Sprite _activeSprite; + final String _inactiveAssetPath; + late final Sprite _inactiveSprite; + final SpriteComponent _spriteComponent; + + @override + Future onLoad() async { + await super.onLoad(); + await _loadSprites(); + + // TODO(erickzanardo): Look into using onNewState instead. + // Currently doing: onNewState(gameRef.read()) will throw an + // `Exception: build context is not available yet` + deactivate(); + await add(_spriteComponent); + } + @override Body createBody() { renderBody = false; @@ -125,4 +88,26 @@ class SparkyBumper extends Bumper { return world.createBody(bodyDef)..createFixture(fixtureDef); } + + Future _loadSprites() async { + // TODO(alestiago): I think ideally we would like to do: + // Sprite(path).load so we don't require to store the activeAssetPath and + // the inactive assetPath. + _inactiveSprite = await gameRef.loadSprite(_inactiveAssetPath); + _activeSprite = await gameRef.loadSprite(_activeAssetPath); + } + + /// Activates the [DashNestBumper]. + void activate() { + _spriteComponent + ..sprite = _activeSprite + ..size = _activeSprite.originalSize / 10; + } + + /// Deactivates the [DashNestBumper]. + void deactivate() { + _spriteComponent + ..sprite = _inactiveSprite + ..size = _inactiveSprite.originalSize / 10; + } }