Character.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using Spine.Unity;
  2. using Spine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. public enum CharacterState
  7. {
  8. None = 0,
  9. Idle = 1,
  10. Run = 2,
  11. Rise = 3,//¿ÕÖÐÉÏÉý
  12. Fall = 4,//¿ÕÖÐÏÂÂä
  13. Hurt = 5,
  14. Attack = 6,
  15. Summon = 7,
  16. Rush = 8,
  17. Die = 9,
  18. }
  19. public class Character : MonoBehaviour
  20. {
  21. public SkeletonMecanim mecanim;
  22. public Skeleton skeleton;
  23. public MeshRenderer meshRenderer;
  24. public Animator ani;
  25. public Animator aniCollider;
  26. public Rigidbody rb;
  27. public Transform bodyTrans;
  28. public UIHP uiHp;
  29. public CharacterState state;
  30. [HideInInspector]
  31. public float attackTime;
  32. public float totalAttack1Time = 0.5f;
  33. public float totalAttack2Time = 0.5f;
  34. public bool isDie = false;
  35. public int totalHp = 100;
  36. public int hp;
  37. public List<AttackInfo> attack1Infos;
  38. public List<AttackInfo> attack2Infos;
  39. public List<AttackTrigger> attackTriggers;
  40. public AttackType attackType;
  41. public GameObject bulletPrefab;
  42. public List<Transform> shootPos;
  43. [HideInInspector]
  44. public float dieKeepTime;
  45. public float totalDieKeepTime = 2f;
  46. public virtual void Init()
  47. {
  48. if (!mecanim)
  49. {
  50. mecanim = GetComponentInChildren<SkeletonMecanim>();
  51. }
  52. if (skeleton == null)
  53. {
  54. skeleton = mecanim.skeleton;
  55. }
  56. if (!meshRenderer)
  57. {
  58. meshRenderer = mecanim.GetComponent<MeshRenderer>();
  59. }
  60. if (!ani)
  61. {
  62. ani = GetComponentInChildren<Animator>();
  63. }
  64. hp = totalHp;
  65. uiHp.ShowHP(hp, totalHp);
  66. ChangeState(CharacterState.Idle);
  67. }
  68. public virtual void FixedUpdate()
  69. {
  70. OnState();
  71. }
  72. public void Turn()
  73. {
  74. bodyTrans.localScale = new Vector3(-bodyTrans.localScale.x, bodyTrans.localScale.y, bodyTrans.localScale.z);
  75. }
  76. public virtual void OnState()
  77. {
  78. }
  79. public virtual void ChangeState(CharacterState newState)
  80. {
  81. }
  82. public virtual void BeHit(int damage, Vector3 force)
  83. {
  84. hp -= damage;
  85. uiHp.ShowHP(hp, totalHp);
  86. rb.AddForce(force);
  87. if (hp < 0)
  88. {
  89. ChangeState(CharacterState.Die);
  90. }
  91. else
  92. {
  93. ChangeState(CharacterState.Hurt);
  94. }
  95. }
  96. public void AttackShootEvent(int attackId, int shootId)
  97. {
  98. AttackInfo attackInfo;
  99. if (attackId == 1)
  100. {
  101. attackInfo = attack1Infos[shootId];
  102. }
  103. else
  104. {
  105. attackInfo = attack2Infos[shootId];
  106. }
  107. GameObject bulletObj = PoolManager.Instantiate(bulletPrefab);
  108. Bullet bullet = bulletObj.GetComponent<Bullet>();
  109. bullet.damage = attackInfo.damage;
  110. Vector3 attackDir = attackInfo.attackDir.normalized;
  111. if (bodyTrans.localScale.x < 0)
  112. {
  113. attackDir.x = -attackDir.x;
  114. }
  115. bullet.BeShoot(this, shootPos[shootId].position, attackDir, attackInfo.damage, attackDir * attackInfo.force);
  116. }
  117. public virtual Vector3 GetMoveDir()
  118. {
  119. Vector3 moveDir = Vector3.zero;
  120. return moveDir;
  121. }
  122. public virtual void Attack1()
  123. {
  124. Vector3 leftDir = GetMoveDir();
  125. if (leftDir.x > 0.3f)
  126. {
  127. if (bodyTrans.localScale.x > 0)
  128. {
  129. Turn();
  130. }
  131. }
  132. else if (leftDir.x < -0.3f)
  133. {
  134. if (bodyTrans.localScale.x < 0)
  135. {
  136. Turn();
  137. }
  138. }
  139. ani.Play("attack1", 0, 0);
  140. aniCollider.Play("Attack1", 1, 0);
  141. attackTime = totalAttack1Time;
  142. switch (attackType)
  143. {
  144. case AttackType.Melee:
  145. for (int i = 0; i < attack1Infos.Count; i++)
  146. {
  147. attackTriggers[i].damage = attack1Infos[i].damage;
  148. Vector3 attackDir = attack1Infos[i].attackDir.normalized;
  149. if (bodyTrans.localScale.x < 0)
  150. {
  151. attackDir.x = -attackDir.x;
  152. }
  153. attackTriggers[i].force = attackDir * attack1Infos[i].force;
  154. }
  155. break;
  156. case AttackType.Shoot:
  157. break;
  158. default:
  159. break;
  160. }
  161. ChangeState(CharacterState.Attack);
  162. }
  163. public virtual void Attack2()
  164. {
  165. Vector3 leftDir = GetMoveDir();
  166. if (leftDir.x > 0.3f)
  167. {
  168. if (bodyTrans.localScale.x > 0)
  169. {
  170. Turn();
  171. }
  172. }
  173. else if (leftDir.x < -0.3f)
  174. {
  175. if (bodyTrans.localScale.x < 0)
  176. {
  177. Turn();
  178. }
  179. }
  180. ani.Play("attack2", 0, 0);
  181. aniCollider.Play("Attack2", 1, 0);
  182. attackTime = totalAttack2Time;
  183. switch (attackType)
  184. {
  185. case AttackType.Melee:
  186. for (int i = 0; i < attack2Infos.Count; i++)
  187. {
  188. attackTriggers[i].damage = attack2Infos[i].damage;
  189. Vector3 attackDir = attack2Infos[i].attackDir.normalized;
  190. if (bodyTrans.localScale.x < 0)
  191. {
  192. attackDir.x = -attackDir.x;
  193. }
  194. attackTriggers[i].force = attackDir * attack2Infos[i].force;
  195. }
  196. break;
  197. case AttackType.Shoot:
  198. break;
  199. default:
  200. break;
  201. }
  202. ChangeState(CharacterState.Attack);
  203. }
  204. public void SetSortingOrder(int order)
  205. {
  206. meshRenderer.sortingOrder = order;
  207. }
  208. }