MoveCharacter.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 = 1.5f; //上升最大耗时
  32. public float minTime = 0.1f; //上升最小耗时
  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. public float curFallSpeed;
  49. private Vector3 origPos; //初始位置
  50. private float origY;
  51. private float pastTime; //上升已用时间
  52. private float rise = 1;
  53. public int floatState; //0:不漂浮;1:漂浮中;2:飘着;3:掉下去
  54. public GameObject spinee;
  55. public MeshRenderer mesh;
  56. public Material[] mats;
  57. public Material[] outlineMats;
  58. public SkeletonMecanim playerMe;
  59. public Animator playerAni;
  60. public Animator playerCol;
  61. public Transform playerTran;
  62. public BeSearchTrigger playerBst;
  63. public GameObject playerBullet;
  64. public SearchTrigger playerST;
  65. public Foot playerFoot;
  66. public GameObject playerSpinee;
  67. public MeshRenderer playerMesh;
  68. public Material[] playerMats;
  69. public Material[] playerOut;
  70. public bool isInvisible;
  71. public bool canNotChangeHurt;
  72. public GameObject soulPrefab;
  73. public float soulStartSpeed = 1f;
  74. private void Awake()
  75. {
  76. spinee = bodyTrans.GetChild(0).gameObject;
  77. mesh = spinee.GetComponent<MeshRenderer>();
  78. mats = mesh.materials;
  79. origY = transform.position.y;
  80. curFallSpeed = normalFallSpeed;
  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 (!isTran)
  118. {
  119. if (canMove)
  120. {
  121. canMove = false;
  122. if (curFallSpeed == 0)
  123. {
  124. curFallSpeed = normalFallSpeed;
  125. }
  126. isFloat = true;
  127. ChangeMat(0);
  128. curTime = 0;
  129. if (floatState == 0)
  130. {
  131. origPos = transform.position;
  132. origY = origPos.y;
  133. curHeight = origPos.y;
  134. }
  135. origPos.x = transform.position.x;
  136. ChangeState(CharacterState.Rise);
  137. floatState = 1;
  138. riseTime = Random.Range(minTime, maxTime);
  139. backSpeed = Random.Range(1, 4);
  140. if (gameObject.tag == "Enemy" || gameObject.tag == "Player")
  141. {
  142. backSpeed = -backSpeed;
  143. }
  144. rotateSpeed = Random.Range(minRotateSpeed, maxRotateSpeed);
  145. rotateDir = (1.5f - Random.Range(1, 3)) * 2;
  146. height = Random.Range(minHeight, maxHeight);
  147. }
  148. }
  149. else
  150. {
  151. if (pc == null)
  152. {
  153. pc = GetComponentInParent<PlayerController>();
  154. }
  155. pc.FloatStateOn();
  156. }
  157. }
  158. public void FloatDrop()
  159. {
  160. isBeDropped = true;
  161. transform.localEulerAngles = new Vector3(0, 0, 0);
  162. ChangeState(CharacterState.Fall);
  163. floatState = 3;
  164. curFallSpeed = rapidFallSpeed;
  165. }
  166. private void RotateSelf()
  167. {
  168. transform.localEulerAngles += new Vector3(0, 0, 1) * rotateDir * rotateSpeed * Time.deltaTime;
  169. }
  170. public void CharacterFloat()
  171. {
  172. if (floatState == 1)
  173. {
  174. RotateSelf();
  175. curTime += Time.deltaTime;
  176. aniCollider.Play("Rise", 0, 0);
  177. curHeight = Mathf.SmoothDamp(curHeight, height, ref rise, riseTime);
  178. transform.position = new Vector3(origPos.x, curHeight, origPos.z);
  179. if (curHeight >= height - 0.02f)
  180. {
  181. floatState = 2;
  182. pastTime = curTime;
  183. height = transform.position.y;
  184. ChangeState(CharacterState.Float);
  185. }
  186. }
  187. else if (floatState == 2)
  188. {
  189. RotateSelf();
  190. curTime += Time.deltaTime;
  191. transform.position = new Vector3(origPos.x + backSpeed * (curTime - pastTime), height, origPos.z);
  192. if (curTime >= floatTime)
  193. {
  194. transform.localEulerAngles = new Vector3(0, 0, 0);
  195. floatState = 3;
  196. origPos.x = transform.position.x;
  197. ChangeState(CharacterState.Fall);
  198. }
  199. }
  200. else if (floatState == 3)
  201. {
  202. aniCollider.Play("Fall", 0, 0);
  203. if (transform.position.y >= origY + 0.05f)
  204. {
  205. curHeight -= curFallSpeed * Time.deltaTime;
  206. transform.position = new Vector3(origPos.x, curHeight, origPos.z);
  207. }
  208. else if (foot.TrigGround || curHeight <= origY + 0.05f)
  209. {
  210. if (!isBeDropped)
  211. {
  212. //transform.position = origPos;
  213. ChangeState(CharacterState.Idle);
  214. }
  215. else
  216. {
  217. ChangeState(CharacterState.Coma);
  218. isBeDropped = false;
  219. }
  220. ChangeMat(1);
  221. /*
  222. foreach (Material m in mats)
  223. {
  224. m.SetInt("_Outline", 0);
  225. }
  226. */
  227. floatState = 0;
  228. isFloat = false;
  229. canMove = true;
  230. curFallSpeed = normalFallSpeed;
  231. if (gameObject.tag == "Player")
  232. {
  233. if (pc == null)
  234. {
  235. pc = GetComponentInParent<PlayerController>();
  236. }
  237. pc.soulCollector.enabled = true;
  238. }
  239. }
  240. }
  241. }
  242. public void Update()
  243. {
  244. if (beLarger)
  245. {
  246. Enlarge();
  247. }
  248. if (floatState != 0)
  249. {
  250. CharacterFloat();
  251. }
  252. }
  253. public override void BeHit(int damage, Vector3 force, bool changeHurt, float repelValue)
  254. {
  255. if (!isTran)
  256. {
  257. if (isInvisible)
  258. {
  259. return;
  260. }
  261. if (invincibleTime > 0)
  262. {
  263. return;
  264. }
  265. hp -= damage;
  266. uiHp.Show(hp, totalHp);
  267. if (hp <= 0)
  268. {
  269. ChangeState(CharacterState.Die);
  270. rb.AddForce(force);
  271. return;
  272. }
  273. if (isSoulUnstable)
  274. {
  275. GameObject soulObj = PoolManager.Instantiate(soulPrefab, transform.position);
  276. Vector3 dir = Vector3.up;
  277. soulObj.GetComponent<Soul>().Burst(dir * soulStartSpeed);
  278. }
  279. if (canNotChangeHurt)
  280. {
  281. return;
  282. }
  283. if (changeHurt && state == CharacterState.Weak)
  284. {
  285. ChangeState(CharacterState.Hurt);
  286. rb.AddForce(force);
  287. beRepelValue = totalBeRepelValue;
  288. return;
  289. }
  290. beRepelValue -= repelValue;
  291. if (changeHurt && beRepelValue <= 0)
  292. {
  293. ChangeState(CharacterState.Weak);
  294. }
  295. }
  296. else
  297. {
  298. if (pc == null)
  299. {
  300. pc = GetComponentInParent<PlayerController>();
  301. }
  302. pc.BeHit(damage, force, changeHurt, repelValue);
  303. }
  304. }
  305. }