SoundTrackManager.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using Newtonsoft.Json;
  2. using NUnit.Framework;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class SoundTrackManager
  6. {
  7. public string name { set; get; }
  8. public string soundTrack { set; get; }
  9. public string description { set; get; }
  10. public int speed { set; get; } = 100;
  11. public int length { set; get; } = 0;
  12. public List<SoundAction> soundAction { set; get; } = new List<SoundAction>();
  13. public void ImportFromJson(string json)
  14. {
  15. var data = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
  16. speed = int.Parse(data["speed"].ToString());
  17. name = data["name"].ToString();
  18. soundTrack = data["sound_track"].ToString();
  19. description = data["description"].ToString();
  20. length = int.Parse(data["length"].ToString());
  21. soundAction.Clear();
  22. SoundAction[] actions = JsonConvert.DeserializeObject<SoundAction[]>(data["action points"].ToString());
  23. foreach (var action in actions)
  24. {
  25. soundAction.Add(action);
  26. }
  27. }
  28. // 根据音乐时常随机生成
  29. public void GenRandomActions(float actionGap=0.5f)
  30. {
  31. // 读取音乐,并获取长度
  32. AudioClip audioClip = Resources.Load<AudioClip>(soundTrack);
  33. float length = audioClip.length;
  34. soundAction.Clear();
  35. float timeGap = actionGap; // 计算出两个相邻音符时间间距,对应程序难度
  36. float time = 4f;
  37. int combo = 0; // 连续输入音符
  38. while (time < length - 2)
  39. {
  40. int randomInt = Random.Range(0, 7);
  41. if (randomInt < 5)
  42. {
  43. combo++;
  44. time += timeGap;
  45. if (randomInt == 0)
  46. {
  47. SoundAction action = new(time, "tap");
  48. soundAction.Add(action);
  49. }
  50. if (randomInt == 1)
  51. {
  52. SoundAction action = new(time, "left");
  53. soundAction.Add(action);
  54. }
  55. if (randomInt == 2)
  56. {
  57. SoundAction action = new(time, "right");
  58. soundAction.Add(action);
  59. }
  60. if (randomInt == 3)
  61. {
  62. SoundAction action = new(time, "up");
  63. soundAction.Add(action);
  64. }
  65. if (randomInt == 4)
  66. {
  67. SoundAction action = new(time, "down");
  68. soundAction.Add(action);
  69. }
  70. }
  71. else if (randomInt == 5)
  72. {
  73. time += timeGap;
  74. if (combo > 0)
  75. {
  76. combo--;
  77. }
  78. }
  79. else if (randomInt == 6)
  80. {
  81. time += timeGap * combo / 6;
  82. }
  83. }
  84. Debug.Log("random sound action create successful. Total action #:" + soundAction.Count);
  85. }
  86. }
  87. public class SoundAction
  88. {
  89. public float time { set; get; } = 0f;
  90. public string action { set; get; } = "";
  91. public bool isTapped = false; // 表示当前这个音乐动作是否被播放过
  92. public bool isActive = false; // 表示当前这个音乐动作是否正在场景中激活
  93. public SoundAction(float time, string action)
  94. {
  95. this.time = time;
  96. this.action = action;
  97. }
  98. }