Advertisement
Guest User

Untitled

a guest
Aug 8th, 2014
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.61 KB | None | 0 0
  1. /// <summary>
  2. /// Animator controller.
  3. /// DavidOneill
  4. /// 17/7/14
  5. /// This class will manage all animation for characters( players, Enemies, bosses).
  6. /// simple npc animations will be using NpcAnimatorController.
  7. /// </summary>
  8. using UnityEngine;
  9. using System.Collections;
  10. //
  11. [RequireComponent (typeof (Animator))]
  12. //[RequireComponent (typeof (CombatFSM))]
  13. //
  14. public class AnimatorController : MonoBehaviour {
  15.  
  16.     public CombatFSM _combat;
  17.     private Health _health;
  18.     public playerMovement _Move;
  19.     public enemyMovement _enemyMove;
  20.     public enemySprite _sprite;
  21.     private Animator _anim;
  22.     public int _attackInt = 0;
  23.     //public int _weaponType = 0;  //need to add and Id int to weapons in CombatFsm.cs
  24.  
  25.     //These next 2 are for npcs only.
  26.     protected Motion motion; //instance of motion static class.
  27.     private NavMeshAgent _agent = null;
  28.  
  29. //  private AnimatorStateInfo _BaseInfo;
  30. //  private AnimatorStateInfo _Layer1Info;
  31.  
  32. //  static int IdleState = Animator.StringToHash("Base Layer.Idle"); //These will he handy later.//
  33.  
  34.     public string myState;// checks FsmState.
  35.  
  36.     // Use this for initialization
  37.     void Start () {
  38.         //Key References
  39.         _combat = GetComponent<CombatFSM>();
  40.         myState = _combat.currentState.ToString();
  41.         _health = GetComponent<Health>();
  42.         _anim = GetComponent<Animator>();
  43.         motion = new Motion(_anim);
  44.         //_sprite = GetComponentInChildren<enemySprite>();
  45.  
  46.         if(_anim == null || _combat == null || _health == null || _sprite == null)
  47.         {
  48.             Debug.Log ("Key Component Missing!!");
  49.         }
  50.         //MoveMent
  51.         if(_combat.isPlayer)
  52.         {
  53.  
  54.             _Move = GetComponent<playerMovement>();
  55.             _enemyMove = null;
  56.         }
  57.         else
  58.         {
  59.             //_Move = null;
  60.             _enemyMove = GetComponent<enemyMovement>();
  61.             _agent = GetComponent<NavMeshAgent>();
  62.         }
  63.     }
  64.  
  65.     void FixedUpdate()
  66.     {
  67.         if(_combat.isPlayer)
  68.         {
  69.                 float h = _Move.MYdir;
  70.                 float v = _Move.mySpeed;
  71.                 _anim.SetFloat("Speed",v);
  72.                 _anim.SetFloat("Direction",h);
  73.         }
  74.         else// if i am an Npc do this instead.
  75.         {
  76.             if(_agent.enabled)
  77.             {
  78.                 SetUpMotion();
  79.  
  80.             }
  81.         }
  82.  
  83.  
  84.         myState = _combat.currentState.ToString();
  85. //      _BaseInfo = _anim.GetCurrentAnimatorStateInfo(0); //reads the current animator state of the base layer.
  86. //      _Layer1Info = _anim.GetCurrentAnimatorStateInfo(1);// as above, but Layer 1.
  87.  
  88.         if(myState == "Ready")
  89.         {
  90.             _attackInt = 0;
  91.         }
  92.         if(myState == "AttackLight")
  93.         {
  94.             _attackInt = 1;
  95.         }
  96.         if(myState == "AttackHeavy")
  97.         {
  98.             _attackInt = 2;
  99.         }
  100.         if(myState == "AttackCombo")
  101.         {
  102.             _attackInt = 3;
  103.         }
  104.  
  105.     }
  106.  
  107.     void LateUpdate()
  108.     {
  109.         if(_combat.isPlayer)
  110.         {
  111.             _anim.SetBool("Grounded",_Move.grounded);
  112.         }
  113.         else
  114.         {
  115.             _anim.SetBool("Grounded",_enemyMove.grounded);
  116.         }
  117.  
  118.         _anim.SetBool("Hurt",_health.hurt);
  119.  
  120.         _anim.SetBool("Block", _combat.blocking);
  121.         _anim.SetBool("Grappling", _combat.Grappling);
  122.         _anim.SetBool("Grappled", _combat.grappled);
  123.         if(_combat.grabbedObj !=null)
  124.         {
  125.             _anim.SetBool("Grab",true);
  126.         }
  127.         else{
  128.             _anim.SetBool("Grab",false);
  129.         }
  130.         _anim.SetInteger("AttackType", _attackInt);
  131.     }
  132.  
  133.     //Protected Nav mesh Shizzle
  134.     protected void SetUpMotion()
  135.     {
  136.         if(AgentDone())
  137.         {
  138.             motion.Do(0,0);
  139.         }
  140.         else
  141.         {
  142.             float speed = _agent.desiredVelocity.magnitude;
  143.             Vector3 velocity = Quaternion.Inverse(transform.rotation) * _agent.desiredVelocity;
  144.             float angle = Mathf.Atan2(velocity.x,velocity.y) * 180.0f / 3.14159f;
  145.  
  146.             motion.Do(speed,angle);
  147.         }
  148.     }
  149.  
  150.     protected bool AgentDone()
  151.     {
  152.         return !_agent.pathPending && AgentStopping();
  153.     }
  154.  
  155.     protected bool AgentStopping()
  156.     {
  157.         return _agent.remainingDistance <= _agent.stoppingDistance;
  158.     }
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement