Character.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. using Spine.Unity;
  2. using Spine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. public enum CharacterState
  7. {
  8. None = 0,
  9. Idle = 1,
  10. Run = 2,
  11. Rise = 3,//空中上升
  12. Fall = 4,//空中下落
  13. Hurt = 5,
  14. Attack = 6,
  15. KeepAttack = 7,
  16. Summon = 8,
  17. Rush = 9,
  18. Sprint = 10,
  19. Die = 11,
  20. Weak = 12,
  21. PullRope = 13,
  22. Spirits = 14, //召唤英灵
  23. Float = 15, //空中漂浮
  24. FindPlayer = 16, //寻找玩家
  25. ReadyToRush = 17, //瞄准准备冲刺
  26. ReadyToDownRush = 18, //准备落地冲刺
  27. DownRush = 19, //落地冲刺
  28. FinishRush = 20, //结束冲刺
  29. Coma = 21, //昏迷
  30. RushAttack, //带攻击的冲刺
  31. }
  32. public enum AttackType
  33. {
  34. Melee = 0,//近战
  35. Shoot = 1,//射击
  36. Dash = 2, //英灵刺客
  37. }
  38. public enum HpUpType
  39. {
  40. Degree = 0,
  41. Add = 1,
  42. Fixed = 2,
  43. }
  44. public class Character : MonoBehaviour
  45. {
  46. public GameObject[] HitCols;
  47. public int cookNum; //能做多少串
  48. public List<GameObject> cooks; //吃过谁家的串
  49. public SkeletonMecanim mecanim;
  50. public Skeleton skeleton;
  51. public MeshRenderer meshRenderer;
  52. public Animator ani;
  53. public Animator aniCollider;
  54. public Rigidbody rb;
  55. public Transform bodyTrans;
  56. public BeSearchTrigger beSearchTrigger;
  57. public UIHP uiHp;
  58. public CharacterState state;
  59. [HideInInspector]
  60. public float attackTime;
  61. public float totalAttack1Time = 0.5f;
  62. public float totalAttack2Time = 0.5f;
  63. public bool isNonAttack = false;
  64. public HpUpType hptp;
  65. public bool isDie = false;
  66. public int totalHp = 100;
  67. public int hp;
  68. public List<AttackInfo> attack1Infos;
  69. public List<AttackInfo> attack2Infos;
  70. public List<AttackTrigger> attackTriggers;
  71. public AttackType attackType;
  72. public GameObject bulletPrefab;
  73. public List<Transform> shootPos;
  74. [HideInInspector]
  75. public float dieKeepTime;
  76. public float totalDieKeepTime = 2f;
  77. public Character attackTarget;
  78. public bool shootTrack = false;
  79. [HideInInspector]
  80. public float invincibleTime;
  81. public float totalInvincibleTime = 2f;
  82. public Character targetCharacter;
  83. public List<Character> beTargetCharacter = new List<Character>(); //被哪些锁定
  84. public SearchTrigger searchTrigger;
  85. public List<TargetType> targetTypes;
  86. public bool canHitFly;
  87. public bool linked;
  88. public RopeJoint joint;
  89. public CharacterRope rope;
  90. public bool hasHpUp = false;
  91. private float toLargeSize = 0;
  92. private Vector3 speed = new Vector3(1, 1, 0);
  93. public bool beLarger = false;
  94. public virtual void Init()
  95. {
  96. if (!mecanim)
  97. {
  98. mecanim = GetComponentInChildren<SkeletonMecanim>();
  99. }
  100. if (mecanim && skeleton == null)
  101. {
  102. skeleton = mecanim.skeleton;
  103. }
  104. if (!meshRenderer)
  105. {
  106. meshRenderer = mecanim.GetComponent<MeshRenderer>();
  107. }
  108. if (!ani)
  109. {
  110. ani = GetComponentInChildren<Animator>();
  111. }
  112. hp = totalHp;
  113. if (!isNonAttack)
  114. {
  115. uiHp.Show(hp, totalHp);
  116. }
  117. ChangeState(CharacterState.Idle);
  118. linked = false;
  119. if (joint)
  120. {
  121. Destroy(joint);
  122. joint = null;
  123. }
  124. if (rope)
  125. {
  126. rope = null;
  127. }
  128. }
  129. public virtual void FixedUpdate()
  130. {
  131. OnState();
  132. }
  133. private void OnEnable()
  134. {
  135. for (int i = 0; i < attackTriggers.Count; i++)
  136. {
  137. attackTriggers[i].gameObject.SetActive(false);
  138. }
  139. }
  140. public void Turn()
  141. {
  142. bodyTrans.localScale = new Vector3(-bodyTrans.localScale.x, bodyTrans.localScale.y, bodyTrans.localScale.z);
  143. }
  144. public virtual void OnState()
  145. {
  146. }
  147. public virtual void ChangeState(CharacterState newState)
  148. {
  149. }
  150. public virtual void BeHit(int damage, Vector3 force, bool changeHurt, float repelValue)
  151. {
  152. if (invincibleTime > 0)
  153. {
  154. return;
  155. }
  156. hp -= damage;
  157. uiHp.Show(hp, totalHp);
  158. if (hp <= 0)
  159. {
  160. rb.AddForce(force);
  161. ChangeState(CharacterState.Die);
  162. return;
  163. }
  164. }
  165. public virtual void AttackShootEvent(int attackId, int shootId)
  166. {
  167. AttackInfo attackInfo;
  168. if (attackId == 1)
  169. {
  170. attackInfo = attack1Infos[shootId];
  171. }
  172. else
  173. {
  174. attackInfo = attack2Infos[shootId];
  175. }
  176. GameObject bulletObj = PoolManager.Instantiate(bulletPrefab);
  177. Bullet bullet = bulletObj.GetComponent<Bullet>();
  178. Vector3 attackDir = attackInfo.attackDir.normalized;
  179. if (bodyTrans.localScale.x < 0)
  180. {
  181. attackDir.x = -attackDir.x;
  182. }
  183. bullet.BeShoot(this, shootPos[shootId].position, attackDir, attackInfo.damage, attackInfo.force, attackInfo.changeHurt, attackInfo.repelValue, shootTrack, attackTarget ? attackTarget : null);
  184. }
  185. public virtual Vector3 GetMoveDir()
  186. {
  187. Vector3 moveDir = Vector3.zero;
  188. return moveDir;
  189. }
  190. public virtual void Attack1()
  191. {
  192. ani.Play("attack_summon", 0, 0);
  193. if (!isNonAttack)
  194. {
  195. aniCollider.Play("Attack1", 1, 0);
  196. attackTime = totalAttack1Time;
  197. switch (attackType)
  198. {
  199. case AttackType.Melee:
  200. for (int i = 0; i < attack1Infos.Count; i++)
  201. {
  202. attackTriggers[i].damage = attack1Infos[i].damage;
  203. attackTriggers[i].changeHurt = attack1Infos[i].changeHurt;
  204. attackTriggers[i].repelValue = attack1Infos[i].repelValue;
  205. Vector3 attackDir = attack1Infos[i].attackDir.normalized;
  206. if (bodyTrans.localScale.x < 0)
  207. {
  208. attackDir.x = -attackDir.x;
  209. }
  210. attackTriggers[i].force = attackDir * attack1Infos[i].force;
  211. }
  212. break;
  213. case AttackType.Shoot:
  214. case AttackType.Dash:
  215. break;
  216. default:
  217. break;
  218. }
  219. ChangeState(CharacterState.Attack);
  220. }
  221. }
  222. public virtual void Attack2()
  223. {
  224. Vector3 leftDir = GetMoveDir();
  225. if (leftDir.x > 0.3f)
  226. {
  227. if (bodyTrans.localScale.x > 0)
  228. {
  229. Turn();
  230. }
  231. }
  232. else if (leftDir.x < -0.3f)
  233. {
  234. if (bodyTrans.localScale.x < 0)
  235. {
  236. Turn();
  237. }
  238. }
  239. ani.Play("attack_march", 0, 0);
  240. aniCollider.Play("Attack2", 1, 0);
  241. attackTime = totalAttack2Time;
  242. switch (attackType)
  243. {
  244. case AttackType.Melee:
  245. case AttackType.Dash:
  246. for (int i = 0; i < attack2Infos.Count; i++)
  247. {
  248. attackTriggers[i].damage = attack2Infos[i].damage;
  249. attackTriggers[i].changeHurt = attack2Infos[i].changeHurt;
  250. attackTriggers[i].repelValue = attack2Infos[i].repelValue;
  251. Vector3 attackDir = attack2Infos[i].attackDir.normalized;
  252. if (bodyTrans.localScale.x < 0)
  253. {
  254. attackDir.x = -attackDir.x;
  255. }
  256. attackTriggers[i].force = attackDir * attack2Infos[i].force;
  257. }
  258. break;
  259. case AttackType.Shoot:
  260. break;
  261. default:
  262. break;
  263. }
  264. ChangeState(CharacterState.Attack);
  265. }
  266. public void SetSortingOrder(int order)
  267. {
  268. meshRenderer.sortingOrder = order;
  269. }
  270. public void HpUp(float value, float larger)
  271. {
  272. int add = 0;
  273. switch (hptp)
  274. {
  275. case HpUpType.Degree:
  276. value = value / 100;
  277. add = (int)(totalHp * value);
  278. break;
  279. case HpUpType.Add:
  280. add = (int)value;
  281. break;
  282. case HpUpType.Fixed:
  283. add = (int)(totalHp - value);
  284. break;
  285. }
  286. totalHp += add;
  287. hp += add;
  288. uiHp.Show(hp, totalHp);
  289. float cur = transform.localScale.x;
  290. toLargeSize = cur * larger;
  291. beLarger = true;
  292. }
  293. public void Enlarge()
  294. {
  295. transform.localScale = Vector3.SmoothDamp(transform.localScale, new Vector3(1, 1, 1) * toLargeSize, ref speed, 0.3f);
  296. if (transform.localScale.x >= toLargeSize + 0.1f)
  297. {
  298. beLarger = false;
  299. transform.localScale = new Vector3(toLargeSize, toLargeSize, 1);
  300. toLargeSize = 0;
  301. //print(transform.localScale);
  302. }
  303. }
  304. }