using System; using UnityEngine; using Framework.Actors; namespace Framework.Actors.Shooter { /// Manages the visual state of a character in a shooter game public class ShooterActorPresenter : ActorPresenter, IWalkingActorPresenter { /// Name of the animator parameter for the forward velocity private const string ForwardVelocityParameterName = "ForwardVelocity"; /// Name of the animator parameter for the strafing velocity private const string StrafingVelocityParameterName = "StrafingVelocity"; /// Name of the animator parameter for the vertical / jump velocity private const string VerticalVelocityParameterName = "VerticalVelocity"; #region enum AnimatorProperties /// Properties this presenter is accessing [Flags] private enum AnimatorProperties { /// The actor's forward velocity ForwardVelocity = (1 << 0), /// The actor's strafing velocity StrafingVelocity = (1 << 1), /// The actor's vertical / jump velocity VerticalVelocity = (1 << 2), } #endregion // enum AnimatorProperties /// Direction the player is facing in horizontally public Vector2 HorizontalDirection; /// Speed at which the actor is moving forward in units/second public float ForwardVelocity; /// Speed at which the actor is sidestepping in units/second public float StrafingVelocity; /// Speed at which the actor is moving vertically in units/second public float VerticalVelocity; /// Shows the actor during normal ground movement /// /// Override this to transition your actor's animations into the grounded state, /// used while standing on solid ground, walking and running. /// public virtual void SetGroundedState() { State = 0; } /// Shows the actor while it is airborne /// /// Override this to show your actor while it is airborne. Using different /// animations depending on the actor's vertical velocity is recommended. /// public virtual void SetAirState() { State = 1; } /// Called once per visual frame protected override void Update() { base.Update(); // Update speed if it has changed. If the user doesn't use speed at all, // we will simply leave it alone. Animator animator = Animator; if((animator != null) && (animator.runtimeAnimatorController != null)) { if(!this.presentProperties.HasValue) { checkAnimatorProperties(animator); } if(this.ForwardVelocity != this.forwardVelocity) { this.forwardVelocity = this.ForwardVelocity; if((this.presentProperties & AnimatorProperties.ForwardVelocity) != 0) { animator.SetFloat(ForwardVelocityParameterName, this.ForwardVelocity); } } if(this.StrafingVelocity != this.strafingVelocity) { this.strafingVelocity = this.StrafingVelocity; if((this.presentProperties & AnimatorProperties.StrafingVelocity) != 0) { animator.SetFloat(StrafingVelocityParameterName, this.StrafingVelocity); } } if(this.VerticalVelocity != this.verticalVelocity) { this.verticalVelocity = this.VerticalVelocity; if((this.presentProperties & AnimatorProperties.VerticalVelocity) != 0) { animator.SetFloat(VerticalVelocityParameterName, this.VerticalVelocity); } } } } /// Checks for the presence of animator properties /// Animator that will be checked for its properties private void checkAnimatorProperties(Animator animator) { AnimatorControllerParameter[] parameters = animator.parameters; AnimatorProperties presentProperties = 0; int parameterCount = parameters.Length; for(int index = 0; index < parameterCount; ++index) { if(parameters[index].type == AnimatorControllerParameterType.Float) { switch(parameters[index].name) { case ForwardVelocityParameterName: { presentProperties |= AnimatorProperties.ForwardVelocity; break; } case StrafingVelocityParameterName: { presentProperties |= AnimatorProperties.ForwardVelocity; break; } case VerticalVelocityParameterName: { presentProperties |= AnimatorProperties.ForwardVelocity; break; } } } } this.presentProperties = presentProperties; } /// Direction the character is currently facing private float direction; /// Speed at which the character is moving horizontally private float forwardVelocity; /// Speed at which the character is sidestepping private float strafingVelocity; /// Speed at which the character is moving vertically private float verticalVelocity; /// Properites that are present on the animator private AnimatorProperties? presentProperties; } } // namespace Framework.Actors.Shooter