StoryM.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 / 3f : textEn.Split(" ").Length / 3f);
  25. StopCoroutine("WaitForHideStoryText"); StartCoroutine("WaitForHideStoryText", time);
  26. Debug.Log(time + " -> " + textCn);
  27. return time;
  28. }
  29. IEnumerator WaitForStoryText(string textCh, string textEn = "English") {
  30. yield return new WaitForSeconds(SetStoryText(textCh, textEn));
  31. }
  32. IEnumerator WaitForHideStoryText(float delay) {
  33. yield return new WaitForSeconds(delay);
  34. textStory.text = "";
  35. Debug.Log(delay);
  36. DOTween.Kill("StoryBackground"); shapeBack.DOScaleX(0, 0.2f).SetId("StoryBackground");
  37. }
  38. // 刷怪
  39. private Dictionary<Spawner.EnemyType, List<Spawner>> spawners = null;
  40. public void Spawn(Spawner.EnemyType type, int count = 1) {
  41. // 初始化
  42. if (spawners == null) {
  43. spawners = new();
  44. foreach (Spawner spawner in FindObjectsOfType<Spawner>()) {
  45. if (spawners.ContainsKey(spawner.type)) {
  46. spawners[spawner.type].Add(spawner);
  47. } else {
  48. spawners[spawner.type] = new() { spawner };
  49. }
  50. }
  51. }
  52. // 实际刷怪
  53. spawners[type].Shuffle();
  54. for (int i = 0; i < count; i++) {
  55. if (i >= spawners[type].Count) i = 0;
  56. spawners[type][i].Spawn();
  57. }
  58. }
  59. // 流程控制
  60. IEnumerator WaitUntilClear(int remain = 0) {
  61. while (FindObjectsOfType<EnemyBase>().Length - FindObjectsOfType<EnemyArrow>().Length > remain) { // 要算上自己
  62. yield return new WaitForSeconds(0.1f);
  63. }
  64. }
  65. IEnumerator WaitUntilPickDice() {
  66. while (FindObjectsOfType<DiceEntity>().Length > 0) {
  67. yield return new WaitForSeconds(0.1f);
  68. }
  69. }
  70. // 故事
  71. public int stage = 0;
  72. void Start() {
  73. StartCoroutine(test());
  74. StartCoroutine(spawnArrow());
  75. }
  76. bool canSpawnArrow = true;
  77. IEnumerator test() {
  78. while (true) {
  79. canSpawnArrow = true;
  80. Spawn(Spawner.EnemyType.Mover, 3);
  81. SetStoryText("先刷几个瞎晃悠的", "");
  82. yield return new WaitForSeconds(3f);
  83. Spawn(Spawner.EnemyType.Shooter);
  84. SetStoryText("炮塔!", "");
  85. yield return new WaitForSeconds(2f);
  86. Spawn(Spawner.EnemyType.Heavy, 2);
  87. SetStoryText("最后是两个贼硬的……", "");
  88. yield return StartCoroutine(WaitUntilClear(2));
  89. canSpawnArrow = false;
  90. InputM.DropDice(Modules.RandomOne(InputM.keyTypes.ToList()));
  91. SetStoryText("来一个骰子吧……?", "");
  92. yield return StartCoroutine(WaitUntilPickDice());
  93. yield return StartCoroutine(WaitForStoryText("准备好,下一轮要来了!", ""));
  94. }
  95. }
  96. IEnumerator spawnArrow() {
  97. while (true) {
  98. yield return new WaitForSeconds(5f);
  99. if (canSpawnArrow) Spawn(Spawner.EnemyType.Arrow, 2);
  100. }
  101. }
  102. }