| | | 1 | | using System; |
| | | 2 | | using UnityEngine; |
| | | 3 | | |
| | | 4 | | // Base implementation of a movement behavior. |
| | | 5 | | public abstract class MovementBase : IMovement { |
| | | 6 | | // Agent to which the movement behavior is assigned. |
| | 160 | 7 | | public IAgent Agent { get; init; } |
| | | 8 | | |
| | 34 | 9 | | public MovementBase(IAgent agent) { |
| | 17 | 10 | | Agent = agent; |
| | 17 | 11 | | } |
| | | 12 | | |
| | | 13 | | // Determine the agent's actual acceleration input given its intended acceleration input by |
| | | 14 | | // applying physics and other constraints. |
| | | 15 | | public abstract Vector3 Act(in Vector3 accelerationInput); |
| | | 16 | | |
| | | 17 | | // Limit acceleration input to the agent's maximum forward and normal accelerations. |
| | 14 | 18 | | protected Vector3 LimitAccelerationInput(in Vector3 accelerationInput) { |
| | 14 | 19 | | Vector3 forwardAccelerationInput = Vector3.Project(accelerationInput, Agent.Forward); |
| | 14 | 20 | | Vector3 normalAccelerationInput = Vector3.ProjectOnPlane(accelerationInput, Agent.Forward); |
| | | 21 | | |
| | | 22 | | // Limit the forward and the normal acceleration magnitude. |
| | 14 | 23 | | forwardAccelerationInput = |
| | | 24 | | Vector3.ClampMagnitude(forwardAccelerationInput, Agent.MaxForwardAcceleration()); |
| | 14 | 25 | | normalAccelerationInput = |
| | | 26 | | Vector3.ClampMagnitude(normalAccelerationInput, Agent.MaxNormalAcceleration()); |
| | 14 | 27 | | return forwardAccelerationInput + normalAccelerationInput; |
| | 14 | 28 | | } |
| | | 29 | | } |