Character.cs 14 KB

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