示例#1
0
        /// <summary>
        /// Update each flock member, Each bird want to fly with or flee from everything
        /// it sees depending on what type it is
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        /// <param name="cat"></param>
        public void Update(GameTime gameTime)
        {
            foreach (var enemy in hoard)
            {
                enemy.ResetThink();

                foreach (var otherEnemy in hoard)
                {
                    //this check is so we don't try to fly to ourself!
                    if (enemy != otherEnemy)
                    {
                        enemy.ReactTo(otherEnemy, ref hoardParams);
                    }
                }

                //Map
                Rectangle bounds     = enemy.BoundingRectangle;
                int       leftTile   = (int)Math.Floor((float)bounds.Left / Tile.Width) - 1;
                int       rightTile  = (int)Math.Ceiling(((float)bounds.Right / Tile.Width)) + 1;
                int       topTile    = (int)Math.Floor((float)bounds.Top / Tile.Height) - 1;
                int       bottomTile = (int)Math.Ceiling(((float)bounds.Bottom / Tile.Height)) + 1;

                // Reset flag to search for ground collision.
                //isOnGround = false;

                // For each potentially colliding tile,
                for (int y = topTile; y <= bottomTile; ++y)
                {
                    for (int x = leftTile; x <= rightTile; ++x)
                    {
                        // If this tile is collidable,
                        TileType collision = map.GetCollision(x, y);
                        if (collision != TileType.Ground)
                        {
                            // Determine collision depth (with direction) and magnitude.
                            enemy.ReactTo(map.GetBounds(x, y), ref hoardParams);
                        }
                    }
                }
                //Look for the cat
                //thisBird.ReactTo(cat, ref flockParams);

                enemy.Update(gameTime, ref hoardParams);
            }
        }
示例#2
0
        private void HandleCollisions()
        {
            // Get the player's bounding rectangle and find neighboring tiles.
            Rectangle bounds     = BoundingRectangle;
            int       leftTile   = (int)Math.Floor((float)bounds.Left / Tile.Width);
            int       rightTile  = (int)Math.Ceiling(((float)bounds.Right / Tile.Width)) - 1;
            int       topTile    = (int)Math.Floor((float)bounds.Top / Tile.Height);
            int       bottomTile = (int)Math.Ceiling(((float)bounds.Bottom / Tile.Height)) - 1;

            // Reset flag to search for ground collision.
            //isOnGround = false;

            // For each potentially colliding tile,
            for (int y = topTile; y <= bottomTile; ++y)
            {
                for (int x = leftTile; x <= rightTile; ++x)
                {
                    // If this tile is collidable,
                    TileType collision = map.GetCollision(x, y);
                    if (collision != TileType.Ground)
                    {
                        // Determine collision depth (with direction) and magnitude.
                        Rectangle tileBounds = Map.GetBounds(x, y);
                        Vector2   depth      = RectangleExtensions.GetIntersectionDepth(bounds, tileBounds);
                        if (depth != Vector2.Zero)
                        {
                            float absDepthX = Math.Abs(depth.X);
                            float absDepthY = Math.Abs(depth.Y);

                            if (collision == TileType.Wall)
                            {
                                // Y Axis Collision
                                if (absDepthY < absDepthX)
                                {
                                    position = new Vector2(Position.X, Position.Y + depth.Y);
                                    if (velocity.X > 0 && absDepthX < 44)
                                    {
                                        position.X += absDepthY;
                                    }
                                    else if (velocity.X < 0 && absDepthX < 44)
                                    {
                                        position.X -= absDepthY;
                                    }
                                }
                                //X Axis Collision
                                if (absDepthX < absDepthY)
                                {
                                    position = new Vector2(Position.X + depth.X, Position.Y);
                                    if (velocity.Y > 0 && absDepthY < 44)
                                    {
                                        position.Y += absDepthX;
                                    }
                                    else if (velocity.Y < 0 && absDepthY < 44)
                                    {
                                        position.Y -= absDepthX;
                                    }
                                }

                                // Perform further collisions with the new bounds.
                                bounds = BoundingRectangle;
                            }


                            //else if (collision == TileType.Wall) // Ignore platforms.
                            //{
                            //    // Resolve the collision along the X axis.
                            //    Position = new Vector2(Position.X + depth.X, Position.Y);

                            //    // Perform further collisions with the new bounds.
                            //    bounds = BoundingRectangle;
                            //}
                        }
                    }
                }
            }

            // Save the new bounds bottom.
            //previousBottom = bounds.Bottom;
        }