// Copyright (c) 2013-2015 Robert Rouhani and other contributors (see CONTRIBUTORS file). // Licensed under the MIT License - https://raw.github.com/Robmaister/SharpNav/master/LICENSE using System; using System.Runtime.InteropServices; namespace SharpNav { /// /// An area groups together pieces of data through the navmesh generation process. /// [Serializable] [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct Area : IEquatable, IEquatable { /// /// The null area is one that is considered unwalkable. /// public static readonly Area Null = new Area(0); /// /// This is a default in the event that the user does not provide one. /// /// /// If a user only applies IDs to some parts of a , they will most likely choose low /// integer values. Choosing the maximum value makes it unlikely for the "default" area to collide with any /// user-defined areas. /// public static readonly Area Default = new Area(0xff); /// /// The identifier for an area, represented as a byte. /// public readonly byte Id; /// /// Initializes a new instance of the struct. /// /// An identifier for the area. public Area(byte id) { this.Id = id; } /// /// Gets a value indicating whether the area is considered walkable (not ). /// public bool IsWalkable { get { return Id != 0; } } /// /// Implicitly casts a byte to an Area. This is included since an Area is a very thin wrapper around a byte. /// /// The identifier for an area. /// An area with the specified identifier. public static implicit operator Area(byte value) { return new Area(value); } /// /// Compares two areas for equality. /// /// An . /// Another /// A value indicating whether the two s are equal. public static bool operator ==(Area left, Area right) { return left.Equals(right); } /// /// Compares two areas for inequality. /// /// An . /// Another /// A value indicating whether the two s are unequal. public static bool operator !=(Area left, Area right) { return !(left == right); } /// /// Compares this instance with another instance of for equality. /// /// An . /// A value indicating whether the two s are equal. public bool Equals(Area other) { return this.Id == other.Id; } /// /// Compares this instance with a byte representing another for equality. /// /// A byte. /// A value indicating whether this instance and the specified byte are equal. public bool Equals(byte other) { return this.Id == other; } /// /// Compares this instance with another object for equality. /// /// An object. /// A value indicating whether this instance and the specified object are equal. public override bool Equals(object obj) { var areaObj = obj as Area?; var byteObj = obj as byte?; if (areaObj.HasValue) return this.Equals(areaObj.Value); else if (byteObj.HasValue) return this.Equals(byteObj.Value); else return false; } /// /// Generates a hashcode unique to the of this instance. /// /// A hash code. public override int GetHashCode() { return Id.GetHashCode(); } /// /// Converts this instance to a human-readable string. /// /// A string representing this instance. public override string ToString() { if (Id == 0) return "Null/Unwalkable"; else return Id.ToString(); } } }