From ada5ada264d2c323ed0363f95aa26b67c871f8c7 Mon Sep 17 00:00:00 2001 From: RuiAlonso Date: Mon, 9 May 2022 16:43:50 +0200 Subject: [PATCH] refactor: flamebloclistenable --- .../behaviors/ramp_bonus_behavior.dart | 44 ++++++++--------- .../behaviors/ramp_multiplier_behavior.dart | 39 ++++++--------- .../behaviors/ramp_progress_behavior.dart | 49 ++++++++----------- .../behaviors/ramp_reset_behavior.dart | 24 +++------ .../behaviors/ramp_shot_behavior.dart | 35 +++++++------ .../cubit/spaceship_ramp_cubit.dart | 5 +- .../spaceship_ramp/spaceship_ramp.dart | 27 ++++++---- .../pinball_components/sandbox/pubspec.lock | 2 +- .../pinball_components/sandbox/pubspec.yaml | 1 - .../ramp_multiplier_behavior_test.dart | 3 -- .../ramp_progress_behavior_test.dart | 3 -- 11 files changed, 101 insertions(+), 131 deletions(-) diff --git a/lib/game/components/android_acres/behaviors/ramp_bonus_behavior.dart b/lib/game/components/android_acres/behaviors/ramp_bonus_behavior.dart index c40a8b08..497186a1 100644 --- a/lib/game/components/android_acres/behaviors/ramp_bonus_behavior.dart +++ b/lib/game/components/android_acres/behaviors/ramp_bonus_behavior.dart @@ -1,15 +1,15 @@ -import 'dart:async'; - import 'package:flame/components.dart'; import 'package:flame_bloc/flame_bloc.dart'; import 'package:pinball/game/behaviors/behaviors.dart'; import 'package:pinball_components/pinball_components.dart'; -import 'package:pinball_flame/pinball_flame.dart'; /// {@template ramp_bonus_behavior} /// Increases the score when a [Ball] is shot 10 times into the [SpaceshipRamp]. /// {@endtemplate} -class RampBonusBehavior extends Component with ParentIsA { +class RampBonusBehavior extends Component + with + HasGameRef, + FlameBlocListenable { /// {@macro ramp_bonus_behavior} RampBonusBehavior({ required Points points, @@ -19,26 +19,24 @@ class RampBonusBehavior extends Component with ParentIsA { final Points _points; @override - Future onLoad() async { - await super.onLoad(); - await add( - FlameBlocListener( - listenWhen: (previousState, newState) { - final hasChanged = previousState.hits != newState.hits; - final hasHit = newState.hits != 0; - final achievedOneMillionPoints = newState.hits % 10 == 0; + bool listenWhen( + SpaceshipRampState previousState, + SpaceshipRampState newState, + ) { + final hasChanged = previousState.hits != newState.hits; + final hasHit = newState.hits != 0; + final achievedOneMillionPoints = newState.hits % 10 == 0; + + return hasChanged && hasHit && achievedOneMillionPoints; + } - return hasChanged && hasHit && achievedOneMillionPoints; - }, - onNewState: (state) { - parent.add( - ScoringBehavior( - points: _points, - position: Vector2(0, -60), - duration: 2, - ), - ); - }, + @override + void onNewState(SpaceshipRampState state) { + gameRef.add( + ScoringBehavior( + points: _points, + position: Vector2(0, -60), + duration: 2, ), ); } diff --git a/lib/game/components/android_acres/behaviors/ramp_multiplier_behavior.dart b/lib/game/components/android_acres/behaviors/ramp_multiplier_behavior.dart index 9901b7cf..835ae4b3 100644 --- a/lib/game/components/android_acres/behaviors/ramp_multiplier_behavior.dart +++ b/lib/game/components/android_acres/behaviors/ramp_multiplier_behavior.dart @@ -1,36 +1,27 @@ -import 'dart:async'; - import 'package:flame/components.dart'; import 'package:flame_bloc/flame_bloc.dart'; import 'package:pinball/game/game.dart'; import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_flame/pinball_flame.dart'; -/// {@template ramp_multiplier_behavior} /// Increases the multiplier when a [Ball] is shot 5 times into the /// [SpaceshipRamp]. -/// {@endtemplate} -class RampMultiplierBehavior extends Component { - /// {@macro ramp_multiplier_behavior} - RampMultiplierBehavior() : super(); +class RampMultiplierBehavior extends Component + with FlameBlocListenable { + @override + bool listenWhen( + SpaceshipRampState previousState, + SpaceshipRampState newState, + ) { + final hasChanged = + previousState.hits != newState.hits && newState.hits != 0; + final achievedFiveShots = newState.hits % 5 == 0; + final canIncrease = readBloc().state.multiplier != 6; + return hasChanged && achievedFiveShots && canIncrease; + } @override - Future onLoad() async { - await super.onLoad(); - await add( - FlameBlocListener( - listenWhen: (previousState, newState) { - final hasChanged = - previousState.hits != newState.hits && newState.hits != 0; - final achievedFiveShots = newState.hits % 5 == 0; - final canIncrease = - readBloc().state.multiplier != 6; - return hasChanged && achievedFiveShots && canIncrease; - }, - onNewState: (state) { - readBloc().add(const MultiplierIncreased()); - }, - ), - ); + void onNewState(SpaceshipRampState state) { + readBloc().add(const MultiplierIncreased()); } } diff --git a/lib/game/components/android_acres/behaviors/ramp_progress_behavior.dart b/lib/game/components/android_acres/behaviors/ramp_progress_behavior.dart index 42762898..d9381edb 100644 --- a/lib/game/components/android_acres/behaviors/ramp_progress_behavior.dart +++ b/lib/game/components/android_acres/behaviors/ramp_progress_behavior.dart @@ -1,43 +1,34 @@ -import 'dart:async'; - import 'package:flame/components.dart'; import 'package:flame_bloc/flame_bloc.dart'; import 'package:pinball/game/game.dart'; import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_flame/pinball_flame.dart'; -/// {@template ramp_progress_behavior} /// Changes arrow lit when a [Ball] is shot into the [SpaceshipRamp]. -/// {@endtemplate} -class RampProgressBehavior extends Component { - /// {@macro ramp_progress_behavior} - RampProgressBehavior() : super(); +class RampProgressBehavior extends Component + with FlameBlocListenable { + @override + bool listenWhen( + SpaceshipRampState previousState, + SpaceshipRampState newState, + ) { + return previousState.hits != newState.hits && newState.hits != 0; + } @override - Future onLoad() async { - await super.onLoad(); - await add( - FlameBlocListener( - listenWhen: (previousState, newState) => - previousState.hits != newState.hits && newState.hits != 0, - onNewState: (state) { - final gameBloc = readBloc(); - final spaceshipCubit = - readBloc(); + void onNewState(SpaceshipRampState state) { + final gameBloc = readBloc(); + final spaceshipCubit = readBloc(); - final canProgress = !gameBloc.state.isMaxMultiplier || - (gameBloc.state.isMaxMultiplier && !state.fullArrowLit); + final canProgress = !gameBloc.state.isMaxMultiplier || + (gameBloc.state.isMaxMultiplier && !state.fullArrowLit); - if (canProgress) { - spaceshipCubit.onProgressed(); - } + if (canProgress) { + spaceshipCubit.onProgressed(); + } - if (spaceshipCubit.state.fullArrowLit && - !gameBloc.state.isMaxMultiplier) { - spaceshipCubit.onProgressed(); - } - }, - ), - ); + if (spaceshipCubit.state.fullArrowLit && !gameBloc.state.isMaxMultiplier) { + spaceshipCubit.onProgressed(); + } } } diff --git a/lib/game/components/android_acres/behaviors/ramp_reset_behavior.dart b/lib/game/components/android_acres/behaviors/ramp_reset_behavior.dart index 1468fb72..314a4be7 100644 --- a/lib/game/components/android_acres/behaviors/ramp_reset_behavior.dart +++ b/lib/game/components/android_acres/behaviors/ramp_reset_behavior.dart @@ -4,24 +4,16 @@ import 'package:pinball/game/game.dart'; import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_flame/pinball_flame.dart'; -/// {@template ramp_reset_behavior} /// Reset [SpaceshipRamp] state when GameState.rounds changes. -/// /// {@endtemplate} -class RampResetBehavior extends Component { - /// {@macro ramp_reset_behavior} - RampResetBehavior() : super(); +class RampResetBehavior extends Component + with FlameBlocListenable { + @override + bool listenWhen(GameState previousState, GameState newState) { + return previousState.rounds != newState.rounds; + } @override - Future onLoad() async { - await super.onLoad(); - await add( - FlameBlocListener( - listenWhen: (previousState, newState) => - previousState.rounds != newState.rounds, - onNewState: (state) { - readBloc().onReset(); - }, - ), - ); + void onNewState(GameState state) { + readBloc().onReset(); } } diff --git a/lib/game/components/android_acres/behaviors/ramp_shot_behavior.dart b/lib/game/components/android_acres/behaviors/ramp_shot_behavior.dart index 8c37dece..2447873d 100644 --- a/lib/game/components/android_acres/behaviors/ramp_shot_behavior.dart +++ b/lib/game/components/android_acres/behaviors/ramp_shot_behavior.dart @@ -1,15 +1,15 @@ -import 'dart:async'; - import 'package:flame/components.dart'; import 'package:flame_bloc/flame_bloc.dart'; import 'package:pinball/game/behaviors/behaviors.dart'; import 'package:pinball_components/pinball_components.dart'; -import 'package:pinball_flame/pinball_flame.dart'; /// {@template ramp_shot_behavior} /// Increases the score when a [Ball] is shot into the [SpaceshipRamp]. /// {@endtemplate} -class RampShotBehavior extends Component with ParentIsA { +class RampShotBehavior extends Component + with + HasGameRef, + FlameBlocListenable { /// {@macro ramp_shot_behavior} RampShotBehavior({ required Points points, @@ -19,20 +19,19 @@ class RampShotBehavior extends Component with ParentIsA { final Points _points; @override - Future onLoad() async { - await super.onLoad(); - await add( - FlameBlocListener( - listenWhen: (previousState, newState) => - previousState.hits != newState.hits && newState.hits != 0, - onNewState: (state) { - parent.add( - ScoringBehavior( - points: _points, - position: Vector2(0, -45), - ), - ); - }, + bool listenWhen( + SpaceshipRampState previousState, + SpaceshipRampState newState, + ) { + return previousState.hits != newState.hits && newState.hits != 0; + } + + @override + void onNewState(SpaceshipRampState state) { + gameRef.add( + ScoringBehavior( + points: _points, + position: Vector2(0, -45), ), ); } diff --git a/packages/pinball_components/lib/src/components/spaceship_ramp/cubit/spaceship_ramp_cubit.dart b/packages/pinball_components/lib/src/components/spaceship_ramp/cubit/spaceship_ramp_cubit.dart index a117d2e4..0765b3b6 100644 --- a/packages/pinball_components/lib/src/components/spaceship_ramp/cubit/spaceship_ramp_cubit.dart +++ b/packages/pinball_components/lib/src/components/spaceship_ramp/cubit/spaceship_ramp_cubit.dart @@ -25,10 +25,7 @@ class SpaceshipRampCubit extends Cubit { void onReset() { emit( - const SpaceshipRampState( - hits: 0, - lightState: ArrowLightState.inactive, - ), + const SpaceshipRampState.initial(), ); } } diff --git a/packages/pinball_components/lib/src/components/spaceship_ramp/spaceship_ramp.dart b/packages/pinball_components/lib/src/components/spaceship_ramp/spaceship_ramp.dart index 5bf6418c..745def69 100644 --- a/packages/pinball_components/lib/src/components/spaceship_ramp/spaceship_ramp.dart +++ b/packages/pinball_components/lib/src/components/spaceship_ramp/spaceship_ramp.dart @@ -171,7 +171,11 @@ class _SpaceshipRampBackgroundRampSpriteComponent extends SpriteComponent /// {@endtemplate} @visibleForTesting class SpaceshipRampArrowSpriteComponent - extends SpriteGroupComponent with HasGameRef, ZIndex { + extends SpriteGroupComponent + with + HasGameRef, + ZIndex, + FlameBlocListenable { /// {@macro spaceship_ramp_arrow_sprite_component} SpaceshipRampArrowSpriteComponent() : super( @@ -181,17 +185,22 @@ class SpaceshipRampArrowSpriteComponent zIndex = ZIndexes.spaceshipRampArrow; } + @override + bool listenWhen( + SpaceshipRampState previousState, + SpaceshipRampState newState, + ) { + return previousState.lightState != newState.lightState; + } + + @override + void onNewState(SpaceshipRampState state) { + current = state.lightState; + } + @override Future onLoad() async { await super.onLoad(); - await add( - FlameBlocListener( - listenWhen: (previousState, newState) => - previousState.lightState != newState.lightState, - onNewState: (state) => current = state.lightState, - ), - ); - final sprites = {}; this.sprites = sprites; for (final spriteState in ArrowLightState.values) { diff --git a/packages/pinball_components/sandbox/pubspec.lock b/packages/pinball_components/sandbox/pubspec.lock index 2cdd35c7..b5ac88b7 100644 --- a/packages/pinball_components/sandbox/pubspec.lock +++ b/packages/pinball_components/sandbox/pubspec.lock @@ -107,7 +107,7 @@ packages: source: hosted version: "1.1.1" flame_bloc: - dependency: "direct main" + dependency: transitive description: name: flame_bloc url: "https://pub.dartlang.org" diff --git a/packages/pinball_components/sandbox/pubspec.yaml b/packages/pinball_components/sandbox/pubspec.yaml index cbe1c7be..791020d0 100644 --- a/packages/pinball_components/sandbox/pubspec.yaml +++ b/packages/pinball_components/sandbox/pubspec.yaml @@ -9,7 +9,6 @@ environment: dependencies: dashbook: ^0.1.7 flame: ^1.1.1 - flame_bloc: ^1.4.0 flame_forge2d: git: url: https://github.com/flame-engine/flame diff --git a/test/game/components/android_acres/behaviors/ramp_multiplier_behavior_test.dart b/test/game/components/android_acres/behaviors/ramp_multiplier_behavior_test.dart index a77684bf..916d401b 100644 --- a/test/game/components/android_acres/behaviors/ramp_multiplier_behavior_test.dart +++ b/test/game/components/android_acres/behaviors/ramp_multiplier_behavior_test.dart @@ -28,9 +28,6 @@ class _TestGame extends Forge2DGame { Assets.images.android.ramp.arrow.active3.keyName, Assets.images.android.ramp.arrow.active4.keyName, Assets.images.android.ramp.arrow.active5.keyName, - Assets.images.android.rail.main.keyName, - Assets.images.android.rail.exit.keyName, - Assets.images.score.fiveThousand.keyName, ]); } diff --git a/test/game/components/android_acres/behaviors/ramp_progress_behavior_test.dart b/test/game/components/android_acres/behaviors/ramp_progress_behavior_test.dart index fec9b72b..8bbc25d9 100644 --- a/test/game/components/android_acres/behaviors/ramp_progress_behavior_test.dart +++ b/test/game/components/android_acres/behaviors/ramp_progress_behavior_test.dart @@ -28,9 +28,6 @@ class _TestGame extends Forge2DGame { Assets.images.android.ramp.arrow.active3.keyName, Assets.images.android.ramp.arrow.active4.keyName, Assets.images.android.ramp.arrow.active5.keyName, - Assets.images.android.rail.main.keyName, - Assets.images.android.rail.exit.keyName, - Assets.images.score.fiveThousand.keyName, ]); }