StoryM.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using DG.Tweening;
  4. using UnityEngine;
  5. using UnityEngine.Localization.Settings;
  6. using UnityEngine.UI;
  7. using System.Linq;
  8. public class StoryM : MonoBehaviour {
  9. // 文本
  10. public TMPro.TextMeshProUGUI textStory;
  11. public RectTransform shapeBack;
  12. /// <summary>
  13. /// 设置旁白,并返回预期时间。旁白字幕将在预期时间后消失。
  14. /// </summary>
  15. public float SetStoryText(string textCn, string textEn = "English") {
  16. bool isCn = LocalizationSettings.SelectedLocale.LocaleName == "Chinese (Simplified) (zh-CN)";
  17. textStory.text = isCn ? textCn : textEn;
  18. LayoutRebuilder.ForceRebuildLayoutImmediate(textStory.rectTransform);
  19. // 动画
  20. textStory.color = Color.black;
  21. DOTween.Kill("StoryFont"); textStory.DOColor(Color.white, 0.25f).SetId("StoryFont");
  22. DOTween.Kill("StoryBackground"); shapeBack.DOScaleX((textStory.rectTransform.rect.width + 40f) / 100f, 0.15f).SetId("StoryBackground");
  23. // 消失
  24. float time = 1.5f + (isCn ? textCn.Length / 1.5f : textEn.Split(" ").Length / 2f);
  25. StopCoroutine(nameof(WaitForHideStoryText)); StartCoroutine(WaitForHideStoryText(time));
  26. return time;
  27. }
  28. IEnumerator WaitForStoryText(string textCh, string textEn = "English") {
  29. yield return new WaitForSeconds(SetStoryText(textCh, textEn));
  30. }
  31. IEnumerator WaitForHideStoryText(float delay) {
  32. yield return new WaitForSeconds(delay);
  33. textStory.text = "";
  34. DOTween.Kill("StoryBackground"); shapeBack.DOScaleX(0, 0.2f).SetId("StoryBackground");
  35. }
  36. // 刷怪
  37. private Dictionary<Spawner.EnemyType, List<Spawner>> spawners = null;
  38. public void Spawn(Spawner.EnemyType type, int count = 1) {
  39. // 初始化
  40. if (spawners == null) {
  41. spawners = new();
  42. foreach (Spawner spawner in FindObjectsOfType<Spawner>()) {
  43. if (spawners.ContainsKey(spawner.type)) {
  44. spawners[spawner.type].Add(spawner);
  45. } else {
  46. spawners[spawner.type] = new() { spawner };
  47. }
  48. }
  49. }
  50. // 实际刷怪
  51. spawners[type].Shuffle();
  52. for (int i = 0; i < Mathf.Min(6, count); i++) {
  53. spawners[type][i].Spawn();
  54. }
  55. }
  56. // 流程控制
  57. IEnumerator WaitUntilClear(int remain = 0) {
  58. while (FindObjectsOfType<EnemyBase>().Length - FindObjectsOfType<EnemyArrow>().Length > remain) { // 要算上自己
  59. yield return new WaitForSeconds(0.1f);
  60. }
  61. }
  62. IEnumerator WaitUntilPickDice() {
  63. while (FindObjectsOfType<DiceEntity>().Length > 0) {
  64. yield return new WaitForSeconds(0.1f);
  65. }
  66. }
  67. // 故事
  68. public int stage = 0;
  69. void Start() {
  70. StartCoroutine(test());
  71. StartCoroutine(spawnArrow());
  72. }
  73. bool canSpawnArrow = true;
  74. IEnumerator test() {
  75. while (true) {
  76. canSpawnArrow = true;
  77. Spawn(Spawner.EnemyType.Mover, 3);
  78. SetStoryText("先刷几个瞎晃悠的", "");
  79. yield return new WaitForSeconds(4f);
  80. Spawn(Spawner.EnemyType.Heavy, 2);
  81. SetStoryText("然后是两个贼硬的……", "");
  82. yield return StartCoroutine(WaitUntilClear(2));
  83. canSpawnArrow = false;
  84. InputM.DropDice(Modules.RandomOne(InputM.keyTypes.ToList()));
  85. SetStoryText("来一个骰子吧……?", "");
  86. yield return StartCoroutine(WaitUntilPickDice());
  87. yield return StartCoroutine(WaitForStoryText("准备好,下一轮要来了!", ""));
  88. }
  89. }
  90. IEnumerator spawnArrow() {
  91. while (true) {
  92. yield return new WaitForSeconds(5f);
  93. if (canSpawnArrow) Spawn(Spawner.EnemyType.Arrow, 2);
  94. }
  95. }
  96. }