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: Add lookAt method for PositionComponent #1891

Merged
merged 33 commits into from
Sep 18, 2022
Merged
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
75c324e
Add lookAt method
ufrshubham Sep 6, 2022
a4287c9
Add test for lookAt
ufrshubham Sep 6, 2022
d269b52
Add angularOffset
ufrshubham Sep 6, 2022
39e3f83
Replace angularOffset with nativeAngle
ufrshubham Sep 10, 2022
fdc1985
Merge branch 'main' into feat/lookAt
ufrshubham Sep 10, 2022
609f239
Add lookAt tests with nativeAngle
ufrshubham Sep 10, 2022
3d05af8
Merge branch 'main' into feat/lookAt
ufrshubham Sep 10, 2022
8ba397c
Resolve merge conflicts
ufrshubham Sep 10, 2022
d2d3048
Add nativeAngle as optional parameter
ufrshubham Sep 10, 2022
f48b3a3
Remove _nativeDirection
ufrshubham Sep 10, 2022
7f65989
Add angleTo
ufrshubham Sep 11, 2022
02eb465
Add corner case for lookAt
ufrshubham Sep 11, 2022
e1ad4ea
Revert "Add nativeAngle as optional parameter"
ufrshubham Sep 11, 2022
d8c500e
Add nativeAngle as input for sprite components
ufrshubham Sep 11, 2022
e26083e
Merge branch 'main' into feat/lookAt
ufrshubham Sep 11, 2022
edc4948
Consider absoluteAngle in angleTo calculations
ufrshubham Sep 11, 2022
ab218d8
Add lookAt test for nested components
ufrshubham Sep 11, 2022
08957d8
Update comments with csys for target
ufrshubham Sep 12, 2022
c9d7e2f
Auto-format components.md
ufrshubham Sep 13, 2022
01a2173
Add nativeAngle docs
ufrshubham Sep 13, 2022
b5e3509
Add lookAt and angleTo examples
ufrshubham Sep 13, 2022
2870f4d
Fixed codelink
ufrshubham Sep 13, 2022
4df5648
Merge branch 'main' into feat/lookAt
ufrshubham Sep 13, 2022
ec53148
Revert auto-format changes
ufrshubham Sep 13, 2022
f213ec3
Fix grammar in example description
ufrshubham Sep 13, 2022
7d75fdd
Rename comp to component
ufrshubham Sep 13, 2022
48bbed3
Return delta angle from angleTo
ufrshubham Sep 13, 2022
91ff297
Update expected values for angleTo tests
ufrshubham Sep 13, 2022
499eef0
Use RotateEffect.by instead of RotateEffect.to
ufrshubham Sep 13, 2022
09fdc45
Merge branch 'main' into feat/lookAt
ufrshubham Sep 13, 2022
1863a15
Merge branch 'main' into feat/lookAt
ufrshubham Sep 17, 2022
2a9de0c
Merge branch 'main' into feat/lookAt
ufrshubham Sep 18, 2022
51266f6
Merge branch 'main' into feat/lookAt
ufrshubham Sep 18, 2022
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
20 changes: 16 additions & 4 deletions packages/flame/lib/src/components/position_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class PositionComponent extends Component
Vector2? size,
Vector2? scale,
double? angle,
this.angularOffset = 0,
Anchor? anchor,
super.children,
super.priority,
Expand All @@ -87,6 +88,9 @@ class PositionComponent extends Component
if (angle != 0) {
transform.angle = angle ?? 0;
}

transform.angle += angularOffset;
ufrshubham marked this conversation as resolved.
Show resolved Hide resolved

if (scale != null) {
transform.scale = scale;
}
Expand All @@ -98,6 +102,12 @@ class PositionComponent extends Component
final NotifyingVector2 _size;
Anchor _anchor;

/// Should be used to account for any angular offset needed to
/// orient the component in desired direction. It can be thought
/// of as rotating the local co-ordinate system of the component
/// by [angularOffset].
double angularOffset;
ufrshubham marked this conversation as resolved.
Show resolved Hide resolved

/// The decorator is used to apply visual effects to a component.
///
/// By default, the [PositionComponent] is equipped with a
Expand Down Expand Up @@ -132,9 +142,9 @@ class PositionComponent extends Component
/// rotated around its anchor point in the clockwise direction if the
/// angle is positive, or counterclockwise if the angle is negative.
@override
double get angle => transform.angle;
double get angle => transform.angle - angularOffset;
ufrshubham marked this conversation as resolved.
Show resolved Hide resolved
@override
set angle(double a) => transform.angle = a;
set angle(double a) => transform.angle = a + angularOffset;
ufrshubham marked this conversation as resolved.
Show resolved Hide resolved

/// The scale factor of this component. The scale can be different along
/// the X and Y dimensions. A scale greater than 1 makes the component
Expand Down Expand Up @@ -333,8 +343,10 @@ class PositionComponent extends Component
Vector2 get absoluteCenter => absolutePositionOfAnchor(Anchor.center);

/// Rotates the component to look at given target.
void lookAt(Vector2 target) =>
angle = Vector2(0, -1).angleToSigned(target - absolutePosition);
void lookAt(Vector2 target) {
st-pasha marked this conversation as resolved.
Show resolved Hide resolved
final defaultOrientation = Vector2(0, -1)..rotate(angularOffset);
angle = defaultOrientation.angleToSigned(target - absolutePosition);
}

//#endregion

Expand Down