Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.80 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using Game.CharacterController;
  4. using Spine.Unity;
  5.  
  6. namespace Game.Player {
  7.    
  8.     public class PlayerAnimationController : MonoBehaviour
  9.     {
  10.  
  11.         //public Animator animator;
  12.         public SkeletonAnimation SpineInstance;
  13.         private PlayerInput _playerInput;
  14.         private WeaponSystem _playerWeaponSystem;
  15.  
  16.         public struct AnimState {
  17.             public bool isWalking;
  18.             public bool isAttacking;
  19.  
  20.             public Spine.TrackEntry motionTrack;
  21.             public Spine.TrackEntry attackTrack;
  22.  
  23.             public void Reset()
  24.             {
  25.                 isWalking = isAttacking = false;
  26.                 attackTrack = motionTrack = null;
  27.             }
  28.         }
  29.  
  30.         public AnimState _animState;
  31.  
  32.         void Awake() {
  33.             _playerInput = GetComponent<PlayerInput> ();
  34.             _playerWeaponSystem = GetComponent<WeaponSystem> ();
  35.             RegisterEvents ();
  36.  
  37.             _animState.Reset ();
  38.  
  39.         }
  40.  
  41.         void Start() {
  42.             SpineInstance.AnimationState.Start += delegate (Spine.TrackEntry trackEntry) {
  43.                 // You can also use an anonymous delegate.
  44.                 Debug.Log(string.Format("track {0} started a new animation.", trackEntry.TrackIndex));
  45.             };
  46.  
  47.             //set the character to idle
  48.             _animState.motionTrack = SpineInstance.AnimationState.SetAnimation (0, "Idle", true);
  49.  
  50.         }
  51.  
  52.         void RegisterEvents() {
  53.  
  54.             //listen to events from PlayerInput
  55.             _playerInput.events.OnWalk.AddListener(OnWalk);
  56.             _playerInput.events.OnWalkStop.AddListener (OnWalkStop);
  57.             _playerInput.events.OnJumpStart.AddListener (OnJumpStart);
  58.  
  59.             //listen to events from the weapon system
  60.             _playerWeaponSystem.events.OnBlockStart.AddListener(OnBlockStart);
  61.             _playerWeaponSystem.events.OnBlockEnd.AddListener(OnBlockEnd);
  62.             _playerWeaponSystem.events.OnAttackStart.AddListener (OnAttackStart);
  63.             _playerWeaponSystem.events.OnHeavyAttackStart.AddListener (OnHeavyAttackStart);
  64.             _playerWeaponSystem.events.OnWarcryHealStart.AddListener (OnWarcry);
  65.             _playerWeaponSystem.events.OnWarcrySlowTimeStart.AddListener (OnWarcry);
  66.  
  67.         }
  68.  
  69.         #region Player Movement
  70.         public void OnWalk() {
  71.  
  72.             if (!_animState.isWalking) {
  73.                 _animState.motionTrack = SpineInstance.AnimationState.SetAnimation (0, "Run-1", true);
  74.                 _animState.isWalking = true;
  75.             }
  76.             //animator.SetBool ("IsWalking", true);
  77.         }
  78.  
  79.         public void OnWalkStop() {
  80.  
  81.             if (_animState.isWalking) {
  82.                 _animState.motionTrack = SpineInstance.AnimationState.SetAnimation (0, "Idle", true);
  83.                 _animState.isWalking = false;
  84.             }
  85.  
  86.             //animator.SetBool ("IsWalking", false);
  87.         }
  88.  
  89.         public void OnJumpStart() {
  90.             //animator.SetBool ("IsJumping", true);
  91.         }
  92.         #endregion
  93.  
  94.         #region Player Weapon System
  95.         public void OnBlockStart() {
  96.             //animator.SetBool ("IsBlocking", true);
  97.         }
  98.         public void OnBlockEnd() {
  99.             //animator.SetBool ("IsBlocking", false);
  100.         }
  101.  
  102.         public void OnAttackStart() {
  103.            
  104.             if (!_animState.isAttacking) {
  105.  
  106.                 _animState.attackTrack = SpineInstance.AnimationState.SetAnimation (1, "Attack_normal-down", false);
  107.                 _animState.attackTrack.Alpha = 1f;
  108.                 _animState.attackTrack.AnimationEnd = 0.6f;
  109.                 _animState.attackTrack.TimeScale = 1.2f;
  110.  
  111.                 //we need to disable the attack until the animation is completed
  112.                 _playerWeaponSystem.mainHandWeapon.canAttack = false;
  113.  
  114.                 _animState.attackTrack.Complete += delegate(Spine.TrackEntry trackEntry) {
  115.                     //Debug.Log("Ended");
  116.                     //Debug.Break();
  117.                     _animState.isAttacking = false;
  118.                     _playerWeaponSystem.mainHandWeapon.canAttack = true;
  119.                     SpineInstance.AnimationState.ClearTrack(1);
  120.                 };
  121.  
  122.                 _animState.isAttacking = true;
  123.             }
  124.  
  125.             //animator.SetBool ("IsAttacking", true);
  126.         }
  127.  
  128.  
  129.         public void OnHeavyAttackStart() {
  130.             //animator.SetBool ("IsHeavyAttacking", true);
  131.         }
  132.  
  133.         #endregion
  134.  
  135.         #region Player Warcries
  136.  
  137.         public void OnWarcry() {
  138.             //animator.SetBool ("IsUsingWarcry", true);
  139.         }
  140.  
  141.         #endregion
  142.  
  143.  
  144.     }
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement