/// <summary> /// Adds a temporary brain to Npc, last added brain is active /// </summary> /// <param name="newBrain"></param> public virtual void AddBrain(ABrain newBrain) { if (newBrain == null) throw new ArgumentNullException("newBrain"); if (newBrain.IsActive) throw new ArgumentException("The new brain is already active.", "newBrain"); lock (BrainSync) { Brain.Stop(); ArrayList brains = new ArrayList(m_brains); brains.Add(newBrain); m_brains = brains; // make new array list to avoid locks in the Brain property newBrain.Body = this; newBrain.Start(); } }
/// <summary> /// Sets the NPC own brain /// </summary> /// <param name="brain">The new brain</param> /// <returns>The old own brain</returns> public virtual ABrain SetOwnBrain(ABrain brain) { if (brain == null) return null; if (brain.IsActive) throw new ArgumentException("The new brain is already active.", "brain"); lock (BrainSync) { ABrain oldBrain = m_ownBrain; bool activate = oldBrain.IsActive; if (activate) oldBrain.Stop(); m_ownBrain = brain; m_ownBrain.Body = this; if (activate) m_ownBrain.Start(); return oldBrain; } }