From 3f57ccaa54e60788ba2b5b85bb1c74aab9bb472b Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Sun, 5 Jun 2022 17:57:17 +0200 Subject: [PATCH 1/2] feat: Helpers for whether a `PositionComponent` is flipped. --- .../src/components/position_component.dart | 6 ++++ .../components/position_component_test.dart | 34 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/packages/flame/lib/src/components/position_component.dart b/packages/flame/lib/src/components/position_component.dart index 8a54998bfb8..4f6c0581222 100644 --- a/packages/flame/lib/src/components/position_component.dart +++ b/packages/flame/lib/src/components/position_component.dart @@ -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 horizontally. + bool get isFlippedVertically => transform.scale.y.isNegative; + //#endregion /// Internal handler that must be invoked whenever either the [size] diff --git a/packages/flame/test/components/position_component_test.dart b/packages/flame/test/components/position_component_test.dart index 30c7ebd4d30..848aa6f44d4 100644 --- a/packages/flame/test/components/position_component_test.dart +++ b/packages/flame/test/components/position_component_test.dart @@ -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) From c4ee27ebfa7dcd52db52084b8d67d901529478d8 Mon Sep 17 00:00:00 2001 From: Lukas Klingsbo Date: Sun, 5 Jun 2022 22:25:06 +0200 Subject: [PATCH 2/2] Update packages/flame/lib/src/components/position_component.dart --- packages/flame/lib/src/components/position_component.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/flame/lib/src/components/position_component.dart b/packages/flame/lib/src/components/position_component.dart index 4f6c0581222..c025c832517 100644 --- a/packages/flame/lib/src/components/position_component.dart +++ b/packages/flame/lib/src/components/position_component.dart @@ -352,7 +352,7 @@ class PositionComponent extends Component /// Whether it is currently flipped horizontally. bool get isFlippedHorizontally => transform.scale.x.isNegative; - /// Whether it is currently flipped horizontally. + /// Whether it is currently flipped vertically. bool get isFlippedVertically => transform.scale.y.isNegative; //#endregion