public void Draw(SpriteBatch spriteBatch, Camera camera) { Point min = ConvertPositionToCell(camera.position); Point max = ConvertPositionToCell(camera.position + new Vector2( spriteBatch.GraphicsDevice.Viewport.Width + TileWidth - 1, spriteBatch.GraphicsDevice.Viewport.Height + TileHeight - 1)); foreach (MapLayer layer in Layers) layer.Draw(spriteBatch, camera, min, max); }
public void Draw(SpriteBatch batch, Camera camera, Point min, Point max) { batch.Begin(SpriteSortMode.Texture, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, null, camera.TransformMatrix); min.X = (int)Math.Max(min.X, 0); min.Y = (int)Math.Max(min.Y, 0); max.X = (int)Math.Min(max.X, Width); max.Y = (int)Math.Min(max.Y, Width); for (int x = min.X; x < max.X; x++) { for (int y = min.Y; y < max.Y; y++) { int textureIndex = map[y, x]; Texture2D texture = textures[0]; Rectangle source = new Rectangle(0,0,16,16); if (textureIndex == -1) // It's empty. Is there anything interesting near it? { //Directions named from where the tile is coming. bool up = false, down = false, left =false, right = false; bool dl = false, dr = false, ul = false, ur = false; //Work out where there IS a tile on this layer if (y > 0 && GetCellIndex(x, y - 1) == 0) down = true; if (y < Height && GetCellIndex(x, y + 1) == 0) up = true; if (x > 0 && GetCellIndex(x - 1, y) == 0) left = true; if (x < Width && GetCellIndex(x + 1, y) == 0) right = true; if (x > 0 && y > 0 && GetCellIndex(x - 1, y - 1) == 0) ul = true; if (x > 0 && y < Height && GetCellIndex(x - 1, y + 1) == 0) dl = true; if (x < Width && y > 0 && GetCellIndex(x + 1, y - 1) == 0) ur = true; if (x < Width && y < Width && GetCellIndex(x + 1, y + 1) == 0) dr = true; //If no tiles need to be drawn around this one, then skip testing for drawing. if (!down && !up && !left && !right && !ul && !dl && !ur && !dr) continue; //Corners if (dl && !up && !left) fringeDraw(batch, x, y, 80, 0); if (dr && !up && !right) fringeDraw(batch, x, y, 48, 0); if (ur && !down && !right) fringeDraw(batch, x, y, 48, 32); if (ul && !down && !left) fringeDraw(batch, x, y, 80, 32); //One side if (down && !left && !up && !right) fringeDraw(batch, x, y, 64, 32); else if (!down && left && !up && !right) fringeDraw(batch, x, y, 80, 16); else if (!down && !left && up && !right) fringeDraw(batch, x, y, 64, 0); else if (!down && !left && !up && right) fringeDraw(batch, x, y, 48, 16); //Two sides (corner) else if (down && left && !up && !right) fringeDraw(batch, x, y, 0, 16); else if (down && !left && !up && right) fringeDraw(batch, x, y, 16, 16); else if (!down && left && up && !right) fringeDraw(batch, x, y, 0, 32); else if (!down && !left && up && right) fringeDraw(batch, x, y, 16, 32); // Two sides (opposite) else if (down && !left && up && !right) { fringeDraw(batch, x, y, 64, 0); fringeDraw(batch, x, y, 64, 32); } else if (!down && left && !up && right) { fringeDraw(batch, x, y, 48, 16); fringeDraw(batch, x, y, 80, 16); } //Three sides else if (down && left && right && !up) fringeDraw(batch, x, y, 32, 16); else if (!down && left && right && up) fringeDraw(batch, x, y, 32, 32); else if (down && left && !right && up) fringeDraw(batch, x, y, 16, 0); else if (down && !left && right && up) fringeDraw(batch, x, y, 32, 0); //Four sides else if (down && left && right && up) fringeDraw(batch, x, y, 64, 16); } else //It's a 0 and hence is actually a tile we need to draw fully batch.Draw( texture, new Rectangle( x * Map.TileWidth, y * Map.TileHeight, Map.TileWidth, Map.TileHeight), source, Color.White ); } } batch.End(); }