StoryM.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 = 1f + (isCn ? textCn.Length / 4f : textEn.Split(" ").Length / 4f);
  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. Debug.Log("隐藏:" + delay);
  35. HideStoryText();
  36. }
  37. void HideStoryText() {
  38. StopCoroutine("WaitForHideStoryText");
  39. textStory.text = "";
  40. DOTween.Kill("StoryBackground"); shapeBack.DOScaleX(0, 0.2f).SetId("StoryBackground");
  41. }
  42. // 刷怪
  43. private Dictionary<Spawner.EnemyType, List<Spawner>> spawners = null;
  44. public void Spawn(Spawner.EnemyType type, int count = 1) {
  45. // 初始化
  46. if (spawners == null) {
  47. spawners = new();
  48. foreach (Spawner spawner in FindObjectsOfType<Spawner>()) {
  49. if (spawners.ContainsKey(spawner.type)) {
  50. spawners[spawner.type].Add(spawner);
  51. } else {
  52. spawners[spawner.type] = new() { spawner };
  53. }
  54. }
  55. }
  56. // 实际刷怪
  57. spawners[type].Shuffle();
  58. int id = 0;
  59. for (int i = 0; i < count; i++) {
  60. if (id >= spawners[type].Count) id = 0;
  61. spawners[type][id].Spawn();
  62. id++;
  63. }
  64. }
  65. // 流程控制
  66. IEnumerator WaitUntilClear(int remain, float waitTime = -1, string textCn = "", string textEn = "") {
  67. int targetStage = stage;
  68. while (GetEnemyCount() > remain && !respawning && targetStage == stage) {
  69. if (waitTime >= 0f && waitTime < 0.3f) SetStoryText(textCn, textEn);
  70. waitTime -= 0.3f;
  71. yield return new WaitForSeconds(0.3f);
  72. }
  73. }
  74. int GetEnemyCount() => FindObjectsOfType<EnemyBase>().Length - FindObjectsOfType<EnemyArrow>().Length;
  75. IEnumerator WaitUntilPickDice(float waitTime = -1, string textCn = "", string textEn = "") {
  76. int targetStage = stage;
  77. while (GetDiceCount() > 0 && !respawning && targetStage == stage) {
  78. if (waitTime >= 0f && waitTime < 0.3f) SetStoryText(textCn, textEn);
  79. waitTime -= 0.3f;
  80. yield return new WaitForSeconds(0.3f);
  81. }
  82. }
  83. int GetDiceCount() => FindObjectsOfType<DiceEntity>().Count(dice => !dice.pickedUp);
  84. // 故事
  85. public int stage = 0; private bool stageCompleted = true;
  86. public Image panDeath;
  87. void Start() {
  88. StartCoroutine("ArrowSpawner");
  89. StartCoroutine("Main");
  90. }
  91. IEnumerator Main() {
  92. while (true) {
  93. if (Input.GetKey(KeyCode.LeftControl) && Input.GetKeyUp(KeyCode.Q)) {
  94. SetStoryText("已跳过阶段:" + stage, "Skip Stage: " + stage);
  95. stageCompleted = true;
  96. }
  97. if (Player.hp <= 0) {
  98. stageCompleted = false;
  99. yield return StartCoroutine("Respawn");
  100. }
  101. if (stageCompleted) {
  102. deathInStage = 0;
  103. stageCompleted = false;
  104. StopCoroutine("Stage" + stage);
  105. stage++;
  106. Debug.Log("已到达阶段:" + stage);
  107. StartCoroutine("Stage" + stage);
  108. }
  109. yield return new WaitForFixedUpdate();
  110. }
  111. }
  112. private int deathInStage = 0; private bool respawning = false, noDifficulty = false;
  113. IEnumerator Respawn() {
  114. deathInStage++; respawning = true;
  115. // 停止场景
  116. arrowMax = 0;
  117. Player.instance.transform.localScale = Vector3.zero;
  118. Player.instance.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
  119. Player.instance.enabled = false;
  120. StopCoroutine("Stage" + stage);
  121. StopCoroutine("HideStoryText");
  122. yield return new WaitForSeconds(1.5f);
  123. if (!noDifficulty) {
  124. _ = Modules.randomDefault.Next(0, 4) switch {
  125. 0 => SetStoryText("完了,寄咯!", "Woops, you died!"),
  126. 1 => SetStoryText("哦吼,下辈子加油吧", "Ouch, work harder next life!"),
  127. 2 => SetStoryText("诶,怎么回事?", "Yo, what's happening?"),
  128. 3 => SetStoryText("还好我没开极限模式……", "Luckily I didn't have hardcore mode on..."),
  129. _ => throw new System.NotImplementedException(),
  130. };
  131. } else {
  132. _ = Modules.randomDefault.Next(0, 3) switch {
  133. 0 => SetStoryText("不是吧?不至于吧?", "What? How you die from that?"), //TODO: t 改了
  134. 1 => SetStoryText("你是特么故意的吧!", "Excuse me? Are you doing this on purpose?"), //TODO: t 肯定语气
  135. 2 => SetStoryText("嗯?", "Hey!"),
  136. _ => throw new System.NotImplementedException(),
  137. };
  138. }
  139. yield return new WaitForSeconds(0.5f);
  140. // 转为黑屏
  141. panDeath.DOFade(1, 0.3f); panDeath.GetComponentInChildren<TMPro.TextMeshProUGUI>().DOFade(1, 1f);
  142. yield return new WaitForSeconds(0.3f);
  143. // 复原
  144. FindObjectsOfType<EnemyBase>().ToList().ForEach(e => Modules.DestroyGameObject(e.gameObject));
  145. Player.hp = 3;
  146. Player.instance.currentBullet = Player.instance.maxBullet;
  147. yield return new WaitForSeconds(3f);
  148. // 淡出黑屏,玩家重生
  149. respawning = false;
  150. panDeath.DOFade(0, 0.8f); panDeath.GetComponentInChildren<TMPro.TextMeshProUGUI>().DOFade(0, 0.3f);
  151. Player.instance.enabled = true;
  152. Player.instance.transform.localScale = Vector3.one;
  153. Player.instance.transform.position = new Vector3(-6, 10, 0);
  154. // 继续游戏
  155. yield return new WaitForSeconds(2f);
  156. StartCoroutine("Stage" + stage);
  157. }
  158. IEnumerator Stage1() {
  159. arrowMax = 0; noDifficulty = false;
  160. yield return new WaitForSeconds(3f);
  161. yield return StartCoroutine(WaitForStoryText("哦,哦!你好,欢迎欢迎", "Oh, oh! Hello there!"));
  162. yield return StartCoroutine(WaitForStoryText("先来过个教程啥的吧", "Let's go through a quick tutrial before we start."));
  163. yield return StartCoroutine(WaitForStoryText("游戏的按键操作写在了右上角,应该是个人都看得懂", "All control buttons are shown on the top-right corner. I bet nobody can't understand that."));
  164. SetStoryText("你不如先拿它们俩试试按键吧!", "Now, let's try some buttons with these two cute guys!");
  165. Spawn(Spawner.EnemyType.Mover, 2);
  166. yield return StartCoroutine(WaitUntilClear(0, 10f, "快点把它干掉,然后才好继续……", "Come on! You have to defeat them to continue..."));
  167. stageCompleted = true;
  168. }
  169. IEnumerator Stage2() {
  170. arrowMax = 1; noDifficulty = false;
  171. Spawn(Spawner.EnemyType.Mover, 2);
  172. yield return StartCoroutine(WaitForStoryText("哦,还剩下最后一条教程……", "Oh, a final reminder before I leave...")); //TODO t
  173. Spawn(Spawner.EnemyType.Mover, deathInStage <= 0 ? 2 : 1);
  174. yield return StartCoroutine(WaitForStoryText("“弹药不够的时候记得右键换弹!”", "Don't forget to reload the weapon before you run out of ammo!")); //TODO t
  175. Spawn(Spawner.EnemyType.Shooter, 1);
  176. Spawn(Spawner.EnemyType.Mover, 1);
  177. SetStoryText("加油,新手教程结束了!", "There you go, that's end of the tutrial!");
  178. yield return StartCoroutine(WaitUntilClear(1, 8f, "仔细一看,这个角色的手上连枪都没有……", "Hmm... He does not even have a gun in his hand after a closer look."));
  179. if (Player.hp < 3) {
  180. Player.hp = 3;
  181. Spawn(Spawner.EnemyType.Mover, 1);
  182. yield return StartCoroutine(WaitForStoryText("对了,教程结束得先给你回个血……", "Whoops, I almost forgot to heal you after the tutrial."));
  183. }
  184. stageCompleted = true;
  185. }
  186. IEnumerator Stage3() {
  187. arrowMax = 3; noDifficulty = false;
  188. Spawn(Spawner.EnemyType.Mover, 1);
  189. yield return StartCoroutine(WaitForStoryText("说实话啊,我感觉这个游戏有点……敷衍", "To be honest, I think this game is kind of boring...")); //TODO: t 文案改了
  190. Spawn(Spawner.EnemyType.Mover, 1);
  191. yield return StartCoroutine(WaitForStoryText("我看了看,这游戏往后的新内容就只剩一种换皮怪了!", "Let's see... We only have one more skin to replace our monsters!"));
  192. Spawn(Spawner.EnemyType.Mover, 1);
  193. yield return StartCoroutine(WaitForStoryText("然后就只剩无尽模式了……但无尽模式谁玩啊?", "And then, all we have is endless mode of \"Survive with infinite monsters!\"...")); //TODO: t 文案改了
  194. Spawn(Spawner.EnemyType.Shooter, 1);
  195. yield return StartCoroutine(WaitForStoryText("没有升级,没有装备,没有峰回路转的牛逼剧情", "No upgrade, no equipments, no twist to the plot or any boss to fight."));
  196. Spawn(Spawner.EnemyType.Mover, 1);
  197. yield return StartCoroutine(WaitForStoryText("并且最重要的是,说好的“丢骰子”呢?", "And most importantly, where's our \"Roll of the Dice\" theme?")); //TODO t
  198. yield return StartCoroutine(WaitForStoryText("骰子、骰子……让我找找哪儿有骰子……?", "Dice... Dice... Where is the dice...?")); //TODO: t
  199. SetStoryText("这个游戏里一个骰子都没有!", "Not a single dice here!"); //TODO: t
  200. arrowMax = 0;
  201. Spawn(Spawner.EnemyType.Mover, deathInStage <= 0 ? 2 : 1);
  202. yield return StartCoroutine(WaitUntilClear(0, 6f, "切题呢?作者到底咋想的?", "What's the point? What's wrong with this author?"));
  203. noDifficulty = true;
  204. yield return StartCoroutine(WaitForStoryText("要不然这样,我们随便拿个东西,把它弄成骰子算了", "How about we just grab something and make it a dice?"));
  205. yield return new WaitForSeconds(2f);
  206. InputM.DropDice(InputM.KeyType.Fire);
  207. SetStoryText("嘿,成了!", "Let's go!");
  208. stageCompleted = true;
  209. }
  210. IEnumerator Stage4() {
  211. arrowMax = 0; noDifficulty = true;
  212. yield return StartCoroutine(WaitUntilPickDice(8f, "看到右边掉下来的东西了么?把它捡起来摇一摇!", "You see the falling stuff on the right? Pick it up and try shaking it!"));
  213. stageCompleted = true;
  214. }
  215. IEnumerator Stage5() {
  216. arrowMax = 0; noDifficulty = true;
  217. yield return new WaitForSeconds(1f);
  218. yield return StartCoroutine(WaitForStoryText("噔噔噔……!", "Pow!")); //TODO: t 这是在说骰子的音效
  219. yield return new WaitForSeconds(7f);
  220. yield return StartCoroutine(WaitForStoryText("你看,这就是天才制作的骰子", "See, what a master piece!")); //TODO: t 是骰子!
  221. yield return new WaitForSeconds(1.5f);
  222. Spawn(Spawner.EnemyType.Mover, 1);
  223. if (Player.hp < 3) {
  224. Player.hp = 3;
  225. yield return StartCoroutine(WaitForStoryText("让我帮你回个血,我们继续!", "Let me heal you first and then keep going!"));
  226. } else {
  227. yield return StartCoroutine(WaitForStoryText("来,我们继续!", "Let's keep going!"));
  228. }
  229. stageCompleted = true;
  230. }
  231. IEnumerator Stage6() {
  232. arrowMax = 1; noDifficulty = false;
  233. Spawn(Spawner.EnemyType.Mover, 3);
  234. yield return StartCoroutine(WaitUntilClear(1, 6f, "骰子有六面,按键有六个,简直完美!", "Six sides to six buttons, perfect!"));
  235. Spawn(Spawner.EnemyType.Shooter, 1);
  236. yield return StartCoroutine(WaitForStoryText("感觉如何?", "How do you feel?"));
  237. arrowMax = deathInStage <= 1 ? 3 : 1;
  238. yield return StartCoroutine(WaitForStoryText("是不是还想马上再来一颗?", "Want to have another dice now?"));
  239. Spawn(Spawner.EnemyType.Mover, 2);
  240. yield return StartCoroutine(WaitForStoryText("但如果给你太多骰子,那岂不是显得它很……普通", "But if I give you too many dices, it will look... ordinary."));
  241. arrowMax = 1;
  242. Spawn(Spawner.EnemyType.Shooter, 1);
  243. yield return StartCoroutine(WaitForStoryText("不如让我们先等等,我先给你看看之前说的新敌人", "Why don't we wait while I show you the new enemy we were talking about...?"));
  244. SetStoryText("额……就是换皮的那个", "Ugh... That monster with new skin!");
  245. Spawn(Spawner.EnemyType.Mover, 1);
  246. yield return StartCoroutine(WaitUntilClear(1, 6f, "别抱太大期望就是了……", "Just don't hope too much on that..."));
  247. stageCompleted = true;
  248. }
  249. IEnumerator Stage7() {
  250. arrowMax = 0; noDifficulty = false;
  251. yield return StartCoroutine(WaitForStoryText("让我找找它在哪……", "Hold on... I can't find where it is."));
  252. yield return new WaitForSeconds(1.5f);
  253. SetStoryText("卧槽!小心!", ""); // TODO: t 这里说的是要掉下来一堆圆锯片了
  254. for (int i = 0; i < Mathf.Max(6, 15 - deathInStage * 2); i++) {
  255. Spawn(Spawner.EnemyType.Mover, 1);
  256. yield return new WaitForSeconds(0.4f);
  257. }
  258. yield return StartCoroutine(WaitForStoryText("我把圆锯片的仓库弄漏了……", "I mix up the wrong repository for circular saw blades..."));
  259. yield return StartCoroutine(WaitForStoryText("明明打算之后用来做点装饰的,唉", "Awww... I was gonna use it for decoration later."));
  260. yield return StartCoroutine(WaitUntilClear(6, 6f, "出了这么大问题,玩家给游戏打差评怎么办?", ""));
  261. if (Player.hp < 3) {
  262. Player.hp = 3;
  263. yield return StartCoroutine(WaitForStoryText("我帮你回个血吧……唉,惹麻烦了……", ""));
  264. } else {
  265. yield return StartCoroutine(WaitForStoryText("唉,惹麻烦了……", ""));
  266. }
  267. yield return StartCoroutine(WaitUntilClear(2, 7f, "我的锯片……", ""));
  268. yield return StartCoroutine(WaitForStoryText("要不然……我再给你一颗特制按键骰子™作为补偿?", ""));
  269. yield return new WaitForSeconds(1f);
  270. InputM.DropDice(new List<InputM.KeyType>() { InputM.KeyType.Left, InputM.KeyType.Right, InputM.KeyType.Crouch }.RandomOne());
  271. SetStoryText("你的补偿来了!", "");
  272. stageCompleted = true;
  273. }
  274. IEnumerator Stage8() {
  275. arrowMax = 0; noDifficulty = true;
  276. yield return StartCoroutine(WaitUntilPickDice(6f, "难道说你不喜欢特制按键骰子™吗?别吧", ""));
  277. stageCompleted = true;
  278. }
  279. IEnumerator Stage9() {
  280. arrowMax = 0; noDifficulty = true;
  281. yield return new WaitForSeconds(7f);
  282. stageCompleted = true;
  283. }
  284. IEnumerator Stage10() {
  285. arrowMax = deathInStage <= 2 ? 0 : 2; noDifficulty = false;
  286. yield return StartCoroutine(WaitForStoryText("唔,看起来特制按键骰子™的威力还挺大……", ""));
  287. Spawn(Spawner.EnemyType.Mover, deathInStage <= 0 ? 4 : 3);
  288. yield return new WaitForSeconds(1.5f);
  289. SetStoryText("呼,圆锯片看起来也还剩了不少", "");
  290. yield return StartCoroutine(WaitUntilClear(1, 10f, "哇哦……", ""));
  291. Spawn(Spawner.EnemyType.Shooter, deathInStage <= 1 ? 3 : 2);
  292. yield return StartCoroutine(WaitForStoryText("看起来我低估了特制按键骰子™的……", ""));
  293. SetStoryText("刺激程度", "");
  294. yield return StartCoroutine(WaitUntilClear(2, 10f, "这不比原来好玩多了嘛!", ""));
  295. stageCompleted = true;
  296. }
  297. IEnumerator Stage11() {
  298. arrowMax = 2; noDifficulty = false;
  299. yield return StartCoroutine(WaitForStoryText("既然骰子™能为游戏的玩法带来颠覆性的体验", ""));
  300. SetStoryText("那我们为啥不再立即多来几颗呢?", "");
  301. yield return new WaitForSeconds(2f);
  302. Spawn(Spawner.EnemyType.Heavy, 1);
  303. yield return new WaitForSeconds(1f);
  304. yield return StartCoroutine(WaitForStoryText("什么?", ""));
  305. Spawn(Spawner.EnemyType.Heavy, 1);
  306. yield return StartCoroutine(WaitForStoryText("那是我之前说的最后一种换皮怪!", ""));
  307. yield return StartCoroutine(WaitForStoryText("它怎么在这个时候漏下来了?", ""));
  308. Spawn(Spawner.EnemyType.Heavy, 2);
  309. }
  310. int arrowMax = 0; float arrowTime = 4f;
  311. IEnumerator ArrowSpawner() {
  312. while (true) {
  313. yield return new WaitForSeconds(arrowTime);
  314. if (arrowMax > 0) Spawn(Spawner.EnemyType.Arrow, Modules.randomDefault.Next(0, arrowMax));
  315. }
  316. }
  317. }