feat: added previewline and ball booster in DebugGame

pull/127/head
RuiAlonso 4 years ago
parent e6fd5f90fa
commit 6aaac0a942

@ -6,6 +6,7 @@ import 'package:flame/extensions.dart';
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/material.dart';
import 'package:pinball/game/game.dart'; import 'package:pinball/game/game.dart';
import 'package:pinball/gen/assets.gen.dart'; import 'package:pinball/gen/assets.gen.dart';
import 'package:pinball_components/pinball_components.dart' hide Assets; import 'package:pinball_components/pinball_components.dart' hide Assets;
@ -109,11 +110,16 @@ class PinballGame extends Forge2DGame
} }
} }
class DebugPinballGame extends PinballGame with TapDetector { class DebugPinballGame extends PinballGame with TapDetector, PanDetector {
DebugPinballGame({required PinballTheme theme}) : super(theme: theme); DebugPinballGame({required PinballTheme theme}) : super(theme: theme);
Vector2? lineStart;
Vector2? lineEnd;
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
unawaited(add(PreviewLine()));
await super.onLoad(); await super.onLoad();
await _loadBackground(); await _loadBackground();
} }
@ -141,4 +147,63 @@ class DebugPinballGame extends PinballGame with TapDetector {
ControlledBall.debug()..initialPosition = info.eventPosition.game, ControlledBall.debug()..initialPosition = info.eventPosition.game,
); );
} }
@override
void onPanStart(DragStartInfo info) {
lineStart = info.eventPosition.game;
}
@override
void onPanUpdate(DragUpdateInfo info) {
lineEnd = info.eventPosition.game;
}
@override
void onPanEnd(DragEndInfo info) {
if (lineEnd != null) {
final line = lineEnd! - lineStart!;
onLine(line);
lineEnd = null;
lineStart = null;
}
}
void onLine(Vector2 line) {
final ball = ControlledBall.debug()..initialPosition = lineStart!;
add(ball);
ball.mounted.then((value) => ball.boost(line * 20));
}
}
class PreviewLine extends PositionComponent with HasGameRef<DebugPinballGame> {
static final _previewLinePaint = Paint()
..color = Colors.pink
..strokeWidth = 0.2
..style = PaintingStyle.stroke;
Vector2? lineStart;
Vector2? lineEnd;
@override
void update(double dt) {
super.update(dt);
lineEnd = gameRef.lineEnd?.clone()?..multiply(Vector2(1, -1));
}
@override
void render(Canvas canvas) {
super.render(canvas);
if (lineEnd != null) {
lineStart = gameRef.lineStart?.clone()?..multiply(Vector2(1, -1));
canvas.drawLine(
lineStart!.toOffset(),
lineEnd!.toOffset(),
_previewLinePaint,
);
}
}
} }

@ -11,6 +11,6 @@ class BallBoosterExample extends LineGame {
final ball = Ball(baseColor: Colors.transparent); final ball = Ball(baseColor: Colors.transparent);
add(ball); add(ball);
ball.mounted.then((value) => ball.boost(line * -1 * 20)); ball.mounted.then((value) => ball.boost(line * 20));
} }
} }

@ -13,7 +13,6 @@ void main() {
group('PinballGame', () { group('PinballGame', () {
TestWidgetsFlutterBinding.ensureInitialized(); TestWidgetsFlutterBinding.ensureInitialized();
final flameTester = FlameTester(PinballGameTest.create); final flameTester = FlameTester(PinballGameTest.create);
final debugModeFlameTester = FlameTester(DebugPinballGameTest.create);
// 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:
@ -61,6 +60,10 @@ void main() {
); );
}); });
}); });
});
group('DebugPinballGame', () {
final debugModeFlameTester = FlameTester(DebugPinballGameTest.create);
debugModeFlameTester.test('adds a ball on tap up', (game) async { debugModeFlameTester.test('adds a ball on tap up', (game) async {
await game.ready(); await game.ready();
@ -79,5 +82,48 @@ void main() {
equals(1), equals(1),
); );
}); });
debugModeFlameTester.test('adds a ball on pan detection', (game) async {
await game.ready();
final eventStartPosition = MockEventPosition();
when(() => eventStartPosition.game).thenReturn(Vector2.all(10));
final dragStartInfo = MockDragStartInfo();
when(() => dragStartInfo.eventPosition).thenReturn(eventStartPosition);
final eventUpdatePosition = MockEventPosition();
when(() => eventUpdatePosition.game).thenReturn(Vector2.all(20));
final dragUpdateInfo = MockDragUpdateInfo();
when(() => dragUpdateInfo.eventPosition).thenReturn(eventUpdatePosition);
final dragEndInfo = MockDragEndInfo();
game.onPanStart(dragStartInfo);
game.onPanUpdate(dragUpdateInfo);
game.onPanEnd(dragEndInfo);
await game.ready();
final balls = game.children.whereType<Ball>();
expect(
balls.length,
equals(1),
);
});
group('PreviewLine', () {
debugModeFlameTester.test(
'loads correctly',
(game) async {
final previewLine = PreviewLine();
await game.ready();
await game.ensureAdd(previewLine);
expect(game.contains(previewLine), isTrue);
},
);
});
}); });
} }

@ -58,6 +58,12 @@ class MockRawKeyUpEvent extends Mock implements RawKeyUpEvent {
class MockTapUpInfo extends Mock implements TapUpInfo {} class MockTapUpInfo extends Mock implements TapUpInfo {}
class MockDragStartInfo extends Mock implements DragStartInfo {}
class MockDragUpdateInfo extends Mock implements DragUpdateInfo {}
class MockDragEndInfo extends Mock implements DragEndInfo {}
class MockEventPosition extends Mock implements EventPosition {} class MockEventPosition extends Mock implements EventPosition {}
class MockBonusLetter extends Mock implements BonusLetter {} class MockBonusLetter extends Mock implements BonusLetter {}

Loading…
Cancel
Save