fix: fixed merge conflicts on component barrel

pull/40/head
RuiAlonso 4 years ago
commit 10da643c9b

@ -6,7 +6,6 @@ import 'package:pinball/game/game.dart';
///
/// The bottom [Component]s are the [Flipper]s and the [Baseboard]s.
/// {@endtemplate}
// TODO(alestiago): Add [SlingShot] once provided.
// TODO(alestiago): Consider renaming once entire Board is defined.
class BottomGroup extends Component {
/// {@macro bottom_group}
@ -70,7 +69,15 @@ class _BottomGroupSide extends Component {
Flipper.height,
),
);
final slingShot = SlingShot(
side: _side,
position: _position +
Vector2(
(Flipper.width) * direction,
Flipper.height + SlingShot.size.y,
),
);
await addAll([flipper, baseboard]);
await addAll([flipper, baseboard, slingShot]);
}
}

@ -4,6 +4,7 @@ export 'board.dart';
export 'board_side.dart';
export 'bonus_word.dart';
export 'flipper.dart';
export 'initial_position.dart';
export 'jetpack_ramp.dart';
export 'joint_anchor.dart';
export 'launcher_ramp.dart';

@ -0,0 +1,17 @@
import 'package:flame_forge2d/flame_forge2d.dart';
/// Forces a given [BodyComponent] to position their [body] to an
/// [initialPosition].
mixin InitialPosition<T extends Forge2DGame> on BodyComponent<T> {
/// The initial position of the [body].
late final Vector2 initialPosition;
@override
void onMount() {
super.onMount();
assert(
body.position == initialPosition,
'Body position is not equal to initial position.',
);
}
}

@ -33,18 +33,19 @@ class SlingShot extends BodyComponent {
/// left.
final BoardSide _side;
List<FixtureDef> _createFixtureDefs() {
final fixtures = <FixtureDef>[];
/// The size of the [SlingShot] body.
// TODO(alestiago): Use size from PositionedBodyComponent instead,
// once a sprite is given.
final size = Vector2(10, 10);
static final Vector2 size = Vector2(6, 8);
List<FixtureDef> _createFixtureDefs() {
final fixtures = <FixtureDef>[];
// TODO(alestiago): This magic number can be deduced by specifying the
// angle and using polar coordinate system to place the bottom right
// vertex.
// Something as: y = -size.y * math.cos(angle)
const additionalIncrement = 2;
const additionalIncrement = 3;
final triangleVertices = _side.isLeft
? [
Vector2(0, 0),
@ -78,7 +79,7 @@ class SlingShot extends BodyComponent {
);
// TODO(alestiago): Play with restitution value once game is bundled.
final kickerFixtureDef = FixtureDef(kicker)
..restitution = 20.0
..restitution = 10.0
..friction = 0;
fixtures.add(kickerFixtureDef);

@ -26,7 +26,7 @@ class Wall extends BodyComponent {
final fixtureDef = FixtureDef(shape)
..restitution = 0.1
..friction = 0.3;
..friction = 0;
final bodyDef = BodyDef()
..userData = this

@ -45,10 +45,10 @@ void main() {
await game.ready();
await game.ensureAdd(bottomGroup);
final leftFlippers = bottomGroup.findNestedChildren<Flipper>(
final rightFlippers = bottomGroup.findNestedChildren<Flipper>(
condition: (flipper) => flipper.side.isRight,
);
expect(leftFlippers.length, equals(1));
expect(rightFlippers.length, equals(1));
},
);
@ -59,8 +59,20 @@ void main() {
await game.ready();
await game.ensureAdd(bottomGroup);
final leftFlippers = bottomGroup.findNestedChildren<Baseboard>();
expect(leftFlippers.length, equals(2));
final baseboards = bottomGroup.findNestedChildren<Baseboard>();
expect(baseboards.length, equals(2));
},
);
flameTester.test(
'has two SlingShots',
(game) async {
final bottomGroup = BottomGroup(position: Vector2.zero(), spacing: 0);
await game.ready();
await game.ensureAdd(bottomGroup);
final slingShots = bottomGroup.findNestedChildren<SlingShot>();
expect(slingShots.length, equals(2));
},
);
});

@ -0,0 +1,66 @@
// ignore_for_file: cascade_invocations
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pinball/game/game.dart';
class TestBodyComponent extends BodyComponent with InitialPosition {
@override
Body createBody() {
return world.createBody(BodyDef());
}
}
class TestPositionedBodyComponent extends BodyComponent with InitialPosition {
@override
Body createBody() {
return world.createBody(BodyDef()..position = initialPosition);
}
}
void main() {
final flameTester = FlameTester(Forge2DGame.new);
group('InitialPosition', () {
test('correctly sets and gets', () {
final component = TestBodyComponent()..initialPosition = Vector2(1, 2);
expect(component.initialPosition, Vector2(1, 2));
});
test('can only be set once', () {
final component = TestBodyComponent()..initialPosition = Vector2(1, 2);
expect(
() => component.initialPosition = Vector2(3, 4),
throwsA(isA<Error>()),
);
});
flameTester.test(
'returns normally '
'when the body sets the position to initial position',
(game) async {
final component = TestPositionedBodyComponent()
..initialPosition = Vector2.zero();
await expectLater(
() async => game.ensureAdd(component),
returnsNormally,
);
},
);
flameTester.test(
'throws AssertionError '
'when not setting initialPosition to body',
(game) async {
final component = TestBodyComponent()..initialPosition = Vector2.zero();
await game.ensureAdd(component);
await expectLater(
() async => game.ensureAdd(component),
throwsAssertionError,
);
},
);
});
}

@ -106,7 +106,7 @@ void main() {
);
flameTester.test(
'has friction',
'has no friction',
(game) async {
final wall = Wall(
start: Vector2.zero(),
@ -115,7 +115,7 @@ void main() {
await game.ensureAdd(wall);
final fixture = wall.body.fixtures[0];
expect(fixture.friction, greaterThan(0));
expect(fixture.friction, equals(0));
},
);
});

Loading…
Cancel
Save