Character.cs 9.6 KB

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