GAM456-SpaceInvaders / SpaceInvaders / 6_Collision / CollisionSubject.cs
CollisionSubject.cs
Raw
using System;
using System.Diagnostics;


namespace SpaceInvaders
{
    public class CollisionSubject
    {
        //-------------------------------------------------------------------
        //  FIELDS
        //-------------------------------------------------------------------

        public GameObject pObjA;
        public GameObject pObjB;

        private SLinkMan poSLinkMan;

        
        //-------------------------------------------------------------------
        //  CONSTRUCTION
        //-------------------------------------------------------------------

        public CollisionSubject()
        {
            this.pObjA = null;
            this.pObjB = null;

            this.poSLinkMan = new SLinkMan();
            Debug.Assert(this.poSLinkMan != null);
        }


        //-------------------------------------------------------------------
        //  PUBLIC METHODS
        //-------------------------------------------------------------------

        public void AttachListener(CollisionObserver observer)
        {
            Debug.Assert(observer != null);
            observer.pSubject = this;

            this.poSLinkMan.AddToFront(observer);
        }

        public void NotifyListeners()
        {
            Iterator pIterator = this.poSLinkMan.GetIterator();
            CollisionObserver pNode = (CollisionObserver)pIterator.Peek();

            while(!pIterator.IsDone())
            {
                //call listener
                pNode.Notify();

                //get next
                pNode = (CollisionObserver)pIterator.Next();
            }
        }

        public void DetatchListeners()
        {
            Debug.Assert(this.poSLinkMan != null);

            Iterator it = this.poSLinkMan.GetIterator();
            Node pNode = it.First();
            Node pNext;

            while(!it.IsDone())
            {
                // save ref to next
                pNext = it.Next();

                // remove node
                this.poSLinkMan.Remove(pNode);

                // update curr node ref
                pNode = pNext;
            }
        }


    } // end class

} // end namespace