Skip to content

Commit

Permalink
make tent hold time configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
Pathoschild committed Jun 14, 2021
1 parent 2c17e44 commit 07b6b9f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 deletions.
12 changes: 12 additions & 0 deletions SleepyEye/Framework/ModConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace SleepyEye.Framework
{
/// <summary>The mod configuration.</summary>
internal class ModConfig
{
/*********
** Accessors
*********/
/// <summary>The number of seconds until the tent tool should trigger a save.</summary>
public int SecondsUntilSave { get; set; } = 7;
}
}
46 changes: 44 additions & 2 deletions SleepyEye/Mod.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,54 @@
using System;
using SleepyEye.Framework;
using SpaceShared;
using SpaceShared.APIs;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley.Menus;

namespace SleepyEye
{
/// <summary>The mod entry point.</summary>
internal class Mod : StardewModdingAPI.Mod
{
/*********
** Accessors
*********/
/// <summary>The static mod instance.</summary>
public static Mod Instance;

/// <inheritdoc />
public override void Entry(IModHelper helper)
{
// load config
this.ApplyConfig(helper.ReadConfig<ModConfig>());

// init
Mod.Instance = this;
Log.Monitor = this.Monitor;

helper.Events.Display.MenuChanged += this.OnMenuChanged;
helper.Events.GameLoop.GameLaunched += this.OnGameLaunched;
}

/// <summary>Raised after the game is launched, right before the first update tick. This happens once per game session (unrelated to loading saves). All mods are loaded and initialised at this point, so this is a good time to set up mod integrations.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void OnGameLaunched(object sender, GameLaunchedEventArgs e)
{
var gmcm = this.Helper.ModRegistry.GetApi<IGenericModConfigMenuApi>("spacechase0.GenericModConfigMenu");
if (gmcm != null)
{
gmcm.RegisterModConfig(this.ModManifest, revertToDefault: () => this.ApplyConfig(new ModConfig()), saveToFile: this.SaveConfig);
gmcm.RegisterSimpleOption(this.ModManifest, "Seconds until save", "The number of seconds until the tent tool should trigger a save.", () => (int)TentTool.UseDelay.TotalSeconds, (int val) => TentTool.UseDelay = TimeSpan.FromSeconds(val));
}
}

/// <summary>Raised after a game menu is opened, closed, or replaced.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void OnMenuChanged(object sender, MenuChangedEventArgs e)
{
if (!(e.NewMenu is ShopMenu menu) || menu.portraitPerson.Name != "Pierre")
if (e.NewMenu is not ShopMenu menu || menu.portraitPerson.Name != "Pierre")
return;

Log.Debug("Adding tent to shop");
Expand All @@ -34,5 +60,21 @@ private void OnMenuChanged(object sender, MenuChangedEventArgs e)
forSale.Add(item);
itemPriceAndStock.Add(item, new[] { item.salePrice(), item.Stack });
}

/// <summary>Apply the given mod configuration.</summary>
/// <param name="config">The configuration model.</param>
private void ApplyConfig(ModConfig config)
{
TentTool.UseDelay = TimeSpan.FromSeconds(config.SecondsUntilSave);
}

/// <summary>Save the current mod configuration.</summary>
private void SaveConfig()
{
this.Helper.WriteConfig(new ModConfig
{
SecondsUntilSave = (int)TentTool.UseDelay.TotalSeconds
});
}
}
}
16 changes: 10 additions & 6 deletions SleepyEye/TentTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ public class TentTool : Tool, ISaveElement
/// <summary>Whether the tent triggered an ongoing save.</summary>
private bool IsSaving => this.StartedSaving != null;

/// <summary>How long the tent must be used before a save is triggered.</summary>
private readonly TimeSpan UseDelay = TimeSpan.FromSeconds(7);

/// <summary>How long after a save is triggered before resetting the tool's use and save flags.</summary>
private readonly TimeSpan ResetDelay = TimeSpan.FromSeconds(3);


/*********
** Accessors
*********/
/// <summary>How long the tent must be used before a save is triggered.</summary>
internal static TimeSpan UseDelay { get; set; }


/*********
** Public methods
*********/
Expand Down Expand Up @@ -116,7 +120,7 @@ public override void tickUpdate(GameTime time, SFarmer who)

// save if done
TimeSpan useTime = this.GetTimeSince(this.StartedUsing!.Value);
if (useTime > this.UseDelay)
if (useTime > TentTool.UseDelay)
{
this.CancelUse(who);

Expand All @@ -141,10 +145,10 @@ public override void draw(SpriteBatch b)
{
// get transparency
Color color = Color.White;
if (!this.IsSaving && this.IsUsing && this.GetTimeSince(this.StartedUsing!.Value) < this.UseDelay)
if (!this.IsSaving && this.IsUsing && this.GetTimeSince(this.StartedUsing!.Value) < TentTool.UseDelay)
{
var useTime = this.GetTimeSince(this.StartedUsing!.Value);
color *= (0.2f + (float)useTime.TotalSeconds / 7f * 0.6f);
color *= (0.2f + (float)useTime.TotalSeconds / (float)TentTool.UseDelay.TotalSeconds * 0.6f);
}

// draw
Expand Down
3 changes: 2 additions & 1 deletion SleepyEye/docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
# Release notes
## Upcoming release
* Updated for Stardew Valley 1.5.
* Holding the tent button now triggers a save after seven seconds without waiting for you to release the button.
* The time you need to hold the tent button is now configurable.
* Holding the tent button now triggers a save after the delay without waiting for you to release the button.
* Fixed compatibility with [unofficial 64-bit mode](https://stardewvalleywiki.com/Modding:Migrate_to_64-bit_on_Windows).
* Improved documentation.
* Internal refactoring.
Expand Down

0 comments on commit 07b6b9f

Please sign in to comment.