PR suggestions

pull/28/head
Erick Zanardo 4 years ago
parent 27308d1eaa
commit 7adca67e6d

@ -4,19 +4,15 @@ import 'dart:async';
import 'package:flame/input.dart'; import 'package:flame/input.dart';
import 'package:flame_bloc/flame_bloc.dart'; import 'package:flame_bloc/flame_bloc.dart';
import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
import 'package:pinball_theme/pinball_theme.dart'; import 'package:pinball_theme/pinball_theme.dart';
class PinballGame extends Forge2DGame class PinballGame extends Forge2DGame
with FlameBloc, HasKeyboardHandlerComponents, TapDetector { with FlameBloc, HasKeyboardHandlerComponents {
PinballGame({required this.theme, bool isDebugMode = kDebugMode}) : _isDebugMode = isDebugMode; PinballGame({required this.theme});
final PinballTheme theme; final PinballTheme theme;
final bool _isDebugMode;
// TODO(erickzanardo): Change to the plumber position // TODO(erickzanardo): Change to the plumber position
late final ballStartingPosition = screenToWorld( late final ballStartingPosition = screenToWorld(
Vector2( Vector2(
@ -108,11 +104,14 @@ class PinballGame extends Forge2DGame
), ),
); );
} }
}
class DebugPinballGame extends PinballGame with TapDetector {
DebugPinballGame({ required PinballTheme theme}) : super(theme: theme);
@override @override
void onTapUp(TapUpInfo info) { void onTapUp(TapUpInfo info) {
if (_isDebugMode) {
add(Ball(position: info.eventPosition.game)); add(Ball(position: info.eventPosition.game));
} }
}
} }

@ -1,6 +1,7 @@
// ignore_for_file: public_member_api_docs // ignore_for_file: public_member_api_docs
import 'package:flame/game.dart'; import 'package:flame/game.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
@ -29,9 +30,12 @@ class PinballGamePage extends StatelessWidget {
} }
class PinballGameView extends StatefulWidget { class PinballGameView extends StatefulWidget {
const PinballGameView({Key? key, required this.theme}) : super(key: key); const PinballGameView({Key? key, required this.theme, bool isDebugMode = kDebugMode})
: _isDebugMode = isDebugMode,
super(key: key);
final PinballTheme theme; final PinballTheme theme;
final bool _isDebugMode;
@override @override
State<PinballGameView> createState() => _PinballGameViewState(); State<PinballGameView> createState() => _PinballGameViewState();
@ -47,7 +51,8 @@ class _PinballGameViewState extends State<PinballGameView> {
// TODO(erickzanardo): Revisit this when we start to have more assets // TODO(erickzanardo): Revisit this when we start to have more assets
// this could expose a Stream (maybe even a cubit?) so we could show the // this could expose a Stream (maybe even a cubit?) so we could show the
// the loading progress with some fancy widgets. // the loading progress with some fancy widgets.
_game = PinballGame(theme: widget.theme)..preLoadAssets(); _game = (widget._isDebugMode ? DebugPinballGame(theme: widget.theme) : PinballGame(theme: widget.theme))
..preLoadAssets();
} }
@override @override

@ -12,9 +12,7 @@ void main() {
group('PinballGame', () { group('PinballGame', () {
TestWidgetsFlutterBinding.ensureInitialized(); TestWidgetsFlutterBinding.ensureInitialized();
final flameTester = FlameTester(PinballGameTest.create); final flameTester = FlameTester(PinballGameTest.create);
final debugModeFlameTester = FlameTester( final debugModeFlameTester = FlameTester(DebugPinballGame.new);
() => PinballGame(isDebugMode: true),
);
// TODO(alestiago): test if [PinballGame] registers // TODO(alestiago): test if [PinballGame] registers
// [BallScorePointsCallback] once the following issue is resolved: // [BallScorePointsCallback] once the following issue is resolved:

@ -100,5 +100,45 @@ void main() {
); );
}, },
); );
testWidgets('renders the real game when not in debug mode', (tester) async {
final gameBloc = MockGameBloc();
whenListen(
gameBloc,
Stream.value(const GameState.initial()),
initialState: const GameState.initial(),
);
await tester.pumpApp(
const PinballGameView(isDebugMode: false),
gameBloc: gameBloc,
);
expect(
find.byWidgetPredicate(
(w) => w is GameWidget<PinballGame> && w.game is! DebugPinballGame,
),
findsOneWidget,
);
});
testWidgets('renders the debug game when on debug mode', (tester) async {
final gameBloc = MockGameBloc();
whenListen(
gameBloc,
Stream.value(const GameState.initial()),
initialState: const GameState.initial(),
);
await tester.pumpApp(
const PinballGameView(),
gameBloc: gameBloc,
);
expect(
find.byWidgetPredicate(
(w) => w is GameWidget<PinballGame> && w.game is DebugPinballGame,
),
findsOneWidget,
);
});
}); });
} }

Loading…
Cancel
Save