StoryM.cs 4.0 KB

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