AttributeStatus.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using System;
  4. using Sirenix.OdinInspector;
  5. //各个状态
  6. public enum SpecialState
  7. {
  8. Null = -1,
  9. FloatState = 0,
  10. BlownUp = 1,
  11. ShotDown = 2,
  12. Weak = 3,
  13. }
  14. public class AttributeStatus : MonoBehaviour
  15. {
  16. //组件
  17. private MoveCharacter character;
  18. private Rigidbody rb;
  19. private Foot foot;
  20. private HitFeedbackSystem hitFeedbackSystem;
  21. //behit参数
  22. [DisplayOnly] public bool canChangeHitStun;
  23. [DisplayOnly] public bool haveSpecialStates;
  24. [DisplayOnly] public SpecialState curSpecialStates = SpecialState.Null;
  25. [LabelText("控制时间")] [DisplayOnly] public float attributeTime;
  26. [TabGroup("漂浮")]
  27. [DisplayOnly] public int floatingState; //0:不漂浮;1:漂浮中;2:飘着;3:掉下去
  28. private Vector3 origPos; //初始位置
  29. private float curHeight; //当前所在高度
  30. private float riseTime; //上升时间
  31. private float backSpeed; //返回速度
  32. private float rotateSpeed; //旋转速度
  33. private float rotateDir; //旋转方向
  34. private float height; //漂浮高度
  35. private float rise = 1;
  36. private float normalFallSpeed = 15f;
  37. [TabGroup("击飞击落")] [DisplayOnly] public int hitState;
  38. [TabGroup("击飞击落")] [DisplayOnly] public bool isFly;
  39. [TabGroup("击飞击落")] [LabelText("X方向阻力")] public float decelerationRatioX = 2f;
  40. [TabGroup("击飞击落")] [LabelText("Y方向阻力")] public float decelerationRatioY = 15f;
  41. [TabGroup("易伤")]
  42. [DisplayOnly] public bool haveVulnerable;
  43. [TabGroup("易伤")]
  44. [DisplayOnly] public float vulnerableTime;
  45. private float vulnerableRate;
  46. //抗性
  47. [Serializable]
  48. public struct Resistances
  49. {
  50. //控制效果抗性
  51. [Range(0, 1)]
  52. [LabelText("漂浮抗性")]
  53. public float Float;
  54. [Range(0, 1)]
  55. [LabelText("击飞抗性")]
  56. public float BlowUp;
  57. [Range(0, 1)]
  58. [LabelText("击落抗性")]
  59. public float ShotDown;
  60. [Range(0, 1)]
  61. [LabelText("击晕抗性")]
  62. public float Weak;
  63. [Space]
  64. //非控制效果抗性
  65. [LabelText("护甲值")] public int armor;
  66. }
  67. [LabelText("抗性")] public Resistances resistances;
  68. private void Awake()
  69. {
  70. character = GetComponentInParent<MoveCharacter>();
  71. rb = character.rb;
  72. foot = character.foot;
  73. hitFeedbackSystem = GetComponent<HitFeedbackSystem>();
  74. }
  75. public void Update()
  76. {
  77. //易伤
  78. vulnerableTime -= Time.deltaTime;
  79. if (vulnerableTime <= 0)
  80. {
  81. haveVulnerable = false;
  82. }
  83. }
  84. public void AddSpecialState(AttackInfo attackInfo, Character attackFrom)
  85. {
  86. if (canChangeHitStun)
  87. {
  88. canChangeHitStun = false;
  89. hitFeedbackSystem.EnterHitStun(attackFrom);
  90. return;
  91. }
  92. if (haveSpecialStates)
  93. {
  94. haveSpecialStates = false;
  95. hitFeedbackSystem.canHitStun = false;
  96. switch (curSpecialStates)
  97. {
  98. case SpecialState.FloatState:
  99. AddFloat(attackInfo.floatState);
  100. break;
  101. case SpecialState.BlownUp:
  102. if (rb.useGravity)
  103. {
  104. AddBlowUp(attackInfo.blowUp, attackFrom.bodyTrans.localScale.x < 0 ? -1 : 1);
  105. //虾兵特殊攻击先留着
  106. //AddBlowUp(attackInfo.blowUp, attackFrom.transform.position.x < character.transform.position.x ? -1 : 1);
  107. }
  108. break;
  109. case SpecialState.ShotDown:
  110. if (!rb.useGravity)
  111. {
  112. AddShotDown(attackInfo.shotDown, attackFrom.bodyTrans.localScale.x < 0 ? -1 : 1);
  113. //虾兵特殊攻击先留着
  114. //AddShotDown(attackInfo.shotDown, attackFrom.transform.position.x < character.transform.position.x ? -1 : 1);
  115. }
  116. break;
  117. case SpecialState.Weak:
  118. AddWeak(attackInfo.weak);
  119. break;
  120. }
  121. }
  122. }
  123. //CharacterState为SpecialStatus时调用此函数
  124. public void SpecialStateEffect(SpecialState specialState)
  125. {
  126. switch (specialState)
  127. {
  128. //漂浮
  129. case SpecialState.FloatState:
  130. switch (floatingState)
  131. {
  132. case 1:
  133. character.transform.localEulerAngles += new Vector3(0, 0, 1) * rotateDir * rotateSpeed * Time.deltaTime;
  134. curHeight = Mathf.SmoothDamp(curHeight, height, ref rise, riseTime);
  135. character.transform.position = new Vector3(origPos.x, curHeight, origPos.z);
  136. if (curHeight >= height - 0.02f)
  137. {
  138. floatingState = 2;
  139. height = character.transform.position.y;
  140. }
  141. break;
  142. case 2:
  143. character.transform.localEulerAngles += new Vector3(0, 0, 1) * rotateDir * rotateSpeed * Time.deltaTime;
  144. break;
  145. case 3:
  146. if (character.transform.position.y >= origPos.y + 0.05f)
  147. {
  148. curHeight -= normalFallSpeed * Time.deltaTime;
  149. character.transform.position = new Vector3(origPos.x, curHeight, origPos.z);
  150. }
  151. else if (foot.TrigGround || curHeight <= origPos.y + 0.05f)
  152. {
  153. floatingState = 0;
  154. character.isAdjustHeight = 1;
  155. OutSpecialState();
  156. return;
  157. }
  158. break;
  159. }
  160. PlayerController playerController = GetComponent<PlayerController>();
  161. if (playerController != null)
  162. {
  163. if (playerController.mp > 0)
  164. {
  165. playerController.lostMp += playerController.mpReplySpeed * Time.deltaTime;
  166. playerController.mp -= playerController.mpReplySpeed * Time.deltaTime;
  167. }
  168. if (playerController.lostMp >= playerController.addMp)
  169. {
  170. Instantiate(playerController.soul, character.transform.position, new Quaternion(0, 0, 0, 0), null);
  171. playerController.lostMp = 0;
  172. }
  173. }
  174. attributeTime -= Time.deltaTime;
  175. if (attributeTime <= 0)
  176. {
  177. character.transform.localEulerAngles = Vector3.zero;
  178. floatingState = 3;
  179. rb.useGravity = true;
  180. }
  181. break;
  182. //击飞
  183. case SpecialState.BlownUp:
  184. //击落
  185. case SpecialState.ShotDown:
  186. switch (hitState)
  187. {
  188. case 0:
  189. Vector3 vel = rb.velocity;
  190. if (isFly && foot.TrigGround && vel.y <= 0.01f)
  191. {
  192. vel = Vector3.zero;
  193. character.ani.Play("weak", 0, 0);
  194. hitState = 1;
  195. }
  196. else
  197. {
  198. vel.x -= decelerationRatioX * Time.deltaTime;
  199. vel.y -= decelerationRatioY * Time.deltaTime;
  200. isFly = true;
  201. }
  202. rb.velocity = vel;
  203. break;
  204. case 1:
  205. //眩晕状态
  206. if (attributeTime <= 0)
  207. {
  208. character.isAdjustHeight = 1;
  209. OutSpecialState();
  210. }
  211. else
  212. {
  213. rb.velocity = Vector3.zero;
  214. attributeTime -= Time.deltaTime;
  215. }
  216. break;
  217. }
  218. break;
  219. //眩晕
  220. case SpecialState.Weak:
  221. if (attributeTime <= 0)
  222. {
  223. OutSpecialState();
  224. }
  225. else
  226. {
  227. rb.velocity = Vector3.zero;
  228. attributeTime -= Time.deltaTime;
  229. }
  230. break;
  231. }
  232. }
  233. public void OutSpecialState()
  234. {
  235. curSpecialStates = SpecialState.Null;
  236. if (character.canFly)
  237. {
  238. rb.useGravity = false;
  239. rb.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezePositionY;
  240. }
  241. character.ChangeState(CharacterState.Idle);
  242. }
  243. public int DamageCalculation(int damage)
  244. {
  245. damage = (int)(damage * (1 + vulnerableRate) + 0.5f);
  246. return damage;
  247. }
  248. //判断优先级,ture为优先级高于当前控制
  249. public bool PriorityOrder(SpecialState specialState)
  250. {
  251. if (curSpecialStates == SpecialState.Null)
  252. {
  253. curSpecialStates = specialState;
  254. haveSpecialStates = true;
  255. return true;
  256. }
  257. else
  258. {
  259. if (curSpecialStates >= specialState)
  260. {
  261. curSpecialStates = specialState;
  262. haveSpecialStates = true;
  263. return true;
  264. }
  265. else
  266. {
  267. canChangeHitStun = true;
  268. return false;
  269. }
  270. }
  271. }
  272. //受到漂浮
  273. public void AddFloat(AttackInfo.FloatState floatState)
  274. {
  275. rb.useGravity = false;
  276. floatingState = 1;
  277. origPos = character.transform.position;
  278. curHeight = origPos.y;
  279. riseTime = UnityEngine.Random.Range(floatState.upTime.x, floatState.upTime.y);
  280. backSpeed = UnityEngine.Random.Range(floatState.backSpeed.x, floatState.backSpeed.y);
  281. if (gameObject.tag == "Enemy" || gameObject.tag == "Player")
  282. {
  283. backSpeed = -backSpeed;
  284. }
  285. rotateSpeed = UnityEngine.Random.Range(floatState.rotateSpeed.x, floatState.rotateSpeed.y);
  286. rotateDir = (1.5f - UnityEngine.Random.Range(1, 3)) * 2;
  287. height = UnityEngine.Random.Range(floatState.height.x, floatState.height.y);
  288. attributeTime = floatState.time * (1 - resistances.Float);
  289. character.ChangeState(CharacterState.SpecialStatus_Float);
  290. character.ChangeStateText(CharacterState.SpecialStatus_Float);
  291. }
  292. //受到击飞
  293. public void AddBlowUp(AttackInfo.BlowUp blowUp, float dir)
  294. {
  295. attributeTime = blowUp.time * (1 - resistances.BlowUp);
  296. Vector3 vec3 = blowUp.dir.normalized;
  297. if (dir < 0)
  298. {
  299. vec3.x = -vec3.x;
  300. }
  301. rb.useGravity = true;
  302. rb.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionZ;
  303. rb.velocity = Vector3.zero;
  304. rb.AddForce(vec3 * blowUp.force * (1 - resistances.BlowUp), ForceMode.Impulse);
  305. character.ani.Play("hitted", 0, 0);
  306. hitState = 0;
  307. isFly = false;
  308. character.ChangeState(CharacterState.SpecialStatus_BlowUp);
  309. character.ChangeStateText(CharacterState.SpecialStatus_BlowUp);
  310. }
  311. //受到击落
  312. public void AddShotDown(AttackInfo.ShotDown shotDown, float dir)
  313. {
  314. attributeTime = shotDown.time * (1 - resistances.ShotDown);
  315. Vector3 vec3 = shotDown.dir.normalized;
  316. if (dir < 0)
  317. {
  318. vec3.x = -vec3.x;
  319. }
  320. rb.useGravity = true;
  321. rb.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionZ;
  322. rb.velocity = Vector3.zero;
  323. rb.AddForce(vec3 * shotDown.force * (1 - resistances.ShotDown), ForceMode.Impulse);
  324. character.ani.Play("hitted", 0, 0);
  325. hitState = 0;
  326. isFly = false;
  327. character.ChangeState(CharacterState.SpecialStatus_ShotDown);
  328. character.ChangeStateText(CharacterState.SpecialStatus_ShotDown);
  329. }
  330. //受到击晕
  331. public void AddWeak(AttackInfo.Weak weak)
  332. {
  333. attributeTime = weak.time * (1 - resistances.Weak);
  334. character.ani.Play("weak", 0, 0);
  335. character.ChangeState(CharacterState.SpecialStatus_Weak);
  336. character.ChangeStateText(CharacterState.SpecialStatus_Weak);
  337. }
  338. //受到穿甲
  339. public int AddArmor(AttackInfo.Armor armor, int damage)
  340. {
  341. //计算护甲减免
  342. int am = resistances.armor;
  343. if (am > 0)
  344. {
  345. am = am - armor.rate;
  346. if (am < 0)
  347. {
  348. am = 0;
  349. }
  350. }
  351. return am;
  352. }
  353. //受到易伤
  354. public void AddVulnerable(AttackInfo.Vulnerable vulnerable)
  355. {
  356. vulnerableTime = vulnerable.time;
  357. vulnerableRate = vulnerable.rate;
  358. haveVulnerable = true;
  359. }
  360. }