using System; using System.Configuration; using System.IO; namespace Nibriboard.RippleSpace { /// /// Represents a location in absolute plane-space. /// public class LocationReference : Reference { /// /// The chunk that this location reference fall inside. /// public ChunkReference ContainingChunk { get { if(Plane == null) return null; return new ChunkReference( Plane, X / Plane.ChunkSize, Y / Plane.ChunkSize ); } } public LocationReference(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 LocationReference() : base() { } public override bool Equals(object obj) { LocationReference otherLocationReference = obj as LocationReference; if (otherLocationReference == null) return false; if(X == otherLocationReference.X && Y == otherLocationReference.Y && Plane == otherLocationReference.Plane) { return true; } return false; } public override int GetHashCode() { return $"({Plane.Name})+{X}+{Y}".GetHashCode(); } public override string ToString() { return $"LocationReference: {base.ToString()}"; } public static LocationReference Parse(Plane plane, string source) { // TODO: Decide if this is the format that we want to use for location references if (!source.StartsWith("LocationReference:")) throw new InvalidDataException($"Error: That isn't a valid location reference. Location references start with 'ChunkReference:'."); // Trim the extras off the reference source = source.Substring("LocationReference:".Length); source = source.Trim("() \v\t\r\n".ToCharArray()); int x = int.Parse(source.Substring(0, source.IndexOf(","))); int y = int.Parse(source.Substring(source.IndexOf(",") + 1)); return new LocationReference( plane, x, y ); } /// /// Returns a clone of this LocationReference. /// /// The newly-cloned instance. public override object Clone() { return new LocationReference(Plane, X, Y); } } }