Ui-Info-Suite/SDVModTest/UIElements/ShowItemEffectRanges.cs

233 lines
9.7 KiB
C#
Raw Normal View History

2017-07-20 11:51:05 +08:00
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StardewModdingAPI.Events;
using StardewValley;
2017-11-28 13:27:52 +08:00
using StardewValley.Buildings;
using StardewValley.Locations;
2017-07-20 11:51:05 +08:00
using System;
using System.Collections.Generic;
namespace UIInfoSuite.UIElements
{
class ShowItemEffectRanges : IDisposable
{
private readonly List<Point> _effectiveArea = new List<Point>();
private readonly ModConfig _modConfig;
2018-12-25 12:36:39 +08:00
private readonly IModEvents _events;
2017-07-20 11:51:05 +08:00
2017-11-28 13:27:52 +08:00
private static readonly int[][] _junimoHutArray = new int[17][]
{
new int[17] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
new int[17] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
new int[17] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
new int[17] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
new int[17] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
new int[17] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
new int[17] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
new int[17] { 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 },
new int[17] { 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 },
new int[17] { 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1 },
new int[17] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
new int[17] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
new int[17] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
new int[17] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
new int[17] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
new int[17] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
new int[17] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
};
2018-12-25 12:36:39 +08:00
public ShowItemEffectRanges(ModConfig modConfig, IModEvents events)
2017-07-20 11:51:05 +08:00
{
_modConfig = modConfig;
2018-12-25 12:36:39 +08:00
_events = events;
2017-07-20 11:51:05 +08:00
}
public void ToggleOption(bool showItemEffectRanges)
{
2018-12-25 12:36:39 +08:00
_events.Display.Rendered -= OnRendered;
_events.GameLoop.UpdateTicked -= OnUpdateTicked;
2017-07-20 11:51:05 +08:00
if (showItemEffectRanges)
{
2018-12-25 12:36:39 +08:00
_events.Display.Rendered += OnRendered;
_events.GameLoop.UpdateTicked += OnUpdateTicked;
2017-07-20 11:51:05 +08:00
}
}
public void Dispose()
{
ToggleOption(false);
}
2018-12-25 12:36:39 +08:00
/// <summary>Raised after the game state is updated (≈60 times per second).</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void OnUpdateTicked(object sender, UpdateTickedEventArgs e)
2017-07-20 11:51:05 +08:00
{
2018-12-25 12:36:39 +08:00
if (!e.IsMultipleOf(4))
return;
2017-07-20 11:51:05 +08:00
2018-12-25 12:36:39 +08:00
// check draw tile outlines
_effectiveArea.Clear();
2017-11-28 13:27:52 +08:00
if (Game1.activeClickableMenu == null &&
2017-07-20 11:51:05 +08:00
!Game1.eventUp)
{
2017-11-28 13:27:52 +08:00
if (Game1.currentLocation is BuildableGameLocation buildableLocation)
2017-07-20 11:51:05 +08:00
{
2017-11-28 13:27:52 +08:00
Building building = buildableLocation.getBuildingAt(Game1.currentCursorTile);
if (building is JunimoHut)
2017-07-20 11:51:05 +08:00
{
2017-11-28 13:27:52 +08:00
foreach (var nextBuilding in buildableLocation.buildings)
2017-07-20 11:51:05 +08:00
{
2017-11-28 13:27:52 +08:00
if (nextBuilding is JunimoHut nextHut)
2018-05-06 12:21:18 +08:00
ParseConfigToHighlightedArea(_junimoHutArray, nextHut.tileX.Value + 1, nextHut.tileY.Value + 1);
2017-07-20 11:51:05 +08:00
}
}
}
2017-11-28 13:27:52 +08:00
if (Game1.player.CurrentItem != null)
{
String name = Game1.player.CurrentItem.Name.ToLower();
Item currentItem = Game1.player.CurrentItem;
List<StardewValley.Object> objects = null;
2017-07-20 11:51:05 +08:00
2017-11-28 13:27:52 +08:00
int[][] arrayToUse = null;
2017-07-20 11:51:05 +08:00
2017-11-28 13:27:52 +08:00
if (name.Contains("arecrow"))
2017-07-20 11:51:05 +08:00
{
2017-11-28 13:27:52 +08:00
arrayToUse = new int[17][];
for (int i = 0; i < 17; ++i)
2017-07-20 11:51:05 +08:00
{
2017-11-28 13:27:52 +08:00
arrayToUse[i] = new int[17];
for (int j = 0; j < 17; ++j)
2017-07-20 11:51:05 +08:00
{
2017-11-28 13:27:52 +08:00
arrayToUse[i][j] = (Math.Abs(i - 8) + Math.Abs(j - 8) <= 12) ? 1 : 0;
2017-07-20 11:51:05 +08:00
}
2017-11-28 13:27:52 +08:00
}
ParseConfigToHighlightedArea(arrayToUse, TileUnderMouseX, TileUnderMouseY);
objects = GetObjectsInLocationOfSimilarName("arecrow");
if (objects != null)
{
foreach (StardewValley.Object next in objects)
2017-07-20 11:51:05 +08:00
{
2017-11-28 13:27:52 +08:00
ParseConfigToHighlightedArea(arrayToUse, (int)next.TileLocation.X, (int)next.TileLocation.Y);
2017-07-20 11:51:05 +08:00
}
2017-11-28 13:27:52 +08:00
}
}
else if (name.Contains("sprinkler"))
{
if (name.Contains("iridium"))
{
arrayToUse = _modConfig.IridiumSprinkler;
}
else if (name.Contains("quality"))
{
arrayToUse = _modConfig.QualitySprinkler;
}
else
{
arrayToUse = _modConfig.Sprinkler;
}
if (arrayToUse != null)
ParseConfigToHighlightedArea(arrayToUse, TileUnderMouseX, TileUnderMouseY);
objects = GetObjectsInLocationOfSimilarName("sprinkler");
if (objects != null)
{
foreach (StardewValley.Object next in objects)
2017-07-20 11:51:05 +08:00
{
2017-11-28 13:27:52 +08:00
string objectName = next.name.ToLower();
if (objectName.Contains("iridium"))
{
arrayToUse = _modConfig.IridiumSprinkler;
}
else if (objectName.Contains("quality"))
{
arrayToUse = _modConfig.QualitySprinkler;
}
else
{
arrayToUse = _modConfig.Sprinkler;
}
if (arrayToUse != null)
ParseConfigToHighlightedArea(arrayToUse, (int)next.TileLocation.X, (int)next.TileLocation.Y);
2017-07-20 11:51:05 +08:00
}
}
}
2017-11-28 13:27:52 +08:00
else if (name.Contains("bee house"))
{
ParseConfigToHighlightedArea(_modConfig.Beehouse, TileUnderMouseX, TileUnderMouseY);
}
2017-07-20 11:51:05 +08:00
2017-11-28 13:27:52 +08:00
}
2017-07-20 11:51:05 +08:00
}
}
2018-12-25 12:36:39 +08:00
/// <summary>Raised after the game draws to the sprite patch in a draw tick, just before the final sprite batch is rendered to the screen.</summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
private void OnRendered(object sender, RenderedEventArgs e)
2017-07-20 11:51:05 +08:00
{
2018-12-25 12:36:39 +08:00
// draw tile outlines
2017-07-20 11:51:05 +08:00
foreach (Point point in _effectiveArea)
Game1.spriteBatch.Draw(
Game1.mouseCursors,
Game1.GlobalToLocal(new Vector2(point.X * Game1.tileSize, point.Y * Game1.tileSize)),
new Rectangle(194, 388, 16, 16),
Color.White * 0.7f,
0.0f,
Vector2.Zero,
Game1.pixelZoom,
SpriteEffects.None,
0.01f);
}
private void ParseConfigToHighlightedArea(int[][] highlightedLocation, int xPos, int yPos)
{
int xOffset = highlightedLocation.Length / 2;
for (int i = 0; i < highlightedLocation.Length; ++i)
{
int yOffset = highlightedLocation[i].Length / 2;
for (int j = 0; j < highlightedLocation[i].Length; ++j)
{
if (highlightedLocation[i][j] == 1)
_effectiveArea.Add(new Point(xPos + i - xOffset, yPos + j - yOffset));
}
}
}
private int TileUnderMouseX
{
get { return (Game1.getMouseX() + Game1.viewport.X) / Game1.tileSize; }
}
private int TileUnderMouseY
{
get { return (Game1.getMouseY() + Game1.viewport.Y) / Game1.tileSize; }
}
private List<StardewValley.Object> GetObjectsInLocationOfSimilarName(String nameContains)
{
List<StardewValley.Object> result = new List<StardewValley.Object>();
if (!String.IsNullOrEmpty(nameContains))
{
nameContains = nameContains.ToLower();
var objects = Game1.currentLocation.Objects;
foreach (var nextThing in objects.Values)
{
if (nextThing.name.ToLower().Contains(nameContains))
result.Add(nextThing);
}
}
return result;
}
}
}