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


namespace SpaceInvaders
{
    public class CollisionPair : DLink
    {
        //-------------------------------------------------------------------
        //  FIELDS
        //-------------------------------------------------------------------

        public enum CollideObjs
        {
            Grid_Wall,
            Alien_Shield,
            UFO_Wall,

            Missile_Alien,
            Missile_UFO,
            Missile_Shield,
            Missile_Wall,
            Missile_Bomb,

            Bomb_Shield,
            Bomb_Wall,
            Ship_Bomb,

            Ship_Bumper,

            NullObject,
            Uninitialized
        }

        public CollideObjs name;
        public GameObject treeA;
        public GameObject treeB;
        public CollisionSubject poSubject;


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

        public CollisionPair() : base()
        {
            this.name = CollideObjs.Uninitialized;
            this.treeA = null;
            this.treeB = null;

            this.poSubject = new CollisionSubject();
            Debug.Assert(this.poSubject != null);
        }


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

        public void SetPair(CollideObjs name, GameObject pRootA, GameObject pRootB)
        {
            //make sure they exist
            Debug.Assert(pRootA != null);
            Debug.Assert(pRootB != null);

            this.name = name;
            this.treeA = pRootA;
            this.treeB = pRootB;
        }

        public void SetCollision(GameObject pObjA, GameObject pObjB)
        {
            //cant collide null objs
            Debug.Assert(pObjA != null);
            Debug.Assert(pObjB != null);

            this.poSubject.pObjA = pObjA;
            this.poSubject.pObjB = pObjB;
        }

        public void ProcessPair()
        {
            CollidePair(this.treeA, this.treeB);
        }

        public void AttachListener(CollisionObserver observer)
        {
            this.poSubject.AttachListener(observer);
        }

        public void DetatchListeners()
        {
            this.poSubject.DetatchListeners();
        }

        public void NotifyListeners()
        {
            this.poSubject.NotifyListeners();
        }

        public static void CollidePair(GameObject pTreeA, GameObject pTreeB)
        {
            //tree A vs tree B
            GameObject pNodeA = pTreeA;
            GameObject pNodeB;

            while (pNodeA != null)
            {
                //restart compare
                pNodeB = pTreeB;

                while(pNodeB != null)
                {
                    //print testing pair
                    //Debug.WriteLine("Collide Pair: {0} vs {1}", pNodeA.name, pNodeB.name);

                    //get collision rectA & rectB
                    CollisionRect rectA = pNodeA.GetCollisionObj().pCollisionRect;
                    CollisionRect rectB = pNodeB.GetCollisionObj().pCollisionRect;

                    //test intersection
                    if(CollisionRect.Intersect(rectA, rectB))
                    {
                        //intersect = true, call visitor 
                        pNodeA.Accept(pNodeB);
                        break;
                    }

                    pNodeB = (GameObject)ForwardIterator.GetSibling(pNodeB);
                }

                pNodeA = (GameObject)ForwardIterator.GetSibling(pNodeA);
            }
        }

      
        //-------------------------------------------------------------------
        //  OVERRIDDEN METHODS
        //-------------------------------------------------------------------

        public override bool Compare(Node pNode)
        {
            //check incoming
            Debug.Assert(pNode != null);
            
            CollisionPair pPairB = (CollisionPair)pNode;
            if(this.name == pPairB.name)
            {
                return true;
            }

            return false;
        }

        public override object GetName()
        {
            return this.name;
        }

        public override void Reset()
        {
            this.Clear();
        }

        public override void PrintStats()
        {
            //TODO
            base.PrintStats();
        }


        //-------------------------------------------------------------------
        //  PRIVATE METHODS
        //-------------------------------------------------------------------

        private void Clear()
        {
            this.name = CollideObjs.Uninitialized;
            this.treeA = null;
            this.treeB = null;
        }

    } // end class

} // end namespace