using UnityEngine; #if UNITY_MATHEMATICS using Unity.Mathematics; using static Unity.Mathematics.math; #endif namespace NanoBrain { /// /// Basic IReceptor to receive external input /// [System.Serializable] public class Receptor : Neuron, IReceptor { /// /// Create a new Receptor in a Cluster instance /// /// The Cluster in which the Receptor is created /// The name of the new Receptor public Receptor(Cluster parent, string name) : base(parent, name) { this.array = new NucleusArray(this); if (this.name.IndexOf(":") < 0) this.name += ": 0"; } /// /// Create a new Receptor in a Cluster Prefab /// /// The Cluster Prefab in which the Receptor is created /// The name of the new Receptor public Receptor(ClusterPrefab prefab, string name) : base(prefab, name) { this.array = new NucleusArray(this); } public string GetName() { return this.name; } /// \copydoc NanoBrain::Neuron::ShallowCloneTo public override Nucleus ShallowCloneTo(Cluster parent) { Receptor clone = new(parent, name) { }; CloneFields(clone); return clone; } /// \copydoc NanoBrain::Neuron::Clone public override Nucleus Clone(ClusterPrefab prefab) { Receptor clone = new(prefab, name) { array = this._array }; CloneFields(clone); // Adding receivers will also add synapses to the receivers foreach (Nucleus receiver in this.receivers.ToArray()) clone.AddReceiver(receiver); return clone; } [SerializeReference] private NucleusArray _array; public NucleusArray array { set { _array = value; } } public Nucleus[] nucleiArray { get { return _array.nuclei; } set { _array.nuclei = value; } } public void AddReceptorElement(ClusterPrefab prefab) { IReceptorHelpers.AddReceptorElement(this, prefab); } public void RemoveReceptorElement() { IReceptorHelpers.RemoveReceptorElement(this); } public virtual void AddArrayReceiver(Nucleus receiverToAdd, float weight = 1) { IReceptorHelpers.AddArrayReceiver(this, receiverToAdd, weight); } public override void UpdateStateIsolated() { this.outputValue = this.bias; } #if UNITY_MATHEMATICS public override void UpdateNuclei() { this.stale++; if (this.stale > staleValueForSleep && lengthsq(this.bias) > 0) { this.bias = new float3(0, 0, 0); this.parent.UpdateFromNucleus(this); } } #else public override void UpdateNuclei() { this.stale++; if (this.stale > staleValueForSleep && this.bias.sqrMagnitude > 0) { this.bias = new Vector3(0, 0, 0); this.parent.UpdateFromNucleus(this); } } #endif public override void ProcessStimulus(Vector3 inputValue, int thingId = 0, string thingName = null) { this._array ??= new NucleusArray(this.parent); this._array.ProcessStimulus(thingId, inputValue, thingName); } } }