Advertisement
shadowx320

StateManager.cs

Apr 10th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. namespace SA
  6. {
  7. public class StateManager : MonoBehaviour
  8. {
  9. [Header("Init")]
  10. public GameObject activeModel;
  11.  
  12.  
  13. [Header("Inputs")]
  14. public float vertical;
  15. public float horizontal;
  16. public float moveAmount;
  17. public Vector3 moveDir;
  18. public bool rt, rb, lt, lb;
  19. public bool rollInput;
  20. public bool itemInput;
  21.  
  22. [Header("Stats")]
  23. public float moveSpeed = 2;
  24. public float runSpeed = 3.5f;
  25. public float rotateSpeed = 5;
  26. public float toGround = 0.5f;
  27. public float rollSpeed = 1;
  28.  
  29. [Header("States")]
  30. public bool onGround;
  31. public bool run;
  32. public bool lockOn;
  33. public bool inAction;
  34. public bool canMove;
  35. public bool isTwoHanded;
  36. public bool usingItem;
  37.  
  38. [Header("Other")]
  39. public EnemyTarget lockOnTarget;
  40. public Transform lockOnTransform;
  41. public AnimationCurve roll_curve;
  42.  
  43. [HideInInspector]
  44. public Animator anim;
  45. [HideInInspector]
  46. public Rigidbody rigid;
  47. [HideInInspector]
  48. public AnimatorHook a_hook;
  49. public ActionManager actionManager;
  50. [HideInInspector]
  51. public InventoryManager inventoryManager;
  52.  
  53. [HideInInspector]
  54. public float delta;
  55. public LayerMask ignoreLayers;
  56.  
  57. float _actionDelay;
  58.  
  59. public void Init()
  60. {
  61. SetupAnimator();
  62. rigid = GetComponent<Rigidbody>();
  63. rigid.angularDrag = 999;
  64. rigid.drag = 4;
  65. rigid.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;
  66.  
  67. inventoryManager = GetComponent<InventoryManager>();
  68. inventoryManager.Init();
  69.  
  70. actionManager = GetComponent<ActionManager>();
  71. actionManager.Init(this);
  72.  
  73. a_hook = activeModel.AddComponent<AnimatorHook>();
  74. a_hook.Init(this);
  75.  
  76. gameObject.layer = 8;
  77. ignoreLayers = ~(1<< 9);
  78.  
  79. anim.SetBool("onGround", true);
  80. }
  81.  
  82. public void SetupAnimator()
  83. {
  84. if(activeModel == null)
  85. {
  86. anim = GetComponentInChildren<Animator>();
  87. if(anim == null)
  88. {
  89. Debug.Log("No model found");
  90. }
  91. else
  92. {
  93. activeModel = anim.gameObject;
  94. }
  95. }
  96.  
  97. if(anim == null)
  98. anim = activeModel.GetComponent<Animator>();
  99.  
  100. anim.applyRootMotion = false;
  101. }
  102.  
  103. public void FixedTick(float d)
  104. {
  105. delta = d;
  106.  
  107. usingItem = anim.GetBool("interacting");
  108. DetectItemAction();
  109. DetectAction();
  110. inventoryManager.curWeapon.weaponModel.SetActive(!usingItem);
  111.  
  112. if (inAction)
  113. {
  114.  
  115. anim.applyRootMotion = true;
  116.  
  117. _actionDelay += delta;
  118. if(_actionDelay > 0.3f)
  119. {
  120. inAction = false;
  121. _actionDelay = 0;
  122. }
  123. else
  124. {
  125. return;
  126. }
  127. }
  128.  
  129. canMove = !anim.GetBool("canMove");
  130.  
  131. if(!canMove)
  132. return;
  133.  
  134. //a_hook.rm_multi = 1;
  135. a_hook.CloseRoll();
  136. HandleRolls();
  137.  
  138. anim.applyRootMotion = false;
  139. rigid.drag = (moveAmount > 0 || onGround == false) ? 0 : 4;
  140.  
  141. float targetSpeed = moveSpeed;
  142. if(run)
  143. targetSpeed = runSpeed;
  144.  
  145. if(onGround)
  146. rigid.velocity = moveDir * (targetSpeed * moveAmount);
  147.  
  148. if(run)
  149. lockOn = false;
  150.  
  151. Vector3 targetDir = (lockOn == false)?
  152. moveDir
  153. :
  154. (lockOnTarget != null)?
  155. lockOnTarget.transform.position - transform.position
  156. :
  157. moveDir;
  158.  
  159. targetDir.y = 0;
  160. if(targetDir == Vector3.zero)
  161. targetDir = transform.forward;
  162. Quaternion tr = Quaternion.LookRotation(targetDir);
  163. Quaternion targetRotation = Quaternion.Slerp(transform.rotation, tr, delta * moveAmount * rotateSpeed);
  164. transform.rotation = targetRotation;
  165.  
  166. anim.SetBool("lockon", lockOn);
  167.  
  168. if (lockOn == false)
  169. HandleMovementAnimations();
  170. else
  171. HandleLockOnAnimations(moveDir);
  172. }
  173.  
  174. public void DetectItemAction()
  175. {
  176. if (canMove == false || usingItem)
  177. return;
  178.  
  179. if (itemInput == false)
  180. return;
  181.  
  182. ItemAction slot = actionManager.consumableItem;
  183. string targetAnim = slot.targetAnim;
  184. if(string.IsNullOrEmpty(targetAnim))
  185. return;
  186.  
  187.  
  188. //inventoryManager.curWeapon.weaponModel.SetActive(false);
  189. usingItem = true;
  190. anim.Play(targetAnim);
  191.  
  192. }
  193.  
  194. public void DetectAction()
  195. {
  196.  
  197. if (canMove == false || usingItem)
  198. return;
  199.  
  200. if (rb == false && rt == false && lt == false && lb == false)
  201. return;
  202.  
  203. string targetAnim = null;
  204.  
  205. Action slot = actionManager.GetActionSlot(this);
  206. if (slot == null)
  207. return;
  208. targetAnim = slot.targetAnim;
  209.  
  210.  
  211. if(string.IsNullOrEmpty(targetAnim))
  212. return;
  213.  
  214. canMove = false;
  215. inAction = true;
  216. anim.CrossFade(targetAnim, 0.2f);
  217. }
  218.  
  219. public void Tick(float d)
  220. {
  221. delta = d;
  222. onGround = OnGround();
  223. anim.SetBool("onGround", onGround);
  224. }
  225.  
  226. void HandleRolls()
  227. {
  228. if (!rollInput || usingItem)
  229. return;
  230.  
  231. float v = vertical;
  232. float h = horizontal;
  233. v = (moveAmount > 0.3f)? 1 : 0;
  234. h = 0;
  235.  
  236. if(v != 0)
  237. {
  238. if (moveDir == Vector3.zero)
  239. moveDir = transform.forward;
  240. Quaternion targetRot = Quaternion.LookRotation(moveDir);
  241. transform.rotation = targetRot;
  242. a_hook.InitForRoll();
  243. a_hook.rm_multi = rollSpeed;
  244. }
  245. else
  246. {
  247. a_hook.rm_multi = 1.3f;
  248. }
  249.  
  250. anim.SetFloat("vertical", v);
  251. anim.SetFloat("horizontal", h);
  252.  
  253. canMove = false;
  254. inAction = true;
  255. anim.CrossFade("Rolls", 0.2f);
  256. }
  257.  
  258. void HandleMovementAnimations()
  259. {
  260. anim.SetBool("run", run);
  261. anim.SetFloat("vertical", moveAmount, 0.4f, delta);
  262. }
  263.  
  264. void HandleLockOnAnimations(Vector3 moveDir)
  265. {
  266. Vector3 relativeDir = transform.InverseTransformDirection(moveDir);
  267. float h = relativeDir.x;
  268. float v = relativeDir.z;
  269.  
  270. anim.SetFloat("vertical", v, 0.2f, delta);
  271. anim.SetFloat("horizontal", h, 0.2f, delta);
  272. }
  273.  
  274. public bool OnGround()
  275. {
  276. bool r = false;
  277.  
  278. Vector3 origin = transform.position + (Vector3.up * toGround);
  279. Vector3 dir = -Vector3.up;
  280. float dis = toGround + 0.3f;
  281. Debug.DrawRay(origin, dir * dis);
  282. RaycastHit hit;
  283. if(Physics.Raycast(origin, dir, out hit, dis))
  284. {
  285. r = true;
  286. Vector3 targetPosition = hit.point;
  287. transform.position = targetPosition;
  288. }
  289.  
  290. return r;
  291. }
  292.  
  293. public void HandleTwoHanded()
  294. {
  295. anim.SetBool("two_handed", isTwoHanded);
  296.  
  297. if (isTwoHanded)
  298. actionManager.UpdateActionsTwoHanded();
  299. else
  300. actionManager.UpdateActionsOneHanded();
  301. }
  302. }
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement