feat: applying suggestions

pull/9/head
Erick Zanardo 2 years ago
parent 9fc554927d
commit 5a2ee2c2ac

@ -29,13 +29,12 @@ class Ball extends BodyComponent<PinballGame>
return world.createBody(bodyDef)..createFixture(fixtureDef);
}
void ballLost() {
final bloc = gameRef.read<GameBloc>();
void lost() {
shouldRemove = true;
final shouldBallRespwan = !bloc.state.isLastBall;
bloc.add(const BallLost());
final bloc = gameRef.read<GameBloc>()..add(const BallLost());
final shouldBallRespwan = !bloc.state.isLastBall;
if (shouldBallRespwan) {
gameRef.spawnBall();
}

@ -3,32 +3,14 @@
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:pinball/game/components/components.dart';
enum WallType {
fatal,
passive,
}
class Wall extends BodyComponent {
Wall({
required this.start,
required this.end,
this.type = WallType.passive,
});
factory Wall.bottom(Forge2DGame game) {
final bottomRight = game.screenToWorld(game.camera.viewport.effectiveSize);
final bottomLeft = Vector2(0, bottomRight.y);
return Wall(
type: WallType.fatal,
start: bottomRight,
end: bottomLeft,
);
}
final Vector2 start;
final Vector2 end;
final WallType type;
@override
Body createBody() {
@ -47,14 +29,21 @@ class Wall extends BodyComponent {
}
}
class BallWallContactCallback extends ContactCallback<Ball, Wall> {
class BottomWall extends Wall {
BottomWall(Forge2DGame game)
: super(
start: game.screenToWorld(game.camera.viewport.effectiveSize),
end: Vector2(
0,
game.screenToWorld(game.camera.viewport.effectiveSize).y,
),
);
}
class BottomWallBallContactCallback extends ContactCallback<Ball, BottomWall> {
@override
void begin(Ball ball, Wall wall, Contact contact) {
if (wall.type == WallType.fatal) {
ball
..ballLost()
..shouldRemove = true;
}
void begin(Ball ball, BottomWall wall, Contact contact) {
ball.lost();
}
@override

@ -21,7 +21,7 @@ class PinballGame extends Forge2DGame with FlameBloc {
spawnBall();
addContactCallback(BallScorePointsCallback());
await add(Wall.bottom(this));
addContactCallback(BallWallContactCallback());
await add(BottomWall(this));
addContactCallback(BottomWallBallContactCallback());
}
}

@ -107,7 +107,7 @@ void main() {
(game, tester) async {
await game.ready();
game.children.whereType<Ball>().first.ballLost();
game.children.whereType<Ball>().first.lost();
await tester.pump();
verify(() => gameBloc.add(const BallLost())).called(1);

@ -12,24 +12,23 @@ void main() {
TestWidgetsFlutterBinding.ensureInitialized();
group('Wall', () {
group('BallWallContactCallback', () {
group('BottomWallBallContactCallback', () {
test(
'removes the ball on begin contact when the wall is a fatal one',
'removes the ball on begin contact when the wall is a bottom one',
() {
final game = MockPinballGame();
final wall = MockWall();
final wall = MockBottomWall();
final ball = MockBall();
when(() => wall.type).thenReturn(WallType.fatal);
when(() => ball.gameRef).thenReturn(game);
BallWallContactCallback()
BottomWallBallContactCallback()
// Remove once https://github.com/flame-engine/flame/pull/1415
// is merged
..end(MockBall(), MockWall(), MockContact())
..end(MockBall(), MockBottomWall(), MockContact())
..begin(ball, wall, MockContact());
verify(() => ball.shouldRemove = true).called(1);
verify(ball.lost).called(1);
},
);
});

@ -6,6 +6,8 @@ class MockPinballGame extends Mock implements PinballGame {}
class MockWall extends Mock implements Wall {}
class MockBottomWall extends Mock implements BottomWall {}
class MockBall extends Mock implements Ball {}
class MockContact extends Mock implements Contact {}

Loading…
Cancel
Save