Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 91 additions & 13 deletions Assets/PC2D/Scripts/PlatformerAnimation2D.cs
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using UnityEngine;
using System.Collections.Generic;

namespace PC2D
{
Expand All @@ -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<int, bool> _hasAnimation = new Dictionary<int, bool>();

// Use this for initialization
void Start()
{
_motor = GetComponent<PlatformerMotor2D>();
_animator = visualChild.GetComponent<Animator>();
_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
Expand All @@ -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)
{
Expand All @@ -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);
}
}
}
Expand All @@ -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;
}
}
Expand All @@ -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));
}
}
}
}