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: Helpers for whether a PositionComponent is flipped. #1700

Merged
merged 4 commits into from
Jun 6, 2022
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
6 changes: 6 additions & 0 deletions packages/flame/lib/src/components/position_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,12 @@ class PositionComponent extends Component
transform.flipVertically();
}

/// Whether it is currently flipped horizontally.
bool get isFlippedHorizontally => transform.scale.x.isNegative;

/// Whether it is currently flipped vertically.
bool get isFlippedVertically => transform.scale.y.isNegative;

//#endregion

/// Internal handler that must be invoked whenever either the [size]
Expand Down
34 changes: 34 additions & 0 deletions packages/flame/test/components/position_component_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,40 @@ void main() {
expect(component.center, closeToVector(x, y, epsilon: 1e-14));
});

test('isHorizontallyFlipped', () {
final component = PositionComponent()
..size = Vector2(10, 10)
..position = Vector2(50, 20)
..anchor = const Anchor(0.6, 0.8);

expect(component.isFlippedHorizontally, isFalse);
component.flipHorizontally();
expect(component.isFlippedHorizontally, isTrue);
component.flipHorizontally();
expect(component.isFlippedHorizontally, isFalse);
component.flipHorizontallyAroundCenter();
expect(component.isFlippedHorizontally, isTrue);
component.flipHorizontallyAroundCenter();
expect(component.isFlippedHorizontally, isFalse);
Copy link
Member

Choose a reason for hiding this comment

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

I wonder what happens if "flip" with one version and "unflip" with the other

Copy link
Member Author

Choose a reason for hiding this comment

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

I think that it will move the component from its original position (which I think makes sense), if I remember correctly this is documented somewhere, maybe on the dartdocs.

});

test('isVerticallyFlipped', () {
final component = PositionComponent()
..size = Vector2(10, 10)
..position = Vector2(50, 20)
..anchor = const Anchor(0.6, 0.8);

expect(component.isFlippedVertically, isFalse);
component.flipVertically();
expect(component.isFlippedVertically, isTrue);
component.flipVertically();
expect(component.isFlippedVertically, isFalse);
component.flipVerticallyAroundCenter();
expect(component.isFlippedVertically, isTrue);
component.flipVerticallyAroundCenter();
expect(component.isFlippedVertically, isFalse);
});

test('rotations', () {
final component = PositionComponent()
..size = Vector2(8, 6)
Expand Down