| | | 1 | | using UnityEngine; |
| | | 2 | | |
| | | 3 | | // Base implementation of a launch angle planner. |
| | | 4 | | public abstract class LaunchAnglePlannerBase : ILaunchAnglePlanner { |
| | 91 | 5 | | public IAgent Agent { get; init; } |
| | | 6 | | |
| | 42 | 7 | | public LaunchAnglePlannerBase(IAgent agent) { |
| | 21 | 8 | | Agent = agent; |
| | 21 | 9 | | } |
| | | 10 | | |
| | | 11 | | // Calculate the optimal launch angle in degrees and the time-to-target in seconds. |
| | | 12 | | public abstract LaunchAngleOutput Plan(in LaunchAngleInput input); |
| | 11 | 13 | | public LaunchAngleOutput Plan(in Vector3 targetPosition) { |
| | 11 | 14 | | Direction direction = ConvertToRelativeDirection(targetPosition); |
| | 11 | 15 | | return Plan( |
| | | 16 | | new LaunchAngleInput { Distance = direction.Distance, Altitude = direction.Altitude }); |
| | 11 | 17 | | } |
| | | 18 | | |
| | | 19 | | // Return the absolute intercept position given the absolute target position. |
| | | 20 | | public abstract Vector3 InterceptPosition(in Vector3 targetPosition); |
| | | 21 | | |
| | | 22 | | // Convert from a 3D vector to a 2D direction that ignores the azimuth and that is relative to the |
| | | 23 | | // agent's position. |
| | 29 | 24 | | protected Direction ConvertToRelativeDirection(in Vector3 position) { |
| | 29 | 25 | | Vector3 relativePosition = position - Agent.Position; |
| | 29 | 26 | | return new Direction { Distance = |
| | | 27 | | Vector3.ProjectOnPlane(relativePosition, Vector3.up).magnitude, |
| | | 28 | | Altitude = Vector3.Project(relativePosition, Vector3.up).y }; |
| | 29 | 29 | | } |
| | | 30 | | } |