From f9b89df54fa3b4d01a337635dbae20305aa2f076 Mon Sep 17 00:00:00 2001 From: Chris Hoopes Date: Wed, 20 Jan 2016 19:21:20 -0500 Subject: [PATCH] Stores animations as hashes instead of strings, checks if animations exist on load and only plays those that do so that animator script is agnostic of animation controller. Also adds ability to mark jump animation as play once so that jump doesn't loop. Ability to play jump animation is reset when controller lands or a mid-air jump is started. --- Assets/PC2D/Scripts/PlatformerAnimation2D.cs | 104 ++++++++++++++++--- 1 file changed, 91 insertions(+), 13 deletions(-) mode change 100644 => 100755 Assets/PC2D/Scripts/PlatformerAnimation2D.cs diff --git a/Assets/PC2D/Scripts/PlatformerAnimation2D.cs b/Assets/PC2D/Scripts/PlatformerAnimation2D.cs old mode 100644 new mode 100755 index a82ac3f9..f8685f88 --- a/Assets/PC2D/Scripts/PlatformerAnimation2D.cs +++ b/Assets/PC2D/Scripts/PlatformerAnimation2D.cs @@ -1,4 +1,5 @@ using UnityEngine; +using System.Collections.Generic; namespace PC2D { @@ -11,21 +12,55 @@ namespace PC2D public class PlatformerAnimation2D : MonoBehaviour { public float jumpRotationSpeed; + public bool jumpPlayOnce; public GameObject visualChild; private PlatformerMotor2D _motor; private Animator _animator; private bool _isJumping; + private bool _jumpPlayed = false; private bool _currentFacingLeft; + private int _animationIdle; + private int _animationWalk; + private int _animationJump; + private int _animationFall; + private int _animationDash; + private int _animationCling; + private int _animationSlip; + private int _animationOnCorner; + + private Dictionary _hasAnimation = new Dictionary(); + // Use this for initialization void Start() { _motor = GetComponent(); _animator = visualChild.GetComponent(); - _animator.Play("Idle"); + + _animationIdle = Animator.StringToHash("Idle"); + _animationWalk = Animator.StringToHash("Walk"); + _animationJump = Animator.StringToHash("Jump"); + _animationFall = Animator.StringToHash("Fall"); + _animationDash = Animator.StringToHash("Dash"); + _animationCling = Animator.StringToHash("Cling"); + _animationSlip = Animator.StringToHash("Slip"); + _animationOnCorner = Animator.StringToHash("On Corner"); + + CheckAnimation(_animationIdle); + CheckAnimation(_animationWalk); + CheckAnimation(_animationJump); + CheckAnimation(_animationFall); + CheckAnimation(_animationDash); + CheckAnimation(_animationCling); + CheckAnimation(_animationSlip); + CheckAnimation(_animationOnCorner); + + PlayAnimation(_animationIdle); _motor.onJump += SetCurrentFacingLeft; + _motor.onJump += ResetJumpPlayed; + _motor.onLanded += ResetJumpPlayed; } // Update is called once per frame @@ -37,7 +72,10 @@ void Update() _motor.motorState == PlatformerMotor2D.MotorState.FallingFast)) { _isJumping = true; - _animator.Play("Jump"); + if (!_jumpPlayed) + { + PlayAnimation(_animationJump); + } if (_motor.velocity.x <= -0.1f) { @@ -59,34 +97,34 @@ void Update() if (_motor.motorState == PlatformerMotor2D.MotorState.Falling || _motor.motorState == PlatformerMotor2D.MotorState.FallingFast) { - _animator.Play("Fall"); + PlayAnimation(_animationFall); } else if (_motor.motorState == PlatformerMotor2D.MotorState.WallSliding || _motor.motorState == PlatformerMotor2D.MotorState.WallSticking) { - _animator.Play("Cling"); + PlayAnimation(_animationCling); } else if (_motor.motorState == PlatformerMotor2D.MotorState.OnCorner) { - _animator.Play("On Corner"); + PlayAnimation(_animationOnCorner); } else if (_motor.motorState == PlatformerMotor2D.MotorState.Slipping) { - _animator.Play("Slip"); + PlayAnimation(_animationSlip); } else if (_motor.motorState == PlatformerMotor2D.MotorState.Dashing) { - _animator.Play("Dash"); + PlayAnimation(_animationDash); } else { if (_motor.velocity.sqrMagnitude >= 0.1f * 0.1f) { - _animator.Play("Walk"); + PlayAnimation(_animationWalk); } else { - _animator.Play("Idle"); + PlayAnimation(_animationIdle); } } } @@ -100,11 +138,15 @@ void Update() { valueCheck = _motor.velocity.x; } - - if (Mathf.Abs(valueCheck) >= 0.1f) + + if (valueCheck >= 0.1f) + { + visualChild.transform.localScale = Vector3.one; + } + else if (valueCheck <= -0.1f) { - Vector3 newScale = visualChild.transform.localScale; - newScale.x = Mathf.Abs(newScale.x) * ((valueCheck > 0) ? 1.0f : -1.0f); + Vector3 newScale = Vector3.one; + newScale.x = -1; visualChild.transform.localScale = newScale; } } @@ -113,5 +155,41 @@ private void SetCurrentFacingLeft() { _currentFacingLeft = _motor.facingLeft; } + + private void ResetJumpPlayed() + { + _jumpPlayed = false; + } + + private void PlayAnimation(int animation) + { + if (HasAnimation(animation)) + { + _animator.Play(animation); + + if (jumpPlayOnce && animation == _animationJump) + { + _jumpPlayed = true; + } + } + } + + private bool HasAnimation(int animation) + { + if (_hasAnimation.ContainsKey(animation)) + { + return _hasAnimation[animation]; + } + + return false; + } + + private void CheckAnimation(int animation) + { + if (!_hasAnimation.ContainsKey(animation)) + { + _hasAnimation.Add(animation, _animator.HasState(0, animation)); + } + } } }