feat: made SlingShot use InitialPosition

pull/50/head
alestiago 4 years ago
parent b904ef27d7
commit 0c30540fd4

@ -69,12 +69,11 @@ class _BottomGroupSide extends Component {
); );
final slingShot = SlingShot( final slingShot = SlingShot(
side: _side, side: _side,
position: _position + )..initialPosition = _position +
Vector2( Vector2(
(Flipper.width) * direction, (Flipper.width) * direction,
Flipper.height + SlingShot.size.y, Flipper.height + SlingShot.size.y,
), );
);
await addAll([flipper, baseboard, slingShot]); await addAll([flipper, baseboard, slingShot]);
} }

@ -10,22 +10,17 @@ import 'package:pinball/game/game.dart';
/// ///
/// [SlingShot]s are usually positioned above each [Flipper]. /// [SlingShot]s are usually positioned above each [Flipper].
/// {@endtemplate sling_shot} /// {@endtemplate sling_shot}
class SlingShot extends BodyComponent { class SlingShot extends BodyComponent with InitialPosition {
/// {@macro sling_shot} /// {@macro sling_shot}
SlingShot({ SlingShot({
required Vector2 position,
required BoardSide side, required BoardSide side,
}) : _position = position, }) : _side = side {
_side = side {
// TODO(alestiago): Use sprite instead of color when provided. // TODO(alestiago): Use sprite instead of color when provided.
paint = Paint() paint = Paint()
..color = const Color(0xFF00FF00) ..color = const Color(0xFF00FF00)
..style = PaintingStyle.fill; ..style = PaintingStyle.fill;
} }
/// The initial position of the [SlingShot] body.
final Vector2 _position;
/// Whether the [SlingShot] is on the left or right side of the board. /// Whether the [SlingShot] is on the left or right side of the board.
/// ///
/// A [SlingShot] with [BoardSide.left] propels the [Ball] to the right, /// A [SlingShot] with [BoardSide.left] propels the [Ball] to the right,
@ -88,7 +83,7 @@ class SlingShot extends BodyComponent {
@override @override
Body createBody() { Body createBody() {
final bodyDef = BodyDef()..position = _position; final bodyDef = BodyDef()..position = initialPosition;
final body = world.createBody(bodyDef); final body = world.createBody(bodyDef);
_createFixtureDefs().forEach(body.createFixture); _createFixtureDefs().forEach(body.createFixture);

@ -13,9 +13,8 @@ void main() {
'loads correctly', 'loads correctly',
(game) async { (game) async {
final slingShot = SlingShot( final slingShot = SlingShot(
position: Vector2.zero(),
side: BoardSide.left, side: BoardSide.left,
); )..initialPosition = Vector2(0, 0);
await game.ensureAdd(slingShot); await game.ensureAdd(slingShot);
expect(game.contains(slingShot), isTrue); expect(game.contains(slingShot), isTrue);
@ -28,9 +27,8 @@ void main() {
(game) async { (game) async {
final position = Vector2.all(10); final position = Vector2.all(10);
final slingShot = SlingShot( final slingShot = SlingShot(
position: position,
side: BoardSide.left, side: BoardSide.left,
); )..initialPosition = position;
await game.ensureAdd(slingShot); await game.ensureAdd(slingShot);
expect(slingShot.body.position, equals(position)); expect(slingShot.body.position, equals(position));
@ -41,9 +39,8 @@ void main() {
'is static', 'is static',
(game) async { (game) async {
final slingShot = SlingShot( final slingShot = SlingShot(
position: Vector2.zero(),
side: BoardSide.left, side: BoardSide.left,
); )..initialPosition = Vector2(0, 0);
await game.ensureAdd(slingShot); await game.ensureAdd(slingShot);
expect(slingShot.body.bodyType, equals(BodyType.static)); expect(slingShot.body.bodyType, equals(BodyType.static));
@ -56,9 +53,8 @@ void main() {
'exists', 'exists',
(game) async { (game) async {
final slingShot = SlingShot( final slingShot = SlingShot(
position: Vector2.zero(),
side: BoardSide.left, side: BoardSide.left,
); )..initialPosition = Vector2(0, 0);
await game.ensureAdd(slingShot); await game.ensureAdd(slingShot);
expect(slingShot.body.fixtures[0], isA<Fixture>()); expect(slingShot.body.fixtures[0], isA<Fixture>());
@ -69,9 +65,8 @@ void main() {
'shape is triangular', 'shape is triangular',
(game) async { (game) async {
final slingShot = SlingShot( final slingShot = SlingShot(
position: Vector2.zero(),
side: BoardSide.left, side: BoardSide.left,
); )..initialPosition = Vector2(0, 0);
await game.ensureAdd(slingShot); await game.ensureAdd(slingShot);
final fixture = slingShot.body.fixtures[0]; final fixture = slingShot.body.fixtures[0];
@ -85,13 +80,11 @@ void main() {
'when side is left or right', 'when side is left or right',
(game) async { (game) async {
final leftSlingShot = SlingShot( final leftSlingShot = SlingShot(
position: Vector2.zero(),
side: BoardSide.left, side: BoardSide.left,
); )..initialPosition = Vector2(0, 0);
final rightSlingShot = SlingShot( final rightSlingShot = SlingShot(
position: Vector2.zero(),
side: BoardSide.right, side: BoardSide.right,
); )..initialPosition = Vector2(0, 0);
await game.ensureAdd(leftSlingShot); await game.ensureAdd(leftSlingShot);
await game.ensureAdd(rightSlingShot); await game.ensureAdd(rightSlingShot);
@ -109,9 +102,8 @@ void main() {
'has no friction', 'has no friction',
(game) async { (game) async {
final slingShot = SlingShot( final slingShot = SlingShot(
position: Vector2.zero(),
side: BoardSide.left, side: BoardSide.left,
); )..initialPosition = Vector2(0, 0);
await game.ensureAdd(slingShot); await game.ensureAdd(slingShot);
final fixture = slingShot.body.fixtures[0]; final fixture = slingShot.body.fixtures[0];
@ -125,9 +117,8 @@ void main() {
'exists', 'exists',
(game) async { (game) async {
final slingShot = SlingShot( final slingShot = SlingShot(
position: Vector2.zero(),
side: BoardSide.left, side: BoardSide.left,
); )..initialPosition = Vector2(0, 0);
await game.ensureAdd(slingShot); await game.ensureAdd(slingShot);
expect(slingShot.body.fixtures[1], isA<Fixture>()); expect(slingShot.body.fixtures[1], isA<Fixture>());
@ -138,9 +129,8 @@ void main() {
'shape is edge', 'shape is edge',
(game) async { (game) async {
final slingShot = SlingShot( final slingShot = SlingShot(
position: Vector2.zero(),
side: BoardSide.left, side: BoardSide.left,
); )..initialPosition = Vector2(0, 0);
await game.ensureAdd(slingShot); await game.ensureAdd(slingShot);
final fixture = slingShot.body.fixtures[1]; final fixture = slingShot.body.fixtures[1];
@ -152,9 +142,8 @@ void main() {
'has restitution', 'has restitution',
(game) async { (game) async {
final slingShot = SlingShot( final slingShot = SlingShot(
position: Vector2.zero(),
side: BoardSide.left, side: BoardSide.left,
); )..initialPosition = Vector2(0, 0);
await game.ensureAdd(slingShot); await game.ensureAdd(slingShot);
final fixture = slingShot.body.fixtures[1]; final fixture = slingShot.body.fixtures[1];
@ -166,9 +155,8 @@ void main() {
'has no friction', 'has no friction',
(game) async { (game) async {
final slingShot = SlingShot( final slingShot = SlingShot(
position: Vector2.zero(),
side: BoardSide.left, side: BoardSide.left,
); )..initialPosition = Vector2(0, 0);
await game.ensureAdd(slingShot); await game.ensureAdd(slingShot);
final fixture = slingShot.body.fixtures[1]; final fixture = slingShot.body.fixtures[1];

Loading…
Cancel
Save