refactor: moved FlameBlocProvider inside SpaceshipRamp

pull/416/head
RuiAlonso 3 years ago
parent a5b05f0ac8
commit 584fec5338

@ -16,18 +16,13 @@ class AndroidAcres extends Component {
AndroidAcres() AndroidAcres()
: super( : super(
children: [ children: [
FlameBlocProvider<SpaceshipRampCubit, SpaceshipRampState>( SpaceshipRamp(
create: SpaceshipRampCubit.new,
children: [ children: [
SpaceshipRamp( RampShotBehavior(points: Points.fiveThousand),
children: [ RampBonusBehavior(points: Points.oneMillion),
RampShotBehavior(points: Points.fiveThousand), RampProgressBehavior(),
RampBonusBehavior(points: Points.oneMillion), RampMultiplierBehavior(),
RampProgressBehavior(), RampResetBehavior(),
RampMultiplierBehavior(),
RampResetBehavior(),
],
),
], ],
), ),
SpaceshipRail(), SpaceshipRail(),

@ -9,7 +9,7 @@ import 'package:pinball_flame/pinball_flame.dart';
/// {@template ramp_bonus_behavior} /// {@template ramp_bonus_behavior}
/// Increases the score when a [Ball] is shot 10 times into the [SpaceshipRamp]. /// Increases the score when a [Ball] is shot 10 times into the [SpaceshipRamp].
/// {@endtemplate} /// {@endtemplate}
class RampBonusBehavior extends Component with ParentIsA<SpaceshipRamp> { class RampBonusBehavior extends Component with ParentIsA<FlameBlocProvider> {
/// {@macro ramp_bonus_behavior} /// {@macro ramp_bonus_behavior}
RampBonusBehavior({ RampBonusBehavior({
required Points points, required Points points,

@ -10,7 +10,7 @@ import 'package:pinball_flame/pinball_flame.dart';
/// Increases the multiplier when a [Ball] is shot 5 times into the /// Increases the multiplier when a [Ball] is shot 5 times into the
/// [SpaceshipRamp]. /// [SpaceshipRamp].
/// {@endtemplate} /// {@endtemplate}
class RampMultiplierBehavior extends Component with ParentIsA<SpaceshipRamp> { class RampMultiplierBehavior extends Component {
/// {@macro ramp_multiplier_behavior} /// {@macro ramp_multiplier_behavior}
RampMultiplierBehavior() : super(); RampMultiplierBehavior() : super();

@ -9,7 +9,7 @@ import 'package:pinball_flame/pinball_flame.dart';
/// {@template ramp_progress_behavior} /// {@template ramp_progress_behavior}
/// Changes arrow lit when a [Ball] is shot into the [SpaceshipRamp]. /// Changes arrow lit when a [Ball] is shot into the [SpaceshipRamp].
/// {@endtemplate} /// {@endtemplate}
class RampProgressBehavior extends Component with ParentIsA<SpaceshipRamp> { class RampProgressBehavior extends Component {
/// {@macro ramp_progress_behavior} /// {@macro ramp_progress_behavior}
RampProgressBehavior() : super(); RampProgressBehavior() : super();

@ -7,7 +7,7 @@ import 'package:pinball_flame/pinball_flame.dart';
/// {@template ramp_reset_behavior} /// {@template ramp_reset_behavior}
/// Reset [SpaceshipRamp] state when GameState.rounds changes. /// Reset [SpaceshipRamp] state when GameState.rounds changes.
/// /// {@endtemplate} /// /// {@endtemplate}
class RampResetBehavior extends Component with ParentIsA<SpaceshipRamp> { class RampResetBehavior extends Component {
/// {@macro ramp_reset_behavior} /// {@macro ramp_reset_behavior}
RampResetBehavior() : super(); RampResetBehavior() : super();

@ -9,7 +9,7 @@ import 'package:pinball_flame/pinball_flame.dart';
/// {@template ramp_shot_behavior} /// {@template ramp_shot_behavior}
/// Increases the score when a [Ball] is shot into the [SpaceshipRamp]. /// Increases the score when a [Ball] is shot into the [SpaceshipRamp].
/// {@endtemplate} /// {@endtemplate}
class RampShotBehavior extends Component with ParentIsA<SpaceshipRamp> { class RampShotBehavior extends Component with ParentIsA<FlameBlocProvider> {
/// {@macro ramp_shot_behavior} /// {@macro ramp_shot_behavior}
RampShotBehavior({ RampShotBehavior({
required Points points, required Points points,

@ -26,20 +26,26 @@ class SpaceshipRamp extends Component {
Iterable<Component>? children, Iterable<Component>? children,
}) : super( }) : super(
children: [ children: [
_SpaceshipRampOpening( FlameBlocProvider<SpaceshipRampCubit, SpaceshipRampState>(
outsideLayer: Layer.spaceship, create: SpaceshipRampCubit.new,
outsidePriority: ZIndexes.ballOnSpaceship, children: [
rotation: math.pi, _SpaceshipRampOpening(
) outsideLayer: Layer.spaceship,
..initialPosition = Vector2(-13.7, -18.6) outsidePriority: ZIndexes.ballOnSpaceship,
..layer = Layer.spaceshipEntranceRamp, rotation: math.pi,
_SpaceshipRampBackground(), )
SpaceshipRampBoardOpening()..initialPosition = Vector2(3.4, -39.5), ..initialPosition = Vector2(-13.7, -18.6)
_SpaceshipRampForegroundRailing(), ..layer = Layer.spaceshipEntranceRamp,
SpaceshipRampBase()..initialPosition = Vector2(3.4, -42.5), _SpaceshipRampBackground(),
_SpaceshipRampBackgroundRailingSpriteComponent(), SpaceshipRampBoardOpening()
SpaceshipRampArrowSpriteComponent(), ..initialPosition = Vector2(3.4, -39.5),
...?children, _SpaceshipRampForegroundRailing(),
SpaceshipRampBase()..initialPosition = Vector2(3.4, -42.5),
_SpaceshipRampBackgroundRailingSpriteComponent(),
SpaceshipRampArrowSpriteComponent(),
...?children,
],
),
], ],
); );
@ -155,8 +161,7 @@ class _SpaceshipRampBackgroundRampSpriteComponent extends SpriteComponent
/// {@endtemplate} /// {@endtemplate}
@visibleForTesting @visibleForTesting
class SpaceshipRampArrowSpriteComponent class SpaceshipRampArrowSpriteComponent
extends SpriteGroupComponent<ArrowLightState> extends SpriteGroupComponent<ArrowLightState> with HasGameRef, ZIndex {
with HasGameRef, ParentIsA<SpaceshipRamp>, ZIndex {
/// {@macro spaceship_ramp_arrow_sprite_component} /// {@macro spaceship_ramp_arrow_sprite_component}
SpaceshipRampArrowSpriteComponent() SpaceshipRampArrowSpriteComponent()
: super( : super(
@ -210,7 +215,7 @@ extension on ArrowLightState {
} }
class SpaceshipRampBoardOpening extends BodyComponent class SpaceshipRampBoardOpening extends BodyComponent
with Layered, ZIndex, InitialPosition, ParentIsA<SpaceshipRamp> { with Layered, ZIndex, InitialPosition {
SpaceshipRampBoardOpening() SpaceshipRampBoardOpening()
: super( : super(
renderBody: false, renderBody: false,

@ -1,7 +1,6 @@
import 'dart:async'; import 'dart:async';
import 'package:flame/input.dart'; import 'package:flame/input.dart';
import 'package:flame_bloc/flame_bloc.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:pinball_components/pinball_components.dart'; import 'package:pinball_components/pinball_components.dart';
@ -38,7 +37,7 @@ class SpaceshipRampGame extends BallGame with KeyboardEvents {
@override @override
Color backgroundColor() => Colors.white; Color backgroundColor() => Colors.white;
late final SpaceshipRampCubit _bloc; late final SpaceshipRamp _spaceshipRamp;
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
@ -46,17 +45,8 @@ class SpaceshipRampGame extends BallGame with KeyboardEvents {
camera.followVector2(Vector2(-12, -50)); camera.followVector2(Vector2(-12, -50));
_bloc = SpaceshipRampCubit(); _spaceshipRamp = SpaceshipRamp();
await add( await add(_spaceshipRamp);
FlameBlocProvider<SpaceshipRampCubit, SpaceshipRampState>(
create: () => _bloc,
children: [
SpaceshipRamp(
children: [SpaceshipRamp()],
),
],
),
);
await traceAllBodies(); await traceAllBodies();
} }
@ -67,7 +57,9 @@ class SpaceshipRampGame extends BallGame with KeyboardEvents {
) { ) {
if (event is RawKeyDownEvent && if (event is RawKeyDownEvent &&
event.logicalKey == LogicalKeyboardKey.space) { event.logicalKey == LogicalKeyboardKey.space) {
_bloc.onProgressed(); _spaceshipRamp
.readBloc<SpaceshipRampCubit, SpaceshipRampState>()
.onProgressed();
return KeyEventResult.handled; return KeyEventResult.handled;
} }

Loading…
Cancel
Save