Demonic.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. using Spine.Unity;
  2. using Spine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. public enum DemonicState
  7. {
  8. Idle = 0,
  9. Walk = 2,
  10. Rise = 3,//空中上升
  11. Fall = 4,//空中下落
  12. Hurt = 5,
  13. Attack1 = 6,
  14. Attack2 = 7,
  15. Die = 8,
  16. }
  17. public enum DemonicTargetType
  18. {
  19. Player = 0,
  20. Demonic = 1,
  21. FlyDemonic = 2,
  22. LowHPDemonic = 3,
  23. Tower = 4,
  24. }
  25. public class Demonic : MonoBehaviour
  26. {
  27. public SkeletonMecanim mecanim;
  28. public Skeleton skeleton;
  29. public Animator ani;
  30. public Rigidbody rb;
  31. public Foot foot;
  32. public Collider bodyCollider;
  33. public float extraRiseGravity = 0; //上升时额外重力加速度
  34. public float extraFallGravity = -10; //下落时额外重力加速度
  35. //public float jumpSpeed = 10;
  36. //public float airJumpSpeed = 10;
  37. public float maxMoveSpeed = 5;
  38. //public float moveAcc = 5f;
  39. //public float airMoveAcc = 3f;
  40. public DemonicState state;
  41. [HideInInspector]
  42. public float invincibleTime;
  43. public float totalInvincibleTime = 2f;
  44. [HideInInspector]
  45. public float hurtKeepTime;
  46. public float totalHurtKeepTime = 0.5f;
  47. [HideInInspector]
  48. public float attackTime;
  49. public float totalAttack1Time = 0.5f;
  50. public float totalAttack2Time = 0.5f;
  51. public bool isDie = false;
  52. public int totalHp = 100;
  53. public int hp;
  54. public bool canFly = false;
  55. public float attackDistance;
  56. private void Awake()
  57. {
  58. if (!mecanim)
  59. {
  60. mecanim = GetComponentInChildren<SkeletonMecanim>();
  61. skeleton = mecanim.skeleton;
  62. }
  63. if (!ani)
  64. {
  65. ani = GetComponentInChildren<Animator>();
  66. }
  67. ChangeState(DemonicState.Idle);
  68. }
  69. private void FixedUpdate()
  70. {
  71. OnState();
  72. }
  73. public void Turn()
  74. {
  75. transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
  76. }
  77. public Vector3 GetMoveDir()
  78. {
  79. return Vector3.zero;
  80. }
  81. public bool GetAttack()
  82. {
  83. return false;
  84. }
  85. public void OnState()
  86. {
  87. invincibleTime -= Time.deltaTime;
  88. hurtKeepTime -= Time.deltaTime;
  89. attackTime -= Time.deltaTime;
  90. Vector3 leftDir = GetMoveDir();
  91. bool isAttack = GetAttack();
  92. switch (state)
  93. {
  94. case DemonicState.Idle:
  95. if (isAttack)
  96. {
  97. ChangeState(DemonicState.Attack2);
  98. break;
  99. }
  100. if (!foot.TrigGround)
  101. {
  102. if (rb.velocity.y > 0)
  103. {
  104. ChangeState(DemonicState.Rise);
  105. break;
  106. }
  107. else
  108. {
  109. ChangeState(DemonicState.Fall);
  110. break;
  111. }
  112. }
  113. if (leftDir.x > 0.3f || leftDir.x < -0.3f)
  114. {
  115. ChangeState(DemonicState.Walk);
  116. break;
  117. }
  118. //rb.velocity = Vector3.zero;
  119. break;
  120. case DemonicState.Walk:
  121. if (isAttack)
  122. {
  123. ChangeState(DemonicState.Attack2);
  124. break;
  125. }
  126. if (!foot.TrigGround)
  127. {
  128. if (rb.velocity.y > 0)
  129. {
  130. ChangeState(DemonicState.Rise);
  131. break;
  132. }
  133. else
  134. {
  135. ChangeState(DemonicState.Fall);
  136. break;
  137. }
  138. }
  139. if (leftDir.x < 0.3f && leftDir.x > -0.3f)
  140. {
  141. ChangeState(DemonicState.Idle);
  142. break;
  143. }
  144. if (leftDir.x > 0.3f)
  145. {
  146. //rb.velocity += Vector3.right * moveAcc * Time.deltaTime;
  147. rb.velocity = Vector3.right * maxMoveSpeed;
  148. //if (rb.velocity.x > maxMoveSpeed)
  149. //{
  150. // rb.velocity = new Vector3(maxMoveSpeed, rb.velocity.y, rb.velocity.z);
  151. //}
  152. if (transform.localScale.x > 0)
  153. {
  154. Turn();
  155. }
  156. }
  157. else if (leftDir.x < -0.3f)
  158. {
  159. //rb.velocity -= Vector3.right * moveAcc * Time.deltaTime;
  160. rb.velocity = Vector3.left * maxMoveSpeed;
  161. //if (rb.velocity.x < -maxMoveSpeed)
  162. //{
  163. // rb.velocity = new Vector3(-maxMoveSpeed, rb.velocity.y, rb.velocity.z);
  164. //}
  165. if (transform.localScale.x < 0)
  166. {
  167. Turn();
  168. }
  169. }
  170. break;
  171. case DemonicState.Rise:
  172. if (rb.velocity.y <= 0)
  173. {
  174. ChangeState(DemonicState.Fall);
  175. break;
  176. }
  177. //if (btnJumpPress || cacheJumpTime > 0)
  178. //{
  179. // if (!airJumped && rb.velocity.y < airJumpSpeed)
  180. // {
  181. // airJumped = true;
  182. // AirJump();
  183. // break;
  184. // }
  185. //}
  186. rb.velocity += Vector3.up * extraRiseGravity * Time.deltaTime;
  187. break;
  188. case DemonicState.Fall:
  189. if (foot.TrigGround)
  190. {
  191. ChangeState(DemonicState.Idle);
  192. break;
  193. }
  194. //if (foot.canStepPlayers.Count > 0)
  195. //{
  196. // Jump(jumpSpeed / 2);
  197. // StepOther();
  198. // break;
  199. //}
  200. //if (foot.canStepEnemyList.Count > 0)
  201. //{
  202. // Jump(jumpSpeed / 2);
  203. // StepEnemy();
  204. // break;
  205. //}
  206. //if (btnJumpPress || cacheJumpTime > 0)
  207. //{
  208. // if (!airJumped)
  209. // {
  210. // airJumped = true;
  211. // AirJump();
  212. // break;
  213. // }
  214. // else if (canJumpTick >= runner.Tick)
  215. // {
  216. // Jump();
  217. // break;
  218. // }
  219. //}
  220. rb.velocity += Vector3.up * extraFallGravity * Time.deltaTime;
  221. break;
  222. case DemonicState.Hurt:
  223. if (hurtKeepTime <= 0)
  224. {
  225. ChangeState(DemonicState.Idle);
  226. break;
  227. }
  228. if (!foot.TrigGround)
  229. {
  230. rb.velocity += Vector3.up * extraFallGravity * Time.deltaTime;
  231. }
  232. rb.velocity = rb.velocity / 5 * 4;
  233. break;
  234. case DemonicState.Attack1:
  235. if (attackTime <= 0)
  236. {
  237. ChangeState(DemonicState.Idle);
  238. break;
  239. }
  240. break;
  241. case DemonicState.Attack2:
  242. if (attackTime <= 0)
  243. {
  244. ChangeState(DemonicState.Idle);
  245. break;
  246. }
  247. break;
  248. default:
  249. break;
  250. }
  251. }
  252. public void ChangeState(DemonicState newState)
  253. {
  254. switch (state)
  255. {
  256. case DemonicState.Idle:
  257. break;
  258. case DemonicState.Walk:
  259. break;
  260. case DemonicState.Rise:
  261. break;
  262. case DemonicState.Fall:
  263. break;
  264. case DemonicState.Hurt:
  265. break;
  266. case DemonicState.Attack1:
  267. break;
  268. case DemonicState.Attack2:
  269. break;
  270. case DemonicState.Die:
  271. isDie = false;
  272. break;
  273. default:
  274. break;
  275. }
  276. DemonicState oldState = state;
  277. state = newState;
  278. switch (newState)
  279. {
  280. case DemonicState.Idle:
  281. ani.Play("idle", 0, 0);
  282. rb.velocity = Vector3.zero;
  283. //animalAni.SetInteger("state", (int)PlayerState.Idle);
  284. break;
  285. case DemonicState.Walk:
  286. ani.Play("walk", 0, 0);
  287. //animalAni.SetInteger("state", (int)PlayerState.Walk);
  288. break;
  289. case DemonicState.Rise:
  290. break;
  291. case DemonicState.Fall:
  292. //animalAni.SetInteger("state", (int)PlayerState.Fall);
  293. break;
  294. case DemonicState.Hurt:
  295. ani.Play("hitted", 0, 0);
  296. hurtKeepTime = totalHurtKeepTime;
  297. invincibleTime = totalInvincibleTime;
  298. //ani.Play("Invincible", 2, 0);
  299. break;
  300. case DemonicState.Attack1:
  301. ani.Play("attack1", 0, 0);
  302. attackTime = totalAttack1Time;
  303. break;
  304. case DemonicState.Attack2:
  305. ani.Play("attack2", 0, 0);
  306. attackTime = totalAttack2Time;
  307. break;
  308. case DemonicState.Die:
  309. ani.Play("die", 0, 0);
  310. isDie = true;
  311. break;
  312. default:
  313. break;
  314. }
  315. }
  316. }