using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using V3.Objects; namespace V3.Map { // ReSharper disable once ClassNeverInstantiated.Global public sealed class FogOfWar { private const int FogRange = 64; private const int SightRadius = 1000; private Point mMapSize; private Texture2D mFog; private readonly List mFogRectangle = new List(); /// /// Get the size of the map and create an boolean array /// /// the size of the map public void LoadGrid(Point size) { mMapSize = size; CreateArray(); } /// /// An array so save whether the sprites already walked on this area /// private void CreateArray() { for (int i = -FogRange; i < mMapSize.Y; i += FogRange) { for (int j = -FogRange * 2; j < mMapSize.X; j += FogRange) { mFogRectangle.Add(new Rectangle(j, i, mFog.Width, mFog.Height)); } } } /// /// The position from creatures which can open the fog /// /// creatures which are able to open the fog public void Update(ICreature creature) { Ellipse creatureEllipse = new Ellipse(creature.Position, SightRadius, SightRadius); var markedForDeletion = new List(); foreach (var fog in mFogRectangle) { if (!creature.IsDead && creatureEllipse.Contains(fog.Center.ToVector2())) { markedForDeletion.Add(fog); } } foreach (var fogToDelete in markedForDeletion) { mFogRectangle.Remove(fogToDelete); } } /// /// The sprite for the fog /// /// public void LoadContent(ContentManager content) { mFog = content.Load("Sprites/cloud"); } /// /// Try to draw fog of war efficiently. /// /// Sprite batch used. public void DrawFog(SpriteBatch spriteBatch) { /* var screen = camera.ScreenRectangle; int fogPerRow = (mMapSize.X + FogRange) / FogRange; int fogPerColumn = (mMapSize.Y + FogRange) / FogRange; for (int i = screen.Y / FogRange; i < (screen.Y + screen.Height) / FogRange; i++) { for (int j = screen.X / FogRange; j < (screen.X + screen.Width) / FogRange; j++) { spriteBatch.Draw(mFog, mFogRectangle[i * fogPerRow], Color.Black); } } */ foreach (var fog in mFogRectangle) { spriteBatch.Draw(mFog, fog, Color.Black); } } public void SetFog(List fog) { mFogRectangle.Clear(); mFogRectangle.AddRange(fog); } public List GetFog() { return mFogRectangle; } } }