MoveCharacter.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 int dropDamage; //漂浮摔落伤害
  47. public float normalFallSpeed;
  48. public float rapidFallSpeed;
  49. public float curFallSpeed;
  50. private Vector3 origPos; //初始位置
  51. private float origY;
  52. private float pastTime; //上升已用时间
  53. private float rise = 1;
  54. public int floatState; //0:不漂浮;1:漂浮中;2:飘着;3:掉下去
  55. public GameObject spinee;
  56. public MeshRenderer mesh;
  57. public Material[] mats;
  58. public Material[] outlineMats;
  59. public Material[] outline1Mats;
  60. public SkeletonMecanim playerMe;
  61. public Animator playerAni;
  62. public Animator playerCol;
  63. public Transform playerTran;
  64. public BeSearchTrigger playerBst;
  65. public GameObject playerBullet;
  66. public SearchTrigger playerST;
  67. public Foot playerFoot;
  68. public GameObject playerSpinee;
  69. public MeshRenderer playerMesh;
  70. public Material[] playerMats;
  71. public Material[] playerOut;
  72. public bool isInvisible;
  73. public bool canNotChangeHurt;
  74. public GameObject soulPrefab;
  75. public float soulStartSpeed = 1f;
  76. private void Awake()
  77. {
  78. spinee = bodyTrans.GetChild(0).gameObject;
  79. mesh = spinee.GetComponent<MeshRenderer>();
  80. mats = mesh.materials;
  81. origY = transform.position.y;
  82. curFallSpeed = normalFallSpeed;
  83. }
  84. private void Start()
  85. {
  86. playerMe = mecanim;
  87. playerAni = ani;
  88. playerCol = aniCollider;
  89. playerTran = bodyTrans;
  90. playerBst = beSearchTrigger;
  91. playerBullet = bulletPrefab;
  92. playerST = searchTrigger;
  93. playerFoot = foot;
  94. playerSpinee = spinee;
  95. playerMesh = mesh;
  96. playerMats = mats;
  97. playerOut = outlineMats;
  98. curFallSpeed = normalFallSpeed;
  99. }
  100. //0:漂浮 1:正常 2:灵魂不稳定
  101. public void ChangeMat(int state)
  102. {
  103. if(outline1Mats.Length == 0)
  104. {
  105. Transform particleSystem = bodyTrans.GetChild(1).GetChild(0);
  106. if (particleSystem == null ||
  107. ( particleSystem != null && particleSystem.name != "Particle System"))
  108. {
  109. return;
  110. }
  111. ParticleSystem.MainModule mainModule;
  112. switch (state)
  113. {
  114. case 0:
  115. break;
  116. case 1:
  117. print(particleSystem.name);
  118. mainModule =
  119. particleSystem.GetChild(1).GetComponent<ParticleSystem>().main;
  120. mainModule.startColor = new ParticleSystem.MinMaxGradient(Color.white);
  121. mainModule =
  122. particleSystem.GetChild(2).GetComponent<ParticleSystem>().main;
  123. mainModule.startColor = new ParticleSystem.MinMaxGradient(Color.white);
  124. break;
  125. case 2:
  126. mainModule =
  127. particleSystem.GetChild(1).GetComponent<ParticleSystem>().main;
  128. mainModule.startColor = new ParticleSystem.MinMaxGradient(Color.yellow);
  129. mainModule =
  130. particleSystem.GetChild(2).GetComponent<ParticleSystem>().main;
  131. mainModule.startColor = new ParticleSystem.MinMaxGradient(Color.yellow);
  132. break;
  133. default:
  134. break;
  135. }
  136. return;
  137. }
  138. if (spinee == null || mesh == null || mats == null)
  139. {
  140. spinee = transform.GetChild(0).GetChild(0).gameObject;
  141. mesh = spinee.GetComponent<MeshRenderer>();
  142. mats = mesh.materials;
  143. }
  144. switch (state)
  145. {
  146. case 0:
  147. mesh.materials = outlineMats;
  148. break;
  149. case 1:
  150. mesh.materials = mats;
  151. break;
  152. case 2:
  153. mesh.materials = outline1Mats;
  154. break;
  155. }
  156. }
  157. public void FloatStateOn()
  158. {
  159. if (!isTran)
  160. {
  161. if (canMove)
  162. {
  163. canMove = false;
  164. if (curFallSpeed == 0)
  165. {
  166. curFallSpeed = normalFallSpeed;
  167. }
  168. isFloat = true;
  169. ChangeMat(0);
  170. curTime = 0;
  171. if (floatState == 0)
  172. {
  173. origPos = transform.position;
  174. origY = origPos.y;
  175. curHeight = origPos.y;
  176. }
  177. origPos.x = transform.position.x;
  178. ChangeState(CharacterState.Rise);
  179. floatState = 1;
  180. riseTime = Random.Range(minTime, maxTime);
  181. backSpeed = Random.Range(1, 4);
  182. if (gameObject.tag == "Enemy" || gameObject.tag == "Player")
  183. {
  184. backSpeed = -backSpeed;
  185. }
  186. rotateSpeed = Random.Range(minRotateSpeed, maxRotateSpeed);
  187. rotateDir = (1.5f - Random.Range(1, 3)) * 2;
  188. height = Random.Range(minHeight, maxHeight);
  189. }
  190. }
  191. else
  192. {
  193. if (pc == null)
  194. {
  195. pc = GetComponentInParent<PlayerController>();
  196. }
  197. pc.FloatStateOn();
  198. }
  199. }
  200. public void FloatDrop()
  201. {
  202. isBeDropped = true;
  203. transform.localEulerAngles = new Vector3(0, 0, 0);
  204. ChangeState(CharacterState.Fall);
  205. floatState = 3;
  206. curFallSpeed = rapidFallSpeed;
  207. }
  208. private void RotateSelf()
  209. {
  210. transform.localEulerAngles += new Vector3(0, 0, 1) * rotateDir * rotateSpeed * Time.deltaTime;
  211. }
  212. public void CharacterFloat()
  213. {
  214. if (floatState == 1)
  215. {
  216. RotateSelf();
  217. curTime += Time.deltaTime;
  218. aniCollider.Play("Rise", 0, 0);
  219. curHeight = Mathf.SmoothDamp(curHeight, height, ref rise, riseTime);
  220. transform.position = new Vector3(origPos.x, curHeight, origPos.z);
  221. if (curHeight >= height - 0.02f)
  222. {
  223. floatState = 2;
  224. pastTime = curTime;
  225. height = transform.position.y;
  226. ChangeState(CharacterState.Float);
  227. }
  228. }
  229. else if (floatState == 2)
  230. {
  231. RotateSelf();
  232. curTime += Time.deltaTime;
  233. transform.position = new Vector3(origPos.x + backSpeed * (curTime - pastTime), height, origPos.z);
  234. if (curTime >= floatTime)
  235. {
  236. transform.localEulerAngles = new Vector3(0, 0, 0);
  237. floatState = 3;
  238. origPos.x = transform.position.x;
  239. ChangeState(CharacterState.Fall);
  240. }
  241. }
  242. else if (floatState == 3)
  243. {
  244. aniCollider.Play("Fall", 0, 0);
  245. if (!isBeDropped && transform.position.y >= origY + 0.05f || isBeDropped && !foot.TrigGround)
  246. {
  247. curHeight -= curFallSpeed * Time.deltaTime;
  248. transform.position = new Vector3(origPos.x, curHeight, origPos.z);
  249. }
  250. else if (foot.TrigGround || !isBeDropped && curHeight <= origY + 0.05f)
  251. {
  252. if (!isBeDropped)
  253. {
  254. //transform.position = origPos;
  255. ChangeState(CharacterState.Idle);
  256. }
  257. else
  258. {
  259. ChangeState(CharacterState.Coma);
  260. BeHit(dropDamage, Vector3.zero, false, 0);
  261. isBeDropped = false;
  262. }
  263. ChangeMat(1);
  264. /*
  265. foreach (Material m in mats)
  266. {
  267. m.SetInt("_Outline", 0);
  268. }
  269. */
  270. floatState = 0;
  271. isFloat = false;
  272. canMove = true;
  273. curFallSpeed = normalFallSpeed;
  274. if (gameObject.tag == "Player")
  275. {
  276. if (pc == null)
  277. {
  278. pc = GetComponentInParent<PlayerController>();
  279. }
  280. pc.soulCollector.enabled = true;
  281. }
  282. }
  283. }
  284. }
  285. public void Update()
  286. {
  287. if (beLarger)
  288. {
  289. Enlarge();
  290. }
  291. if (floatState != 0)
  292. {
  293. CharacterFloat();
  294. }
  295. if (isSoulUnstable)
  296. {
  297. soulUnstableTime -= Time.deltaTime;
  298. ChangeMat(2);
  299. if(soulUnstableTime < 0)
  300. {
  301. isSoulUnstable = false;
  302. ChangeMat(1);
  303. }
  304. }
  305. }
  306. public float easyToGetHit = 0.2f;
  307. public bool canNotBeHit;
  308. public override void BeHit(int damage, Vector3 force, bool changeHurt, float repelValue)
  309. {
  310. if (!isTran)
  311. {
  312. if (canNotBeHit)
  313. {
  314. return;
  315. }
  316. if (isInvisible)
  317. {
  318. return;
  319. }
  320. if (invincibleTime > 0)
  321. {
  322. return;
  323. }
  324. if (isFloat)
  325. {
  326. damage = (int)((1 + easyToGetHit) * damage);
  327. }
  328. hp -= damage;
  329. uiHp.Show(hp, totalHp);
  330. if (hp <= 0)
  331. {
  332. ChangeState(CharacterState.Die);
  333. rb.AddForce(force);
  334. return;
  335. }
  336. if (isSoulUnstable)
  337. {
  338. isSoulUnstable = false;
  339. ChangeMat(1);
  340. GameObject soulObj = PoolManager.Instantiate(soulPrefab, transform.position);
  341. Vector3 dir = Vector3.up;
  342. soulObj.GetComponent<Soul>().Burst(dir * soulStartSpeed);
  343. }
  344. if (canNotChangeHurt)
  345. {
  346. return;
  347. }
  348. if (changeHurt && state == CharacterState.Weak)
  349. {
  350. ChangeState(CharacterState.Hurt);
  351. rb.AddForce(force);
  352. beRepelValue = totalBeRepelValue;
  353. return;
  354. }
  355. beRepelValue -= repelValue;
  356. if (changeHurt && beRepelValue <= 0)
  357. {
  358. ChangeState(CharacterState.Weak);
  359. }
  360. }
  361. else
  362. {
  363. if (pc == null)
  364. {
  365. pc = GetComponentInParent<PlayerController>();
  366. }
  367. pc.BeHit(damage, force, changeHurt, repelValue);
  368. }
  369. }
  370. }