RockRoller.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. using Base.Common;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using cfg;
  6. using TMPro;
  7. public class RockRoller : Enemy
  8. {
  9. public int hitTime = 1;
  10. public int curHitTime = 0;
  11. public RockAttackTrigger attackTrigger0;
  12. public AttackTrigger attackEnemyTrigger0;
  13. public List<TargetType> initialTargetTypes = new List<TargetType>();
  14. public GameObject stone;
  15. public string smallStone;
  16. public float baseRotateSpeed = 90f;
  17. public float rotateMul = 1f;
  18. private float rotateDirection = -1f;
  19. private float dieTime = 0.1f;
  20. private float deathTimer = 0f;
  21. private bool isDying;
  22. private bool isFalled = false;
  23. public Enemy owner;
  24. private bool isGather = false;
  25. public float gatherTime = 1f;
  26. private float gatherTimer = 0f;
  27. private bool isWeak = false;
  28. public float weakTime = 1f;
  29. private float weakTimer = 0f;
  30. public float addForce = 1f;
  31. public SpriteRenderer RockSpriterRenderer;
  32. public List<Sprite> spriteList = new List<Sprite>();
  33. public List<float> scaleList = new List<float>();
  34. public bool isBig;
  35. private string enemyStr;
  36. public GameObject stoneBreakEff;
  37. public Transform effectSpawnPoint;
  38. public float effectDestroyDelay = 2f;
  39. public float smallStoneFoece = 20f;
  40. public override void Init()
  41. {
  42. base.Init();
  43. }
  44. private void Awake()
  45. {
  46. base.Awake();
  47. InitializeComponents();
  48. InitializeAttackMethods();
  49. InitializeTargetTypes();
  50. InitializeStoneReference();
  51. InitializeSpriter();
  52. InitializeScale();
  53. InitializeEffectSpawnPoint();
  54. }
  55. private void InitializeComponents()
  56. {
  57. TryGetComponent<Rigidbody>(out rb);
  58. }
  59. private void InitializeAttackMethods()
  60. {
  61. if (attackTrigger0 != null && attackTrigger0.attackMethod.attackInfo.damage == 0 &&
  62. attackEnemyTrigger0 != null && attackEnemyTrigger0.attackMethod.attackInfo.damage == 0)
  63. {
  64. attackTrigger0.attackMethod = attackController.attackMethod_march[0];
  65. attackEnemyTrigger0.attackMethod = attackController.attackMethod_summon[0];
  66. }
  67. }
  68. private void InitializeTargetTypes()
  69. {
  70. if (attackController != null && attackController.targetTypes != null)
  71. {
  72. initialTargetTypes.Clear();
  73. initialTargetTypes.AddRange(attackController.targetTypes);
  74. }
  75. }
  76. private void InitializeStoneReference()
  77. {
  78. if (stone == null)
  79. {
  80. stone = gameObject;
  81. }
  82. enemyStr = $"Prefab/Enemy/{smallStone}";
  83. }
  84. private void InitializeSpriter()
  85. {
  86. if (RockSpriterRenderer != null)
  87. {
  88. changeSpriter(0);
  89. }
  90. }
  91. private void InitializeScale()
  92. {
  93. changeScale(0);
  94. }
  95. public void changeSpriter(int id)
  96. {
  97. RockSpriterRenderer.sprite = spriteList[id];
  98. }
  99. public void changeScale(int id)
  100. {
  101. transform.localScale = new Vector3(1f, 1f, 1f)*scaleList[id];
  102. }
  103. private void InitializeEffectSpawnPoint()
  104. {
  105. if (effectSpawnPoint == null && stone != null)
  106. {
  107. effectSpawnPoint = stone.transform;
  108. }
  109. }
  110. protected override void OnEnable()
  111. {
  112. base.OnEnable();
  113. ResetState();
  114. }
  115. private void ResetState()
  116. {
  117. hitFeedbackSystem.canBeHitStun = false;
  118. hitFeedbackSystem.canFreeze = false;
  119. isDying = false;
  120. isFalled = false;
  121. deathTimer = 0f;
  122. gatherTimer = 0f;
  123. isGather = false;
  124. weakTimer = 0f;
  125. isWeak = false;
  126. rotateDirection = -1f;
  127. curHitTime = 0;
  128. stone.SetActive(true);
  129. if (state == CharacterState.None)
  130. {
  131. ChangeState(CharacterState.Idle);
  132. }
  133. if (attackEnemyTrigger0 != null)
  134. attackEnemyTrigger0.gameObject.SetActive(false);
  135. if (attackTrigger0 != null)
  136. attackTrigger0.gameObject.SetActive(true);
  137. changeSpriter(0);
  138. changeScale(0);
  139. ResetTargetTypes();
  140. }
  141. private void ResetTargetTypes()
  142. {
  143. if (attackController != null && attackController.targetTypes != null)
  144. {
  145. attackController.targetTypes.Clear();
  146. attackController.targetTypes.AddRange(initialTargetTypes);
  147. }
  148. }
  149. public void Update()
  150. {
  151. UpdateHitFeedback();
  152. if (owner == null) return;
  153. HandleOwnerSpecialState();
  154. Rotate();
  155. if (GameManager.instance.gameType == GameType.GameEnd)
  156. {
  157. ChangeState(CharacterState.Die);
  158. }
  159. }
  160. public void Rotate()
  161. {
  162. if (stone == null) return;
  163. float currentSpeed = baseRotateSpeed;
  164. if (rb != null)
  165. {
  166. currentSpeed = rb.velocity.magnitude * rotateMul * baseRotateSpeed;
  167. currentSpeed = Mathf.Clamp(currentSpeed, 0, baseRotateSpeed * 5);
  168. }
  169. stone.transform.Rotate(Vector3.forward, rotateDirection * currentSpeed * Time.deltaTime);
  170. }
  171. private void PlayBreakEff()
  172. {
  173. Vector3 spawnPos = effectSpawnPoint.position;
  174. GameObject effectObj;
  175. if (PoolManager.instance != null)
  176. {
  177. effectObj = PoolManager.Instantiate(stoneBreakEff, spawnPos);
  178. }
  179. else
  180. {
  181. effectObj = Instantiate(stoneBreakEff, effectSpawnPoint);
  182. }
  183. effectObj.transform.localScale = transform.localScale;
  184. // 自动销毁特效,防止内存泄漏
  185. if (effectDestroyDelay > 0)
  186. {
  187. Destroy(effectObj, effectDestroyDelay);
  188. }
  189. }
  190. private void UpdateHitFeedback()
  191. {
  192. hitFeedbackSystem.canBeHitStun = false;
  193. hitFeedbackSystem.canFreeze = false;
  194. }
  195. private void HandleOwnerSpecialState()
  196. {
  197. if (owner.state == CharacterState.SpecialStatus_BlowUp)
  198. {
  199. if (attackTrigger0 != null)
  200. attackTrigger0.gameObject.SetActive(false);
  201. UpdateTargetTypesForAttack();
  202. }
  203. }
  204. private void UpdateTargetTypesForAttack()
  205. {
  206. if (attackController != null)
  207. {
  208. attackController.targetTypes.Clear();
  209. attackController.targetTypes.Add(TargetType.Enemy);
  210. attackController.targetTypes.Add(TargetType.EnemyTower);
  211. }
  212. }
  213. public override void FixedUpdate()
  214. {
  215. OnState();
  216. HandleDyingState();
  217. }
  218. private void HandleDyingState()
  219. {
  220. if (owner != null && owner.state == CharacterState.SpecialStatus_BlowUp &&
  221. owner.attributeStatus.hitState == 1 && !isDying)
  222. {
  223. stone.SetActive(false);
  224. StartDying();
  225. }
  226. if (isDying)
  227. {
  228. deathTimer += Time.deltaTime;
  229. if (deathTimer >= dieTime)
  230. {
  231. HandleDeath();
  232. }
  233. }
  234. }
  235. private void StartDying()
  236. {
  237. if (attackEnemyTrigger0 != null)
  238. attackEnemyTrigger0.gameObject.SetActive(true);
  239. isDying = true;
  240. isFalled = true;
  241. deathTimer = 0f;
  242. }
  243. private void HandleDeath()
  244. {
  245. if (owner != null && curHitTime >= hitTime)
  246. {
  247. PlayBreakEff();
  248. owner.gameObject.SetActive(false);
  249. if (isBig)
  250. {
  251. SpawnSmallStones();
  252. }
  253. curHitTime = 0;
  254. }
  255. isDying = false;
  256. }
  257. private void SpawnSmallStones()
  258. {
  259. attackEnemyTrigger0.gameObject.SetActive(false);
  260. ResetTargetTypes();
  261. SingleEnemyConfig cfgEnemy = GameManager.instance.allCfgData.CfgEnemy.Get("小滚石怪");
  262. // 生成第一个小石头
  263. GameObject stone1 = Util.Instantiate(enemyStr, transform.position - new Vector3(0, 0, 0), active: true);
  264. ConfigureSmallStone(stone1, cfgEnemy);
  265. SmallStoneForce(stone1, new Vector3(-1, 1.2f, 0), smallStoneFoece);
  266. // 生成第二个小石头
  267. GameObject stone2 = Util.Instantiate(enemyStr, transform.position - new Vector3(0, 0, 0), active: true);
  268. ConfigureSmallStone(stone2, cfgEnemy);
  269. SmallStoneForce(stone2, new Vector3(1, 1.2f, 0), smallStoneFoece);
  270. isFalled = false;
  271. }
  272. private void SmallStoneForce(GameObject stone, Vector3 forceDirection, float forceMul)
  273. {
  274. Rigidbody rockRb = stone.GetComponent<Rigidbody>();
  275. RockRoller Roller = stone.GetComponent<RockRoller>();
  276. Roller.ChangeState(CharacterState.Fall);
  277. rockRb.AddForce(forceDirection * forceMul, ForceMode.Impulse);
  278. }
  279. private void ConfigureSmallStone(GameObject stoneObj, SingleEnemyConfig cfgEnemy)
  280. {
  281. RockRoller rock = stoneObj.GetComponent<RockRoller>();
  282. if (rock != null)
  283. {
  284. Debug.Log(cfgEnemy.AttackMarch[0]);
  285. Debug.Log(cfgEnemy.AttackSummon);
  286. Debug.Log(cfgEnemy.MinMoveSpeed);
  287. Debug.Log(cfgEnemy.MaxMoveSpeed);
  288. rock.attackTrigger0.attackMethod.attackInfo.damage = cfgEnemy.AttackMarch[0];
  289. rock.attackEnemyTrigger0.attackMethod.attackInfo.damage = cfgEnemy.AttackSummon;
  290. rock.owner.moveSpeed = Random.Range(cfgEnemy.MinMoveSpeed, cfgEnemy.MaxMoveSpeed);
  291. }
  292. }
  293. public override void ChangeState(CharacterState newState)
  294. {
  295. if (state == newState || newState == CharacterState.FramePause || state == CharacterState.Die)
  296. {
  297. return;
  298. }
  299. ExitCurrentState();
  300. EnterNewState(newState);
  301. }
  302. private void ExitCurrentState()
  303. {
  304. switch (state)
  305. {
  306. case CharacterState.Idle:
  307. rb.velocity = Vector3.zero;
  308. break;
  309. case CharacterState.Die:
  310. isDie = false;
  311. break;
  312. }
  313. }
  314. private void EnterNewState(CharacterState newState)
  315. {
  316. CharacterState oldState = state;
  317. state = newState;
  318. switch (newState)
  319. {
  320. case CharacterState.Idle:
  321. EnterIdleState();
  322. break;
  323. case CharacterState.Rush:
  324. EnterRushState();
  325. break;
  326. case CharacterState.Rise:
  327. EnterRiseState();
  328. break;
  329. case CharacterState.Fall:
  330. EnterFallState();
  331. break;
  332. case CharacterState.Die:
  333. EnterDieState();
  334. break;
  335. case CharacterState.SpecialStatus_BlowUp:
  336. EnterBlowUpState();
  337. break;
  338. }
  339. }
  340. private void EnterIdleState()
  341. {
  342. rotateDirection = -1f;
  343. rb.velocity = Vector3.zero;
  344. if (attackEnemyTrigger0 != null) attackEnemyTrigger0.gameObject.SetActive(false);
  345. if (attackTrigger0 != null) attackTrigger0.gameObject.SetActive(false);
  346. }
  347. private void EnterRushState()
  348. {
  349. rb.velocity = Vector3.zero;
  350. if (attackEnemyTrigger0 != null) attackEnemyTrigger0.gameObject.SetActive(false);
  351. if (attackTrigger0 != null) attackTrigger0.gameObject.SetActive(true);
  352. }
  353. private void EnterRiseState()
  354. {
  355. isGather = true;
  356. gatherTimer = 0f;
  357. }
  358. private void EnterFallState()
  359. {
  360. rotateDirection = 1f;
  361. weakTimer = 0f;
  362. }
  363. private void EnterDieState()
  364. {
  365. isDie = true;
  366. dieKeepTime = totalDieKeepTime;
  367. if (GameManager.instance.gameType != GameType.GameEnd)
  368. {
  369. DropSouls();
  370. }
  371. }
  372. private void EnterBlowUpState()
  373. {
  374. rotateDirection = 1f;
  375. curHitTime += 1;
  376. isDying = false;
  377. isFalled = true;
  378. deathTimer = 0f;
  379. if (owner != null) owner.attributeStatus.hitState = 0;
  380. }
  381. public override void OnState()
  382. {
  383. switch (state)
  384. {
  385. case CharacterState.Idle:
  386. ChangeState(CharacterState.Rise);
  387. break;
  388. case CharacterState.Rise:
  389. HandleRiseState();
  390. break;
  391. case CharacterState.Rush:
  392. HandleRushState();
  393. break;
  394. case CharacterState.Fall:
  395. HandleFallState();
  396. break;
  397. case CharacterState.SpecialStatus_BlowUp:
  398. attributeStatus.SpecialStateEffect(SpecialState.BlownUp);
  399. break;
  400. case CharacterState.Die:
  401. dieKeepTime -= Time.deltaTime;
  402. if (dieKeepTime <= 0)
  403. {
  404. if (killer != null && killer.GetComponent<Demonic>())
  405. {
  406. SoldierType st = killer.GetComponent<Demonic>().soldierType;
  407. SoldierEXP.expInstance.AddEXP(st, (int)(exp * LevelSelect.EXPRatio + 0.5f));
  408. }
  409. if (dieEffect)
  410. {
  411. PoolManager.Instantiate(dieEffect, transform.position);
  412. }
  413. GameManager gameManager = GameManager.instance;
  414. if (gameManager.gameType == GameType.GameEnd)
  415. {
  416. GameObject fx = PoolManager.Instantiate(gameManager.dropGoldFX);
  417. fx.transform.position = transform.position;
  418. GameObject injuryNum = PoolManager.Instantiate(gameManager.dropGoldText);
  419. injuryNum.transform.position = new Vector3(
  420. transform.position.x + injuryNumPos_march.x + Random.Range(-injuryNumRandom_march.x / 2f, injuryNumRandom_march.x / 2f),
  421. transform.position.y + injuryNumPos_march.y + Random.Range(-injuryNumRandom_march.y / 2f, injuryNumRandom_march.y / 2f),
  422. transform.position.z);
  423. TextMeshProUGUI text = injuryNum.GetComponentInChildren<TextMeshProUGUI>();
  424. text.text = $"+{gameManager.enemyGoldDrop}";
  425. }
  426. gameObject.SetActive(false);
  427. break;
  428. }
  429. break;
  430. break;
  431. }
  432. }
  433. private void HandleRiseState()
  434. {
  435. if (isGather && gatherTimer < gatherTime)
  436. {
  437. gatherTimer += Time.deltaTime;
  438. }
  439. else
  440. {
  441. Debug.Log("gatherFinsh");
  442. isGather = false;
  443. ChangeState(CharacterState.Rush);
  444. }
  445. }
  446. private void HandleRushState()
  447. {
  448. Vector3 velocity = rb.velocity;
  449. if (Mathf.Abs(velocity.x) < moveSpeed)
  450. {
  451. velocity.x += addForce;
  452. }
  453. rb.velocity = velocity;
  454. }
  455. private void HandleFallState()
  456. {
  457. if (!foot.TrigGround)
  458. {
  459. isWeak = true;
  460. }
  461. if (isWeak && weakTimer < weakTime && foot.TrigGround)
  462. {
  463. rb.velocity = Vector3.zero;
  464. weakTimer += Time.deltaTime;
  465. }
  466. else if (weakTimer >= weakTime)
  467. {
  468. isWeak = false;
  469. ChangeState(CharacterState.Idle);
  470. }
  471. }
  472. public void OnDisable()
  473. {
  474. EnemyCreater.instance.OnEnemyRecycle(this);
  475. ResetState();
  476. }
  477. }