Files
stardew-access/stardew-access/Features/ReadTile.cs

125 lines
3.7 KiB
C#
Raw Normal View History

using Microsoft.Xna.Framework;
2022-01-12 16:01:29 +05:30
using StardewModdingAPI;
using StardewValley;
namespace stardew_access.Features
2022-01-12 16:01:29 +05:30
{
/// <summary>
/// Reads the name and information about a tile.
/// </summary>
2022-01-12 16:01:29 +05:30
public class ReadTile
{
private bool isBusy; // To pause execution of run method between fixed intervals
private int delay; // Length of each interval (in ms)
private bool shouldPause; // To pause the execution
private Vector2 prevTile;
2022-01-12 16:01:29 +05:30
public ReadTile()
{
isBusy = false;
delay = 100;
2022-01-12 16:01:29 +05:30
}
public void update()
{
if (this.isBusy)
return;
if (this.shouldPause)
return;
this.isBusy = true;
this.run();
Task.Delay(delay).ContinueWith(_ => { this.isBusy = false; });
}
/// <summary>
2022-05-16 21:07:08 +05:30
/// Pauses the feature for the provided time.
/// </summary>
/// <param name="time">The amount of time we want to pause the execution (in ms).<br/>Default is 2500 (2.5s).</param>
2022-05-16 21:07:08 +05:30
public void pauseUntil(int time = 2500)
{
this.shouldPause = true;
Task.Delay(time).ContinueWith(_ => { this.shouldPause = false; });
}
2022-05-16 21:07:08 +05:30
/// <summary>
/// Pauses the feature
/// </summary>
public void pause()
{
this.shouldPause = true;
}
/// <summary>
/// Resumes the feature
/// </summary>
public void resume()
{
this.shouldPause = false;
}
public void run(bool manuallyTriggered = false, bool playersPosition = false)
2022-01-12 16:01:29 +05:30
{
try
{
2022-03-19 12:54:53 +05:30
Vector2 tile;
2022-05-10 20:47:40 +05:30
2022-03-19 12:54:53 +05:30
#region Get Tile
2022-05-10 20:47:40 +05:30
int x, y;
2022-03-19 12:54:53 +05:30
if (!playersPosition)
{
// Grab tile
2022-05-11 13:14:46 +05:30
tile = CurrentPlayer.FacingTile;
2022-03-19 12:54:53 +05:30
}
else
{
// Player's standing tile
2022-05-11 13:14:46 +05:30
tile = CurrentPlayer.Position;
2022-03-19 12:54:53 +05:30
}
x = (int)tile.X;
y = (int)tile.Y;
2022-05-10 20:47:40 +05:30
#endregion
2022-01-12 16:01:29 +05:30
if (Context.IsPlayerFree)
{
if (!manuallyTriggered && prevTile != tile)
2022-01-12 16:01:29 +05:30
{
2022-04-09 15:48:13 +05:30
if (MainClass.ScreenReader != null)
MainClass.ScreenReader.PrevTextTile = " ";
2022-01-12 16:01:29 +05:30
}
var currentLocation = Game1.currentLocation;
bool isColliding = TileInfo.IsCollidingAtTile(currentLocation, x, y);
(string? name, string? category) info = TileInfo.getNameWithCategoryNameAtTile(tile, currentLocation);
2022-01-12 16:01:29 +05:30
#region Narrate toSpeak
2022-05-10 20:47:40 +05:30
if (info.name != null)
2022-04-09 15:48:13 +05:30
if (MainClass.ScreenReader != null)
if (manuallyTriggered)
2022-05-10 20:47:40 +05:30
MainClass.ScreenReader.Say($"{info.name}, Category: {info.category}", true);
else
2022-05-10 20:47:40 +05:30
MainClass.ScreenReader.SayWithTileQuery(info.name, x, y, true);
#endregion
#region Play colliding sound effect
if (isColliding && prevTile != tile)
{
Game1.playSound("colliding");
2022-01-21 15:30:16 +05:30
}
2022-01-12 16:01:29 +05:30
#endregion
2022-03-19 12:54:53 +05:30
if (!manuallyTriggered)
prevTile = tile;
2022-01-12 16:01:29 +05:30
}
}
catch (Exception e)
{
2022-03-19 12:54:53 +05:30
MainClass.ErrorLog($"Error in Read Tile:\n{e.Message}\n{e.StackTrace}");
2022-01-12 16:01:29 +05:30
}
}
}
}