using System; using System.Diagnostics; namespace SpaceInvaders { public abstract class Composite : GameObject { //------------------------------------------------------------------- // FIELDS //------------------------------------------------------------------- protected DLinkMan pDLinkMan; protected int numNodes = 0; //------------------------------------------------------------------- // CONSTRUCTION //------------------------------------------------------------------- public Composite() : base(Component.Type.Composite, GameObject.ObjectName.NullObject, Sprite.SpriteName.NullObject) { //LTN: own by composite this.pDLinkMan = new DLinkMan(); } public Composite(GameObject.ObjectName name, Sprite.SpriteName spriteName) : base(Component.Type.Composite, name, spriteName) { this.pDLinkMan = new DLinkMan(); } //------------------------------------------------------------------- // PUBLIC METHODS //------------------------------------------------------------------- public Component GetHead() { Debug.Assert(this.pDLinkMan != null); Component pHead = (GameObject)this.pDLinkMan.pHead; return pHead; } public int GetNumNodes() { return this.numNodes; } public GameObject GetNodeIndex(int index) { Debug.Assert(index >= 0); Debug.Assert(index < numNodes); Iterator pIterator = this.pDLinkMan.GetIterator(); Debug.Assert(pIterator != null); GameObject pObj = (GameObject)pIterator.First(); int count = 0; while(!pIterator.IsDone()) { if(count == index) { break; } pObj = (GameObject)pIterator.Next(); count++; } return pObj; } public override void AddComponent(Component pComponent) { Debug.Assert(pComponent != null); Debug.Assert(this.pDLinkMan != null); this.pDLinkMan.AddToFront(pComponent); pComponent.pParent = this; this.numNodes++; } public override void RemoveComponent(Component pComponent) { Debug.Assert(pComponent != null); Debug.Assert(this.pDLinkMan != null); this.pDLinkMan.Remove(pComponent); this.numNodes--; } public override void Ressurect() { Debug.Assert(this.pDLinkMan.pHead == null); base.Ressurect(); } public override void Move(float x, float y) { //get iterator Iterator pIterator = this.pDLinkMan.GetIterator(); Debug.Assert(pIterator != null); GameObject pObject = (GameObject)pIterator.First(); //iterate thro nodes while(!pIterator.IsDone()) { //check node before update Debug.Assert(pObject != null); pObject.Move(x, y); pObject = (GameObject)pIterator.Next(); } } public override void PrintComponent() { Debug.WriteLine("\nComposite:"); // walk through the list and render Iterator pIt = this.pDLinkMan.GetIterator(); Debug.Assert(pIt != null); GameObject pNode = (GameObject)pIt.First(); // Walk through the nodes while (!pIt.IsDone()) { // Update the node Debug.Assert(pNode != null); pNode.PrintComponent(); pNode = (GameObject)pIt.Next(); } } } }