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: allowed specifying renderBody via constructor #1548

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 13 additions & 9 deletions packages/flame_forge2d/lib/body_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,27 @@ import 'forge2d_game.dart';
/// seen
abstract class BodyComponent<T extends Forge2DGame> extends Component
with HasGameRef<T>, HasPaint {
static const defaultColor = Color.fromARGB(255, 255, 255, 255);
late Body body;

/// [renderBody] is true by default for [BodyComponent], if set to false
/// the body wont be rendered. If you render something on top of the
/// [BodyComponent], or doesn't want it to be seen, you probably want to set
/// it to false.
bool renderBody = true;

BodyComponent({
Paint? paint,
Iterable<Component>? children,
int? priority,
this.renderBody = true,
}) : super(children: children, priority: priority) {
this.paint = paint ?? (Paint()..color = defaultColor);
}

static const defaultColor = Color.fromARGB(255, 255, 255, 255);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can skip this one and rely on the default color of HasPaint

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should do this on another PR since this would be a breaking change. In addition, it is out of the scope of the PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I was thinking of it since you did some changes to paint in this PR too

Copy link
Contributor Author

@alestiago alestiago Apr 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oopsy. Did I change something? I believe I only reordered the constructor to be right after the class declaration.

late Body body;

/// Specifies if the body's fixtures should be rendered.
///
/// [renderBody] is true by default for [BodyComponent], if set to false
/// the body wont be rendered.
///
/// If you render something on top of the [BodyComponent], or doesn't want it
/// to be seen, you probably want to set it to false.
bool renderBody;

/// You should create the Forge2D [Body] in this method when you extend
/// the BodyComponent
Body createBody();
Expand Down
24 changes: 24 additions & 0 deletions packages/flame_forge2d/test/body_component_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:flame_forge2d/body_component.dart';
import 'package:test/expect.dart';
import 'package:test/scaffolding.dart';

class TestBodyComponent extends BodyComponent {
@override
void noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}

void main() {
group('BodyComponent', () {
group('renderBody', () {
test('is true by default', () {
final body = TestBodyComponent();
expect(body.renderBody, isTrue);
});

test('sets and gets', () {
final body = TestBodyComponent()..renderBody = false;
expect(body.renderBody, isFalse);
});
});
});
}