Character.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. //控制效果
  22. SpecialStatus_Float,
  23. SpecialStatus_ShotDown,
  24. SpecialStatus_BlowUp,
  25. SpecialStatus_Weak,
  26. LockSoul = 25, //在锁魂塔中
  27. Conduct = 26, //在指挥中
  28. }
  29. public class Character : MonoBehaviour
  30. {
  31. [Header("骨骼")]
  32. public SkeletonMecanim mecanim;
  33. public Skeleton skeleton;
  34. [HideInInspector]
  35. public MeshRenderer meshRenderer;
  36. [Header("动画控制器")]
  37. public Animator ani;
  38. [Header("重要动画时间")]
  39. public float totalDieKeepTime = 2f;
  40. public float totalAttack_summonTime = 0.5f;
  41. public float totalAttack_marchTime = 0.5f;
  42. [Header("死亡后多久尸体消失")]
  43. [HideInInspector]
  44. public float dieKeepTime;
  45. [Header("受击距离")]
  46. public float beHitDistance;
  47. [HideInInspector]
  48. public float attackDis;
  49. [Header("组件")]
  50. public Rigidbody rb;
  51. public Transform bodyTrans;
  52. public BeSearchTrigger beSearchTrigger;
  53. public SearchTrigger searchTrigger;
  54. public GameObject bodyCollider;
  55. public UIHP uiHp;
  56. public BeHitTrigger beHitTrigger;
  57. public AttackController attackController;
  58. [DisplayOnly]
  59. public Character targetCharacter;
  60. [HideInInspector]
  61. public Character attackTarget;
  62. [Header("角色状态")]
  63. [DisplayOnly]
  64. public CharacterState state;
  65. public int totalHp = 100;
  66. public int hp;
  67. [DisplayOnly]
  68. public bool isDie = false; //已死亡
  69. [DisplayOnly]
  70. public bool isRevive; //从虚弱状态恢复中
  71. public bool canNotAddForce; //不会被打飞
  72. public bool canNotChangeHurt; //不会被打虚弱
  73. [HideInInspector]
  74. public float invincibleTime; //无敌时间
  75. public GameObject injuryNumText;//伤害跳字
  76. public bool showInjuryNum; //伤害跳字开关
  77. public bool canFly = false;
  78. [Header("锁魂塔")]
  79. public LockSoul ls;
  80. public bool isInSoulTower; //在锁魂塔范围内
  81. [Header("体型增大")]
  82. [HideInInspector]
  83. public bool beLarger = false;
  84. public float toLargeSize = 0; //变大程度
  85. private Vector3 speed = new Vector3(1, 1, 0); //变大速度
  86. [Header("特效")]
  87. public GameObject cookEffect; //吃串加血
  88. [Header("传送门")]
  89. public bool Attack_summonShootCanTransmit; //普攻1弓箭可以被传送
  90. //调试开关
  91. [Header("debug攻击者")] public bool debugAttackFrom;
  92. public virtual void Init()
  93. {
  94. //确保组件不丢失
  95. if (!mecanim)
  96. {
  97. mecanim = GetComponentInChildren<SkeletonMecanim>();
  98. }
  99. if (mecanim && skeleton == null)
  100. {
  101. skeleton = mecanim.skeleton;
  102. }
  103. if (!meshRenderer)
  104. {
  105. meshRenderer = mecanim.GetComponent<MeshRenderer>();
  106. }
  107. if (!ani)
  108. {
  109. ani = GetComponentInChildren<Animator>();
  110. }
  111. //血量重置
  112. hp = totalHp;
  113. uiHp.Show(hp, totalHp);
  114. ChangeState(CharacterState.Idle);
  115. if (attackController != null)
  116. {
  117. attackController.Init();
  118. }
  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. //仅造成伤害
  139. public virtual void BeHit(int damage)
  140. {
  141. if(invincibleTime > 0)
  142. {
  143. return;
  144. }
  145. hp -= damage;
  146. //伤害跳字
  147. if (showInjuryNum)
  148. {
  149. GameObject injuryNum = Instantiate(injuryNumText);
  150. injuryNum.transform.position = new Vector3(transform.position.x + Random.Range(-1f, 1f), transform.position.y + 1, transform.position.z);
  151. TextMeshProUGUI text = injuryNum.GetComponentInChildren<TextMeshProUGUI>();
  152. text.text = damage.ToString();
  153. if (gameObject.CompareTag("Player"))
  154. {
  155. text.color = Color.red;
  156. if (debugAttackFrom)
  157. {
  158. Debug.Log("主角受到" + damage.ToString() + "点伤害");
  159. }
  160. }
  161. }
  162. uiHp.Show(hp, totalHp);
  163. if (hp <= 0)
  164. {
  165. ChangeState(CharacterState.Die);
  166. return;
  167. }
  168. }
  169. //造成伤害附加其他效果
  170. public virtual void BeHit(AttackInfo attackInfo, Character attackFrom)
  171. {
  172. if (invincibleTime > 0)
  173. {
  174. return;
  175. }
  176. }
  177. public virtual Vector3 GetMoveDir()
  178. {
  179. Vector3 moveDir = Vector3.zero;
  180. return moveDir;
  181. }
  182. public virtual void SetSortingOrder(int order)
  183. {
  184. meshRenderer.sortingOrder = order;
  185. }
  186. //吃串增加血量上限
  187. public void HpUp(float value, float larger)
  188. {
  189. cookEffect.transform.GetChild(0).gameObject.SetActive(true);
  190. int add = 0;
  191. value = value / 100;
  192. add = (int)(totalHp * value);
  193. totalHp += add;
  194. hp += add;
  195. uiHp.Show(hp, totalHp);
  196. float cur = transform.localScale.x;
  197. toLargeSize = cur * larger;
  198. beLarger = true;
  199. }
  200. //体型变大
  201. public void Enlarge()
  202. {
  203. transform.localScale = Vector3.SmoothDamp(transform.localScale, new Vector3(1, 1, 1) * (toLargeSize + 0.1f), ref speed, 0.6f);
  204. if (transform.localScale.x >= toLargeSize - 0.1f)
  205. {
  206. beLarger = false;
  207. transform.localScale = new Vector3(toLargeSize, toLargeSize, toLargeSize);
  208. toLargeSize = 0;
  209. }
  210. }
  211. public virtual bool AdjustHeight()
  212. {
  213. return true;
  214. }
  215. }