73 lines
2.6 KiB
C#
73 lines
2.6 KiB
C#
using UnityEngine;
|
|
|
|
public interface IReceptor {
|
|
public string GetName();
|
|
|
|
public Nucleus[] nucleiArray { get; set; }
|
|
|
|
public void AddReceptorElement(ClusterPrefab prefab);
|
|
public void RemoveReceptorElement();
|
|
|
|
public void AddArrayReceiver(Nucleus receiverToAdd, float weight = 1);
|
|
|
|
public void ProcessStimulus(Vector3 inputValue, int thingId = 0, string thingName = null);
|
|
}
|
|
|
|
public static class IReceptorHelpers {
|
|
|
|
public static void AddReceptorElement(IReceptor receptor, ClusterPrefab prefab) {
|
|
if (receptor.nucleiArray.Length == 0) {
|
|
Debug.LogError("Empty perceptoid array, cannot add");
|
|
}
|
|
int newLength = receptor.nucleiArray.Length + 1;
|
|
Nucleus[] newArray = new Nucleus[newLength];
|
|
|
|
string baseName = receptor.GetName();
|
|
int colonPos = baseName.IndexOf(":");
|
|
if (colonPos > 0)
|
|
baseName = baseName[..colonPos];
|
|
|
|
for (int i = 0; i < receptor.nucleiArray.Length; i++)
|
|
newArray[i] = receptor.nucleiArray[i];
|
|
if (receptor.nucleiArray[0] is Nucleus nucleus) {
|
|
newArray[newLength - 1] = nucleus.Clone(prefab);
|
|
newArray[newLength - 1].name = $"{baseName}: {newLength - 1}";
|
|
}
|
|
|
|
foreach (Nucleus element in receptor.nucleiArray) {
|
|
if (element is IReceptor receptorElement) {
|
|
receptorElement.nucleiArray = newArray;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void RemoveReceptorElement(IReceptor receptor) {
|
|
int newLength = receptor.nucleiArray.Length - 1;
|
|
if (newLength == 0) {
|
|
Debug.LogWarning("Perceptoid array cannot be empty");
|
|
}
|
|
Nucleus[] newArray = new Nucleus[newLength];
|
|
for (int i = 0; i < newLength; i++)
|
|
newArray[i] = receptor.nucleiArray[i];
|
|
// Delete the last perception
|
|
if (receptor.nucleiArray[newLength] is Nucleus nucleus)
|
|
Neuron.Delete(nucleus);
|
|
|
|
foreach (Nucleus element in receptor.nucleiArray) {
|
|
if (element is IReceptor receptorElement) {
|
|
receptorElement.nucleiArray = newArray;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public static void AddArrayReceiver(IReceptor receptor, Nucleus receiverToAdd, float weight = 1) {
|
|
foreach (Nucleus element in receptor.nucleiArray) {
|
|
if (element is Cluster cluster)
|
|
cluster.defaultOutput.AddReceiver(receiverToAdd, weight);
|
|
if (element is Neuron neuron)
|
|
neuron.AddReceiver(receiverToAdd, weight);
|
|
}
|
|
|
|
}
|
|
} |