mirror of https://github.com/flutter/pinball.git
parent
40526bf0fe
commit
ff909394ae
@ -1,40 +1,26 @@
|
|||||||
import 'package:flame/components.dart';
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flame_bloc/flame_bloc.dart';
|
||||||
import 'package:pinball/game/game.dart';
|
import 'package:pinball/game/game.dart';
|
||||||
import 'package:pinball_components/pinball_components.dart';
|
import 'package:pinball_components/pinball_components.dart';
|
||||||
import 'package:pinball_flame/pinball_flame.dart';
|
import 'package:pinball_flame/pinball_flame.dart';
|
||||||
|
|
||||||
/// Toggle each [Multiball] when there is a bonus ball.
|
/// Toggle each [Multiball] when there is a bonus ball.
|
||||||
class MultiballsBehavior extends Component
|
class MultiballsBehavior extends Component
|
||||||
with HasGameRef<PinballGame>, ParentIsA<Multiballs> {
|
with
|
||||||
|
HasGameRef<PinballGame>,
|
||||||
|
ParentIsA<Multiballs>,
|
||||||
|
BlocComponent<GameBloc, GameState> {
|
||||||
@override
|
@override
|
||||||
void onMount() {
|
bool listenWhen(GameState? previousState, GameState newState) {
|
||||||
super.onMount();
|
final hasMultiball = newState.bonusHistory.contains(GameBonus.dashNest);
|
||||||
|
final hasChanged = previousState?.bonusHistory != newState.bonusHistory;
|
||||||
var _previousMultiballBonus = 0;
|
return hasChanged && hasMultiball;
|
||||||
|
}
|
||||||
gameRef.read<GameBloc>().stream.listen((state) {
|
|
||||||
// TODO(ruimiguel): only when state.bonusHistory dashNest has changed
|
|
||||||
final multiballBonus = state.bonusHistory.fold<int>(
|
|
||||||
0,
|
|
||||||
(previousValue, bonus) {
|
|
||||||
if (bonus == GameBonus.dashNest) {
|
|
||||||
previousValue++;
|
|
||||||
}
|
|
||||||
return previousValue;
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
if (_previousMultiballBonus != multiballBonus) {
|
|
||||||
_previousMultiballBonus = multiballBonus;
|
|
||||||
|
|
||||||
final hasMultiball = state.bonusHistory.contains(GameBonus.dashNest);
|
|
||||||
|
|
||||||
if (hasMultiball) {
|
@override
|
||||||
parent.children.whereType<Multiball>().forEach((multiball) {
|
void onNewState(GameState state) {
|
||||||
multiball.bloc.onAnimate();
|
parent.children.whereType<Multiball>().forEach((multiball) {
|
||||||
});
|
multiball.bloc.onAnimate();
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue