Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
SoundGeneratorElectricElement.cs
浏览该文件的文档.
1using Engine;
2
3namespace Game {
6
8
10
11 public Random m_random = new();
12
13 public int m_lastToneInput;
14
15 public double m_playAllowedTime;
16
17 public string[] m_tones = [
18 "",
19 "Bell",
20 "Organ",
21 "Ping",
22 "String",
23 "Trumpet",
24 "Voice",
25 "Piano",
26 "PianoLong",
27 "Drums",
28 "Bass",
29 "",
30 "",
31 "",
32 "",
33 "Piano"
34 ];
35
36 public int[] m_maxOctaves = [
37 0,
38 6,
39 5,
40 6,
41 6,
42 6,
43 6,
44 6,
45 6,
46 0,
47 6,
48 0,
49 0,
50 0,
51 0,
52 6
53 ];
54
55 public string[] m_drums = [
56 "Snare",
57 "BassDrum",
58 "ClosedHiHat",
59 "PedalHiHat",
60 "OpenHiHat",
61 "LowTom",
62 "HighTom",
63 "CrashCymbal",
64 "RideCymbal",
65 "HandClap"
66 ];
67
68 public SoundGeneratorElectricElement(SubsystemElectricity subsystemElectricity, CellFace cellFace) : base(subsystemElectricity, cellFace) {
69 m_subsystemNoise = subsystemElectricity.Project.FindSubsystem<SubsystemNoise>(true);
71 Vector3 vector = CellFace.FaceToVector3(cellFace.Face);
72 Vector3 position = new Vector3(cellFace.Point) + new Vector3(0.5f) - 0.2f * vector;
73 m_particleSystem = new SoundParticleSystem(subsystemElectricity.SubsystemTerrain, position, vector);
74 }
75
76 public override bool Simulate() {
77 int num = 0;
78 int num2 = 15;
79 int num3 = 2;
80 int num4 = 0;
81 int rotation = Rotation;
82 foreach (ElectricConnection connection in Connections) {
83 if (connection.ConnectorType != ElectricConnectorType.Output
84 && connection.NeighborConnectorType != 0) {
86 CellFaces[0].Face,
87 rotation,
88 connection.ConnectorFace
89 );
90 if (connectorDirection.HasValue) {
91 if (connectorDirection.Value == ElectricConnectorDirection.In
92 || connectorDirection.Value == ElectricConnectorDirection.Bottom) {
93 num4 = (int)MathF.Round(15f * connection.NeighborElectricElement.GetOutputVoltage(connection.NeighborConnectorFace));
94 }
95 else if (connectorDirection.Value == ElectricConnectorDirection.Left) {
96 num = (int)MathF.Round(15f * connection.NeighborElectricElement.GetOutputVoltage(connection.NeighborConnectorFace));
97 }
98 else if (connectorDirection.Value == ElectricConnectorDirection.Right) {
99 num3 = (int)MathF.Round(15f * connection.NeighborElectricElement.GetOutputVoltage(connection.NeighborConnectorFace));
100 }
101 else if (connectorDirection.Value == ElectricConnectorDirection.Top) {
102 num2 = (int)MathF.Round(15f * connection.NeighborElectricElement.GetOutputVoltage(connection.NeighborConnectorFace));
103 }
104 }
105 }
106 }
107 if (m_lastToneInput == 0
108 && num4 != 0
109 && num != 15
111 m_playAllowedTime = SubsystemElectricity.SubsystemTime.GameTime + 0.079999998211860657;
112 string text = m_tones[num4];
113 float num5 = 0f;
114 string text2 = null;
115 if (text == "Drums") {
116 num5 = 1f;
117 if (num >= 0
118 && num < m_drums.Length) {
119 text2 = $"Audio/SoundGenerator/Drums{m_drums[num]}";
120 }
121 }
122 else if (!string.IsNullOrEmpty(text)) {
123 float num6 = 130.8125f * MathF.Pow(1.05946314f, num + 12 * num3);
124 int num7 = 0;
125 for (int i = 4; i <= m_maxOctaves[num4]; i++) {
126 float num8 = num6 / (523.25f * MathF.Pow(2f, i - 5));
127 if (num7 == 0
128 || (num8 >= 0.5f && num8 < num5)) {
129 num7 = i;
130 num5 = num8;
131 }
132 }
133 text2 = $"Audio/SoundGenerator/{text}C{num7}";
134 }
135 if (num5 != 0f
136 && !string.IsNullOrEmpty(text2)) {
137 CellFace cellFace = CellFaces[0];
138 Vector3 position = new(cellFace.X, cellFace.Y, cellFace.Z);
139 float volume = num2 / 15f;
140 float pitch = Math.Clamp(MathF.Log(num5) / MathF.Log(2f), -1f, 1f);
141 float minDistance = 0.5f + 5f * num2 / 15f;
142 SubsystemElectricity.SubsystemAudio.PlaySound(text2, volume, pitch, position, minDistance, true);
143 float loudness = num2 < 8 ? 0.25f : 0.5f;
144 float range = MathUtils.Lerp(2f, 20f, num2 / 15f);
145 m_subsystemNoise.MakeNoise(position, loudness, range);
146 if (m_particleSystem.SubsystemParticles == null) {
147 m_subsystemParticles.AddParticleSystem(m_particleSystem);
148 }
149 Vector3 hsv = new(22.5f * num + m_random.Float(0f, 22f), 0.5f + num2 / 30f, 1f);
150 m_particleSystem.AddNote(new Color(Color.HsvToRgb(hsv)));
151 }
152 }
153 m_lastToneInput = num4;
154 return false;
155 }
156 }
157}
Engine.Color Color
static float Lerp(float x1, float x2, float f)
ElectricConnectorType ConnectorType
ElectricConnectorType NeighborConnectorType
virtual float GetOutputVoltage(int face)
List< ElectricConnection > Connections
ReadOnlyList< CellFace > CellFaces
RotateableElectricElement(SubsystemElectricity subsystemElectricity, CellFace cellFace)
SoundGeneratorElectricElement(SubsystemElectricity subsystemElectricity, CellFace cellFace)
void PlaySound(string name, float volume, float pitch, float pan, float delay)
static ? ElectricConnectorDirection GetConnectorDirection(int mountingFace, int rotation, int connectorFace)
virtual Subsystem FindSubsystem(Type type, string name, bool throwOnError)
static Vector3 HsvToRgb(Vector3 hsv)
static Vector3 Clamp(Vector3 v, float min, float max)
static Vector3 FaceToVector3(int face)