/// <summary> /// Allows the game component to perform any initialization it needs to before starting /// to run. This is where it can query for any required services and load content. /// </summary> public override void Initialize() { LoadContent(); // Set all the wall and floor tiles m_tiles = new Tile[40, 40]; for (int y = 0; y < 40; y++) { for (int x = 0; x < 40; x++) { int width = m_content.TileWidth; int height = m_content.TileHeight; int x_pos = x * m_content.TileWidth; int y_pos = y * m_content.TileHeight; int random = rand.Next(10) + 1; if (random != 10) { m_tiles[x, y] = new Tile( m_content.GroundSprite, new Rectangle(x_pos, y_pos, width, height), false); } else { m_tiles[x, y] = new Tile( m_content.WallSprite, new Rectangle(x_pos, y_pos, width, height), true); } } } // Initialize the camera m_camera = new StageCamera(StageWidth, StageHeight, Global.screenWidth, Global.screenHeight); m_camera.FollowEntity(m_player); base.Initialize(); }
/// <summary> /// Returns a 3x3 matrix of tiles surrounding a bounding box. The tile /// at [1,1] is the tile the bounds are contained in. If the bounds /// specified overlaps with multiple tiles the tile containing the largest /// area of the bounds will be used as the center tile. /// </summary> /// <param name="bounds">A rectangle on the tile map. Must be smaller /// than the size of a tile.</param> /// <returns></returns> public Tile[,] GetSurroundingTiles(Rectangle bounds) { Tile[,] surroundingTiles = new Tile[3, 3]; // Determine which tiles contain each point that makes the rectangle of the specified bounds. // Ignore points that are outside of the number of tiles in the stage. Vector2[] cTiles = new Vector2[4]; int[] cAreas = new int[4]; SetTilePositionAndArea(0, cTiles, cAreas, bounds, bounds.Left, bounds.Top); SetTilePositionAndArea(1, cTiles, cAreas, bounds, bounds.Right, bounds.Top); SetTilePositionAndArea(2, cTiles, cAreas, bounds, bounds.Left, bounds.Bottom); SetTilePositionAndArea(3, cTiles, cAreas, bounds, bounds.Right, bounds.Bottom); // Determine which tile contains the most of the bounds int cIndex = 0; for (int i = 1; i < 4; i++) { if (cAreas[i] > cAreas[cIndex]) cIndex = i; } // Get each tile surrounding the tile Vector2 stp = cTiles[cIndex]; surroundingTiles[0, 0] = stp.X > 0 && stp.Y > 0 ? m_tiles[(int)stp.X - 1, (int)stp.Y - 1] : null; surroundingTiles[1, 0] = stp.Y > 0 ? m_tiles[(int)stp.X, (int)stp.Y - 1] : null; surroundingTiles[2, 0] = stp.X + 1 < NumTilesX && stp.Y > 0 ? m_tiles[(int)stp.X + 1, (int)stp.Y - 1] : null; surroundingTiles[0, 1] = stp.X > 0 ? m_tiles[(int)stp.X - 1, (int)stp.Y] : null; surroundingTiles[1, 1] = m_tiles[(int)stp.X, (int)stp.Y]; surroundingTiles[2, 1] = stp.X + 1 < NumTilesX ? m_tiles[(int)stp.X + 1, (int)stp.Y] : null; surroundingTiles[0, 2] = stp.X > 0 && stp.Y + 1 < NumTilesY ? m_tiles[(int)stp.X - 1, (int)stp.Y + 1] : null; surroundingTiles[1, 2] = stp.Y + 1 < NumTilesY ? m_tiles[(int)stp.X, (int)stp.Y + 1] : null; surroundingTiles[2, 2] = stp.X + 1 < NumTilesX && stp.Y + 1 < NumTilesY ? m_tiles[(int)stp.X + 1, (int)stp.Y + 1] : null; return surroundingTiles; }