using UnityEngine;
using System.Collections;

public class AnimController : MonoBehaviour 
{

    private Animator _Anim;

    private Rigidbody2D _RigidBody;
    private MovementController _PlayerController;
    private MegaShoot _MegaShoot;
    private Lifebar _Lifebar;

    public bool ForceParams = false;

    public float ForcedSpeed = 0f;
    public bool ForcedGrounded = true;

	void Start () 
    {
        _Anim = gameObject.GetComponentInChildren<Animator>();

        if (_Anim == null)
        {
            Debug.LogError("[" + gameObject.name + "] Animator not found.");
        }
        else
        {
            _RigidBody = gameObject.GetComponent<Rigidbody2D>();
            _PlayerController = gameObject.GetComponent<MovementController>();
            _MegaShoot = gameObject.GetComponent<MegaShoot>();
            _Lifebar = gameObject.GetComponent<Lifebar>();
        }
	}
	
	void Update () 
    {
        if (_Anim != null)
        {
            if (ForceParams)
            {
                // Debug stuff.

                _Anim.SetFloat("Speed", ForcedSpeed);
                _Anim.SetBool("Grounded", ForcedGrounded);
            }
            else
            {
                if (_RigidBody != null)
                {
                    _Anim.SetFloat("Speed", Mathf.Abs(_RigidBody.velocity.x));
                }
                else
                {
                    _Anim.SetFloat("Speed", 0f);
                }

                if (_PlayerController != null && _PlayerController.enabled)
                {
                    _Anim.SetBool("Grounded", _PlayerController.bGrounded);
                    _Anim.SetBool("Fire", _PlayerController.bFire);
                }
                else
                {
                    _Anim.SetBool("Grounded", true);
                    _Anim.SetBool("Fire", false);
                }

                if (_MegaShoot != null && _MegaShoot.enabled)
                {
                    _Anim.SetBool("Charging", _MegaShoot.bPreparingShoot);
                    _Anim.SetBool("Special", _MegaShoot.bShooting);                        
                }

                if (_Lifebar != null && _Lifebar.enabled)
                {
                    _Anim.SetBool("Dead", _Lifebar.IsDead);
                }
                else
                {
                    _Anim.SetBool("Dead", false);
                }
            }
        }
	}
}
