Character.cs 11 KB

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