-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpeakerManager.cs
125 lines (122 loc) · 4.12 KB
/
SpeakerManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using Sandbox.Game.EntityComponents;
using Sandbox.ModAPI.Ingame;
using Sandbox.ModAPI.Interfaces;
using SpaceEngineers.Game.ModAPI.Ingame;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System;
using VRage.Collections;
using VRage.Game.Components;
using VRage.Game.GUI.TextPanel;
using VRage.Game.ModAPI.Ingame.Utilities;
using VRage.Game.ModAPI.Ingame;
using VRage.Game.ObjectBuilders.Definitions;
using VRage.Game;
using VRage;
using VRageMath;
using System.IO;
namespace IngameScript
{
partial class Program
{
public class SpeakerManager
{
Dictionary<WarningSource, DateTime> endTimes;
Dictionary<WarningSource, bool> warningRunning;
int loopTime;
string soundName;
IMyBlockGroup speakerGroup;
List<IMySoundBlock> speakerList;
DateTime? endLoop;
public SpeakerManager()
{
speakerGroup = GridTerminalSystem.GetBlockGroupWithName(hangarSpeakerGroupName);
if (speakerGroup == null)
{
Echo("Cannot find speaker group: " + hangarSpeakerGroupName);
throw new ArgumentException("Cannot find speaker group: " + hangarSpeakerGroupName);
}
speakerList = new List<IMySoundBlock>();
loopTime = soundLength;
soundName = alarmName;
}
public void StartWarning(WarningSource source, int playTime = 0)
{
DateTime endTime;
if (playTime > 0)
{
endTime = DateTime.Now.AddSeconds(playTime);
}
else
{
endTime = DateTime.MaxValue;
}
endTimes[source] = endTime;
warningRunning[source] = true;
CheckState();
}
public void StopWarning(WarningSource source)
{
endTimes.Remove(source);
warningRunning.Remove(source);
CheckState();
}
public void CheckState()
{
foreach(KeyValuePair<WarningSource, DateTime> keyValuePair in endTimes)
{
if(keyValuePair.Value <= DateTime.Now)
{
endTimes.Remove(keyValuePair.Key);
warningRunning.Remove(keyValuePair.Key);
}
}
bool playSound;
if (warningRunning.Count > 0)
{
playSound = true;
}
else
{
playSound = false;
}
speakerList.ForEach((speaker) => setSound(speaker, playSound));
}
public void setSound(IMySoundBlock speaker, bool value)
{
speakerGroup.GetBlocksOfType<IMySoundBlock>(speakerList);
speaker.SelectedSound = soundName;
speaker.LoopPeriod = loopTime;
if (value)
{
if (endLoop == null)
{
ITerminalAction action;
action = speaker.GetActionWithName("OnOff_On");
action.Apply(speaker);
endLoop = DateTime.Now.AddSeconds(loopTime);
speaker.Play();
}
else
{
if (endLoop <= DateTime.Now )
{
ITerminalAction action;
action = speaker.GetActionWithName("OnOff_On");
action.Apply(speaker);
endLoop = DateTime.Now;
speaker.Play();
}
}
}
else
{
endLoop = null;
speaker.Stop();
}
}
}
}
}