refactor: moved FlameBlocProvider inside SpaceshipRamp

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

@ -15,9 +15,6 @@ class AndroidAcres extends Component {
/// {@macro android_acres}
AndroidAcres()
: super(
children: [
FlameBlocProvider<SpaceshipRampCubit, SpaceshipRampState>(
create: SpaceshipRampCubit.new,
children: [
SpaceshipRamp(
children: [
@ -28,8 +25,6 @@ class AndroidAcres extends Component {
RampResetBehavior(),
],
),
],
),
SpaceshipRail(),
AndroidBumper.a(
children: [

@ -9,7 +9,7 @@ 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<SpaceshipRamp> {
class RampBonusBehavior extends Component with ParentIsA<FlameBlocProvider> {
/// {@macro ramp_bonus_behavior}
RampBonusBehavior({
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
/// [SpaceshipRamp].
/// {@endtemplate}
class RampMultiplierBehavior extends Component with ParentIsA<SpaceshipRamp> {
class RampMultiplierBehavior extends Component {
/// {@macro ramp_multiplier_behavior}
RampMultiplierBehavior() : super();

@ -9,7 +9,7 @@ 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 with ParentIsA<SpaceshipRamp> {
class RampProgressBehavior extends Component {
/// {@macro ramp_progress_behavior}
RampProgressBehavior() : super();

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

@ -9,7 +9,7 @@ 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<SpaceshipRamp> {
class RampShotBehavior extends Component with ParentIsA<FlameBlocProvider> {
/// {@macro ramp_shot_behavior}
RampShotBehavior({
required Points points,

@ -25,6 +25,9 @@ class SpaceshipRamp extends Component {
SpaceshipRamp._({
Iterable<Component>? children,
}) : super(
children: [
FlameBlocProvider<SpaceshipRampCubit, SpaceshipRampState>(
create: SpaceshipRampCubit.new,
children: [
_SpaceshipRampOpening(
outsideLayer: Layer.spaceship,
@ -34,13 +37,16 @@ class SpaceshipRamp extends Component {
..initialPosition = Vector2(-13.7, -18.6)
..layer = Layer.spaceshipEntranceRamp,
_SpaceshipRampBackground(),
SpaceshipRampBoardOpening()..initialPosition = Vector2(3.4, -39.5),
SpaceshipRampBoardOpening()
..initialPosition = Vector2(3.4, -39.5),
_SpaceshipRampForegroundRailing(),
SpaceshipRampBase()..initialPosition = Vector2(3.4, -42.5),
_SpaceshipRampBackgroundRailingSpriteComponent(),
SpaceshipRampArrowSpriteComponent(),
...?children,
],
),
],
);
/// Creates a [SpaceshipRamp] without any children.
@ -155,8 +161,7 @@ class _SpaceshipRampBackgroundRampSpriteComponent extends SpriteComponent
/// {@endtemplate}
@visibleForTesting
class SpaceshipRampArrowSpriteComponent
extends SpriteGroupComponent<ArrowLightState>
with HasGameRef, ParentIsA<SpaceshipRamp>, ZIndex {
extends SpriteGroupComponent<ArrowLightState> with HasGameRef, ZIndex {
/// {@macro spaceship_ramp_arrow_sprite_component}
SpaceshipRampArrowSpriteComponent()
: super(
@ -210,7 +215,7 @@ extension on ArrowLightState {
}
class SpaceshipRampBoardOpening extends BodyComponent
with Layered, ZIndex, InitialPosition, ParentIsA<SpaceshipRamp> {
with Layered, ZIndex, InitialPosition {
SpaceshipRampBoardOpening()
: super(
renderBody: false,

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

Loading…
Cancel
Save