using System; using System.IO; using SBRL.Utilities; namespace Nibriboard.RippleSpace { /// /// References the location of a chunk. /// /// /// Defaults to chunk-space, but absolute plane-space can also be calculated /// and obtained (A /// is returned). /// public class ChunkReference : Reference { /// /// The region size to use. /// Regions are used when saving and loading to avoid too many files being stored in a /// single directory. /// public static int RegionSize = 32; /// /// Converts this ChunkReference into a RegionReference. /// A RegionReference is used when saving, to work out which folder it should go in. /// The size of the regions used is determined by the property. /// public ChunkReference RegionReference { get { return new ChunkReference( Plane, (int)Math.Floor((float)X / (float)RegionSize), (int)Math.Floor((float)Y / (float)RegionSize) ); } } public ChunkReference(Plane inPlane, int inX, int inY) : base(inPlane, inX, inY) { } /// /// Creates a new blank . /// Don't use this yourself! This is only for Newtonsoft.Json to use when deserialising references. /// public ChunkReference() : base() { } /// /// Converts this chunk-space reference to plane-space. /// /// This chunk-space reference in plane-space. public LocationReference InPlanespace() { return new LocationReference( Plane, X * Plane.ChunkSize, Y * Plane.ChunkSize ); } /// /// Returns a rectangle representing the area of the chunk that this ChunkReference references. /// /// A Rectangle representing this ChunkReference's chunk's area. public Rectangle InPlanespaceRectangle() { return new Rectangle( X * Plane.ChunkSize, Y * Plane.ChunkSize, (X * Plane.ChunkSize) + Plane.ChunkSize, (Y * Plane.ChunkSize) + Plane.ChunkSize ); } public string AsFilepath() { return Path.Combine($"Region_{RegionReference.X},{RegionReference.Y}", $"{X},{Y}.chunk"); } public override int GetHashCode() { return $"({Plane.Name})+{X}+{Y}".GetHashCode(); } public override bool Equals(object obj) { ChunkReference otherChunkReference = obj as ChunkReference; if (otherChunkReference == null) return false; if (X == otherChunkReference.X && Y == otherChunkReference.Y && Plane == otherChunkReference.Plane) { return true; } return false; } public override string ToString() { return $"ChunkReference: {base.ToString()}"; } /// /// Returns a clone of this ChunkReference. /// /// The newly-cloned instance. public override object Clone() { return new ChunkReference(Plane, X, Y); } } }