Example #1
0
 /// <summary>
 /// Tests whether this node contains a rectangle
 /// </summary>
 /// <param name="rect">The rectangle to test</param>
 /// <returns>Whether or not this node contains the specified rectangle</returns>
 public bool ContainsRect(FRect rect)
 {
     return(rect.TopLeft.X >= Rect.TopLeft.X &&
            rect.TopLeft.Y >= Rect.TopLeft.Y &&
            rect.BottomRight.X <= Rect.BottomRight.X &&
            rect.BottomRight.Y <= Rect.BottomRight.Y);
 }
Example #2
0
        public Player(PlayerIndex playerIdx)
        {
            #region assign player index & input
            PlayerIdx = playerIdx;

            switch (PlayerIdx)
            {
            case PlayerIndex.One:
                ShootKey = Keys.Space;
                LeftKey  = Keys.A;
                RightKey = Keys.D;

                ScoreLoc1 = new Vector2(5, 5);
                ScoreLoc2 = new Vector2(95, 5);
                break;

            case PlayerIndex.Two:
                ShootKey = Keys.NumPad0;
                LeftKey  = Keys.NumPad4;
                RightKey = Keys.NumPad6;

                ScoreLoc1 = new Vector2(Engine.Instance.Width - 200, 5);
                ScoreLoc2 = new Vector2(ScoreLoc1.X + Level.GuiFont.MeasureString(SCORE_STR).X, 5);
                break;

            default:
                break;
            }
            #endregion

            // Sets the collision rectangle and location
            CollisionRect = new FRect(Engine.Instance.Width / 2 - Texture.Width / 2, Level.DEAD_LINE, Texture.Width, Texture.Height);
        }
Example #3
0
        public void FRect_CollidedSidesWithTouch()
        {
            FRect A = new FRect(0, 0, 5, 5);
            FRect B = new FRect(-4, 0, 5, 5);
            FRect C = new FRect(-2, -2, 6, 8);
            FRect D = new FRect(-20, -20, 1, 1);
            FRect E = new FRect(1, 1, 3, 3);
            FRect F = new FRect(-1, 0, 5, 5);

            Collision.RectSide side = Collision.CollidedSidesWithTouch(A, A);
            Debug.Assert(side.IsSet(Collision.RectSide.Equal), "1");

            side = Collision.CollidedSidesWithTouch(B, A);
            Debug.Assert(side.IsSet(Collision.RectSide.Left) &&
                         side.IsSet(Collision.RectSide.Top) &&
                         side.IsSet(Collision.RectSide.Bottom),
                         "2");

            side = Collision.CollidedSidesWithTouch(C, A);
            Debug.Assert(side.IsSet(Collision.RectSide.Left) &&
                         side.IsSet(Collision.RectSide.Top) &&
                         side.IsSet(Collision.RectSide.Bottom),
                         "3");

            side = Collision.CollidedSidesWithTouch(D, A);
            Debug.Assert(side.IsSet(Collision.RectSide.None), "4");

            side = Collision.CollidedSidesWithTouch(F, A);
            Debug.Assert(side.IsSet(Collision.RectSide.Left) &&
                         side.IsSet(Collision.RectSide.Top) &&
                         side.IsSet(Collision.RectSide.Bottom),
                         "5");
        }
Example #4
0
        public void FRect_Intersects()
        {
            FRect A = new FRect(0, 0, 5, 5);
            FRect B = new FRect(3, 3, 5, 5);
            FRect C = new FRect(1, 1, 1, 1);
            FRect D = new FRect(5, 5, 5, 5);
            FRect E = new FRect(6, 5, 5, 5);
            FRect F = new FRect(5, 6, 5, 5);
            FRect G = new FRect(-1, -1, 7, 7);

            Debug.Assert(A.Intersects(A), "1");
            Debug.Assert(A.Intersects(B), "2");
            Debug.Assert(A.Intersects(C), "3");
            Debug.Assert(!A.Intersects(D), "4");
            Debug.Assert(!A.Intersects(E), "5");
            Debug.Assert(!A.Intersects(F), "6");
            Debug.Assert(A.Intersects(G), "7");

            Debug.Assert(A.IntersectsOrTouches(A), "a");
            Debug.Assert(A.IntersectsOrTouches(B), "b");
            Debug.Assert(A.IntersectsOrTouches(C), "c");
            Debug.Assert(A.IntersectsOrTouches(D), "d");
            Debug.Assert(!A.IntersectsOrTouches(E), "e");
            Debug.Assert(!A.IntersectsOrTouches(F), "f");
            Debug.Assert(A.IntersectsOrTouches(G), "g");
        }
Example #5
0
 /// <summary>
 /// Gets a list of items intersecting a specified rectangle
 /// </summary>
 /// <param name="Rect">The rectangle</param>
 /// <param name="ItemsFound">The list to add found items to (list will not be cleared first)</param>
 public void GetItems(FRect Rect, ref List <QuadTreePositionItem <T> > ItemsList)
 {
     if (ItemsList != null)
     {
         headNode.GetItems(Rect, ref ItemsList);
     }
 }
Example #6
0
 /// <summary>
 /// QuadTreeNode constructor
 /// </summary>
 /// <param name="parentNode">The parent node of this QuadTreeNode</param>
 /// <param name="rect">The rectangle of the QuadTreeNode</param>
 /// <param name="maxItems">Maximum number of items in the QuadTreeNode before partitioning</param>
 public QuadTreeNode(QuadTreeNode <T> parentNode, FRect rect, int maxItems)
 {
     ParentNode    = parentNode;
     Rect          = rect;
     MaxItems      = maxItems;
     IsPartitioned = false;
     Items         = new List <QuadTreePositionItem <T> >();
 }
Example #7
0
 /// <summary>
 /// QuadTreeNode constructor
 /// </summary>
 /// <param name="rect">The rectangle of the QuadTreeNode</param>
 /// <param name="maxItems">Maximum number of items in the QuadTreeNode before partitioning</param>
 /// <param name="worldResize">The function to return the size</param>
 public QuadTreeNode(FRect rect, int maxItems, ResizeDelegate worldResize)
 {
     ParentNode    = null;
     Rect          = rect;
     MaxItems      = maxItems;
     WorldResize   = worldResize;
     IsPartitioned = false;
     Items         = new List <QuadTreePositionItem <T> >();
 }
        /// <summary>
        /// Creates a position item in a QuadTree
        /// </summary>
        /// <param name="parent">The parent of this item</param>
        /// <param name="position">The position of this item</param>
        /// <param name="size">The size of this item</param>
        public QuadTreePositionItem(T parent, Vector2 position, Vector2 size)
        {
            this.rect = new FRect(0f, 0f, 1f, 1f);

            this.parent   = parent;
            this.position = position;
            this.size     = size;
            OnMove();
        }
Example #9
0
        public void FRect_Misc()
        {
            FRect A = new FRect(1, 1, -1, 1);
            FRect B = new FRect(1, 1, 1, -1);
            FRect C = new FRect(1, 1, -1, -1);

            Debug.Assert(!A.IsValid);
            Debug.Assert(!B.IsValid);
            Debug.Assert(!C.IsValid);
        }
Example #10
0
        public Wall(Vector2 location)
        {
            Location     = location;
            WallBoundary = new FRect(Location, 3 * WALL_SEGMENT_SIZE, 3 * WALL_SEGMENT_SIZE);

            for (int y = 0; y < 3; y++)
            {
                for (int x = 0; x < 3; x++)
                {
                    Segments.Add(new WallSegment(Location + new Vector2(x * WALL_SEGMENT_SIZE, y * WALL_SEGMENT_SIZE)));
                }
            }
        }
Example #11
0
        public void FRect_FullyEncloses()
        {
            FRect A = new FRect(0, 0, 10, 10);
            FRect B = new FRect(3, 3, 3, 3);
            FRect C = new FRect(1, 1, 9, 9); // touching bottom and right
            FRect D = new FRect(3, 3, 11, 11);
            FRect E = new FRect(-1, -1, 10, 10);

            Debug.Assert(!A.FullyEncloses(A));
            Debug.Assert(A.FullyEncloses(B));
            Debug.Assert(!A.FullyEncloses(C));
            Debug.Assert(!A.FullyEncloses(D));
            Debug.Assert(!A.FullyEncloses(E));
        }
Example #12
0
        public Enemy(int levelNr, Vector2 location, eEnemyType type)
        {
            #region Assign velocity
            Velocity = 15f + levelNr * 3.0f;
            if (Velocity > 160.0f)
            {
                Velocity = 160.0f;
            }
            #endregion

            CollisionRect = new FRect(location, WIDTH, HEIGHT);

            #region Create enemy by type
            switch (type)
            {
            case eEnemyType.Top:
                Texture     = Common.str2Tex("Enemies/enemy01");
                HP          = 2;
                ScoreValue  = 30;
                ShootChance = 4 + levelNr / 2;
                if (ShootChance > 25)
                {
                    ShootChance = 25;
                }
                break;

            case eEnemyType.Mid:
                Texture     = Common.str2Tex("Enemies/enemy02");
                ScoreValue  = 20;
                HP          = 1;
                ShootChance = 2 + levelNr / 2;
                if (ShootChance > 15)
                {
                    ShootChance = 15;
                }
                break;

            case eEnemyType.Bottom:
                Texture     = Common.str2Tex("Enemies/enemy03");
                ScoreValue  = 10;
                HP          = 1;
                ShootChance = 1 + levelNr / 2;
                if (ShootChance > 10)
                {
                    ShootChance = 10;
                }
                break;
            }
            #endregion
        }
Example #13
0
 public BonusEnemy()
 {
     if (Maths.RandomNr(0, 1) == 0)
     {
         // Move right
         MoveDir       = 1;
         CollisionRect = new FRect(-SIZE, 0, SIZE, SIZE);
     }
     else // Move left
     {
         MoveDir       = -1;
         CollisionRect = new FRect(Engine.Instance.Width, 32, SIZE, SIZE);
         Effects       = SpriteEffects.FlipHorizontally;
     }
 }
Example #14
0
        public void FRect_Contains()
        {
            FRect   A = new FRect(10, 10, 10, 10);
            Vector2 B = new Vector2(10, 10);
            Vector2 C = new Vector2(-10, -10);
            Vector2 D = new Vector2(12, 13);
            Vector2 E = new Vector2(20, 20);
            Vector2 F = new Vector2(15, 25);

            Debug.Assert(A.Contains(B));
            Debug.Assert(!A.Contains(C));
            Debug.Assert(A.Contains(D));
            Debug.Assert(A.Contains(E));
            Debug.Assert(!A.Contains(F));
        }
Example #15
0
 /// <summary>
 /// Determines collision with the Wall AABB and if that yields true it will check the supplied FRect against it's wall segments. If any of those also collides then that one will take damage.
 /// </summary>
 /// <param name="collisionRect">The AABB of the collider</param>
 /// <returns>true if any segment collides with the supplied AABB</returns>
 public bool Collides(FRect collisionRect)
 {
     if (WallBoundary.Intersects(collisionRect))
     {
         foreach (WallSegment segment in Segments)
         {
             if (segment.CollisionRect.Intersects(collisionRect) && segment.HP > 0)
             {
                 segment.HP--;
                 return(true);
             }
         }
     }
     return(false);
 }
Example #16
0
        public void FRect_Creation()
        {
            FRect test = new FRect(10, 11, 12, 13);

            Debug.Assert(test.X == 10 && test.Left == 10);
            Debug.Assert(test.Y == 11 && test.Top == 11);
            Debug.Assert(test.Width == 12);
            Debug.Assert(test.Height == 13);
            Debug.Assert(test.Right == 10 + 12);
            Debug.Assert(test.Bottom == 11 + 13);

            Debug.Assert(test.TopLeft == new Vector2(test.X, test.Y));
            Debug.Assert(test.TopRight == new Vector2(test.X + test.Width, test.Y));
            Debug.Assert(test.BottomRight == new Vector2(test.X + test.Width, test.Y + test.Height));
            Debug.Assert(test.BottomLeft == new Vector2(test.X, test.Y + test.Height));
        }
Example #17
0
        public Projectile(Vector2 centerLoc, Player owner)
        {
            Owner = owner;
            if (Owner == null)
            {// enemy
                MoveDir  = new Vector2(0, 1);
                Texture  = Common.str2Tex("projectile01");
                Velocity = 220;
            }
            else
            {// player
                MoveDir  = new Vector2(0, -1);
                Texture  = Common.str2Tex("projectile02");
                Velocity = 350;
            }

            CollisionRect = new FRect(centerLoc.X - WIDTH / 2, centerLoc.Y - HEIGHT / 2, WIDTH, HEIGHT);
        }
Example #18
0
        public void FRect_OperatorOverloads()
        {
            FRect A = new FRect(32, 32, 64, 64);
            FRect B = new FRect(32, 32, 64, 64);
            FRect C = new FRect(32, 32, 65, 64);
            FRect D = new FRect(32, -32, 64, 64);
            FRect E = new FRect(32, 32, 16, 16);
            FRect T;

            Debug.Assert(A == B);
            Debug.Assert(A != C);
            Debug.Assert(A != D);

            T = A + E;
            Debug.Assert(T.X == 64 && T.Y == 64 && T.Width == 64 && T.Height == 64);
            T = A - E;
            Debug.Assert(T.X == 0 && T.Y == 0 && T.Width == 64 && T.Height == 64);
        }
Example #19
0
        public void FRect_Touch()
        {
            FRect A = new FRect(0, 0, 32, 32);
            FRect B = new FRect(4, 4, 24, 24);   // inside A
            FRect C = new FRect(-4, -4, 36, 36); // fully overlaps A
            FRect D = new FRect(32, 0, 32, 32);
            FRect E = new FRect(0, -32, 32, 32);
            FRect F = new FRect(0, -33, 32, 32);
            FRect G = new FRect(0, 500, 32, 32);

            Debug.Assert(!A.AreOnlyTouching(B, 0));
            Debug.Assert(!A.AreOnlyTouching(C, 0));
            Debug.Assert(A.AreOnlyTouching(D, 0));
            Debug.Assert(A.AreOnlyTouching(E, 0));
            Debug.Assert(A.AreOnlyTouching(F, 1));
            Debug.Assert(!A.AreOnlyTouching(F, 0));
            Debug.Assert(!A.AreOnlyTouching(G, 0));
        }
Example #20
0
        /// <summary>
        /// Resizes the Quadtree field
        /// </summary>
        /// <param name="newWorld">The new field</param>
        /// <remarks>This is an expensive operation, so try to initialize the world to a big enough size</remarks>
        public void Resize(FRect newWorld)
        {
            // Get all of the items in the tree
            List <QuadTreePositionItem <T> > Components = new List <QuadTreePositionItem <T> >();

            GetAllItems(ref Components);

            // Destroy the head node
            headNode.Destroy();
            headNode = null;

            // Create a new head
            headNode = new QuadTreeNode <T>(newWorld, maxItems, Resize);

            // Reinsert the items
            foreach (QuadTreePositionItem <T> m in Components)
            {
                headNode.Insert(m);
            }
        }
Example #21
0
        /// <summary>
        /// Gets a list of items intersecting a specified rectangle
        /// </summary>
        /// <param name="Rect">The rectangle</param>
        /// <param name="ItemsFound">The list to add found items to (list will not be cleared first)</param>
        /// <remarks>ItemsFound is assumed to be initialized, and will not be cleared</remarks>
        public void GetItems(FRect Rect, ref List <QuadTreePositionItem <T> > ItemsFound)
        {
            // test the point against this node
            if (Rect.Intersects(Rect))
            {
                // test the point in each item
                foreach (QuadTreePositionItem <T> Item in Items)
                {
                    if (Item.Rect.Intersects(Rect))
                    {
                        ItemsFound.Add(Item);
                    }
                }

                // query all subtrees
                if (IsPartitioned)
                {
                    TopLeftNode.GetItems(Rect, ref ItemsFound);
                    TopRightNode.GetItems(Rect, ref ItemsFound);
                    BottomLeftNode.GetItems(Rect, ref ItemsFound);
                    BottomRightNode.GetItems(Rect, ref ItemsFound);
                }
            }
        }
 public WallSegment(Vector2 location)
 {
     CollisionRect = new FRect(location, Wall.WALL_SEGMENT_SIZE, Wall.WALL_SEGMENT_SIZE);
 }
Example #23
0
 public void SetMatrix(Graphics gfx, ColorMatrix mtx, FRect rect)
 {
     gfx.SetClip(rect, CombineMode.Replace);
     //gfx.MultiplyTransform(mtx); //??
     gfx.ResetClip();
 }
Example #24
0
 /// <summary>
 /// QuadTree constructor
 /// </summary>
 /// <param name="worldRect">The world rectangle for this QuadTree (a rectangle containing all items at all times)</param>
 /// <param name="maxItems">Maximum number of items in any cell of the QuadTree before partitioning</param>
 public QuadTree(FRect worldRect, int maxItems)
 {
     this.headNode = new QuadTreeNode <T>(worldRect, maxItems, Resize);
     this.maxItems = maxItems;
 }