Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow for bodyDef and fixtureDefs to be prepared earlier #2768

Merged
merged 4 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions doc/bridge_packages/flame_forge2d/forge2d.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,13 @@ A simple `Forge2DGame` implementation example can be seen in the
## BodyComponent

The `BodyComponent` is a wrapper for the `Forge2D` body, which is the body that the physics engine
is interacting with. To create a `BodyComponent` you need to override `createBody()` and create and
return your created body.
is interacting with. To create a `BodyComponent` you can either:

- override `createBody()` and create and return your created body;
- use the default `createBody()` implementation by passing a `BodyDef` instance (and optionally a
list of `FixtureDef` instances) to the BodyComponent's constructor;
- use the default `createBody()` implementation and assign a `BodyDef` instance to `this.bodyDef`,
and optionally a list of `FixtureDef` instances to `this.fixtureDefs`.

The `BodyComponent` is by default having `renderBody = true`, since otherwise, it wouldn't show
anything after you have created a `Body` and added the `BodyComponent` to the game. If you want to
Expand All @@ -61,7 +66,7 @@ turn it off you can just set (or override) `renderBody` to false.
Just like any other Flame component you can add children to the `BodyComponent`, which can be very
useful if you want to add for example animations or other components on top of your body.

The body that you create in `createBody` should be defined according to Flame's coordinate system,
The body that you create should be defined according to Flame's coordinate system,
not according to the coordinate system of Forge2D (where the Y-axis is flipped).

:exclamation: In Forge2D you shouldn't add any bodies as children to other components,
Expand Down
41 changes: 15 additions & 26 deletions packages/flame_forge2d/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,22 @@ class Forge2DExample extends Forge2DGame {
}

class Ball extends BodyComponent with TapCallbacks {
final Vector2 initialPosition;

Ball({Vector2? initialPosition})
: initialPosition = initialPosition ?? Vector2.zero();

@override
Body createBody() {
final shape = CircleShape();
shape.radius = 5;

final fixtureDef = FixtureDef(
shape,
restitution: 0.8,
density: 1.0,
friction: 0.4,
);

final bodyDef = BodyDef(
userData: this,
angularDamping: 0.8,
position: initialPosition,
type: BodyType.dynamic,
);

return world.createBody(bodyDef)..createFixture(fixtureDef);
}
: super(
fixtureDefs: [
FixtureDef(
CircleShape()..radius = 5,
restitution: 0.8,
density: 1.0,
friction: 0.4,
),
],
bodyDef: BodyDef(
angularDamping: 0.8,
position: initialPosition ?? Vector2.zero(),
type: BodyType.dynamic,
),
);

@override
void onTapDown(_) {
Expand All @@ -80,7 +70,6 @@ class Wall extends BodyComponent {
final shape = EdgeShape()..set(_start, _end);
final fixtureDef = FixtureDef(shape, friction: 0.3);
final bodyDef = BodyDef(
userData: this,
position: Vector2.zero(),
);

Expand Down
28 changes: 26 additions & 2 deletions packages/flame_forge2d/lib/body_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
/// Since a pure BodyComponent doesn't have anything drawn on top of it,
/// it is a good idea to turn on [debugMode] for it so that the bodies can be
/// seen
abstract class BodyComponent<T extends Forge2DGame> extends Component
///
/// You can use the optional [bodyDef] and [fixtureDefs] arguments to create
/// the [BodyComponent]'s body without having to create the definitions within
/// the component.
class BodyComponent<T extends Forge2DGame> extends Component
with HasGameReference<T>, HasPaint
implements
CoordinateTransform,
Expand All @@ -21,6 +25,8 @@
super.children,
super.priority,
this.renderBody = true,
this.bodyDef,
this.fixtureDefs,
super.key,
}) {
this.paint = paint ?? (Paint()..color = defaultColor);
Expand All @@ -29,6 +35,16 @@
static const defaultColor = Color.fromARGB(255, 255, 255, 255);
late Body body;

/// The default implementation of [createBody] will use this value to create the [Body],

Check notice on line 38 in packages/flame_forge2d/lib/body_component.dart

View workflow job for this annotation

GitHub Actions / analyze

The line length exceeds the 80-character limit.

Try breaking the line across multiple lines. See https://dart.dev/lints/lines_longer_than_80_chars to learn more about this problem.
/// if it is provided.
///
/// If you do not provide a [BodyDef] here, you must override [createBody].
BodyDef? bodyDef;

/// The default implementation of [createBody] will add these fixtures to the [Body] that it

Check notice on line 44 in packages/flame_forge2d/lib/body_component.dart

View workflow job for this annotation

GitHub Actions / analyze

The line length exceeds the 80-character limit.

Try breaking the line across multiple lines. See https://dart.dev/lints/lines_longer_than_80_chars to learn more about this problem.
/// creates from [bodyDef].
List<FixtureDef>? fixtureDefs;

@override
Vector2 get position => body.position;

Expand All @@ -43,7 +59,15 @@

/// You should create the Forge2D [Body] in this method when you extend
/// the BodyComponent.
Body createBody();
Body createBody() {
assert(
bodyDef != null,
'Ensure this.bodyDef is not null or override createBody',
);
final body = world.createBody(bodyDef!);
fixtureDefs?.forEach(body.createFixture);
return body;
}

@mustCallSuper
@override
Expand Down
51 changes: 50 additions & 1 deletion packages/flame_forge2d/test/body_component_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:flame/components.dart' show PositionComponent;
import 'package:flame/components.dart' show ComponentKey, PositionComponent;
import 'package:flame/extensions.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flame_test/flame_test.dart';
Expand Down Expand Up @@ -350,5 +350,54 @@ void main() {
},
);
});
group('createBody', () {
test('should throw an error if bodyDef is null', () {
final bodyComponent = BodyComponent();
expect(bodyComponent.createBody, throwsAssertionError);
});

group('should create body', () {
final flameTester = FlameTester(Forge2DGame.new);

flameTester.testGameWidget(
'with no fixtures',
setUp: (game, tester) async {
final bodyComponent = BodyComponent(
bodyDef: BodyDef(position: Vector2(33, 44)),
key: ComponentKey.named('tested'),
);
game.world.add(bodyComponent);
},
verify: (game, tester) async {
expect(
game.findByKeyName<BodyComponent>('tested')!.body.position,
Vector2(33, 44),
);
},
);

flameTester.testGameWidget(
'with a set of fixtures',
setUp: (game, tester) async {
final bodyComponent = BodyComponent(
bodyDef: BodyDef(),
fixtureDefs: [
FixtureDef(CircleShape()..radius = 10),
FixtureDef(CircleShape()..radius = 20),
FixtureDef(CircleShape()..radius = 30),
],
key: ComponentKey.named('tested'),
);
game.world.add(bodyComponent);
},
verify: (game, tester) async {
final bodyComponent = game.findByKeyName<BodyComponent>('tested')!;
expect(bodyComponent.body.fixtures[0].shape.radius, 10);
expect(bodyComponent.body.fixtures[1].shape.radius, 20);
expect(bodyComponent.body.fixtures[2].shape.radius, 30);
},
);
});
});
});
}
Loading