mirror of https://github.com/flutter/pinball.git
feat: spawned ScoreText on Ball's position (#210)
parent
b81293c0a4
commit
9615d6105f
@ -1,45 +0,0 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame_bloc/flame_bloc.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
import 'package:pinball_flame/pinball_flame.dart';
|
||||
|
||||
/// {@template score_effect_controller}
|
||||
/// A [ComponentController] responsible for adding [ScoreText]s
|
||||
/// on the game screen when the user earns points.
|
||||
/// {@endtemplate}
|
||||
class ScoreEffectController extends ComponentController<PinballGame>
|
||||
with BlocComponent<GameBloc, GameState> {
|
||||
/// {@macro score_effect_controller}
|
||||
ScoreEffectController(PinballGame component) : super(component);
|
||||
|
||||
int _lastScore = 0;
|
||||
final _random = Random();
|
||||
|
||||
double _noise() {
|
||||
return _random.nextDouble() * 5 * (_random.nextBool() ? -1 : 1);
|
||||
}
|
||||
|
||||
@override
|
||||
bool listenWhen(GameState? previousState, GameState newState) {
|
||||
return previousState?.score != newState.score;
|
||||
}
|
||||
|
||||
@override
|
||||
void onNewState(GameState state) {
|
||||
final newScore = state.score - _lastScore;
|
||||
_lastScore = state.score;
|
||||
|
||||
component.add(
|
||||
ScoreText(
|
||||
text: newScore.toString(),
|
||||
position: Vector2(
|
||||
_noise(),
|
||||
_noise() + (BoardDimensions.bounds.topCenter.dy + 10),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -1,105 +0,0 @@
|
||||
// ignore_for_file: cascade_invocations
|
||||
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
import 'package:pinball/game/game.dart';
|
||||
import 'package:pinball_components/pinball_components.dart';
|
||||
|
||||
import '../../helpers/helpers.dart';
|
||||
|
||||
void main() {
|
||||
group('ScoreEffectController', () {
|
||||
late ScoreEffectController controller;
|
||||
late PinballGame game;
|
||||
|
||||
setUpAll(() {
|
||||
registerFallbackValue(Component());
|
||||
});
|
||||
|
||||
setUp(() {
|
||||
game = MockPinballGame();
|
||||
when(() => game.add(any())).thenAnswer((_) async {});
|
||||
|
||||
controller = ScoreEffectController(game);
|
||||
});
|
||||
|
||||
group('listenWhen', () {
|
||||
test('returns true when the user has earned points', () {
|
||||
const previous = GameState.initial();
|
||||
const current = GameState(
|
||||
score: 10,
|
||||
balls: 3,
|
||||
bonusHistory: [],
|
||||
);
|
||||
expect(controller.listenWhen(previous, current), isTrue);
|
||||
});
|
||||
|
||||
test(
|
||||
'returns true when the user has earned points and there was no '
|
||||
'previous state',
|
||||
() {
|
||||
const current = GameState(
|
||||
score: 10,
|
||||
balls: 3,
|
||||
bonusHistory: [],
|
||||
);
|
||||
expect(controller.listenWhen(null, current), isTrue);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'returns false when no points were earned',
|
||||
() {
|
||||
const current = GameState.initial();
|
||||
const previous = GameState.initial();
|
||||
expect(controller.listenWhen(previous, current), isFalse);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('onNewState', () {
|
||||
test(
|
||||
'adds a ScoreText with the correct score for the '
|
||||
'first time',
|
||||
() {
|
||||
const state = GameState(
|
||||
score: 10,
|
||||
balls: 3,
|
||||
bonusHistory: [],
|
||||
);
|
||||
|
||||
controller.onNewState(state);
|
||||
|
||||
final effect =
|
||||
verify(() => game.add(captureAny())).captured.first as ScoreText;
|
||||
|
||||
expect(effect.text, equals('10'));
|
||||
},
|
||||
);
|
||||
|
||||
test('adds a ScoreTextEffect with the correct score', () {
|
||||
controller.onNewState(
|
||||
const GameState(
|
||||
score: 10,
|
||||
balls: 3,
|
||||
bonusHistory: [],
|
||||
),
|
||||
);
|
||||
|
||||
controller.onNewState(
|
||||
const GameState(
|
||||
score: 14,
|
||||
balls: 3,
|
||||
bonusHistory: [],
|
||||
),
|
||||
);
|
||||
|
||||
final effect =
|
||||
verify(() => game.add(captureAny())).captured.last as ScoreText;
|
||||
|
||||
expect(effect.text, equals('4'));
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
Loading…
Reference in new issue