AttributeStatus.cs 12 KB

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