Character.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. using Spine.Unity;
  2. using Spine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. //角色状态
  7. public enum CharacterState
  8. {
  9. None = 0,
  10. Idle = 1,
  11. Run = 2,
  12. Rise = 3, //空中上升
  13. Fall = 4, //空中下落
  14. //Hurt = 5,
  15. Attack = 6,
  16. KeepAttack = 7,
  17. Summon = 8,
  18. Rush = 9,
  19. Sprint = 10,
  20. Die = 11,
  21. Weak = 12,
  22. PullRope = 13,
  23. Spirits = 14, //召唤英灵
  24. Float = 15, //空中漂浮
  25. FindPlayer = 16, //寻找玩家
  26. ReadyToRush = 17, //瞄准准备冲刺
  27. ReadyToDownRush = 18, //准备落地冲刺
  28. DownRush = 19, //落地冲刺
  29. FinishRush = 20, //结束冲刺
  30. Coma = 21, //昏迷
  31. RushAttack, //带攻击的冲刺
  32. Transfiguration = 23, //变身
  33. }
  34. //攻击类型
  35. public enum AttackType
  36. {
  37. Melee = 0, //近战
  38. Shoot = 1, //射击
  39. Dash = 2, //英灵刺客
  40. }
  41. //血量上限增加类型
  42. public enum HpUpType
  43. {
  44. Degree = 0, //按一定比例加血
  45. Add = 1, //直接加血
  46. Fixed = 2, //加固定血量
  47. }
  48. public class Character : MonoBehaviour
  49. {
  50. [Header("骨骼")]
  51. public SkeletonMecanim mecanim;
  52. public Skeleton skeleton;
  53. [HideInInspector]
  54. public MeshRenderer meshRenderer;
  55. [Header("动画控制器")]
  56. public Animator ani;
  57. public Animator aniCollider;
  58. [Header("组件")]
  59. public Rigidbody rb;
  60. public Transform bodyTrans;
  61. public BeSearchTrigger beSearchTrigger;
  62. public SearchTrigger searchTrigger;
  63. public BeHitTrigger beHitTrigger;
  64. public GameObject bodyCollider;
  65. public UIHP uiHp;
  66. [Header("角色状态")]
  67. public CharacterState state;
  68. public int totalHp = 100;
  69. public int hp;
  70. public bool isDie = false; //已死亡
  71. public bool isRevive; //从虚弱状态恢复中
  72. public bool linked; //被铁链连着
  73. public bool canNotAddForce; //不会被打飞
  74. public bool canNotChangeHurt; //不会被打虚弱
  75. [HideInInspector]
  76. public float invincibleTime; //无敌时间
  77. [Header("死亡后多久尸体消失")]
  78. [HideInInspector]
  79. public float dieKeepTime;
  80. public float totalDieKeepTime = 2f;
  81. [Header("普通攻击信息")]
  82. [HideInInspector]
  83. public bool isNonAttack = false; //无普攻
  84. public bool canHitFly;
  85. public float attackTime;
  86. public float totalAttack1Time = 0.5f;
  87. public float totalAttack2Time = 0.5f;
  88. public AttackType attackType; //攻击类型
  89. public GameObject bulletPrefab; //远程攻击的子弹
  90. public List<Transform> shootPos; //远程攻击的子弹发射位置
  91. public bool shootTrack = false; //远程攻击是否追踪目标
  92. public List<AttackInfo> attack1Infos; //普攻1信息(出场攻击)
  93. public List<AttackInfo> attack2Infos; //普攻2信息
  94. public List<AttackTrigger> attackTriggers; //普攻触发器,敌方进入后播放动画进行攻击
  95. [Header("目标")]
  96. public List<TargetType> targetTypes;
  97. public Character targetCharacter;
  98. public Character attackTarget;
  99. public List<Character> beTargetCharacter = new List<Character>(); //被哪些锁定
  100. [Header("被攻击的触发器")]
  101. public GameObject[] HitCols;
  102. [Header("是否为英灵、是否为变身形态英灵")]
  103. public bool isSpirit;
  104. public bool isTran;
  105. [HideInInspector]
  106. public PlayerController pc;
  107. [Header("血量上限增加类型")]
  108. public HpUpType hptp;
  109. [Header("被铁链连着")]
  110. public RopeJoint joint;
  111. public CharacterRope rope;
  112. [Header("体型增大")]
  113. public bool beLarger = false;
  114. private float toLargeSize = 0; //变大程度
  115. private Vector3 speed = new Vector3(1, 1, 0); //变大速度
  116. [Header("特效")]
  117. public GameObject cookEffect; //吃串加血
  118. public virtual void Init()
  119. {
  120. //确保组件不丢失
  121. if (!mecanim)
  122. {
  123. mecanim = GetComponentInChildren<SkeletonMecanim>();
  124. }
  125. if (mecanim && skeleton == null)
  126. {
  127. skeleton = mecanim.skeleton;
  128. }
  129. if (!meshRenderer)
  130. {
  131. meshRenderer = mecanim.GetComponent<MeshRenderer>();
  132. }
  133. if (!ani)
  134. {
  135. ani = GetComponentInChildren<Animator>();
  136. }
  137. //血量重置
  138. hp = totalHp;
  139. if (!isNonAttack)
  140. {
  141. uiHp.Show(hp, totalHp);
  142. }
  143. ChangeState(CharacterState.Idle);
  144. //被链接状态重置
  145. linked = false;
  146. if (joint)
  147. {
  148. Destroy(joint);
  149. joint = null;
  150. }
  151. if (rope)
  152. {
  153. rope = null;
  154. }
  155. }
  156. public virtual void FixedUpdate()
  157. {
  158. OnState();
  159. }
  160. private void OnEnable()
  161. {
  162. for (int i = 0; i < attackTriggers.Count; i++)
  163. {
  164. attackTriggers[i].gameObject.SetActive(false);
  165. }
  166. }
  167. public void Turn()
  168. {
  169. bodyTrans.localScale = new Vector3(-bodyTrans.localScale.x, bodyTrans.localScale.y, bodyTrans.localScale.z);
  170. }
  171. public virtual void OnState()
  172. {
  173. }
  174. public virtual void ChangeState(CharacterState newState)
  175. {
  176. }
  177. public virtual void BeHit(int damage, Vector3 force, bool changeHurt, float repelValue)
  178. {
  179. //无敌状态下免伤
  180. if (invincibleTime > 0)
  181. {
  182. return;
  183. }
  184. //非无敌状态扣血
  185. hp -= damage;
  186. uiHp.Show(hp, totalHp);
  187. if (hp <= 0)
  188. {
  189. if(!canNotAddForce)
  190. rb.AddForce(force);
  191. ChangeState(CharacterState.Die);
  192. return;
  193. }
  194. }
  195. public virtual void AttackShootEvent(int attackId, int shootId)
  196. {
  197. AttackInfo attackInfo;
  198. if (attackId == 1)
  199. {
  200. attackInfo = attack1Infos[shootId];
  201. }
  202. else
  203. {
  204. attackInfo = attack2Infos[shootId];
  205. }
  206. GameObject bulletObj = PoolManager.Instantiate(bulletPrefab);
  207. Bullet bullet = bulletObj.GetComponent<Bullet>();
  208. Vector3 attackDir = attackInfo.attackDir.normalized;
  209. if (bodyTrans.localScale.x < 0)
  210. {
  211. attackDir.x = -attackDir.x;
  212. }
  213. bullet.BeShoot(this, shootPos[shootId].position, attackDir, attackInfo.damage, attackInfo.force, attackInfo.changeHurt, attackInfo.repelValue, shootTrack, attackTarget ? attackTarget : null);
  214. }
  215. public virtual Vector3 GetMoveDir()
  216. {
  217. Vector3 moveDir = Vector3.zero;
  218. return moveDir;
  219. }
  220. public virtual void Attack1()
  221. {
  222. ani.Play("attack_summon", 0, 0);
  223. if (!isNonAttack)
  224. {
  225. aniCollider.Play("Attack1", 1, 0);
  226. attackTime = totalAttack1Time;
  227. switch (attackType)
  228. {
  229. case AttackType.Melee:
  230. for (int i = 0; i < attack1Infos.Count; i++)
  231. {
  232. attackTriggers[i].damage = attack1Infos[i].damage;
  233. attackTriggers[i].changeHurt = attack1Infos[i].changeHurt;
  234. attackTriggers[i].repelValue = attack1Infos[i].repelValue;
  235. Vector3 attackDir = attack1Infos[i].attackDir.normalized;
  236. if (bodyTrans.localScale.x < 0)
  237. {
  238. attackDir.x = -attackDir.x;
  239. }
  240. attackTriggers[i].force = attackDir * attack1Infos[i].force;
  241. }
  242. break;
  243. case AttackType.Shoot:
  244. case AttackType.Dash:
  245. break;
  246. default:
  247. break;
  248. }
  249. ChangeState(CharacterState.Attack);
  250. }
  251. }
  252. public virtual void Attack2()
  253. {
  254. Vector3 leftDir = GetMoveDir();
  255. if (leftDir.x > 0.3f)
  256. {
  257. if (bodyTrans.localScale.x > 0)
  258. {
  259. Turn();
  260. }
  261. }
  262. else if (leftDir.x < -0.3f)
  263. {
  264. if (bodyTrans.localScale.x < 0)
  265. {
  266. Turn();
  267. }
  268. }
  269. ani.Play("attack_march", 0, 0);
  270. aniCollider.Play("Attack2", 1, 0);
  271. attackTime = totalAttack2Time;
  272. switch (attackType)
  273. {
  274. case AttackType.Melee:
  275. case AttackType.Dash:
  276. for (int i = 0; i < attack2Infos.Count; i++)
  277. {
  278. attackTriggers[i].damage = attack2Infos[i].damage;
  279. attackTriggers[i].changeHurt = attack2Infos[i].changeHurt;
  280. attackTriggers[i].repelValue = attack2Infos[i].repelValue;
  281. Vector3 attackDir = attack2Infos[i].attackDir.normalized;
  282. if (bodyTrans.localScale.x < 0)
  283. {
  284. attackDir.x = -attackDir.x;
  285. }
  286. attackTriggers[i].force = attackDir * attack2Infos[i].force;
  287. }
  288. break;
  289. case AttackType.Shoot:
  290. break;
  291. default:
  292. break;
  293. }
  294. ChangeState(CharacterState.Attack);
  295. }
  296. public void SetSortingOrder(int order)
  297. {
  298. meshRenderer.sortingOrder = order;
  299. }
  300. //吃串增加血量上限
  301. public void HpUp(float value, float larger)
  302. {
  303. cookEffect.transform.GetChild(0).gameObject.SetActive(true);
  304. int add = 0;
  305. switch (hptp)
  306. {
  307. case HpUpType.Degree:
  308. value = value / 100;
  309. add = (int)(totalHp * value);
  310. break;
  311. case HpUpType.Add:
  312. add = (int)value;
  313. break;
  314. case HpUpType.Fixed:
  315. add = (int)(totalHp - value);
  316. break;
  317. }
  318. totalHp += add;
  319. hp += add;
  320. uiHp.Show(hp, totalHp);
  321. float cur = transform.localScale.x;
  322. toLargeSize = cur * larger;
  323. beLarger = true;
  324. }
  325. //体型变大
  326. public void Enlarge()
  327. {
  328. transform.localScale = Vector3.SmoothDamp(transform.localScale, new Vector3(1, 1, 1) * toLargeSize, ref speed, 0.3f);
  329. if (transform.localScale.x >= toLargeSize + 0.1f)
  330. {
  331. beLarger = false;
  332. transform.localScale = new Vector3(toLargeSize, toLargeSize, 1);
  333. toLargeSize = 0;
  334. //print(transform.localScale);
  335. }
  336. }
  337. }