using System; using System.Collections.Generic; using System.Linq; namespace Nibriboard.RippleSpace { /// /// Represents a line drawn across the plane. /// public class DrawnLine { /// /// The id of line that this is part of. /// Note that this id may not be unique - several lines that were all /// drawn at once may also have the same id. This is such that a single /// line that was split across multiple chunks can still be referenced. /// public readonly Guid LineId; /// /// The width of the line. /// public int LineWidth; /// /// The colour of the line. /// public string Colour; /// /// The points that represent the line. /// public List Points = new List(); /// /// Whether this line spans multiple chunks. /// public bool SpansMultipleChunks { get { // TODO: Make this more intelligent such that connecting lines // can be stored that connect lines that span multiple chunks if (Points.Count == 0) return false; ChunkReference containingChunk = Points[0].ContainingChunk; foreach(LocationReference point in Points) { if (point.ContainingChunk != containingChunk) return true; } return false; } } /// /// Gets a reference in chunk-space ot the chunk that this line starts in. /// public ChunkReference ContainingChunk { get { if (Points.Count == 0) throw new InvalidOperationException("Error: This line doesn't contain any points yet!"); return Points[0].ContainingChunk; } } public DrawnLine() : this(Guid.NewGuid()) { } protected DrawnLine(Guid inLineId) { LineId = inLineId; } public List SplitOnChunks(int chunkSize) { List results = new List(); // Don't bother splitting the line up if it all falls in the same chunk if (!SpansMultipleChunks) { results.Add(this); return results; } DrawnLine nextLine = new DrawnLine(); ChunkReference currentChunk = null; foreach(LocationReference point in Points) { if(currentChunk != null && point.ContainingChunk != currentChunk) { // We're heading into a new chunk! Split the line up here. // TODO: Add connecting lines to each DrawnLine instance to prevent gaps results.Add(nextLine); nextLine = new DrawnLine(LineId); } nextLine.Points.Add(point); } return results; } } }