mirror of https://github.com/flutter/pinball.git
parent
4c0d33596d
commit
c5865a4252
@ -1,29 +0,0 @@
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
part 'flutter_forest_state.dart';
|
||||
|
||||
// TODO(alestiago): Evaluate if there is any useful documentation that could
|
||||
// be added to this class.
|
||||
// ignore: public_member_api_docs
|
||||
class FlutterForestCubit extends Cubit<FlutterForestState> {
|
||||
// ignore: public_member_api_docs
|
||||
FlutterForestCubit() : super(const FlutterForestState.initial());
|
||||
|
||||
/// Event added when a bumper contacts with a ball.
|
||||
void onBumperActivated(DashNestBumper dashNestBumper) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
activatedBumpers: state.activatedBumpers.union({dashNestBumper}),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Event added after the bonus is activated.
|
||||
void onBonusApplied() {
|
||||
emit(
|
||||
state.copyWith(activatedBumpers: const {}),
|
||||
);
|
||||
}
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
// ignore_for_file: public_member_api_docs
|
||||
|
||||
part of 'flutter_forest_cubit.dart';
|
||||
|
||||
class FlutterForestState extends Equatable {
|
||||
const FlutterForestState({
|
||||
required this.activatedBumpers,
|
||||
});
|
||||
|
||||
const FlutterForestState.initial() : this(activatedBumpers: const {});
|
||||
|
||||
final Set<DashNestBumper> activatedBumpers;
|
||||
|
||||
bool get hasBonus => activatedBumpers.length == 3;
|
||||
|
||||
FlutterForestState copyWith({
|
||||
Set<DashNestBumper>? activatedBumpers,
|
||||
}) {
|
||||
return FlutterForestState(
|
||||
activatedBumpers: activatedBumpers ?? this.activatedBumpers,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object> get props => [activatedBumpers];
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:bloc_test/bloc_test.dart';
|
||||
import 'package:flame_test/flame_test.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:pinball/game/components/flutter_forest/behaviors/behaviors.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
import '../../../../helpers/helpers.dart';
|
||||
|
||||
void main() {
|
||||
group('FlutterForestBonusBehavior', () {
|
||||
late GameBloc gameBloc;
|
||||
|
||||
setUp(() {
|
||||
gameBloc = MockGameBloc();
|
||||
whenListen(
|
||||
gameBloc,
|
||||
const Stream<GameState>.empty(),
|
||||
initialState: const GameState.initial(),
|
||||
);
|
||||
});
|
||||
|
||||
final flameBlocTester = FlameBlocTester<PinballGame, GameBloc>(
|
||||
gameBuilder: EmptyPinballTestGame.new,
|
||||
blocBuilder: () => gameBloc,
|
||||
);
|
||||
|
||||
flameBlocTester.testGameWidget(
|
||||
'adds GameBonus.dashNest to the game when all bumpers are active',
|
||||
setUp: (game, tester) async {
|
||||
final behavior = FlutterForestBonusBehavior();
|
||||
final parent = FlutterForest.test();
|
||||
final bumpers = [
|
||||
DashNestBumper.test(),
|
||||
DashNestBumper.test(),
|
||||
DashNestBumper.test(),
|
||||
];
|
||||
await parent.addAll(bumpers);
|
||||
await game.ensureAdd(parent);
|
||||
await parent.ensureAdd(behavior);
|
||||
|
||||
for (final bumper in bumpers) {
|
||||
bumper.bloc.onBallContacted();
|
||||
}
|
||||
await tester.pump();
|
||||
|
||||
verify(
|
||||
() => gameBloc.add(const BonusActivated(GameBonus.dashNest)),
|
||||
).called(1);
|
||||
},
|
||||
);
|
||||
|
||||
flameBlocTester.testGameWidget(
|
||||
'adds a new ball to the game when all bumpers are active',
|
||||
setUp: (game, tester) async {
|
||||
final behavior = FlutterForestBonusBehavior();
|
||||
final parent = FlutterForest.test();
|
||||
final bumpers = [
|
||||
DashNestBumper.test(),
|
||||
DashNestBumper.test(),
|
||||
DashNestBumper.test(),
|
||||
];
|
||||
await parent.addAll(bumpers);
|
||||
await game.ensureAdd(parent);
|
||||
await parent.ensureAdd(behavior);
|
||||
|
||||
for (final bumper in bumpers) {
|
||||
bumper.bloc.onBallContacted();
|
||||
}
|
||||
await game.ready();
|
||||
|
||||
expect(
|
||||
game.descendants().whereType<Ball>().single,
|
||||
isNotNull,
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
Loading…
Reference in new issue