add IEquatable<SDate> interface to SDate (#307)

This commit is contained in:
Jesse Plamondon-Willard 2017-06-18 20:55:12 -04:00
parent 0a8c07cc07
commit 3e50c90230
1 changed files with 35 additions and 26 deletions

View File

@ -5,7 +5,7 @@ using StardewValley;
namespace StardewModdingAPI.Utilities namespace StardewModdingAPI.Utilities
{ {
/// <summary>Represents a Stardew Valley date.</summary> /// <summary>Represents a Stardew Valley date.</summary>
public class SDate public class SDate : IEquatable<SDate>
{ {
/********* /*********
** Properties ** Properties
@ -66,6 +66,12 @@ namespace StardewModdingAPI.Utilities
this.Year = year; this.Year = year;
} }
/// <summary>Get the current in-game date.</summary>
public static SDate Now()
{
return new SDate(Game1.dayOfMonth, Game1.currentSeason, Game1.year);
}
/// <summary>Get a new date with the given number of days added.</summary> /// <summary>Get a new date with the given number of days added.</summary>
/// <param name="offset">The number of days to add.</param> /// <param name="offset">The number of days to add.</param>
/// <returns>Returns the resulting date.</returns> /// <returns>Returns the resulting date.</returns>
@ -108,15 +114,37 @@ namespace StardewModdingAPI.Utilities
return $"{this.Day:00} {this.Season} Y{this.Year}"; return $"{this.Day:00} {this.Season} Y{this.Year}";
} }
/// <summary>Get the current in-game date.</summary> /****
public static SDate Now() ** IEquatable
****/
/// <summary>Get whether this instance is equal to another.</summary>
/// <param name="other">The other value to compare.</param>
public bool Equals(SDate other)
{ {
return new SDate(Game1.dayOfMonth, Game1.currentSeason, Game1.year); return this == other;
} }
/********* /// <summary>Get whether this instance is equal to another.</summary>
** Operator methods /// <param name="obj">The other value to compare.</param>
*********/ public override bool Equals(object obj)
{
return obj is SDate other && this == other;
}
/// <summary>Get a hash code which uniquely identifies a date.</summary>
public override int GetHashCode()
{
// return the number of days since 01 spring Y1
int yearIndex = this.Year - 1;
return
yearIndex * this.DaysInSeason * this.SeasonsInYear
+ this.GetSeasonIndex() * this.DaysInSeason
+ this.Day;
}
/****
** Operators
****/
/// <summary>Get whether one date is equal to another.</summary> /// <summary>Get whether one date is equal to another.</summary>
/// <param name="date">The base date to compare.</param> /// <param name="date">The base date to compare.</param>
/// <param name="other">The other date to compare.</param> /// <param name="other">The other date to compare.</param>
@ -166,25 +194,6 @@ namespace StardewModdingAPI.Utilities
return date?.GetHashCode() < other?.GetHashCode(); return date?.GetHashCode() < other?.GetHashCode();
} }
/// <summary>Overrides the equals function.</summary>
/// <param name="obj">Object being compared.</param>
/// <returns>The equalaity of the object.</returns>
public override bool Equals(object obj)
{
return obj is SDate other && this == other;
}
/// <summary>Get a hash code which uniquely identifies a date.</summary>
public override int GetHashCode()
{
// return the number of days since 01 spring Y1
int yearIndex = this.Year - 1;
return
yearIndex * this.DaysInSeason * this.SeasonsInYear
+ this.GetSeasonIndex() * this.DaysInSeason
+ this.Day;
}
/********* /*********
** Private methods ** Private methods