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
Show file tree
Hide file tree
Changes from 10 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class CompositeHitbox extends PositionComponent
super.size,
super.scale,
super.angle,
super.nativeAngle,
st-pasha marked this conversation as resolved.
Show resolved Hide resolved
super.anchor,
Iterable<ShapeHitbox>? super.children,
super.priority,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class CustomPainterComponent extends PositionComponent {
super.size,
super.scale,
super.angle,
super.nativeAngle,
super.anchor,
super.children,
super.priority,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class ButtonComponent extends PositionComponent with Tappable {
Vector2? size,
super.scale,
super.angle,
super.nativeAngle,
super.anchor,
super.children,
super.priority,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class HudMarginComponent<T extends FlameGame> extends PositionComponent
super.size,
super.scale,
super.angle,
super.nativeAngle,
super.anchor,
super.children,
super.priority,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class IsometricTileMapComponent extends PositionComponent {
super.size,
super.scale,
super.angle,
super.nativeAngle,
super.anchor,
super.children,
super.priority,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class NineTileBoxComponent extends PositionComponent implements SizeProvider {
super.size,
super.scale,
super.angle,
super.nativeAngle,
super.anchor,
super.children,
super.priority,
Expand Down
1 change: 1 addition & 0 deletions packages/flame/lib/src/components/parallax_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class ParallaxComponent<T extends FlameGame> extends PositionComponent
Vector2? size,
super.scale,
super.angle,
super.nativeAngle,
super.anchor,
super.children,
super.priority,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ParticleSystemComponent extends PositionComponent {
super.size,
super.scale,
super.angle,
super.nativeAngle,
super.anchor,
super.priority,
});
Expand Down
14 changes: 14 additions & 0 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.nativeAngle = 0,
Anchor? anchor,
super.children,
super.priority,
Expand All @@ -98,6 +99,10 @@ class PositionComponent extends Component
final NotifyingVector2 _size;
Anchor _anchor;

/// The angle where this component is looking at in its
/// default state, i.e. when [angle] is equal to zero.
double nativeAngle;
st-pasha 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 @@ -332,6 +337,15 @@ class PositionComponent extends Component
/// The absolute center of the component.
Vector2 get absoluteCenter => absolutePositionOfAnchor(Anchor.center);

/// Rotates the component to look at given target.
void lookAt(Vector2 target) {
st-pasha marked this conversation as resolved.
Show resolved Hide resolved
angle = math.atan2(
target.x - absolutePosition.x,
absolutePosition.y - target.y,
) -
spydon marked this conversation as resolved.
Show resolved Hide resolved
nativeAngle;
st-pasha marked this conversation as resolved.
Show resolved Hide resolved
}

//#endregion

//#region Mutators
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class SpriteAnimationComponent extends PositionComponent
super.size,
super.scale,
super.angle,
super.nativeAngle,
super.anchor,
super.children,
super.priority,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class SpriteAnimationGroupComponent<T> extends PositionComponent
super.size,
super.scale,
super.angle,
super.nativeAngle,
super.anchor,
super.children,
super.priority,
Expand Down
1 change: 1 addition & 0 deletions packages/flame/lib/src/components/sprite_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class SpriteComponent extends PositionComponent
Vector2? size,
super.scale,
super.angle,
super.nativeAngle,
super.anchor,
super.children,
super.priority,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class SpriteGroupComponent<T> extends PositionComponent
super.size,
super.scale,
super.angle,
super.nativeAngle,
super.anchor,
super.children,
super.priority,
Expand Down
1 change: 1 addition & 0 deletions packages/flame/lib/src/components/text_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class TextComponent<T extends TextRenderer> extends PositionComponent {
super.size,
super.scale,
super.angle,
super.nativeAngle,
super.anchor,
super.children,
super.priority,
Expand Down
1 change: 1 addition & 0 deletions packages/flame/lib/src/geometry/shape_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ abstract class ShapeComponent extends PositionComponent with HasPaint {
super.size,
super.scale,
super.angle,
super.nativeAngle,
super.anchor,
super.children,
super.priority,
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 @@ -717,6 +717,40 @@ void main() {
expect(child.position, Vector2(3, 2));
expect(child.absolutePosition, Vector2(15, 21));
});

test('lookAt', () {
final comp = PositionComponent();

final targets = [
Vector2(0, 1),
Vector2.all(2),
Vector2(-1, 0),
Vector2.all(-50)
];
final expectedAngles = [pi, (3 * pi / 4), (-pi / 2), (-pi / 4)];

for (var i = 0; i < 4; ++i) {
comp.lookAt(targets.elementAt(i));
expectDouble(comp.angle, expectedAngles.elementAt(i), epsilon: 1e-10);
}
});

test('lookAt with native angle', () {
final comp = PositionComponent(nativeAngle: pi / 2);

final targets = [
Vector2(0, 1),
Vector2.all(2),
Vector2(-1, 0),
Vector2.all(-50)
];
final expectedAngles = [pi / 2, (pi / 4), -pi, (-3 * pi / 4)];

for (var i = 0; i < 4; ++i) {
comp.lookAt(targets.elementAt(i));
expectDouble(comp.angle, expectedAngles.elementAt(i), epsilon: 1e-10);
}
});
});

group('Rendering', () {
Expand Down