feat: defined DebugGameBallsController

pull/151/head
alestiago 4 years ago
parent f20c2231a0
commit 47ef35d34b

@ -33,7 +33,7 @@ abstract class ComponentController<T extends Component> extends Component {
/// Mixin that attaches a single [ComponentController] to a [Component].
mixin Controls<T extends ComponentController> on Component {
/// The [ComponentController] attached to this [Component].
late final T controller;
late T controller;
@override
@mustCallSuper

@ -33,7 +33,7 @@ class ControlledBall extends Ball with Controls<BallController> {
/// [Ball] used in [DebugPinballGame].
ControlledBall.debug() : super(baseColor: const Color(0xFFFF0000)) {
controller = _DebugBallController(this);
controller = DebugBallController(this);
}
}
@ -57,8 +57,10 @@ class BallController extends ComponentController<Ball>
}
}
class _DebugBallController extends BallController {
_DebugBallController(Ball<Forge2DGame> component) : super(component);
/// {@macro ball_controller}
class DebugBallController extends BallController {
/// {@macro ball_controller}
DebugBallController(Ball<Forge2DGame> component) : super(component);
@override
void lost() {

@ -142,7 +142,9 @@ class DebugPinballGame extends PinballGame with TapDetector {
}) : super(
theme: theme,
audio: audio,
);
) {
controller = _DebugGameBallsController(this);
}
@override
Future<void> onLoad() async {
@ -174,3 +176,21 @@ class DebugPinballGame extends PinballGame with TapDetector {
);
}
}
class _DebugGameBallsController extends _GameBallsController {
_DebugGameBallsController(PinballGame game) : super(game);
@override
bool listenWhen(GameState? previousState, GameState newState) {
final noBallsLeft = component
.descendants()
.whereType<ControlledBall>()
.where(
(ball) => ball.controller is! DebugBallController,
)
.isEmpty;
final canBallRespawn = newState.balls > 0;
return noBallsLeft && canBallRespawn;
}
}

@ -11,11 +11,11 @@ import 'package:pinball_components/pinball_components.dart';
import '../helpers/helpers.dart';
void main() {
group('PinballGame', () {
TestWidgetsFlutterBinding.ensureInitialized();
final flameTester = FlameTester(PinballGameTest.new);
final debugModeFlameTester = FlameTester(DebugPinballGameTest.new);
TestWidgetsFlutterBinding.ensureInitialized();
final flameTester = FlameTester(PinballGameTest.new);
final debugModeFlameTester = FlameTester(DebugPinballGameTest.new);
group('PinballGame', () {
// TODO(alestiago): test if [PinballGame] registers
// [BallScorePointsCallback] once the following issue is resolved:
// https://github.com/flame-engine/flame/issues/1416
@ -139,7 +139,9 @@ void main() {
);
});
});
});
group('DebugPinballGame', () {
debugModeFlameTester.test('adds a ball on tap up', (game) async {
await game.ready();
@ -159,5 +161,23 @@ void main() {
equals(previousBalls.length + 1),
);
});
group('controller', () {
debugModeFlameTester.test(
'ignores debug balls',
(game) async {
final newState = MockGameState();
when(() => newState.balls).thenReturn(2);
game.descendants().whereType<Ball>().forEach(game.remove);
await game.ready();
await game.ensureAdd(ControlledBall.debug());
expect(
game.controller.listenWhen(MockGameState(), newState),
isTrue,
);
},
);
});
});
}

Loading…
Cancel
Save