diff --git a/packages/flame/lib/src/camera/behaviors/follow_behavior.dart b/packages/flame/lib/src/camera/behaviors/follow_behavior.dart index bf977da7166..38d0858502c 100644 --- a/packages/flame/lib/src/camera/behaviors/follow_behavior.dart +++ b/packages/flame/lib/src/camera/behaviors/follow_behavior.dart @@ -1,8 +1,10 @@ import 'package:flame/src/camera/viewfinder.dart'; import 'package:flame/src/camera/viewport.dart'; +import 'package:flame/src/camera/world.dart'; import 'package:flame/src/components/core/component.dart'; import 'package:flame/src/components/position_component.dart'; import 'package:flame/src/effects/provider_interfaces.dart'; +import 'package:meta/meta.dart'; import 'package:vector_math/vector_math.dart'; /// This behavior will make the [owner] follow the [target]. @@ -17,6 +19,10 @@ import 'package:vector_math/vector_math.dart'; /// /// The flags [horizontalOnly]/[verticalOnly] allow constraining the [owner]'s /// movement to the horizontal/vertical directions respectively. +/// +/// If the [target] is a [PositionComponent], the owner follows it wherever it +/// is in the component tree, even if it's a child of another +/// [PositionComponent]. class FollowBehavior({ required final ReadOnlyPositionProvider _target, var PositionProvider? _owner, @@ -51,11 +57,53 @@ class FollowBehavior({ } } + /// The position of the [target] in the coordinate system of the [owner]'s + /// position. + /// + /// The position of a [PositionComponent] is relative to its parent, so if + /// the target is a [PositionComponent] with another parent than the owner, + /// its position is converted into the coordinate system of the owner's + /// parent. The position of an owner that isn't a [PositionComponent], such + /// as a [Viewfinder], is in the coordinate system of the [World] that the + /// target is in, which is rendered without the transforms of the + /// components that the world itself is in. + @internal + Vector2 get targetPosition { + final target = _target; + if (target is! PositionComponent) { + return target.position; + } + final owner = _owner; + if (owner is PositionComponent) { + if (owner.parent == target.parent) { + return target.position; + } + // The owner's parent is used rather than the owner itself, so that the + // owner's own transform, e.g. a zero scale, doesn't affect the result. + final absolutePosition = target.absolutePosition; + return owner.findParent()?.absoluteToLocal( + absolutePosition, + ) ?? + absolutePosition; + } + Vector2 position = target.position; + for (final ancestor in target.ancestors()) { + if (ancestor is World) { + break; + } + if (ancestor is PositionComponent) { + position = ancestor.positionOf(position); + } + } + return position; + } + @override void update(double dt) { + final targetPosition = this.targetPosition; _tempDelta.setValues( - verticalOnly ? 0 : target.position.x - owner.position.x, - horizontalOnly ? 0 : target.position.y - owner.position.y, + verticalOnly ? 0 : targetPosition.x - owner.position.x, + horizontalOnly ? 0 : targetPosition.y - owner.position.y, ); final distance = _tempDelta.length; diff --git a/packages/flame/lib/src/camera/camera_component.dart b/packages/flame/lib/src/camera/camera_component.dart index cff0c14a56e..685ab577c39 100644 --- a/packages/flame/lib/src/camera/camera_component.dart +++ b/packages/flame/lib/src/camera/camera_component.dart @@ -354,8 +354,10 @@ class CameraComponent({ /// Makes the [viewfinder] follow the given [target]. /// /// The [target] here can be any read-only [PositionProvider]. For example, a - /// [PositionComponent] is the most common choice of target. Alternatively, - /// you can use [PositionProviderImpl] to construct the target dynamically. + /// [PositionComponent] is the most common choice of target, and it's + /// followed at its position in the world even when it's a child of another + /// [PositionComponent]. Alternatively, you can use [PositionProviderImpl] to + /// construct the target dynamically. /// /// This method adds a [FollowBehavior] to the viewfinder. If there is another /// [FollowBehavior] currently applied to the viewfinder, it will be removed @@ -376,17 +378,16 @@ class CameraComponent({ bool snap = false, }) { stop(); - viewfinder.add( - FollowBehavior( - target: target, - owner: viewfinder, - maxSpeed: maxSpeed, - horizontalOnly: horizontalOnly, - verticalOnly: verticalOnly, - ), + final followBehavior = FollowBehavior( + target: target, + owner: viewfinder, + maxSpeed: maxSpeed, + horizontalOnly: horizontalOnly, + verticalOnly: verticalOnly, ); + viewfinder.add(followBehavior); if (snap) { - viewfinder.position = target.position; + viewfinder.position = followBehavior.targetPosition; } } diff --git a/packages/flame/test/camera/behaviors/follow_behavior_test.dart b/packages/flame/test/camera/behaviors/follow_behavior_test.dart index 36a2aa6d1d3..9cd2239cd4b 100644 --- a/packages/flame/test/camera/behaviors/follow_behavior_test.dart +++ b/packages/flame/test/camera/behaviors/follow_behavior_test.dart @@ -1,3 +1,5 @@ +import 'dart:math'; + import 'package:flame/camera.dart'; import 'package:flame/components.dart'; import 'package:flame/src/effects/provider_interfaces.dart'; @@ -158,5 +160,165 @@ void main() { game.update(1); } }); + + group('when the target and the owner have different parents', () { + testWithFlameGame('follows a nested target', (game) async { + final container = PositionComponent( + position: Vector2(100, 50), + scale: Vector2.all(2), + angle: pi / 2, + )..addToParent(game); + final target = PositionComponent(position: Vector2(20, 30)) + ..addToParent(container); + final pursuer = PositionComponent() + ..add(FollowBehavior(target: target)) + ..addToParent(game); + await game.ready(); + + game.update(0.01); + // (20, 30) scaled by 2 and turned by a quarter turn is (-60, 40). + expect(pursuer.position, closeToVector(Vector2(40, 90), 1e-4)); + }); + + testWithFlameGame('follows a target from a nested owner', (game) async { + final target = PositionComponent(position: Vector2(300, 200)) + ..addToParent(game); + final container = PositionComponent(position: Vector2(100, 50)) + ..addToParent(game); + final pursuer = PositionComponent() + ..add(FollowBehavior(target: target)) + ..addToParent(container); + await game.ready(); + + game.update(0.01); + expect(pursuer.position, Vector2(200, 150)); + expect(pursuer.absolutePosition, target.absolutePosition); + }); + + testWithFlameGame( + 'follows a target within the same nested parent', + (game) async { + final container = PositionComponent(position: Vector2(100, 50)) + ..addToParent(game); + final target = PositionComponent(position: Vector2(20, 30)) + ..addToParent(container); + final pursuer = PositionComponent() + ..add(FollowBehavior(target: target)) + ..addToParent(container); + await game.ready(); + + game.update(0.01); + expect(pursuer.position, Vector2(20, 30)); + }, + ); + + testWithFlameGame( + 'follows a target nested deeper in the parent of the owner', + (game) async { + final container = PositionComponent(position: Vector2(100, 50)) + ..addToParent(game); + final group = PositionComponent( + position: Vector2(10, 10), + scale: Vector2.all(2), + )..addToParent(container); + final target = PositionComponent(position: Vector2(20, 30)) + ..addToParent(group); + final pursuer = PositionComponent() + ..add(FollowBehavior(target: target)) + ..addToParent(container); + await game.ready(); + + game.update(0.01); + expect(pursuer.position, Vector2(50, 70)); + expect(pursuer.absolutePosition, target.absolutePosition); + }, + ); + + testWithFlameGame( + 'follows a target in another branch of a transformed ancestor', + (game) async { + final level = PositionComponent( + position: Vector2(100, 100), + scale: Vector2.all(2), + angle: pi / 3, + )..addToParent(game); + final targetGroup = PositionComponent( + position: Vector2(10, 20), + angle: -pi / 4, + )..addToParent(level); + final pursuerGroup = PositionComponent( + position: Vector2(-30, 40), + scale: Vector2.all(0.5), + )..addToParent(level); + final target = PositionComponent(position: Vector2(25, -15)) + ..addToParent(targetGroup); + final pursuer = PositionComponent() + ..add(FollowBehavior(target: target)) + ..addToParent(pursuerGroup); + await game.ready(); + + game.update(0.01); + expect( + pursuer.absolutePosition, + closeToVector(target.absolutePosition, 1e-3), + ); + }, + ); + + testWithFlameGame( + 'follows a target with a different parent at a zero scale', + (game) async { + final target = PositionComponent(position: Vector2(300, 200)) + ..addToParent(game); + final container = PositionComponent(position: Vector2(100, 50)) + ..addToParent(game); + // A follower that scales in from zero, e.g. with a ScaleEffect. + final pursuer = PositionComponent(scale: Vector2.zero()) + ..add(FollowBehavior(target: target)) + ..addToParent(container); + await game.ready(); + + game.update(0.01); + expect(pursuer.position, Vector2(200, 150)); + }, + ); + + testWithFlameGame( + 'follows a nested target with a custom owner', + (game) async { + final container = PositionComponent(position: Vector2(100, 50)) + ..addToParent(game); + final target = PositionComponent(position: Vector2(20, 30)) + ..addToParent(container); + final ownerPosition = Vector2.zero(); + final owner = PositionProviderImpl( + getValue: () => ownerPosition, + setValue: ownerPosition.setFrom, + ); + game.add(FollowBehavior(target: target, owner: owner)); + await game.ready(); + + game.update(0.01); + expect(ownerPosition, Vector2(120, 80)); + }, + ); + + testWithFlameGame('moves at max speed towards a nested target', ( + game, + ) async { + final container = PositionComponent(position: Vector2(100, 50)) + ..addToParent(game); + final target = PositionComponent(position: Vector2(200, 350)) + ..addToParent(container); + final pursuer = PositionComponent() + ..add(FollowBehavior(target: target, maxSpeed: 50)) + ..addToParent(game); + await game.ready(); + + // The target is at (300, 400), which is 500 units away. + game.update(1); + expect(pursuer.position, closeToVector(Vector2(30, 40), 1e-4)); + }); + }); }); } diff --git a/packages/flame/test/camera/camera_component_test.dart b/packages/flame/test/camera/camera_component_test.dart index 425f1f83c63..4af088c2458 100644 --- a/packages/flame/test/camera/camera_component_test.dart +++ b/packages/flame/test/camera/camera_component_test.dart @@ -89,6 +89,69 @@ void main() { expect(camera.viewfinder.position, Vector2(100, 100)); }); + testWithFlameGame('follow a target nested in another component', ( + game, + ) async { + final world = World()..addToParent(game); + final camera = CameraComponent(world: world)..addToParent(game); + final ship = PositionComponent(position: Vector2(100, 200)) + ..addToParent(world); + final pilot = PositionComponent(position: Vector2(10, 20)) + ..addToParent(ship); + camera.follow(pilot); + await game.ready(); + + for (var i = 0; i < 20; i++) { + ship.position.add(Vector2(i * 5.0, 20.0 - i)); + game.update(0.01); + expect( + camera.viewfinder.position, + closeToVector(pilot.absolutePosition), + ); + } + }); + + testWithFlameGame('follow with snap a target nested in another component', ( + game, + ) async { + final world = World()..addToParent(game); + final ship = PositionComponent( + position: Vector2(100, 200), + scale: Vector2.all(2), + )..addToParent(world); + final pilot = PositionComponent(position: Vector2(10, 20)) + ..addToParent(ship); + final camera = CameraComponent(world: world) + ..follow(pilot, maxSpeed: 1, snap: true) + ..addToParent(game); + await game.ready(); + + expect(camera.viewfinder.position, Vector2(120, 240)); + }); + + testWithFlameGame('follow a target in a world within a component', ( + game, + ) async { + // The camera renders the world without the transforms of the components + // that the world is in, so they don't move the camera either. + final container = PositionComponent(position: Vector2(100, 50)) + ..addToParent(game); + final world = World()..addToParent(container); + final ship = PositionComponent(position: Vector2(10, 20)) + ..addToParent(world); + final pilot = PositionComponent(position: Vector2(5, 5)) + ..addToParent(ship); + final camera = CameraComponent(world: world) + ..follow(ship, snap: true) + ..addToParent(game); + await game.ready(); + expect(camera.viewfinder.position, Vector2(10, 20)); + + camera.follow(pilot); + game.update(0.01); + expect(camera.viewfinder.position, Vector2(15, 25)); + }); + testWithFlameGame('moveTo', (game) async { final world = World()..addToParent(game); final camera = CameraComponent(world: world)..addToParent(game);