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


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

        public CollisionRect pCollisionRect;
        public SpriteBox pCollisionSprite;  //TODO: change to box proxy?


        //-------------------------------------------------------------------
        //  PRIVATE CONSTRUCTION
        //-------------------------------------------------------------------

        public CollisionObject(SpriteProxy pProxy)
        {
            Debug.Assert(pProxy != null);

            //ref sprite to set size/shape
            Sprite pSprite = pProxy.GetSprite();
            Debug.Assert(pSprite != null);

            this.pCollisionRect = new CollisionRect(pSprite.GetRect());
            Debug.Assert(this.pCollisionRect != null);

            //create sprite
            this.pCollisionSprite = SpriteBoxMan.AddBox(SpriteBox.SpriteBoxName.Box, this.pCollisionRect.x, this.pCollisionRect.y, this.pCollisionRect.width, this.pCollisionRect.height);
            Debug.Assert(this.pCollisionSprite != null);
            this.pCollisionSprite.SetColor(1.0f, 0.0f, 0.0f);
        }

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

        public void Set(SpriteProxy pProxy)
        {
            Debug.Assert(pProxy != null);

            Sprite pSprite = pProxy.GetSprite();
            Debug.Assert(pSprite != null);

            this.pCollisionRect.Set(pSprite.GetRect());

            this.pCollisionSprite = SpriteBoxMan.FindBox(SpriteBox.SpriteBoxName.Box);
            this.pCollisionSprite.SetRect(this.pCollisionRect.x, this.pCollisionRect.y, this.pCollisionRect.width, this.pCollisionRect.height);
        }

        public void UpdatePosition(float x, float y)
        {
            //no angle or scale
            this.pCollisionRect.x = x;
            this.pCollisionRect.y = y;

            this.pCollisionSprite.SetPos(this.pCollisionRect.x, this.pCollisionRect.y);
            this.pCollisionSprite.SetRect(this.pCollisionRect.x, this.pCollisionRect.y, this.pCollisionRect.width, this.pCollisionRect.height);
            this.pCollisionSprite.Update();

        }


    } // end class

} // end namespace