Character.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. using Spine.Unity;
  2. using Spine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using TMPro;
  7. using Sirenix.OdinInspector;
  8. //角色状态
  9. public enum CharacterState
  10. {
  11. None = 0,
  12. Idle = 1,
  13. Run = 2,
  14. Rise = 3, //空中上升
  15. Fall = 4, //空中下落
  16. Attack = 6,
  17. KeepAttack = 7,
  18. Summon = 8,
  19. Rush = 9,
  20. Sprint = 10,
  21. Die = 11,
  22. HitStun, //僵直
  23. //控制效果
  24. SpecialStatus_Float,
  25. SpecialStatus_ShotDown,
  26. SpecialStatus_BlowUp,
  27. SpecialStatus_Weak,
  28. LockSoul = 25, //在锁魂塔中
  29. Conduct = 26, //在指挥中
  30. }
  31. public class Character : MonoBehaviour
  32. {
  33. [FoldoutGroup("组件")] public Rigidbody rb;
  34. [FoldoutGroup("组件")] public Transform bodyTrans;
  35. [FoldoutGroup("组件")] public BeSearchTrigger beSearchTrigger;
  36. [FoldoutGroup("组件")] public SearchTrigger searchTrigger;
  37. [FoldoutGroup("组件")] public GameObject bodyCollider;
  38. [FoldoutGroup("组件")] public UIHP uiHp;
  39. [FoldoutGroup("组件")] public BeHitTrigger beHitTrigger;
  40. [FoldoutGroup("组件")] public AttackController attackController;
  41. [Header("骨骼")]
  42. public SkeletonMecanim mecanim;
  43. public Skeleton skeleton;
  44. [HideInInspector]
  45. public MeshRenderer meshRenderer;
  46. [Header("动画控制器")]
  47. public Animator ani;
  48. [Header("重要动画时间")]
  49. public float totalDieKeepTime = 2f;
  50. public float totalAttack_summonTime = 0.5f;
  51. public float totalAttack_marchTime = 0.5f;
  52. [Header("死亡后多久尸体消失")]
  53. [HideInInspector]
  54. public float dieKeepTime;
  55. [Header("受击距离")]
  56. public float beHitDistance;
  57. [HideInInspector]
  58. public float attackDis;
  59. [DisplayOnly]
  60. public Character targetCharacter;
  61. [HideInInspector]
  62. public Character attackTarget;
  63. [Header("角色状态")]
  64. [DisplayOnly]
  65. public CharacterState state;
  66. public int totalHp = 100;
  67. public int hp;
  68. [DisplayOnly]
  69. public bool isDie = false; //已死亡
  70. [DisplayOnly]
  71. public bool isRevive; //从虚弱状态恢复中
  72. public bool canNotAddForce; //不会被打飞
  73. public bool canNotChangeHurt; //不会被打虚弱
  74. [DisplayOnly]
  75. public float invincibleTime; //无敌时间
  76. public GameObject injuryNumText;//伤害跳字
  77. public GameObject injuryNumTextSummon;//伤害跳字(起手式)
  78. [HideInInspector]
  79. public bool isBeHitBySummonAttack;
  80. public bool showInjuryNum; //伤害跳字开关
  81. public bool canFly = false;
  82. [HideInInspector]public bool nowCanFly; //当前是否能飞行
  83. [Header("状态显示")]
  84. public GameObject statePre;
  85. private GameObject curStateObj;
  86. private TextMeshProUGUI stateText;
  87. private float pastWordTime;
  88. [Header("锁魂塔")]
  89. public LockSoul ls;
  90. public bool isInSoulTower; //在锁魂塔范围内
  91. [Header("体型增大")]
  92. [HideInInspector]
  93. public bool beLarger = false;
  94. public float toLargeSize = 0; //变大程度
  95. private Vector3 speed = new Vector3(1, 1, 0); //变大速度
  96. [Header("特效")]
  97. public GameObject cookEffect; //吃串加血
  98. [Header("传送门")]
  99. public bool Attack_summonShootCanTransmit; //普攻1弓箭可以被传送
  100. //调试开关
  101. [Header("debug攻击者")] public bool debugAttackFrom;
  102. [FoldoutGroup("伤害跳字")] [LabelText("起手式位置")] public Vector2 injuryNumPos_summon = new Vector2(0, 1);
  103. [FoldoutGroup("伤害跳字")] [LabelText("起手式随机范围")] public Vector2 injuryNumRandom_summon = Vector2.one;
  104. [FoldoutGroup("伤害跳字")] [LabelText("行军式位置")] public Vector2 injuryNumPos_march = new Vector2(0, 1);
  105. [FoldoutGroup("伤害跳字")] [LabelText("行军式随机范围")] public Vector2 injuryNumRandom_march = Vector2.one * 2;
  106. public virtual void Init()
  107. {
  108. //确保组件不丢失
  109. if (!mecanim)
  110. {
  111. mecanim = GetComponentInChildren<SkeletonMecanim>();
  112. }
  113. if (mecanim && skeleton == null)
  114. {
  115. skeleton = mecanim.skeleton;
  116. }
  117. if (!meshRenderer)
  118. {
  119. meshRenderer = mecanim.GetComponent<MeshRenderer>();
  120. }
  121. if (!ani)
  122. {
  123. ani = GetComponentInChildren<Animator>();
  124. }
  125. //血量重置
  126. hp = totalHp;
  127. nowCanFly = canFly;
  128. uiHp.Show(hp, totalHp);
  129. ChangeState(CharacterState.Idle);
  130. if (attackController != null)
  131. {
  132. attackController.Init();
  133. }
  134. }
  135. public virtual void FixedUpdate()
  136. {
  137. OnState();
  138. }
  139. public void Turn()
  140. {
  141. bodyTrans.localScale = new Vector3(-bodyTrans.localScale.x, bodyTrans.localScale.y, bodyTrans.localScale.z);
  142. }
  143. public virtual void OnState()
  144. {
  145. if (pastWordTime > 0)
  146. {
  147. pastWordTime -= Time.deltaTime;
  148. if (pastWordTime <= 0)
  149. {
  150. curStateObj.SetActive(false);
  151. pastWordTime = 0;
  152. }
  153. }
  154. }
  155. public virtual void ChangeState(CharacterState newState)
  156. {
  157. }
  158. public void DebugAttackFrom(string attackFrom, int damage)
  159. {
  160. Debug.Log(attackFrom + "对" + gameObject.name + "使用了" + damage.ToString() + "点伤害的攻击");
  161. }
  162. //仅造成伤害
  163. public virtual void BeHit(int damage)
  164. {
  165. if (invincibleTime > 0)
  166. {
  167. return;
  168. }
  169. hp -= damage;
  170. //伤害跳字
  171. if (showInjuryNum)
  172. {
  173. GameObject injuryNum;
  174. //是起手式
  175. if (isBeHitBySummonAttack)
  176. {
  177. injuryNum = Instantiate(injuryNumTextSummon);
  178. injuryNum.transform.position = new Vector3(
  179. transform.position.x + injuryNumPos_summon.x + Random.Range(-injuryNumRandom_summon.x/2f, injuryNumRandom_summon.x/2f),
  180. transform.position.y + injuryNumPos_summon.y + Random.Range(-injuryNumRandom_summon.y/2f, injuryNumRandom_summon.y/2f),
  181. transform.position.z);
  182. }
  183. //不是起手式
  184. else
  185. {
  186. injuryNum = Instantiate(injuryNumText);
  187. injuryNum.transform.position = injuryNum.transform.position = new Vector3(
  188. transform.position.x + injuryNumPos_march.x + Random.Range(-injuryNumRandom_march.x/2f, injuryNumRandom_march.x/2f),
  189. transform.position.y + injuryNumPos_march.y + Random.Range(-injuryNumRandom_march.y/2f, injuryNumRandom_march.y/2f),
  190. transform.position.z);
  191. }
  192. TextMeshProUGUI text = injuryNum.GetComponentInChildren<TextMeshProUGUI>();
  193. text.text = damage.ToString();
  194. if (gameObject.CompareTag("Player"))
  195. {
  196. text.color = Color.red;
  197. if (debugAttackFrom)
  198. {
  199. Debug.Log("主角受到" + damage.ToString() + "点伤害");
  200. }
  201. }
  202. }
  203. uiHp.Show(hp, totalHp);
  204. if (hp <= 0)
  205. {
  206. ChangeState(CharacterState.Die);
  207. return;
  208. }
  209. }
  210. //造成伤害附加其他效果
  211. public virtual void BeHit(AttackInfo attackInfo, Character attackFrom)
  212. {
  213. }
  214. public virtual Vector3 GetMoveDir()
  215. {
  216. Vector3 moveDir = Vector3.zero;
  217. return moveDir;
  218. }
  219. public virtual void SetSortingOrder(int order)
  220. {
  221. meshRenderer.sortingOrder = order;
  222. }
  223. //吃串增加血量上限
  224. public void HpUp(float value, float larger)
  225. {
  226. cookEffect.transform.GetChild(0).gameObject.SetActive(true);
  227. int add = 0;
  228. value = value / 100;
  229. add = (int)(totalHp * value);
  230. totalHp += add;
  231. hp += add;
  232. uiHp.Show(hp, totalHp);
  233. float cur = transform.localScale.x;
  234. toLargeSize = cur * larger;
  235. beLarger = true;
  236. }
  237. //体型变大
  238. public void Enlarge()
  239. {
  240. transform.localScale = Vector3.SmoothDamp(transform.localScale, new Vector3(1, 1, 1) * (toLargeSize + 0.1f), ref speed, 0.6f);
  241. if (transform.localScale.x >= toLargeSize - 0.1f)
  242. {
  243. beLarger = false;
  244. transform.localScale = new Vector3(toLargeSize, toLargeSize, toLargeSize);
  245. toLargeSize = 0;
  246. }
  247. }
  248. public virtual bool AdjustHeight()
  249. {
  250. return true;
  251. }
  252. public void ChangeStateText(CharacterState state)
  253. {
  254. if (curStateObj == null)
  255. {
  256. curStateObj = Instantiate(statePre, transform);
  257. stateText = curStateObj.GetComponentInChildren<TextMeshProUGUI>();
  258. }
  259. pastWordTime += 0.5f;
  260. switch (state)
  261. {
  262. case CharacterState.SpecialStatus_Float:
  263. stateText.text = "漂浮";
  264. break;
  265. case CharacterState.SpecialStatus_ShotDown:
  266. stateText.text = "击落";
  267. break;
  268. case CharacterState.SpecialStatus_BlowUp:
  269. stateText.text = "击飞";
  270. break;
  271. case CharacterState.SpecialStatus_Weak:
  272. stateText.text = "击晕";
  273. break;
  274. case CharacterState.HitStun:
  275. stateText.text = "僵直";
  276. break;
  277. }
  278. curStateObj.SetActive(true);
  279. }
  280. }