4 using System.Collections.Generic;
14 [
Editable,
Serialize(
"",
IsPropertySaveable.Yes, description:
"Identifiers or tags of items that, when contained, allow the terminal buttons to be used. Multiple ones should be separated by commas.", alwaysUseInstanceValues:
true)]
17 private int RequiredSignalCount {
get;
set; }
18 private ItemContainer Container {
get;
set; }
19 private HashSet<ItemPrefab> ActivatingItemPrefabs {
get;
set; } =
new HashSet<ItemPrefab>();
22 private bool AllowUsingButtons => ActivatingItemPrefabs.None() || (Container !=
null && Container.Inventory.AllItems.Any(i => i !=
null && ActivatingItemPrefabs.Any(p => p == i.Prefab)));
26 RequiredSignalCount = element.
GetChildElements(
"TerminalButton").Count(c => c.GetAttribute(
"style") !=
null);
27 if (RequiredSignalCount < 1)
29 DebugConsole.ThrowError($
"Error in item \"{item.Name}\": no TerminalButton elements defined for the ButtonTerminal component!",
32 InitProjSpecific(element);
43 Signals =
new string[RequiredSignalCount];
44 for (
int i = 0; i < RequiredSignalCount; i++)
49 else if (
Signals.Length != RequiredSignalCount)
51 string[] newSignals =
new string[RequiredSignalCount];
52 if (
Signals.Length < RequiredSignalCount)
55 for (
int i =
Signals.Length; i < RequiredSignalCount; i++)
57 newSignals[i] =
string.Empty;
62 for (
int i = 0; i < RequiredSignalCount; i++)
70 ActivatingItemPrefabs.Clear();
77 ActivatingItemPrefabs.Add(prefab);
82 .ForEach(p => ActivatingItemPrefabs.Add(p));
85 if (ActivatingItemPrefabs.None())
87 DebugConsole.ThrowError($
"Error in item \"{item.Name}\": no activating item prefabs found with identifiers or tags \"{ActivatingItems}\"");
92 if (containers.Count() != 1)
94 DebugConsole.ThrowError($
"Error in item \"{item.Name}\": the ButtonTerminal component requires exactly one ItemContainer component!");
97 Container = containers.FirstOrDefault();
99 OnItemLoadedProjSpecific();
102 partial
void OnItemLoadedProjSpecific();
104 private bool SendSignal(
int signalIndex,
Character sender,
bool isServerMessage =
false)
106 if (!isServerMessage && !AllowUsingButtons) {
return false; }
107 string signal =
Signals[signalIndex];
108 string connectionName = $
"signal_out{signalIndex + 1}";
109 item.
SendSignal(
new Signal(signal, sender: sender), connectionName);
113 private readonly
struct EventData : IEventData
115 public readonly
int SignalIndex;
117 public EventData(
int signalIndex)
119 SignalIndex = signalIndex;
124 => TryExtractEventData<EventData>(data, out _);
128 var eventData = ExtractEventData<EventData>(extraData);
ContentPackage? ContentPackage
IEnumerable< ContentXElement > GetChildElements(string name)
void SendSignal(string signal, string connectionName)
static readonly PrefabCollection< ItemPrefab > Prefabs
The base class for components holding the different functionalities of the item
static MapEntityPrefab Find(string name, string identifier=null, bool showErrorMessages=true)
Find a matching map entity prefab
void WriteRangedInteger(int val, int min, int max)