NanoBrain-unitypackage/NucleusArray.cs
2026-02-06 15:12:34 +01:00

119 lines
3.9 KiB
C#

using System.Linq;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class NucleusArray {
[SerializeReference]
private Nucleus[] _nuclei;
public Nucleus[] nuclei {
get {
return _nuclei;
}
}
public string name;
public NucleusArray(Nucleus nucleus) {
this.name = nucleus.name;
this._nuclei = new Nucleus[1];
this._nuclei[0] = nucleus;
}
public NucleusArray(ClusterPrefab cluster) {
this.name = cluster.name;
this._nuclei = new Nucleus[0];
}
public NucleusArray(int size, string name) {
this.name = name;
this._nuclei = new Nucleus[size];
}
public void AddNucleus() {
if (this._nuclei.Length == 0) {
Debug.LogError("Empty perceptoid array, cannot add");
return;
}
int newLength = this._nuclei.Length + 1;
Nucleus[] newArray = new Nucleus[newLength];
for (int i = 0; i < this._nuclei.Length; i++)
newArray[i] = this._nuclei[i];
if (this._nuclei[0] is Nucleus nucleus)
newArray[newLength - 1] = nucleus.Clone();
this._nuclei = newArray;
}
public void RemoveNucleus() {
int newLength = this._nuclei.Length - 1;
if (newLength == 0) {
Debug.LogWarning("Perceptoid array cannot be empty");
return;
}
Nucleus[] newPerceptei = new Nucleus[newLength];
for (int i = 0; i < newLength; i++)
newPerceptei[i] = this._nuclei[i];
// Delete the last perception
if (this._nuclei[newLength] is Nucleus nucleus)
Neuron.Delete(nucleus); //this._nuclei[newLength]);
this._nuclei = newPerceptei;
}
public Dictionary<int, Nucleus> thingReceivers = new();
public virtual void ProcessStimulus(int thingId, Vector3 inputValue, string thingName = null) {
CleanupReceivers();
if (!thingReceivers.TryGetValue(thingId, out Nucleus selectedReceiver)) {
// Debug.Log($"No receiver found for {thingId}");
foreach (Nucleus receptor in this.nuclei) {
if (receptor is not Nucleus receiver)
continue;
if (thingReceivers.ContainsValue(receiver) == false) {
// receiver is not used yet
// Debug.Log($"{thingId} -> {receiver.name}");
thingReceivers.Add(thingId, receiver);
selectedReceiver = receiver;
break;
}
}
}
if (selectedReceiver == null)
return;
if (thingName != null) {
string baseName = selectedReceiver.name;
int colonPos = selectedReceiver.name.IndexOf(":");
if (colonPos > 0)
baseName = selectedReceiver.name[..colonPos];
selectedReceiver.name = baseName + ": " + thingName;
}
if (selectedReceiver is Neuron selectedNucleus)
selectedNucleus.ProcessStimulus(inputValue);
selectedReceiver.parent.UpdateStateIsolated();
}
private void CleanupReceivers() {
// Remove a thing-receiver connection when the nucleus is inactive
List<int> receiversToRemove = new();
foreach (KeyValuePair<int, Nucleus> item in thingReceivers) {
if (item.Value.isSleeping) {
//Nucleus n = item.Value as Nucleus;
receiversToRemove.Add(item.Key);
}
}
foreach (int thingId in receiversToRemove) {
Nucleus selectedReceiver = thingReceivers[thingId];
thingReceivers.Remove(thingId);
int colonPos = selectedReceiver.name.IndexOf(":");
if (colonPos > 0)
selectedReceiver.name = selectedReceiver.name[..colonPos];
}
}
}