AttributeStatus.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. private void Awake()
  61. {
  62. character = GetComponentInParent<MoveCharacter>();
  63. rb = character.rb;
  64. foot = character.foot;
  65. }
  66. public void Update()
  67. {
  68. //易伤
  69. vulnerableTime -= Time.deltaTime;
  70. if (vulnerableTime <= 0)
  71. {
  72. haveVulnerable = false;
  73. }
  74. }
  75. //CharacterState为SpecialStatus时调用此函数
  76. public void SpecialStateEffect(SpecialState specialState)
  77. {
  78. switch (specialState)
  79. {
  80. //漂浮
  81. case SpecialState.FloatState:
  82. switch (floatingState)
  83. {
  84. case 1:
  85. character.transform.localEulerAngles += new Vector3(0, 0, 1) * rotateDir * rotateSpeed * Time.deltaTime;
  86. curHeight = Mathf.SmoothDamp(curHeight, height, ref rise, riseTime);
  87. character.transform.position = new Vector3(origPos.x, curHeight, origPos.z);
  88. if (curHeight >= height - 0.02f)
  89. {
  90. floatingState = 2;
  91. height = character.transform.position.y;
  92. }
  93. break;
  94. case 2:
  95. character.transform.localEulerAngles += new Vector3(0, 0, 1) * rotateDir * rotateSpeed * Time.deltaTime;
  96. break;
  97. case 3:
  98. if (character.transform.position.y >= origPos.y + 0.05f)
  99. {
  100. curHeight -= normalFallSpeed * Time.deltaTime;
  101. character.transform.position = new Vector3(origPos.x, curHeight, origPos.z);
  102. }
  103. else if (foot.TrigGround || curHeight <= origPos.y + 0.05f)
  104. {
  105. floatingState = 0;
  106. character.ChangeState(CharacterState.Idle);
  107. curSpecialStates = SpecialState.Null;
  108. return;
  109. }
  110. break;
  111. }
  112. PlayerController playerController = GetComponent<PlayerController>();
  113. if (playerController != null)
  114. {
  115. if (playerController.mp > 0)
  116. {
  117. playerController.lostMp += playerController.mpReplySpeed * Time.deltaTime;
  118. playerController.mp -= playerController.mpReplySpeed * Time.deltaTime;
  119. }
  120. if (playerController.lostMp >= playerController.addMp)
  121. {
  122. Instantiate(playerController.soul, character.transform.position, new Quaternion(0, 0, 0, 0), null);
  123. playerController.lostMp = 0;
  124. }
  125. }
  126. attributeTime -= Time.deltaTime;
  127. if (attributeTime <= 0)
  128. {
  129. character.transform.localEulerAngles = Vector3.zero;
  130. floatingState = 3;
  131. rb.useGravity = true;
  132. }
  133. break;
  134. //击飞
  135. case SpecialState.BlownUp:
  136. if (rb.velocity.magnitude > 0)
  137. {
  138. //击飞中
  139. Vector3 vel = rb.velocity;
  140. if (foot.TrigGround && vel.y <= 0.01f)
  141. {
  142. vel = Vector3.zero;
  143. }
  144. else
  145. {
  146. vel.x -= decelerationRatioX * Time.deltaTime;
  147. vel.y -= decelerationRatioY * Time.deltaTime;
  148. }
  149. rb.velocity = vel;
  150. }
  151. else
  152. {
  153. //眩晕状态
  154. if (attributeTime <= 0)
  155. {
  156. curSpecialStates = SpecialState.Null;
  157. character.ChangeState(CharacterState.Idle);
  158. }
  159. else
  160. {
  161. rb.velocity = Vector3.zero;
  162. attributeTime -= Time.deltaTime;
  163. }
  164. }
  165. break;
  166. //击落
  167. case SpecialState.ShotDown:
  168. if (rb.velocity.magnitude > 0)
  169. {
  170. //击落中
  171. Vector3 vel = rb.velocity;
  172. if (foot.TrigGround && vel.y <= 0.01f)
  173. {
  174. vel = Vector3.zero;
  175. }
  176. else
  177. {
  178. vel.x -= decelerationRatioX * Time.deltaTime;
  179. vel.y -= decelerationRatioY * Time.deltaTime;
  180. }
  181. rb.velocity = vel;
  182. }
  183. else
  184. {
  185. //眩晕状态
  186. if (attributeTime <= 0)
  187. {
  188. //被击晕后上升
  189. rb.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezePositionY;
  190. rb.useGravity = false;
  191. if (character.AdjustHeight())
  192. {
  193. curSpecialStates = SpecialState.Null;
  194. character.ChangeState(CharacterState.Idle);
  195. }
  196. }
  197. else
  198. {
  199. rb.velocity = Vector3.zero;
  200. attributeTime -= Time.deltaTime;
  201. }
  202. }
  203. break;
  204. //眩晕
  205. case SpecialState.Weak:
  206. if (attributeTime <= 0)
  207. {
  208. curSpecialStates = SpecialState.Null;
  209. character.ChangeState(CharacterState.Idle);
  210. }
  211. else
  212. {
  213. rb.velocity = Vector3.zero;
  214. attributeTime -= Time.deltaTime;
  215. }
  216. break;
  217. }
  218. }
  219. public int DamageCalculation(int damage)
  220. {
  221. damage = (int)(damage * (1 + vulnerableRate) + 0.5f);
  222. return damage;
  223. }
  224. //判断优先级,ture为优先级高于当前控制
  225. public bool PriorityOrder(SpecialState specialState)
  226. {
  227. if (curSpecialStates != SpecialState.Null && curSpecialStates <= specialState)
  228. {
  229. return true;
  230. }
  231. if(curSpecialStates == SpecialState.Null)
  232. {
  233. return true;
  234. }
  235. return true;
  236. }
  237. //受到漂浮
  238. public void AddFloat(AttackInfo.FloatState floatState)
  239. {
  240. if (!PriorityOrder(SpecialState.FloatState))
  241. {
  242. return;
  243. }
  244. rb.useGravity = false;
  245. floatingState = 1;
  246. origPos = character.transform.position;
  247. curHeight = origPos.y;
  248. riseTime = UnityEngine.Random.Range(floatState.upTime.x, floatState.upTime.y);
  249. backSpeed = UnityEngine.Random.Range(floatState.backSpeed.x, floatState.backSpeed.y);
  250. if (gameObject.tag == "Enemy" || gameObject.tag == "Player")
  251. {
  252. backSpeed = -backSpeed;
  253. }
  254. rotateSpeed = UnityEngine.Random.Range(floatState.rotateSpeed.x, floatState.rotateSpeed.y);
  255. rotateDir = (1.5f - UnityEngine.Random.Range(1, 3)) * 2;
  256. height = UnityEngine.Random.Range(floatState.height.x, floatState.height.y);
  257. attributeTime = floatState.time * (1 - resistances.Float);
  258. curSpecialStates = SpecialState.FloatState;
  259. character.ChangeState(CharacterState.SpecialStatus_Float);
  260. }
  261. //受到击飞
  262. public void AddBlowUp(AttackInfo.BlowUp blowUp, float dir)
  263. {
  264. if (!rb.useGravity)
  265. {
  266. return;
  267. }
  268. if (!PriorityOrder(SpecialState.BlownUp))
  269. {
  270. return;
  271. }
  272. attributeTime = blowUp.time * (1 - resistances.BlowUp);
  273. Vector3 vec3 = blowUp.dir.normalized;
  274. if (dir < 0)
  275. {
  276. vec3.x = -vec3.x;
  277. }
  278. rb.AddForce(vec3 * blowUp.force * (1 - resistances.BlowUp), ForceMode.Impulse);
  279. curSpecialStates = SpecialState.BlownUp;
  280. character.ani.Play("hitted",0,0);
  281. character.ChangeState(CharacterState.SpecialStatus_BlowUp);
  282. }
  283. //受到击落
  284. public void AddShotDown(AttackInfo.ShotDown shotDown, float dir)
  285. {
  286. if (rb.useGravity)
  287. {
  288. return;
  289. }
  290. if (!PriorityOrder(SpecialState.ShotDown))
  291. {
  292. return;
  293. }
  294. attributeTime = shotDown.time * (1 - resistances.ShotDown);
  295. Vector3 vec3 = shotDown.dir.normalized;
  296. if (dir < 0)
  297. {
  298. vec3.x = -vec3.x;
  299. }
  300. rb.useGravity = true;
  301. rb.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionZ;
  302. rb.AddForce(vec3 * shotDown.force * (1 - resistances.ShotDown), ForceMode.Impulse);
  303. curSpecialStates = SpecialState.ShotDown;
  304. character.ani.Play("hitted", 0, 0);
  305. character.ChangeState(CharacterState.SpecialStatus_ShotDown);
  306. }
  307. //受到击晕
  308. public void AddWeak(AttackInfo.Weak weak)
  309. {
  310. if (!PriorityOrder(SpecialState.Weak))
  311. {
  312. return;
  313. }
  314. attributeTime = weak.time * (1 - resistances.Weak);
  315. curSpecialStates = SpecialState.Weak;
  316. character.ani.Play("weak", 0, 0);
  317. character.ChangeState(CharacterState.SpecialStatus_Weak);
  318. }
  319. //受到穿甲
  320. public int AddArmor(AttackInfo.Armor armor,int damage)
  321. {
  322. //计算护甲减免
  323. int am = resistances.armor;
  324. if (am > 0)
  325. {
  326. int ap = armor.rate;
  327. int c = am - ap;
  328. if (c < 0)
  329. {
  330. c = 0;
  331. }
  332. damage = (int)(damage * (100f / (100 + c)) + 0.5f);
  333. }
  334. return damage;
  335. }
  336. //受到易伤
  337. public void AddVulnerable(AttackInfo.Vulnerable vulnerable)
  338. {
  339. vulnerableTime = vulnerable.time;
  340. vulnerableRate = vulnerable.rate;
  341. haveVulnerable = true;
  342. }
  343. }