/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { camera = new Camera(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height); int[,] grid = new int[,] { { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0 }, { 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0 }, { 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0 }, { 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0 }, { 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0 }, { 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0 }, { 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0 }, { 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, { 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0 }, { 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 } }; Model.Terrain[,] data = new Model.Terrain[15, 20]; for (int j = 0; j < 15; j++) { for (int i = 0; i < 20; i++) { switch (grid[j, i]) { case 0: data[j, i] = Model.Terrain.Wall; break; case 1: data[j, i] = Model.Terrain.Floor; break; } } } tileMap = new AutoTileMap(); tileMap.Create(grid); tileMap.SetTexture(Content.Load<Texture2D>("Sprites\\Terrain\\sprAutoTileSequence"), 32, 32); base.Initialize(); }
public void Draw(SpriteBatch spriteBatch, Camera camera) { if (texture == null || subRects == null || tiles == null) { return; } /* Here, we're taking the rotation of the camera and creating a bounding box over the rotated frame. That bounding box will determine which tiles to draw. */ // Get the four corners of the camera Vector3[] points = new Vector3[4] { new Vector3(camera.Left, camera.Top, 0.0F), new Vector3(camera.Right, camera.Top, 0.0F), new Vector3(camera.Left, camera.Bottom, 0.0F), new Vector3(camera.Right, camera.Bottom, 0.0F) }; Point topLeft = new Point((int)points[0].X, (int)points[0].Y); Point bottomRight = new Point((int)points[0].X, (int)points[0].Y); Matrix xform = Matrix.CreateRotationZ(camera.Rotation); // Transform the camera foreach (Vector3 point in points) {//int i = 0; i < points.Length; i++) { Vector3 xformed = Vector3.Transform(point, xform); // Get the maximum and minimum coordinates in the transformation if (xformed.X < topLeft.X) { topLeft.X = (int)Math.Floor(xformed.X); } else if (xformed.X > bottomRight.X) { bottomRight.X = (int)Math.Ceiling(xformed.X); } if (xformed.Y < topLeft.Y) { topLeft.Y = (int)Math.Floor(xformed.Y); } else if (xformed.Y > bottomRight.Y) { bottomRight.Y = (int)Math.Ceiling(xformed.Y); } } // Convert our extrema from pixels to tiles topLeft.X /= TileWidth; topLeft.Y /= TileHeight; bottomRight.X = (int)Math.Ceiling(bottomRight.X / (float)TileWidth); bottomRight.Y = (int)Math.Ceiling(bottomRight.Y / (float)TileHeight); // Draw the tiles int tile; for (int j = topLeft.Y; j < bottomRight.Y; j++) { for (int i = topLeft.X; i < bottomRight.X; i++) { if (i < 0 || i >= Width || j < 0 || j >= Height) { continue; } tile = tiles[j, i]; spriteBatch.Draw(texture, new Vector2(i * TileWidth, j * TileHeight), subRects[tile], Color.White); } } }
public void Draw(SpriteBatch spriteBatch, Camera camera) { tileMap.Draw(spriteBatch, camera); }