Character.cs 8.7 KB

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