MoveCharacter.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. using Spine.Unity;
  2. using Spine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. public class MoveCharacter : Character
  7. {
  8. public bool canMove = true;
  9. public Foot foot;
  10. public float extraRiseGravity = 0; //上升时额外重力加速度
  11. public float extraFallGravity = -10; //下落时额外重力加速度
  12. public float moveSpeed = 5;
  13. //[HideInInspector]
  14. public float beRepelValue;
  15. public float totalBeRepelValue;
  16. [HideInInspector]
  17. public float weakTime;
  18. public float totalWeakTime;
  19. public float weakUpSpeed = 10f;
  20. public float decelerationRatio = 1f;
  21. public float minHurtKeepTime = 0.2f;
  22. [HideInInspector]
  23. public float hurtKeepTime = 0;
  24. public float hurtChangeVelocity = 1;
  25. [Header("新增眩晕参数")]
  26. public float comaTime = 5;
  27. public float pastComaTime;
  28. public bool isCaughtByCook;
  29. public bool isBeDropped;
  30. [Header("新增漂浮效果参数")]
  31. public float maxTime = 0; //上升最大耗时
  32. public float minTime = 6; //上升最小耗时
  33. public float maxHeight = 12; //最大上升高度
  34. public float minHeight = 7; //最小上升高度
  35. public float maxRotateSpeed = 20; //最大旋转速度
  36. public float minRotateSpeed = 5; //最小旋转速度
  37. public float floatTime = 20; //漂浮时间
  38. private float curTime; //漂浮已进行时长
  39. private float height; //漂浮高度
  40. private float riseTime; //上升时间
  41. private float curHeight; //当前所在高度
  42. private float rotateSpeed; //旋转速度
  43. private float rotateDir; //旋转方向
  44. private float backSpeed; //往后退的速度
  45. public bool isFloat; //正在漂浮中
  46. public float normalFallSpeed;
  47. public float rapidFallSpeed;
  48. private float curFallSpeed;
  49. private Vector3 origPos; //初始位置
  50. private float origY;
  51. private float pastTime; //上升已用时间
  52. private float rise = 1;
  53. private float down = -1;
  54. private Vector3 oneClockwise = new Vector3(0, 0, 1);
  55. private Vector3 oneCounterClockwise = new Vector3(0, 0, -1);
  56. public int floatState; //0:不漂浮;1:漂浮中;2:飘着;3:掉下去
  57. public GameObject spinee;
  58. public MeshRenderer mesh;
  59. public Material[] mats;
  60. public Material[] outlineMats;
  61. public SkeletonMecanim playerMe;
  62. public Animator playerAni;
  63. public Animator playerCol;
  64. public Transform playerTran;
  65. public BeSearchTrigger playerBst;
  66. public GameObject playerBullet;
  67. public SearchTrigger playerST;
  68. public Foot playerFoot;
  69. public GameObject playerSpinee;
  70. public MeshRenderer playerMesh;
  71. public Material[] playerMats;
  72. public Material[] playerOut;
  73. public bool isInvisible;
  74. public bool canNotChangeHurt;
  75. private void Awake()
  76. {
  77. spinee = bodyTrans.GetChild(0).gameObject;
  78. mesh = spinee.GetComponent<MeshRenderer>();
  79. mats = mesh.materials;
  80. origY = transform.position.y;
  81. }
  82. private void Start()
  83. {
  84. playerMe = mecanim;
  85. playerAni = ani;
  86. playerCol = aniCollider;
  87. playerTran = bodyTrans;
  88. playerBst = beSearchTrigger;
  89. playerBullet = bulletPrefab;
  90. playerST = searchTrigger;
  91. playerFoot = foot;
  92. playerSpinee = spinee;
  93. playerMesh = mesh;
  94. playerMats = mats;
  95. playerOut = outlineMats;
  96. curFallSpeed = normalFallSpeed;
  97. }
  98. private void ChangeMat(int state)
  99. {
  100. if (spinee == null || mesh == null || mats == null)
  101. {
  102. spinee = transform.GetChild(0).GetChild(0).gameObject;
  103. mesh = spinee.GetComponent<MeshRenderer>();
  104. mats = mesh.materials;
  105. }
  106. if (state == 0)
  107. {
  108. mesh.materials = outlineMats;
  109. }
  110. else
  111. {
  112. mesh.materials = mats;
  113. }
  114. }
  115. public void FloatStateOn()
  116. {
  117. if (canMove)
  118. {
  119. isFloat = true;
  120. ChangeMat(0);
  121. curTime = 0;
  122. if (floatState == 0)
  123. {
  124. origPos = transform.position;
  125. curHeight = origPos.y;
  126. }
  127. origPos.x = transform.position.x;
  128. ChangeState(CharacterState.Rise);
  129. floatState = 1;
  130. riseTime = Random.Range(minTime, maxTime);
  131. backSpeed = Random.Range(1, 4);
  132. if (gameObject.tag == "Enemy" || gameObject.tag == "Player")
  133. {
  134. backSpeed = -backSpeed;
  135. }
  136. rotateSpeed = Random.Range(minRotateSpeed, maxRotateSpeed);
  137. rotateDir = (1.5f - Random.Range(1, 3)) * 2;
  138. height = Random.Range(minHeight, maxHeight);
  139. }
  140. }
  141. public void FloatDrop()
  142. {
  143. isBeDropped = true;
  144. transform.localEulerAngles = new Vector3(0, 0, 0);
  145. ChangeState(CharacterState.Fall);
  146. floatState = 3;
  147. curFallSpeed = rapidFallSpeed;
  148. }
  149. private void RotateSelf()
  150. {
  151. transform.localEulerAngles += new Vector3(0, 0, 1) * rotateDir * rotateSpeed * Time.deltaTime;
  152. }
  153. public void CharacterFloat()
  154. {
  155. if (floatState == 1)
  156. {
  157. RotateSelf();
  158. curTime += Time.deltaTime;
  159. aniCollider.Play("Rise", 0, 0);
  160. curHeight = Mathf.SmoothDamp(curHeight, height, ref rise, riseTime);
  161. transform.position = new Vector3(origPos.x, curHeight, origPos.z);
  162. if (curHeight >= height - 0.02f)
  163. {
  164. floatState = 2;
  165. pastTime = curTime;
  166. height = transform.position.y;
  167. ChangeState(CharacterState.Float);
  168. }
  169. }
  170. else if (floatState == 2)
  171. {
  172. RotateSelf();
  173. curTime += Time.deltaTime;
  174. transform.position = new Vector3(origPos.x + backSpeed * (curTime - pastTime), height, origPos.z);
  175. if (curTime >= floatTime)
  176. {
  177. transform.localEulerAngles = new Vector3(0, 0, 0);
  178. floatState = 3;
  179. origPos.x = transform.position.x;
  180. ChangeState(CharacterState.Fall);
  181. }
  182. }
  183. else if (floatState == 3)
  184. {
  185. aniCollider.Play("Fall", 0, 0);
  186. if (transform.position.y >= origY + 0.05f)
  187. {
  188. curHeight -= curFallSpeed * Time.deltaTime;
  189. transform.position = new Vector3(origPos.x, curHeight, origPos.z);
  190. }
  191. else if (foot.TrigGround || curHeight <= origY + 0.05f)
  192. {
  193. if (!isBeDropped)
  194. {
  195. transform.position = origPos;
  196. ChangeState(CharacterState.Idle);
  197. }
  198. else
  199. {
  200. ChangeState(CharacterState.Coma);
  201. isBeDropped = false;
  202. }
  203. ChangeMat(1);
  204. foreach (Material m in mats)
  205. {
  206. m.SetInt("_Outline", 0);
  207. floatState = 0;
  208. isFloat = false;
  209. curFallSpeed = normalFallSpeed;
  210. if (gameObject.tag == "Player")
  211. {
  212. GetComponent<PlayerController>().soulCollector.enabled = true;
  213. }
  214. }
  215. }
  216. }
  217. }
  218. public void Update()
  219. {
  220. if (beLarger)
  221. {
  222. Enlarge();
  223. }
  224. if (floatState != 0)
  225. {
  226. CharacterFloat();
  227. }
  228. }
  229. public override void BeHit(int damage, Vector3 force, bool changeHurt, float repelValue)
  230. {
  231. if (!isTran)
  232. {
  233. if (isInvisible)
  234. {
  235. return;
  236. }
  237. if (invincibleTime > 0)
  238. {
  239. return;
  240. }
  241. hp -= damage;
  242. uiHp.Show(hp, totalHp);
  243. if (hp <= 0)
  244. {
  245. ChangeState(CharacterState.Die);
  246. rb.AddForce(force);
  247. return;
  248. }
  249. if (canNotChangeHurt)
  250. {
  251. return;
  252. }
  253. if (changeHurt && state == CharacterState.Weak)
  254. {
  255. ChangeState(CharacterState.Hurt);
  256. rb.AddForce(force);
  257. beRepelValue = totalBeRepelValue;
  258. return;
  259. }
  260. beRepelValue -= repelValue;
  261. if (changeHurt && beRepelValue <= 0)
  262. {
  263. ChangeState(CharacterState.Weak);
  264. }
  265. }
  266. else
  267. {
  268. if (pc == null)
  269. {
  270. pc = GetComponentInParent<PlayerController>();
  271. }
  272. pc.BeHit(damage, force, changeHurt, repelValue);
  273. }
  274. }
  275. }