feat: made JointAnchor use InitialPosition

pull/50/head
alestiago 4 years ago
parent 04daf13271
commit eec4684cce

@ -238,14 +238,14 @@ class FlipperAnchor extends JointAnchor {
/// {@macro flipper_anchor}
FlipperAnchor({
required Flipper flipper,
}) : super(
position: Vector2(
flipper.side.isLeft
? flipper.body.position.x - Flipper.width / 2
: flipper.body.position.x + Flipper.width / 2,
flipper.body.position.y,
),
);
}) {
initialPosition = Vector2(
flipper.side.isLeft
? flipper.body.position.x - Flipper.width / 2
: flipper.body.position.x + Flipper.width / 2,
flipper.body.position.y,
);
}
}
/// {@template flipper_anchor_revolute_joint_def}

@ -1,11 +1,12 @@
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:pinball/game/game.dart';
/// {@template joint_anchor}
/// Non visual [BodyComponent] used to hold a [BodyType.dynamic] in [Joint]s
/// with this [BodyType.static].
///
/// It is recommended to [_position] the anchor first and then use the body
/// position as the anchor point when initializing a [JointDef].
/// It is recommended to use [JointAnchor.body.position] to position the anchor
/// point when initializing a [JointDef].
///
/// ```dart
/// initialize(
@ -15,17 +16,13 @@ import 'package:flame_forge2d/flame_forge2d.dart';
/// );
/// ```
/// {@endtemplate}
class JointAnchor extends BodyComponent {
class JointAnchor extends BodyComponent with InitialPosition {
/// {@macro joint_anchor}
JointAnchor({
required Vector2 position,
}) : _position = position;
final Vector2 _position;
JointAnchor();
@override
Body createBody() {
final bodyDef = BodyDef()..position = _position;
final bodyDef = BodyDef()..position = initialPosition;
return world.createBody(bodyDef);
}

@ -98,12 +98,12 @@ class PlungerAnchor extends JointAnchor {
/// {@macro plunger_anchor}
PlungerAnchor({
required Plunger plunger,
}) : super(
position: Vector2(
plunger.body.position.x,
plunger.body.position.y - plunger.compressionDistance,
),
);
}) {
initialPosition = Vector2(
plunger.body.position.x,
plunger.body.position.y - plunger.compressionDistance,
);
}
}
/// {@template plunger_anchor_prismatic_joint_def}

@ -13,7 +13,7 @@ void main() {
flameTester.test(
'loads correctly',
(game) async {
final anchor = JointAnchor(position: Vector2.zero());
final anchor = JointAnchor()..initialPosition = Vector2.zero();
await game.ready();
await game.ensureAdd(anchor);
@ -27,7 +27,7 @@ void main() {
(game) async {
await game.ready();
final position = Vector2.all(10);
final anchor = JointAnchor(position: position);
final anchor = JointAnchor()..initialPosition = position;
await game.ensureAdd(anchor);
game.contains(anchor);
@ -39,7 +39,7 @@ void main() {
'is static',
(game) async {
await game.ready();
final anchor = JointAnchor(position: Vector2.zero());
final anchor = JointAnchor()..initialPosition = Vector2.zero();
await game.ensureAdd(anchor);
expect(anchor.body.bodyType, equals(BodyType.static));
@ -51,7 +51,7 @@ void main() {
flameTester.test(
'has none',
(game) async {
final anchor = JointAnchor(position: Vector2.zero());
final anchor = JointAnchor()..initialPosition = Vector2.zero();
await game.ensureAdd(anchor);
expect(anchor.body.fixtures, isEmpty);

Loading…
Cancel
Save