using System.Collections; using System.Collections.Generic; using Platformer.Core; using Platformer.Model; using UnityEngine; namespace Platformer.Mechanics { /// /// AnimationController integrates physics and animation. It is generally used for simple enemy animation. /// [RequireComponent(typeof(SpriteRenderer), typeof(Animator))] public class AnimationController : KinematicObject { /// /// Max horizontal speed. /// public float maxSpeed = 7; /// /// Max jump velocity /// public float jumpTakeOffSpeed = 7; /// /// Used to indicated desired direction of travel. /// public Vector2 move; /// /// Set to true to initiate a jump. /// public bool jump; /// /// Set to true to set the current jump velocity to zero. /// public bool stopJump; SpriteRenderer spriteRenderer; Animator animator; PlatformerModel model = Simulation.GetModel(); protected virtual void Awake() { spriteRenderer = GetComponent(); animator = GetComponent(); } protected override void ComputeVelocity() { if (jump && IsGrounded) { velocity.y = jumpTakeOffSpeed * model.jumpModifier; jump = false; } else if (stopJump) { stopJump = false; if (velocity.y > 0) { velocity.y = velocity.y * model.jumpDeceleration; } } if (move.x > 0.01f) spriteRenderer.flipX = false; else if (move.x < -0.01f) spriteRenderer.flipX = true; animator.SetBool("grounded", IsGrounded); animator.SetFloat("velocityX", Mathf.Abs(velocity.x) / maxSpeed); targetVelocity = move * maxSpeed; } } }