/// <summary> /// Disconnects this code from its parent /// </summary> public void DisconnectFromParent() { // Since we already contain all the information from our children, this is sufficient PropagateRemoveAll(); parent.children.Remove(this); parent = null; }
void TestAffixContainerGraph() { AffixContainer c1 = new AffixContainer(new Affix[] { AffixInfo.GenerateAffix(AffixType.Health, 4) }); AffixContainer c2 = new AffixContainer(new Affix[] { AffixInfo.GenerateAffix(AffixType.FireRate, 4) }); AffixContainer c3 = new AffixContainer(new Affix[] { AffixInfo.GenerateAffix(AffixType.Health, 20) }); AffixContainer c4 = new AffixContainer(new Affix[] { AffixInfo.GenerateAffix(AffixType.PhysDmgFlat, 4) }); print(c1); print(c2); print(c3); print(c4); //c1.AppendChild(c1); c1.AppendChild(c2); print("Appended c2 to c1"); print(c1); //c2.AppendChild(c1); c2.AppendChild(c3); print("Appended c3 to c2"); print(c1); print(c2); //c3.AppendChild(c1); //c3.AppendChild(c2); c1.AppendChild(c4); print("Appendecd c4 to c1"); print(c1); c2.DisconnectFromParent(); print("Disconnected c2 from c1"); print(c1); }
/// <summary> /// Checks if adding a node as a child of this node would produce a loop. /// </summary> /// <param name="child">The new child</param> /// <returns>Whether or not a loop would be created</returns> protected bool CheckChildProducesLoop(AffixContainer child) { for (AffixContainer current = this; current != null; current = current.parent) { if (current == child) { return(true); } } return(false); }
/// <summary> /// Appends a child to this node /// </summary> /// <param name="child"></param> public void AppendChild(AffixContainer child) { if (child.parent != null) { throw new InvalidOperationException("Child already has a parent!"); } if (child == this) { throw new InvalidOperationException("Can't append self as child!"); } if (CheckChildProducesLoop(child)) { throw new InvalidOperationException("Adding this child would produce a loop!"); } children.Add(child); child.parent = this; child.PropagateAll(); }
void GenerateTestAffixContainer() { AffixContainer container = new AffixContainer(); Affix health1 = AffixInfo.GetAffixInfo(AffixType.Health).GenerateAffix(2); container.Add(health1); container.Add(AffixInfo.GetAffixInfo(AffixType.Health).GenerateAffix(2)); container.Add(health1); container.Add(AffixInfo.GetAffixInfo(AffixType.PhysDmgFlat).GenerateAffix(3)); print(container); container.Remove(health1); print(container); Affix fireRate = AffixInfo.GetAffixInfo(AffixType.FireRate).GenerateAffix(5); container.Remove(fireRate); Affix health2 = AffixInfo.GetAffixInfo(AffixType.Health).GenerateAffix(3); container.Remove(health2); }
/// <summary> /// Removes a child from this node /// </summary> /// <param name="child"></param> public void RemoveChild(AffixContainer child) { child.DisconnectFromParent(); }
/// <summary> /// Attaches this node to a parent. /// </summary> /// <param name="parent"></param> public void AttachToParent(AffixContainer parent) { parent.AppendChild(this); }