/// <summary> /// Draw the level /// </summary> /// <param name="elapsedTime">The time between this and the last frame</param> public void Draw(float elapsedTime) { if (Paused) { // TODO: Draw "Paused" Overlay } // Draw level Viewport viewport = game.GraphicsDevice.Viewport; basicEffect.World = Matrix.CreateScale(2, 2, 1); basicEffect.View = Matrix.CreateTranslation(new Vector3(0, scrollDistance, 0)); basicEffect.Projection = Matrix.CreateOrthographicOffCenter(0, viewport.Width, viewport.Height, 0, 0, -1); spriteBatch.Begin(0, null, SamplerState.LinearClamp, null, null, basicEffect); for (int i = 0; i < CurrentMap.LayerCount; i++) { // To minimize drawn tiles, we limit ourselves to those onscreen int miny = (int)((-scrollDistance - 2 * CurrentMap.Layers[i].ScrollOffset) / (CurrentMap.TileHeight * 2)); int maxy = miny + 15; // And those that exist if (miny < 0) { miny = 0; } if (maxy > CurrentMap.Height) { maxy = CurrentMap.Height; } for (int y = miny; y < maxy; y++) { // Since our maps are only as wide as our rendering area, // no need for optimizaiton here for (int x = 0; x < CurrentMap.Width; x++) { int index = x + y * CurrentMap.Width; TileData tileData = CurrentMap.Layers[i].TileData[index]; if (tileData.TileID != 0) { Tile tile = CurrentMap.Tiles[tileData.TileID - 1]; Rectangle onScreen = new Rectangle( x * CurrentMap.TileWidth, (int)(y * CurrentMap.TileHeight + CurrentMap.Layers[i].ScrollOffset), CurrentMap.TileWidth, CurrentMap.TileHeight); spriteBatch.Draw(CurrentMap.Textures[tile.TextureID], onScreen, tile.Source, Color.White, 0f, new Vector2(CurrentMap.TileWidth / 2, CurrentMap.TileHeight / 2), tileData.SpriteEffects, CurrentMap.Layers[i].LayerDepth); } } } } // Draw only the game objects that appear within our scrolling region Rectangle bounds = new Rectangle(0, (int)(-scrollDistance / 2), CurrentMap.Width * CurrentMap.TileWidth, 16 * CurrentMap.TileHeight); foreach (uint goID in ScrollingShooterGame.GameObjectManager.QueryRegion(bounds)) { GameObject go = ScrollingShooterGame.GameObjectManager.GetObject(goID); go.Draw(elapsedTime, spriteBatch); } spriteBatch.End(); }