using System; using UnityEngine; namespace Framework.Actors.Platformer { /// Manages a platformer actor moving around on a 2D plane public class PlatformerActorController : ActorController { /// Direction the actor is facing in /// /// -1.0 means the actor is facing to the left, +1.0 means the actor is /// facing to the right. 0.0 is neutral. This is used for various actions /// such as the direction the actor is shooting in or dashing towards. /// public float FacingDirection; /// Number of jumps the actor has done without touching the ground public int ConsecutiveJumpCount; /// Presenter driving the Mecanim animation state machine public new PlatformerActorPresenter Presenter { get { if(this.platformerPresenter == null) { this.platformerPresenter = base.Presenter as PlatformerActorPresenter; } return this.platformerPresenter; } } /// Ability definitions that control what moves the actor can use public Abilities Abilities { get { this.abilities = GetComponent(); if(this.abilities == null) { this.abilities = gameObject.AddComponent(); } return this.abilities; } } /// Called each visual frame to update the visible state of the actor protected override void Update() { base.Update(); // If a platformer presenter is present, update its speed and velocity // so the correct running and/or jumping animations can play. if(this.platformerPresenter != null) { // Forward the current facing direction to the presenter this.platformerPresenter.Direction = FacingDirection; // Try to obtain the velocity either from a rigidbody or an ActorPhysics component Vector3 velocity; { if(Rigidbody != null) { velocity = Rigidbody.velocity; } else if(ActorPhysics != null) { velocity = ActorPhysics.Velocity; } else { return; } } // Forward the horizontal and vertical velocity to the presenter this.platformerPresenter.HorizontalVelocity = Mathf.Sqrt( (velocity.x * velocity.x) + (velocity.z * velocity.z) ); this.platformerPresenter.VerticalVelocity = velocity.y; } } /// Called when a player assumes control of the actor /// Index of the player that has assumed control protected override void OnControlTaken(int playerIndex) { #if !UNITY_EDITOR Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; #endif } /// Called when a player releases control of the actor /// Index of the player that has released control protected override void OnControlReleased(int playerIndex) { Cursor.visible = true; Cursor.lockState = CursorLockMode.None; } /// Presenter driving the Mecanim animation state machine private PlatformerActorPresenter platformerPresenter; /// Abilities of the actor private Abilities abilities; } } // namespace Framework.Actors.Platformer