Skip to content

Commit

Permalink
feat: Helpers for whether a PositionComponent is flipped. (#1700)
Browse files Browse the repository at this point in the history
Helpers that just check if the scale is negative so that the user can now whether the component has been flipped.
  • Loading branch information
spydon authored Jun 6, 2022
1 parent 1b59d80 commit cf67147
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
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);
});

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

0 comments on commit cf67147

Please sign in to comment.