Character.cs 9.1 KB

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