Character.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. }
  38. //攻击类型
  39. public enum AttackType
  40. {
  41. Melee = 0, //近战
  42. Shoot = 1, //射击
  43. Dash = 2, //英灵刺客
  44. }
  45. //血量上限增加类型
  46. public enum HpUpType
  47. {
  48. Degree = 0, //按一定比例加血
  49. Add = 1, //直接加血
  50. Fixed = 2, //加固定血量
  51. }
  52. public class Character : MonoBehaviour
  53. {
  54. [Header("骨骼")]
  55. public SkeletonMecanim mecanim;
  56. public Skeleton skeleton;
  57. [HideInInspector]
  58. public MeshRenderer meshRenderer;
  59. [Header("动画控制器")]
  60. public Animator ani;
  61. public Animator aniCollider;
  62. [Header("组件")]
  63. public Rigidbody rb;
  64. public Transform bodyTrans;
  65. public BeSearchTrigger beSearchTrigger;
  66. public SearchTrigger searchTrigger;
  67. public GameObject bodyCollider;
  68. public UIHP uiHp;
  69. public BeHitTrigger beHitTrigger;
  70. [Header("角色状态")]
  71. public CharacterState state;
  72. public int totalHp = 100;
  73. public int hp;
  74. public bool isDie = false; //已死亡
  75. public bool isRevive; //从虚弱状态恢复中
  76. public bool linked; //被铁链连着
  77. public bool canNotAddForce; //不会被打飞
  78. public bool canNotChangeHurt; //不会被打虚弱
  79. [HideInInspector]
  80. public float invincibleTime; //无敌时间
  81. public GameObject injuryNumText;//伤害跳字
  82. public bool showInjuryNum; //伤害跳字开关
  83. public bool canFly = false;
  84. [Header("护甲")]
  85. public int armor;
  86. [Header("锁魂塔")]
  87. public LockSoul ls;
  88. public bool isInSoulTower; //在锁魂塔范围内
  89. [Header("死亡后多久尸体消失")]
  90. public float totalDieKeepTime = 2f;
  91. [HideInInspector]
  92. public float dieKeepTime;
  93. [Header("普通攻击信息")]
  94. public bool canHitFly;
  95. [HideInInspector]
  96. public bool isNonAttack = false; //无普攻
  97. public float attackTime;
  98. public float totalAttack1Time = 0.5f;
  99. public float totalAttack2Time = 0.5f;
  100. public AttackType attackType; //攻击类型
  101. public GameObject bulletPrefab; //远程攻击的子弹
  102. public List<Transform> shootPos; //远程攻击的子弹发射位置
  103. public bool shootTrack = false; //远程攻击是否追踪目标
  104. public List<AttackInfo> attack1Infos; //普攻1信息(出场攻击)
  105. public List<AttackInfo> attack2Infos; //普攻2信息
  106. public List<AttackTrigger> attackTriggers; //普攻触发器,敌方进入后播放动画进行攻击
  107. public GameObject addAttackEffect;
  108. public int armorPiercing; //穿甲率
  109. public int[] curDamage1;
  110. public int[] curDamage2;
  111. [Header("目标")]
  112. public List<TargetType> targetTypes;
  113. public Character targetCharacter;
  114. public Character attackTarget;
  115. public List<Character> beTargetCharacter = new List<Character>(); //被哪些锁定
  116. public float getDistanceOffset = 0f;
  117. [Header("是否为英灵、是否为变身形态英灵")]
  118. public bool isSpirit;
  119. public bool isTran;
  120. [HideInInspector]
  121. public PlayerController pc;
  122. [Header("血量上限增加类型")]
  123. public HpUpType hptp;
  124. [Header("被铁链连着")]
  125. public RopeJoint joint;
  126. public CharacterRope rope;
  127. [Header("体型增大")]
  128. public bool beLarger = false;
  129. public float toLargeSize = 0; //变大程度
  130. private Vector3 speed = new Vector3(1, 1, 0); //变大速度
  131. [Header("特效")]
  132. public GameObject cookEffect; //吃串加血
  133. [Header("传送门")]
  134. public bool attack1ShootCanTransmit; //普攻1弓箭可以被传送
  135. //调试开关
  136. [Header("debug攻击者")] public bool debugAttackFrom;
  137. public virtual void Init()
  138. {
  139. //确保组件不丢失
  140. if (!mecanim)
  141. {
  142. mecanim = GetComponentInChildren<SkeletonMecanim>();
  143. }
  144. if (mecanim && skeleton == null)
  145. {
  146. skeleton = mecanim.skeleton;
  147. }
  148. if (!meshRenderer)
  149. {
  150. meshRenderer = mecanim.GetComponent<MeshRenderer>();
  151. }
  152. if (!ani)
  153. {
  154. ani = GetComponentInChildren<Animator>();
  155. }
  156. //血量重置
  157. hp = totalHp;
  158. if (!isNonAttack)
  159. {
  160. uiHp.Show(hp, totalHp);
  161. }
  162. ChangeState(CharacterState.Idle);
  163. //被链接状态重置
  164. linked = false;
  165. if (joint)
  166. {
  167. Destroy(joint);
  168. joint = null;
  169. }
  170. if (rope)
  171. {
  172. rope = null;
  173. }
  174. curDamage1 = new int[attack1Infos.Count];
  175. for (int i = 0; i < attack1Infos.Count; i++)
  176. {
  177. curDamage1[i] = attack1Infos[i].damage;
  178. }
  179. curDamage2 = new int[attack2Infos.Count];
  180. for (int i = 0; i < attack2Infos.Count; i++)
  181. {
  182. curDamage2[i] = attack2Infos[i].damage;
  183. }
  184. }
  185. public virtual void FixedUpdate()
  186. {
  187. OnState();
  188. }
  189. private void OnEnable()
  190. {
  191. if (attackTriggers != null)
  192. {
  193. for (int i = 0; i < attackTriggers.Count; i++)
  194. {
  195. attackTriggers[i].gameObject.SetActive(false);
  196. }
  197. }
  198. }
  199. public void Turn()
  200. {
  201. bodyTrans.localScale = new Vector3(-bodyTrans.localScale.x, bodyTrans.localScale.y, bodyTrans.localScale.z);
  202. }
  203. public virtual void OnState()
  204. {
  205. }
  206. public virtual void ChangeState(CharacterState newState)
  207. {
  208. }
  209. public void DebugAttackFrom(string attackFrom, int damage)
  210. {
  211. Debug.Log(attackFrom + "对" + gameObject.name + "造成了" + damage.ToString() + "点伤害");
  212. }
  213. public virtual void BeHit(int damage, Vector3 force, bool changeHurt, float repelValue)
  214. {
  215. //无敌状态下免伤
  216. if (invincibleTime > 0)
  217. {
  218. return;
  219. }
  220. //非无敌状态扣血
  221. hp -= damage;
  222. //伤害跳字
  223. if (showInjuryNum)
  224. {
  225. GameObject injuryNum = Instantiate(injuryNumText);
  226. injuryNum.transform.position = new Vector3(transform.position.x + Random.Range(-1f, 1f), transform.position.y + 1, transform.position.z);
  227. TextMeshProUGUI text = injuryNum.GetComponentInChildren<TextMeshProUGUI>();
  228. text.text = damage.ToString();
  229. if (gameObject.CompareTag("Player"))
  230. {
  231. text.color = Color.red;
  232. }
  233. }
  234. uiHp.Show(hp, totalHp);
  235. if (hp <= 0)
  236. {
  237. if (!canNotAddForce)
  238. rb.AddForce(force);
  239. ChangeState(CharacterState.Die);
  240. return;
  241. }
  242. }
  243. public virtual void AttackShootEvent(int attackId, int shootId)
  244. {
  245. AttackInfo attackInfo;
  246. if (attackId == 1)
  247. {
  248. attackInfo = attack1Infos[shootId];
  249. }
  250. else
  251. {
  252. attackInfo = attack2Infos[shootId];
  253. attackInfo.damage = curDamage2[0];
  254. }
  255. GameObject bulletObj = PoolManager.Instantiate(bulletPrefab);
  256. Bullet bullet = bulletObj.GetComponent<Bullet>();
  257. Vector3 attackDir = attackInfo.attackDir.normalized;
  258. if (bodyTrans.localScale.x < 0)
  259. {
  260. attackDir.x = -attackDir.x;
  261. }
  262. if (attackId == 1 && attack1ShootCanTransmit)
  263. {
  264. bullet.canTransmit = true;
  265. }
  266. bullet.BeShoot(this, shootPos[shootId].position, attackDir, attackInfo.damage, attackInfo.force, attackInfo.changeHurt, attackInfo.repelValue, shootTrack, attackTarget ? attackTarget : null);
  267. }
  268. public virtual Vector3 GetMoveDir()
  269. {
  270. Vector3 moveDir = Vector3.zero;
  271. return moveDir;
  272. }
  273. public virtual void attack_summon()
  274. {
  275. ani.Play("attack_summon", 0, 0);
  276. if (!isNonAttack)
  277. {
  278. aniCollider.Play("attack_summon", 1, 0);
  279. attackTime = totalAttack1Time;
  280. switch (attackType)
  281. {
  282. case AttackType.Melee:
  283. for (int i = 0; i < attack1Infos.Count; i++)
  284. {
  285. attackTriggers[i].damage = curDamage1[i];
  286. attackTriggers[i].changeHurt = attack1Infos[i].changeHurt;
  287. attackTriggers[i].repelValue = attack1Infos[i].repelValue;
  288. Vector3 attackDir = attack1Infos[i].attackDir.normalized;
  289. if (bodyTrans.localScale.x < 0)
  290. {
  291. attackDir.x = -attackDir.x;
  292. }
  293. attackTriggers[i].force = attackDir * attack1Infos[i].force;
  294. }
  295. break;
  296. case AttackType.Shoot:
  297. case AttackType.Dash:
  298. break;
  299. default:
  300. break;
  301. }
  302. ChangeState(CharacterState.Attack);
  303. }
  304. }
  305. public virtual void attack_march()
  306. {
  307. Vector3 leftDir = GetMoveDir();
  308. if (leftDir.x > 0.3f)
  309. {
  310. if (bodyTrans.localScale.x > 0)
  311. {
  312. Turn();
  313. }
  314. }
  315. else if (leftDir.x < -0.3f)
  316. {
  317. if (bodyTrans.localScale.x < 0)
  318. {
  319. Turn();
  320. }
  321. }
  322. ani.Play("attack_march", 0, 0);
  323. aniCollider.Play("attack_march", 1, 0);
  324. attackTime = totalAttack2Time;
  325. if (attackTriggers.Count > 0)
  326. {
  327. for (int i = 0; i < attack2Infos.Count; i++)
  328. {
  329. attackTriggers[i].damage = curDamage2[i];
  330. attackTriggers[i].changeHurt = attack2Infos[i].changeHurt;
  331. attackTriggers[i].repelValue = attack2Infos[i].repelValue;
  332. Vector3 attackDir = attack2Infos[i].attackDir.normalized;
  333. if (bodyTrans.localScale.x < 0)
  334. {
  335. attackDir.x = -attackDir.x;
  336. }
  337. attackTriggers[i].force = attackDir * attack2Infos[i].force;
  338. }
  339. }
  340. ChangeState(CharacterState.Attack);
  341. }
  342. public virtual void SetSortingOrder(int order)
  343. {
  344. meshRenderer.sortingOrder = order;
  345. }
  346. //吃串增加血量上限
  347. public void HpUp(float value, float larger)
  348. {
  349. cookEffect.transform.GetChild(0).gameObject.SetActive(true);
  350. int add = 0;
  351. switch (hptp)
  352. {
  353. case HpUpType.Degree:
  354. value = value / 100;
  355. add = (int)(totalHp * value);
  356. break;
  357. case HpUpType.Add:
  358. add = (int)value;
  359. break;
  360. case HpUpType.Fixed:
  361. add = (int)(totalHp - value);
  362. break;
  363. }
  364. totalHp += add;
  365. hp += add;
  366. uiHp.Show(hp, totalHp);
  367. float cur = transform.localScale.x;
  368. toLargeSize = cur * larger;
  369. beLarger = true;
  370. }
  371. //体型变大
  372. public void Enlarge()
  373. {
  374. transform.localScale = Vector3.SmoothDamp(transform.localScale, new Vector3(1, 1, 1) * (toLargeSize + 0.1f), ref speed, 0.6f);
  375. if (transform.localScale.x >= toLargeSize - 0.1f)
  376. {
  377. beLarger = false;
  378. transform.localScale = new Vector3(toLargeSize, toLargeSize, toLargeSize);
  379. toLargeSize = 0;
  380. }
  381. }
  382. }