refactor: changed ramp behaviors to new flame bloc

pull/416/head
RuiAlonso 3 years ago
parent 356d75baf5
commit 71efa29102

@ -1,6 +1,7 @@
import 'dart:async';
import 'package:flame/components.dart';
import 'package:flame_bloc/flame_bloc.dart';
import 'package:flutter/material.dart';
import 'package:pinball/game/behaviors/behaviors.dart';
import 'package:pinball_components/pinball_components.dart';
@ -22,39 +23,30 @@ class RampBonusBehavior extends Component with ParentIsA<SpaceshipRamp> {
@visibleForTesting
RampBonusBehavior.test({
required Points points,
required this.subscription,
}) : _points = points,
super();
final Points _points;
/// Subscription to [SpaceshipRampState] at [SpaceshipRamp].
@visibleForTesting
StreamSubscription? subscription;
@override
void onMount() {
super.onMount();
subscription = subscription ??
parent.bloc.stream.listen((state) {
final achievedOneMillionPoints = state.hits % 10 == 0;
if (achievedOneMillionPoints) {
parent.add(
ScoringBehavior(
points: _points,
position: Vector2(0, -60),
duration: 2,
),
);
}
});
}
@override
void onRemove() {
subscription?.cancel();
super.onRemove();
Future<void> onLoad() async {
await super.onLoad();
await add(
FlameBlocListener<SpaceshipRampCubit, SpaceshipRampState>(
listenWhen: (previousState, newState) =>
previousState.hits != newState.hits &&
newState.hits != 0 &&
newState.hits % 10 == 0,
onNewState: (state) {
parent.add(
ScoringBehavior(
points: _points,
position: Vector2(0, -60),
duration: 2,
),
);
},
),
);
}
}

@ -11,8 +11,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>, FlameBlocReader<GameBloc, GameState> {
class RampMultiplierBehavior extends Component with ParentIsA<SpaceshipRamp> {
/// {@macro ramp_multiplier_behavior}
RampMultiplierBehavior() : super();
@ -20,39 +19,25 @@ class RampMultiplierBehavior extends Component
///
/// This can be used for testing [RampMultiplierBehavior] in isolation.
@visibleForTesting
RampMultiplierBehavior.test({
required this.subscription,
}) : super();
/// Subscription to [SpaceshipRampState] at [SpaceshipRamp].
@visibleForTesting
StreamSubscription? subscription;
@override
void onMount() {
super.onMount();
var previousState = const SpaceshipRampState.initial();
subscription = subscription ??
parent.bloc.stream.listen((state) {
final listenWhen =
previousState.hits != state.hits && state.hits != 0;
if (listenWhen) {
final achievedFiveShots = state.hits % 5 == 0;
if (achievedFiveShots) {
bloc.add(const MultiplierIncreased());
}
previousState = state;
}
});
}
RampMultiplierBehavior.test() : super();
@override
void onRemove() {
subscription?.cancel();
super.onRemove();
Future<void> onLoad() async {
await super.onLoad();
await add(
FlameBlocListener<SpaceshipRampCubit, SpaceshipRampState>(
listenWhen: (previousState, newState) {
final hasChanged =
previousState.hits != newState.hits && newState.hits != 0;
final achievedFiveShots = newState.hits % 5 == 0;
final canIncrease =
readBloc<GameBloc, GameState>().state.multiplier != 6;
return hasChanged && achievedFiveShots && canIncrease;
},
onNewState: (state) {
readBloc<GameBloc, GameState>().add(const MultiplierIncreased());
},
),
);
}
}

@ -4,15 +4,13 @@ import 'package:flame/components.dart';
import 'package:flame_bloc/flame_bloc.dart';
import 'package:flutter/cupertino.dart';
import 'package:pinball/game/behaviors/behaviors.dart';
import 'package:pinball/game/game.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<SpaceshipRamp>, FlameBlocReader<GameBloc, GameState> {
class RampShotBehavior extends Component with ParentIsA<SpaceshipRamp> {
/// {@macro ramp_shot_behavior}
RampShotBehavior({
required Points points,
@ -25,38 +23,27 @@ class RampShotBehavior extends Component
@visibleForTesting
RampShotBehavior.test({
required Points points,
required this.subscription,
}) : _points = points,
super();
final Points _points;
/// Subscription to [SpaceshipRampState] at [SpaceshipRamp].
@visibleForTesting
StreamSubscription? subscription;
@override
void onMount() {
super.onMount();
subscription = subscription ??
parent.bloc.stream.listen((state) {
final achievedOneMillionPoints = state.hits % 10 == 0;
if (!achievedOneMillionPoints) {
parent.add(
ScoringBehavior(
points: _points,
position: Vector2(0, -45),
),
);
}
});
}
@override
void onRemove() {
subscription?.cancel();
super.onRemove();
Future<void> onLoad() async {
await super.onLoad();
await add(
FlameBlocListener<SpaceshipRampCubit, SpaceshipRampState>(
listenWhen: (previousState, newState) =>
previousState.hits != newState.hits && newState.hits != 0,
onNewState: (state) {
parent.add(
ScoringBehavior(
points: _points,
position: Vector2(0, -45),
),
);
},
),
);
}
}

@ -16,7 +16,8 @@ class RampBallAscendingContactBehavior
if (other is! Ball) return;
if (other.body.linearVelocity.y < 0) {
parent.parent.bloc.onAscendingBallEntered();
readBloc<SpaceshipRampCubit, SpaceshipRampState>()
.onAscendingBallEntered();
}
}
}

Loading…
Cancel
Save