Character.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using Spine.Unity;
  2. using Spine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using TMPro;
  7. //角色状态
  8. public enum CharacterState
  9. {
  10. None = 0,
  11. Idle = 1,
  12. Run = 2,
  13. Rise = 3, //空中上升
  14. Fall = 4, //空中下落
  15. Attack = 6,
  16. KeepAttack = 7,
  17. Summon = 8,
  18. Rush = 9,
  19. Sprint = 10,
  20. Die = 11,
  21. Weak = 12,
  22. Float = 15, //空中漂浮
  23. Coma = 21, //昏迷
  24. LockSoul = 25, //在锁魂塔中
  25. Conduct = 26, //在指挥中
  26. SpecialStatus,
  27. }
  28. public class Character : MonoBehaviour
  29. {
  30. [Header("骨骼")]
  31. public SkeletonMecanim mecanim;
  32. public Skeleton skeleton;
  33. [HideInInspector]
  34. public MeshRenderer meshRenderer;
  35. [Header("动画控制器")]
  36. public Animator ani;
  37. [Header("重要动画时间")]
  38. public float totalDieKeepTime = 2f;
  39. public float totalAttack_summonTime = 0.5f;
  40. public float totalAttack_marchTime = 0.5f;
  41. [Header("死亡后多久尸体消失")]
  42. [HideInInspector]
  43. public float dieKeepTime;
  44. [Header("组件")]
  45. public Rigidbody rb;
  46. public Transform bodyTrans;
  47. public BeSearchTrigger beSearchTrigger;
  48. public SearchTrigger searchTrigger;
  49. public GameObject bodyCollider;
  50. public UIHP uiHp;
  51. public BeHitTrigger beHitTrigger;
  52. public AttackController attackController;
  53. [HideInInspector]
  54. public Character targetCharacter;
  55. [HideInInspector]
  56. public Character attackTarget;
  57. [Header("角色状态")]
  58. [DisplayOnly]
  59. public CharacterState state;
  60. public int totalHp = 100;
  61. public int hp;
  62. [DisplayOnly]
  63. public bool isDie = false; //已死亡
  64. [DisplayOnly]
  65. public bool isRevive; //从虚弱状态恢复中
  66. public bool canNotAddForce; //不会被打飞
  67. public bool canNotChangeHurt; //不会被打虚弱
  68. [HideInInspector]
  69. public float invincibleTime; //无敌时间
  70. public GameObject injuryNumText;//伤害跳字
  71. public bool showInjuryNum; //伤害跳字开关
  72. public bool canFly = false;
  73. [Header("护甲")]
  74. public int armor;
  75. [Header("锁魂塔")]
  76. public LockSoul ls;
  77. public bool isInSoulTower; //在锁魂塔范围内
  78. [Header("体型增大")]
  79. [HideInInspector]
  80. public bool beLarger = false;
  81. public float toLargeSize = 0; //变大程度
  82. private Vector3 speed = new Vector3(1, 1, 0); //变大速度
  83. [Header("特效")]
  84. public GameObject cookEffect; //吃串加血
  85. [Header("传送门")]
  86. public bool Attack_summonShootCanTransmit; //普攻1弓箭可以被传送
  87. //调试开关
  88. [Header("debug攻击者")] public bool debugAttackFrom;
  89. public virtual void Init()
  90. {
  91. //确保组件不丢失
  92. if (!mecanim)
  93. {
  94. mecanim = GetComponentInChildren<SkeletonMecanim>();
  95. }
  96. if (mecanim && skeleton == null)
  97. {
  98. skeleton = mecanim.skeleton;
  99. }
  100. if (!meshRenderer)
  101. {
  102. meshRenderer = mecanim.GetComponent<MeshRenderer>();
  103. }
  104. if (!ani)
  105. {
  106. ani = GetComponentInChildren<Animator>();
  107. }
  108. //血量重置
  109. hp = totalHp;
  110. uiHp.Show(hp, totalHp);
  111. ChangeState(CharacterState.Idle);
  112. if (attackController == null)
  113. {
  114. attackController = GetComponent<AttackController>();
  115. }
  116. attackController.Init();
  117. attackTarget = attackController.attackTarget;
  118. targetCharacter = attackController.targetCharacter;
  119. }
  120. public virtual void FixedUpdate()
  121. {
  122. OnState();
  123. }
  124. public void Turn()
  125. {
  126. bodyTrans.localScale = new Vector3(-bodyTrans.localScale.x, bodyTrans.localScale.y, bodyTrans.localScale.z);
  127. }
  128. public virtual void OnState()
  129. {
  130. }
  131. public virtual void ChangeState(CharacterState newState)
  132. {
  133. }
  134. public void DebugAttackFrom(string attackFrom, int damage)
  135. {
  136. Debug.Log(attackFrom + "对" + gameObject.name + "造成了" + damage.ToString() + "点伤害");
  137. }
  138. public virtual void BeHit(int damage, Vector3 force, bool changeHurt, float repelValue)
  139. {
  140. //无敌状态下免伤
  141. if (invincibleTime > 0)
  142. {
  143. return;
  144. }
  145. //非无敌状态扣血
  146. hp -= damage;
  147. //伤害跳字
  148. if (showInjuryNum)
  149. {
  150. GameObject injuryNum = Instantiate(injuryNumText);
  151. injuryNum.transform.position = new Vector3(transform.position.x + Random.Range(-1f, 1f), transform.position.y + 1, transform.position.z);
  152. TextMeshProUGUI text = injuryNum.GetComponentInChildren<TextMeshProUGUI>();
  153. text.text = damage.ToString();
  154. if (gameObject.CompareTag("Player"))
  155. {
  156. text.color = Color.red;
  157. }
  158. }
  159. uiHp.Show(hp, totalHp);
  160. if (hp <= 0)
  161. {
  162. if (!canNotAddForce)
  163. rb.AddForce(force);
  164. ChangeState(CharacterState.Die);
  165. return;
  166. }
  167. }
  168. public virtual Vector3 GetMoveDir()
  169. {
  170. Vector3 moveDir = Vector3.zero;
  171. return moveDir;
  172. }
  173. public virtual void SetSortingOrder(int order)
  174. {
  175. meshRenderer.sortingOrder = order;
  176. }
  177. //吃串增加血量上限
  178. public void HpUp(float value, float larger)
  179. {
  180. cookEffect.transform.GetChild(0).gameObject.SetActive(true);
  181. int add = 0;
  182. value = value / 100;
  183. add = (int)(totalHp * value);
  184. totalHp += add;
  185. hp += add;
  186. uiHp.Show(hp, totalHp);
  187. float cur = transform.localScale.x;
  188. toLargeSize = cur * larger;
  189. beLarger = true;
  190. }
  191. //体型变大
  192. public void Enlarge()
  193. {
  194. transform.localScale = Vector3.SmoothDamp(transform.localScale, new Vector3(1, 1, 1) * (toLargeSize + 0.1f), ref speed, 0.6f);
  195. if (transform.localScale.x >= toLargeSize - 0.1f)
  196. {
  197. beLarger = false;
  198. transform.localScale = new Vector3(toLargeSize, toLargeSize, toLargeSize);
  199. toLargeSize = 0;
  200. }
  201. }
  202. }