diff options
| author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-09-08 14:02:08 -0400 |
|---|---|---|
| committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-09-08 14:02:08 -0400 |
| commit | af1a2bde8219c5d4b8660b13702725626a4a5647 (patch) | |
| tree | a18bf2227299276604124b6a243ccda84d0413a2 /src/StardewModdingAPI/Utilities | |
| parent | 8df1ab7e1102dedf744e799af2cede560893c92a (diff) | |
| parent | 23951220ae84f3132832c942b61a8e81aee1fbfe (diff) | |
| download | SMAPI-af1a2bde8219c5d4b8660b13702725626a4a5647.tar.gz SMAPI-af1a2bde8219c5d4b8660b13702725626a4a5647.tar.bz2 SMAPI-af1a2bde8219c5d4b8660b13702725626a4a5647.zip | |
Merge branch 'develop' into stable
Diffstat (limited to 'src/StardewModdingAPI/Utilities')
| -rw-r--r-- | src/StardewModdingAPI/Utilities/SButton.cs | 28 | ||||
| -rw-r--r-- | src/StardewModdingAPI/Utilities/SDate.cs | 58 |
2 files changed, 47 insertions, 39 deletions
diff --git a/src/StardewModdingAPI/Utilities/SButton.cs b/src/StardewModdingAPI/Utilities/SButton.cs index c4833b0b..33058a64 100644 --- a/src/StardewModdingAPI/Utilities/SButton.cs +++ b/src/StardewModdingAPI/Utilities/SButton.cs @@ -1,5 +1,6 @@ -using System; +using System; using Microsoft.Xna.Framework.Input; +using StardewValley; namespace StardewModdingAPI.Utilities { @@ -655,5 +656,30 @@ namespace StardewModdingAPI.Utilities button = 0; return false; } + + /// <summary>Get the <see cref="InputButton"/> equivalent for the given button.</summary> + /// <param name="input">The button to convert.</param> + /// <param name="button">The Stardew Valley input button equivalent.</param> + /// <returns>Returns whether the value was converted successfully.</returns> + public static bool TryGetStardewInput(this SButton input, out InputButton button) + { + // keyboard + if (input.TryGetKeyboard(out Keys key)) + { + button = new InputButton(key); + return true; + } + + // mouse + if (input == SButton.MouseLeft || input == SButton.MouseRight) + { + button = new InputButton(mouseLeft: input == SButton.MouseLeft); + return true; + } + + // not valid + button = default(InputButton); + return false; + } } } diff --git a/src/StardewModdingAPI/Utilities/SDate.cs b/src/StardewModdingAPI/Utilities/SDate.cs index d7631598..5073259d 100644 --- a/src/StardewModdingAPI/Utilities/SDate.cs +++ b/src/StardewModdingAPI/Utilities/SDate.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; using StardewValley; @@ -86,34 +86,27 @@ namespace StardewModdingAPI.Utilities /// <exception cref="ArithmeticException">The offset would result in an invalid date (like year 0).</exception> public SDate AddDays(int offset) { - // simple case - int day = this.Day + offset; - string season = this.Season; - int year = this.Year; + // get new hash code + int hashCode = this.GetHashCode() + offset; + if (hashCode < 1) + throw new ArithmeticException($"Adding {offset} days to {this} would result in a date before 01 spring Y1."); - // handle season transition - if (day > this.DaysInSeason || day < 1) - { - // get season index - int curSeasonIndex = this.GetSeasonIndex(); - - // get season offset - int seasonOffset = day / this.DaysInSeason; - if (day < 1) - seasonOffset -= 1; - - // get new date - day = this.GetWrappedIndex(day, this.DaysInSeason); - season = this.Seasons[this.GetWrappedIndex(curSeasonIndex + seasonOffset, this.Seasons.Length)]; - year += seasonOffset / this.Seasons.Length; - } + // get day + int day = hashCode % 28; + if (day == 0) + day = 28; - // validate - if (year < 1) - throw new ArithmeticException($"Adding {offset} days to {this} would result in invalid date {day:00} {season} {year}."); + // get season index + int seasonIndex = hashCode / 28; + if (seasonIndex > 0 && hashCode % 28 == 0) + seasonIndex -= 1; + seasonIndex %= 4; - // return new date - return new SDate(day, season, year); + // get year + int year = hashCode / (this.Seasons.Length * this.DaysInSeason) + 1; + + // create date + return new SDate(day, this.Seasons[seasonIndex], year); } /// <summary>Get a string representation of the date. This is mainly intended for debugging or console messages.</summary> @@ -142,7 +135,7 @@ namespace StardewModdingAPI.Utilities /// <summary>Get a hash code which uniquely identifies a date.</summary> public override int GetHashCode() { - // return the number of days since 01 spring Y1 + // return the number of days since 01 spring Y1 (inclusively) int yearIndex = this.Year - 1; return yearIndex * this.DaysInSeason * this.SeasonsInYear @@ -239,16 +232,5 @@ namespace StardewModdingAPI.Utilities throw new InvalidOperationException($"The current season '{this.Season}' wasn't recognised."); return index; } - - /// <summary>Get the real index in an array which should be treated as a two-way loop.</summary> - /// <param name="index">The index in the looped array.</param> - /// <param name="length">The number of elements in the array.</param> - private int GetWrappedIndex(int index, int length) - { - int wrapped = index % length; - if (wrapped < 0) - wrapped += length; - return wrapped; - } } } |
