Character.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. //Hurt = 5,
  16. Attack = 6,
  17. KeepAttack = 7,
  18. Summon = 8,
  19. Rush = 9,
  20. Sprint = 10,
  21. Die = 11,
  22. Weak = 12,
  23. PullRope = 13,
  24. Spirits = 14, //召唤英灵
  25. Float = 15, //空中漂浮
  26. FindPlayer = 16, //寻找玩家
  27. ReadyToRush = 17, //瞄准准备冲刺
  28. ReadyToDownRush = 18, //准备落地冲刺
  29. DownRush = 19, //落地冲刺
  30. FinishRush = 20, //结束冲刺
  31. Coma = 21, //昏迷
  32. RushAttack = 22, //带攻击的冲刺
  33. Transfiguration = 23, //变身
  34. BaGua = 24, //八卦状态
  35. LockSoul = 25, //在锁魂塔中
  36. Conduct = 26, //在指挥中
  37. SpecialStatus,
  38. }
  39. //攻击类型
  40. public enum AttackType
  41. {
  42. Melee = 0, //近战
  43. Shoot = 1, //射击
  44. Dash = 2, //英灵刺客
  45. }
  46. //血量上限增加类型
  47. public enum HpUpType
  48. {
  49. Degree = 0, //按一定比例加血
  50. Add = 1, //直接加血
  51. Fixed = 2, //加固定血量
  52. }
  53. [System.Serializable]
  54. public struct SpineAniKey
  55. {
  56. public string aniName;
  57. public List<AttackKeyType> keys;
  58. }
  59. public enum KeyType
  60. {
  61. AttackStart,
  62. AttackEnd,
  63. }
  64. [System.Serializable]
  65. public struct AttackKeyType
  66. {
  67. public KeyType attackType;
  68. public string startKeyName;
  69. public float startKeyTime;
  70. public KeyType endType;
  71. public string endKeyName;
  72. public float endKeyTime;
  73. }
  74. public class Character : MonoBehaviour
  75. {
  76. [Header("骨骼")]
  77. public SkeletonMecanim mecanim;
  78. public Skeleton skeleton;
  79. [HideInInspector]
  80. public MeshRenderer meshRenderer;
  81. [Header("动画控制器")]
  82. public Animator ani;
  83. [Header("重要动画时间")]
  84. public float totalDieKeepTime = 2f;
  85. public float totalAttack_summonTime = 0.5f;
  86. public float totalAttack_marchTime = 0.5f;
  87. [Header("所有攻击帧事件及时间")]
  88. public List<SpineAniKey> attackKeys;
  89. [Header("死亡后多久尸体消失")]
  90. [HideInInspector]
  91. public float dieKeepTime;
  92. [Header("组件")]
  93. public Rigidbody rb;
  94. public Transform bodyTrans;
  95. public BeSearchTrigger beSearchTrigger;
  96. public SearchTrigger searchTrigger;
  97. public GameObject bodyCollider;
  98. public UIHP uiHp;
  99. public BeHitTrigger beHitTrigger;
  100. [Header("角色状态")]
  101. public CharacterState state;
  102. public int totalHp = 100;
  103. public int hp;
  104. public bool isDie = false; //已死亡
  105. public bool isRevive; //从虚弱状态恢复中
  106. public bool linked; //被铁链连着
  107. public bool canNotAddForce; //不会被打飞
  108. public bool canNotChangeHurt; //不会被打虚弱
  109. [HideInInspector]
  110. public float invincibleTime; //无敌时间
  111. public GameObject injuryNumText;//伤害跳字
  112. public bool showInjuryNum; //伤害跳字开关
  113. public bool canFly = false;
  114. [Header("护甲")]
  115. public int armor;
  116. [Header("锁魂塔")]
  117. public LockSoul ls;
  118. public bool isInSoulTower; //在锁魂塔范围内
  119. [Header("普通攻击信息")]
  120. public bool canHitFly;
  121. [HideInInspector]
  122. public bool isNonAttack = false; //无普攻
  123. public AttackType attackType; //攻击类型
  124. public GameObject bulletPrefab; //远程攻击的子弹
  125. public List<Transform> shootPos; //远程攻击的子弹发射位置
  126. public bool shootTrack = false; //远程攻击是否初始时瞄准目标
  127. public bool shootAlwaysTrack = false; //远程攻击是否始终追踪
  128. public List<AttackInfo> Attack_summonInfos; //普攻1信息(出场攻击)
  129. public List<AttackInfo> Attack_marchInfos; //普攻2信息
  130. public List<AttackTrigger> attackTriggers; //普攻触发器,敌方进入后播放动画进行攻击
  131. public GameObject addAttackEffect;
  132. public int armorPiercing; //穿甲率
  133. public int[] curDamage1;
  134. public int[] curDamage2;
  135. public float attackTime;
  136. public float attackKeyCount; //攻击进行时间
  137. public float nextStartKeyTime, nextEndKeyTime; //下一个出现/消失attacktrigger的时间
  138. public List<float> keyTimes; //所有的帧事件时间
  139. public int curKeyNum; //当前锁定到第几个帧事件
  140. public bool isAttackTriggerOn = false; //当前Attack Trigger是否开启
  141. [Header("目标")]
  142. public List<TargetType> targetTypes;
  143. public Character targetCharacter;
  144. public Character attackTarget;
  145. public List<Character> beTargetCharacter = new List<Character>(); //被哪些锁定
  146. public float getDistanceOffset = 0f;
  147. [Header("是否为英灵、是否为变身形态英灵")]
  148. public bool isSpirit;
  149. public bool isTran;
  150. [HideInInspector]
  151. public PlayerController pc;
  152. [Header("血量上限增加类型")]
  153. public HpUpType hptp;
  154. [Header("体型增大")]
  155. public bool beLarger = false;
  156. public float toLargeSize = 0; //变大程度
  157. private Vector3 speed = new Vector3(1, 1, 0); //变大速度
  158. [Header("特效")]
  159. public GameObject cookEffect; //吃串加血
  160. [Header("传送门")]
  161. public bool Attack_summonShootCanTransmit; //普攻1弓箭可以被传送
  162. //调试开关
  163. [Header("debug攻击者")] public bool debugAttackFrom;
  164. public virtual void Init()
  165. {
  166. //确保组件不丢失
  167. if (!mecanim)
  168. {
  169. mecanim = GetComponentInChildren<SkeletonMecanim>();
  170. }
  171. if (mecanim && skeleton == null)
  172. {
  173. skeleton = mecanim.skeleton;
  174. }
  175. if (!meshRenderer)
  176. {
  177. meshRenderer = mecanim.GetComponent<MeshRenderer>();
  178. }
  179. if (!ani)
  180. {
  181. ani = GetComponentInChildren<Animator>();
  182. }
  183. //血量重置
  184. hp = totalHp;
  185. if (!isNonAttack)
  186. {
  187. uiHp.Show(hp, totalHp);
  188. }
  189. ChangeState(CharacterState.Idle);
  190. curDamage1 = new int[Attack_summonInfos.Count];
  191. for (int i = 0; i < Attack_summonInfos.Count; i++)
  192. {
  193. curDamage1[i] = Attack_summonInfos[i].damage;
  194. }
  195. curDamage2 = new int[Attack_marchInfos.Count];
  196. for (int i = 0; i < Attack_marchInfos.Count; i++)
  197. {
  198. curDamage2[i] = Attack_marchInfos[i].damage;
  199. }
  200. }
  201. public virtual void FixedUpdate()
  202. {
  203. OnState();
  204. }
  205. private void OnEnable()
  206. {
  207. if (attackTriggers != null)
  208. {
  209. for (int i = 0; i < attackTriggers.Count; i++)
  210. {
  211. attackTriggers[i].gameObject.SetActive(false);
  212. }
  213. }
  214. }
  215. public void Turn()
  216. {
  217. bodyTrans.localScale = new Vector3(-bodyTrans.localScale.x, bodyTrans.localScale.y, bodyTrans.localScale.z);
  218. }
  219. public virtual void OnState()
  220. {
  221. }
  222. public virtual void ChangeState(CharacterState newState)
  223. {
  224. }
  225. public void DebugAttackFrom(string attackFrom, int damage)
  226. {
  227. Debug.Log(attackFrom + "对" + gameObject.name + "造成了" + damage.ToString() + "点伤害");
  228. }
  229. public virtual void BeHit(int damage, Vector3 force, bool changeHurt, float repelValue)
  230. {
  231. //无敌状态下免伤
  232. if (invincibleTime > 0)
  233. {
  234. return;
  235. }
  236. //非无敌状态扣血
  237. hp -= damage;
  238. //伤害跳字
  239. if (showInjuryNum)
  240. {
  241. GameObject injuryNum = Instantiate(injuryNumText);
  242. injuryNum.transform.position = new Vector3(transform.position.x + Random.Range(-1f, 1f), transform.position.y + 1, transform.position.z);
  243. TextMeshProUGUI text = injuryNum.GetComponentInChildren<TextMeshProUGUI>();
  244. text.text = damage.ToString();
  245. if (gameObject.CompareTag("Player"))
  246. {
  247. text.color = Color.red;
  248. }
  249. }
  250. uiHp.Show(hp, totalHp);
  251. if (hp <= 0)
  252. {
  253. if (!canNotAddForce)
  254. rb.AddForce(force);
  255. ChangeState(CharacterState.Die);
  256. return;
  257. }
  258. }
  259. public virtual void AttackShootEvent(int attackId, int shootId)
  260. {
  261. AttackInfo attackInfo;
  262. if (attackId == 1)
  263. {
  264. attackInfo = Attack_summonInfos[shootId];
  265. }
  266. else
  267. {
  268. attackInfo = Attack_marchInfos[shootId];
  269. attackInfo.damage = curDamage2[0];
  270. }
  271. GameObject bulletObj = PoolManager.Instantiate(bulletPrefab);
  272. Bullet bullet = bulletObj.GetComponent<Bullet>();
  273. Vector3 attackDir = attackInfo.attackDir.normalized;
  274. if (bodyTrans.localScale.x < 0)
  275. {
  276. attackDir.x = -attackDir.x;
  277. }
  278. if (attackId == 1 && Attack_summonShootCanTransmit)
  279. {
  280. bullet.canTransmit = true;
  281. }
  282. bullet.BeShoot(this, shootPos[shootId].position, attackDir, attackInfo.damage, attackInfo.force, attackInfo.changeHurt, attackInfo.repelValue, shootTrack, shootAlwaysTrack, attackTarget ? attackTarget : null);
  283. }
  284. public virtual Vector3 GetMoveDir()
  285. {
  286. Vector3 moveDir = Vector3.zero;
  287. return moveDir;
  288. }
  289. public void SetNextKeyTimes()
  290. {
  291. if (curKeyNum < keyTimes.Count)
  292. {
  293. nextStartKeyTime = keyTimes[curKeyNum];
  294. nextEndKeyTime = keyTimes[curKeyNum + 1];
  295. curKeyNum += 2;
  296. }
  297. }
  298. public virtual void Attack_summon()
  299. {
  300. ani.Play("attack_summon", 0, 0);
  301. if (!isNonAttack)
  302. {
  303. if (attackType == AttackType.Shoot)
  304. {
  305. foreach(AttackTrigger at in attackTriggers)
  306. {
  307. at.isShoot = true;
  308. at.GetComponent<Collider>().enabled = false;
  309. at.type = AttackTrigger.attackTpye.summon;
  310. }
  311. }
  312. attackTime = totalAttack_summonTime;
  313. attackKeyCount = 0;
  314. keyTimes = new List<float>();
  315. foreach(SpineAniKey sak in attackKeys)
  316. {
  317. if (sak.aniName == "attack_summon")
  318. {
  319. foreach(AttackKeyType akt in sak.keys)
  320. {
  321. keyTimes.Add(akt.startKeyTime);
  322. keyTimes.Add(akt.endKeyTime);
  323. }
  324. }
  325. }
  326. curKeyNum = 0;
  327. SetNextKeyTimes();
  328. switch (attackType)
  329. {
  330. case AttackType.Melee:
  331. for (int i = 0; i < Attack_summonInfos.Count; i++)
  332. {
  333. attackTriggers[i].damage = curDamage1[i];
  334. attackTriggers[i].changeHurt = Attack_summonInfos[i].changeHurt;
  335. attackTriggers[i].repelValue = Attack_summonInfos[i].repelValue;
  336. Vector3 attackDir = Attack_summonInfos[i].attackDir.normalized;
  337. if (bodyTrans.localScale.x < 0)
  338. {
  339. attackDir.x = -attackDir.x;
  340. }
  341. attackTriggers[i].force = attackDir * Attack_summonInfos[i].force;
  342. }
  343. break;
  344. case AttackType.Shoot:
  345. case AttackType.Dash:
  346. break;
  347. default:
  348. break;
  349. }
  350. ChangeState(CharacterState.Attack);
  351. }
  352. }
  353. public virtual void Attack_march()
  354. {
  355. Vector3 leftDir = GetMoveDir();
  356. if (leftDir.x > 0.3f)
  357. {
  358. if (bodyTrans.localScale.x > 0)
  359. {
  360. Turn();
  361. }
  362. }
  363. else if (leftDir.x < -0.3f)
  364. {
  365. if (bodyTrans.localScale.x < 0)
  366. {
  367. Turn();
  368. }
  369. }
  370. ani.Play("attack_march", 0, 0);
  371. if (attackType == AttackType.Shoot)
  372. {
  373. foreach (AttackTrigger at in attackTriggers)
  374. {
  375. at.isShoot = true;
  376. at.GetComponent<Collider>().enabled = false;
  377. at.type = AttackTrigger.attackTpye.march;
  378. }
  379. }
  380. attackTime = totalAttack_marchTime;
  381. attackKeyCount = 0;
  382. keyTimes = new List<float>();
  383. foreach (SpineAniKey sak in attackKeys)
  384. {
  385. if (sak.aniName == "attack_march")
  386. {
  387. foreach (AttackKeyType akt in sak.keys)
  388. {
  389. keyTimes.Add(akt.startKeyTime);
  390. keyTimes.Add(akt.endKeyTime);
  391. }
  392. }
  393. }
  394. curKeyNum = 0;
  395. SetNextKeyTimes();
  396. if (attackTriggers.Count > 0)
  397. {
  398. for (int i = 0; i < Attack_marchInfos.Count; i++)
  399. {
  400. attackTriggers[i].damage = curDamage2[i];
  401. attackTriggers[i].changeHurt = Attack_marchInfos[i].changeHurt;
  402. attackTriggers[i].repelValue = Attack_marchInfos[i].repelValue;
  403. Vector3 attackDir = Attack_marchInfos[i].attackDir.normalized;
  404. if (bodyTrans.localScale.x < 0)
  405. {
  406. attackDir.x = -attackDir.x;
  407. }
  408. attackTriggers[i].force = attackDir * Attack_marchInfos[i].force;
  409. }
  410. }
  411. ChangeState(CharacterState.Attack);
  412. }
  413. public virtual void SetSortingOrder(int order)
  414. {
  415. meshRenderer.sortingOrder = order;
  416. }
  417. //吃串增加血量上限
  418. public void HpUp(float value, float larger)
  419. {
  420. cookEffect.transform.GetChild(0).gameObject.SetActive(true);
  421. int add = 0;
  422. switch (hptp)
  423. {
  424. case HpUpType.Degree:
  425. value = value / 100;
  426. add = (int)(totalHp * value);
  427. break;
  428. case HpUpType.Add:
  429. add = (int)value;
  430. break;
  431. case HpUpType.Fixed:
  432. add = (int)(totalHp - value);
  433. break;
  434. }
  435. totalHp += add;
  436. hp += add;
  437. uiHp.Show(hp, totalHp);
  438. float cur = transform.localScale.x;
  439. toLargeSize = cur * larger;
  440. beLarger = true;
  441. }
  442. //体型变大
  443. public void Enlarge()
  444. {
  445. transform.localScale = Vector3.SmoothDamp(transform.localScale, new Vector3(1, 1, 1) * (toLargeSize + 0.1f), ref speed, 0.6f);
  446. if (transform.localScale.x >= toLargeSize - 0.1f)
  447. {
  448. beLarger = false;
  449. transform.localScale = new Vector3(toLargeSize, toLargeSize, toLargeSize);
  450. toLargeSize = 0;
  451. }
  452. }
  453. }