feat: made Ball use InitialPosition

pull/50/head
alestiago 4 years ago
parent c4f6f46bfd
commit ffbf47d840

@ -6,14 +6,9 @@ import 'package:pinball/game/game.dart';
/// A solid, [BodyType.dynamic] sphere that rolls and bounces along the /// A solid, [BodyType.dynamic] sphere that rolls and bounces along the
/// [PinballGame]. /// [PinballGame].
/// {@endtemplate} /// {@endtemplate}
class Ball extends BodyComponent<PinballGame> { class Ball extends BodyComponent<PinballGame> with InitialPosition {
/// {@macro ball} /// {@macro ball}
Ball({ Ball();
required Vector2 position,
}) : _position = position;
/// The initial position of the [Ball] body.
final Vector2 _position;
/// The size of the [Ball] /// The size of the [Ball]
final Vector2 size = Vector2.all(2); final Vector2 size = Vector2.all(2);
@ -45,7 +40,7 @@ class Ball extends BodyComponent<PinballGame> {
final bodyDef = BodyDef() final bodyDef = BodyDef()
..userData = this ..userData = this
..position = Vector2(_position.x, _position.y + size.y) ..position = initialPosition
..type = BodyType.dynamic; ..type = BodyType.dynamic;
return world.createBody(bodyDef)..createFixture(fixtureDef); return world.createBody(bodyDef)..createFixture(fixtureDef);

@ -78,7 +78,11 @@ class PinballGame extends Forge2DGame
} }
void spawnBall() { void spawnBall() {
add(Ball(position: plunger.body.position)); final ball = Ball();
add(
ball
..initialPosition = plunger.body.position + Vector2(0, ball.size.y / 2),
);
} }
void _addContactCallbacks() { void _addContactCallbacks() {
@ -112,6 +116,8 @@ class DebugPinballGame extends PinballGame with TapDetector {
@override @override
void onTapUp(TapUpInfo info) { void onTapUp(TapUpInfo info) {
add(Ball(position: info.eventPosition.game)); add(
Ball()..initialPosition = info.eventPosition.game,
);
} }
} }

@ -17,7 +17,7 @@ void main() {
flameTester.test( flameTester.test(
'loads correctly', 'loads correctly',
(game) async { (game) async {
final ball = Ball(position: Vector2.zero()); final ball = Ball()..initialPosition = Vector2.all(0);
await game.ready(); await game.ready();
await game.ensureAdd(ball); await game.ensureAdd(ball);
@ -30,22 +30,18 @@ void main() {
'positions correctly', 'positions correctly',
(game) async { (game) async {
final position = Vector2.all(10); final position = Vector2.all(10);
final ball = Ball(position: position); final ball = Ball()..initialPosition = position;
await game.ensureAdd(ball); await game.ensureAdd(ball);
game.contains(ball); game.contains(ball);
final expectedPosition = Vector2( expect(ball.body.position, equals(position));
position.x,
position.y + ball.size.y,
);
expect(ball.body.position, equals(expectedPosition));
}, },
); );
flameTester.test( flameTester.test(
'is dynamic', 'is dynamic',
(game) async { (game) async {
final ball = Ball(position: Vector2.zero()); final ball = Ball()..initialPosition = Vector2.all(0);
await game.ensureAdd(ball); await game.ensureAdd(ball);
expect(ball.body.bodyType, equals(BodyType.dynamic)); expect(ball.body.bodyType, equals(BodyType.dynamic));
@ -57,7 +53,7 @@ void main() {
flameTester.test( flameTester.test(
'exists', 'exists',
(game) async { (game) async {
final ball = Ball(position: Vector2.zero()); final ball = Ball()..initialPosition = Vector2.all(0);
await game.ensureAdd(ball); await game.ensureAdd(ball);
expect(ball.body.fixtures[0], isA<Fixture>()); expect(ball.body.fixtures[0], isA<Fixture>());
@ -67,7 +63,7 @@ void main() {
flameTester.test( flameTester.test(
'is dense', 'is dense',
(game) async { (game) async {
final ball = Ball(position: Vector2.zero()); final ball = Ball()..initialPosition = Vector2.all(0);
await game.ensureAdd(ball); await game.ensureAdd(ball);
final fixture = ball.body.fixtures[0]; final fixture = ball.body.fixtures[0];
@ -78,7 +74,7 @@ void main() {
flameTester.test( flameTester.test(
'shape is circular', 'shape is circular',
(game) async { (game) async {
final ball = Ball(position: Vector2.zero()); final ball = Ball()..initialPosition = Vector2.all(0);
await game.ensureAdd(ball); await game.ensureAdd(ball);
final fixture = ball.body.fixtures[0]; final fixture = ball.body.fixtures[0];

@ -94,7 +94,7 @@ void main() {
final flipper = Flipper.fromSide( final flipper = Flipper.fromSide(
side: BoardSide.left, side: BoardSide.left,
)..initialPosition = Vector2.zero(); )..initialPosition = Vector2.zero();
final ball = Ball(position: Vector2.zero()); final ball = Ball()..initialPosition = Vector2.zero();
await game.ready(); await game.ready();
await game.ensureAddAll([flipper, ball]); await game.ensureAddAll([flipper, ball]);

Loading…
Cancel
Save