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

224 lines
9.1 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;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UIInfoSuite.UIElements
{
class ShowItemEffectRanges : IDisposable
{
private readonly List<Point> _effectiveArea = new List<Point>();
private readonly ModConfig _modConfig;
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 }
};
2017-07-20 11:51:05 +08:00
public ShowItemEffectRanges(ModConfig modConfig)
{
_modConfig = modConfig;
}
public void ToggleOption(bool showItemEffectRanges)
{
GraphicsEvents.OnPostRenderEvent -= DrawTileOutlines;
GameEvents.FourthUpdateTick -= CheckDrawTileOutlines;
if (showItemEffectRanges)
{
GraphicsEvents.OnPostRenderEvent += DrawTileOutlines;
GameEvents.FourthUpdateTick += CheckDrawTileOutlines;
}
}
public void Dispose()
{
ToggleOption(false);
}
private void CheckDrawTileOutlines(object sender, EventArgs e)
{
_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
}
}
private void DrawTileOutlines(object sender, EventArgs e)
{
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;
}
}
}