feat: added a new Ball on new state

pull/89/head
alestiago 4 years ago
parent 12c8ca85e4
commit 674f4a1733

@ -20,13 +20,9 @@ class BonusWord extends Component with BlocComponent<GameBloc, GameState> {
@override @override
bool listenWhen(GameState? previousState, GameState newState) { bool listenWhen(GameState? previousState, GameState newState) {
if ((previousState?.bonusHistory.length ?? 0) < return (previousState?.bonusHistory.length ?? 0) <
newState.bonusHistory.length && newState.bonusHistory.length &&
newState.bonusHistory.last == GameBonus.word) { newState.bonusHistory.last == GameBonus.word;
return true;
}
return false;
} }
@override @override

@ -13,7 +13,8 @@ import 'package:pinball/game/game.dart';
/// is awarded, and the [_BigDashNestBumper] releases a new [Ball]. /// is awarded, and the [_BigDashNestBumper] releases a new [Ball].
/// {@endtemplate} /// {@endtemplate}
// TODO(alestiago): Make a [Blueprint] once nesting [Blueprint] is implemented. // TODO(alestiago): Make a [Blueprint] once nesting [Blueprint] is implemented.
class FlutterForest extends Component with HasGameRef<PinballGame> { class FlutterForest extends Component
with HasGameRef<PinballGame>, BlocComponent<GameBloc, GameState> {
/// {@macro flutter_forest} /// {@macro flutter_forest}
FlutterForest({ FlutterForest({
required this.position, required this.position,
@ -22,6 +23,19 @@ class FlutterForest extends Component with HasGameRef<PinballGame> {
/// The position of the [FlutterForest] on the [Board]. /// The position of the [FlutterForest] on the [Board].
final Vector2 position; final Vector2 position;
@override
bool listenWhen(GameState? previousState, GameState newState) {
return (previousState?.bonusHistory.length ?? 0) <
newState.bonusHistory.length &&
newState.bonusHistory.last == GameBonus.dashNest;
}
@override
void onNewState(GameState state) {
super.onNewState(state);
gameRef.add(Ball()..initialPosition = position);
}
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
gameRef.addContactCallback(_DashNestBumperBallContactCallback()); gameRef.addContactCallback(_DashNestBumperBallContactCallback());
@ -48,7 +62,7 @@ class FlutterForest extends Component with HasGameRef<PinballGame> {
/// When hit, the [GameState.score] is increased. /// When hit, the [GameState.score] is increased.
/// {@endtemplate} /// {@endtemplate}
abstract class _DashNestBumper extends BodyComponent<PinballGame> abstract class _DashNestBumper extends BodyComponent<PinballGame>
with BlocComponent<GameBloc, GameState>, ScorePoints, InitialPosition { with ScorePoints, InitialPosition {
/// {@template dash_nest_bumper} /// {@template dash_nest_bumper}
_DashNestBumper({required this.id}); _DashNestBumper({required this.id});

@ -5,11 +5,13 @@ import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
import '../../helpers/helpers.dart';
void main() { void main() {
TestWidgetsFlutterBinding.ensureInitialized(); TestWidgetsFlutterBinding.ensureInitialized();
group('FlutterForest', () { group('FlutterForest', () {
final flameTester = FlameTester(Forge2DGame.new); final flameTester = FlameTester(PinballGameTest.create);
flameTester.test( flameTester.test(
'loads correctly', 'loads correctly',
@ -21,5 +23,23 @@ void main() {
expect(game.contains(flutterForest), isTrue); expect(game.contains(flutterForest), isTrue);
}, },
); );
flameTester.test(
'adds a new ball onNewState',
(game) async {
await game.ready();
final flutterForest = FlutterForest(position: Vector2(0, 0));
await game.ensureAdd(flutterForest);
final previousBalls = game.descendants().whereType<Ball>().length;
flutterForest.onNewState(MockGameState());
await game.ready();
expect(
game.descendants().whereType<Ball>().length,
greaterThan(previousBalls),
);
},
);
}); });
} }

Loading…
Cancel
Save