Skip to content

Commit

Permalink
Allow for bodyDef and fixtureDefs to be prepared earlier
Browse files Browse the repository at this point in the history
  • Loading branch information
l1553k authored and Paweł Lis committed Sep 25, 2023
1 parent ebfa3c5 commit 513a6eb
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 32 deletions.
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: pass a `BodyDef` instance (and optionally a list
of `FixtureDef` instances) to BodyComponent' constructor' `bodyDef` (and `fixtureDefs`) arguments;
- use the default `createBody()` implementation: 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
27 changes: 25 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 @@ import 'package:flutter/foundation.dart';
/// 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]' 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 @@ abstract class BodyComponent<T extends Forge2DGame> extends Component
super.children,
super.priority,
this.renderBody = true,
this.bodyDef,
this.fixtureDefs,
super.key,
}) {
this.paint = paint ?? (Paint()..color = defaultColor);
Expand All @@ -29,6 +35,15 @@ abstract class BodyComponent<T extends Forge2DGame> extends Component
static const defaultColor = Color.fromARGB(255, 255, 255, 255);
late Body body;

/// Default implementation of [createBody] will use this value, if provided.
///
/// If you do not provide a BodyDef here, you must override [createBody].
BodyDef? bodyDef;

/// Default implementation of [createBody] adds these fixtures to the body
/// that it creates from [bodyDef].
List<FixtureDef>? fixtureDefs;

@override
Vector2 get position => body.position;

Expand All @@ -43,7 +58,15 @@ abstract class BodyComponent<T extends Forge2DGame> extends Component

/// 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);
},
);
});
});
});
}

0 comments on commit 513a6eb

Please sign in to comment.