Skip to content

Commit

Permalink
Added Harvestable Product show
Browse files Browse the repository at this point in the history
  • Loading branch information
nofilenamed committed May 5, 2021
1 parent d698b16 commit cd86e37
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 30 deletions.
Binary file added Capture.Heart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Capture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Milk.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 44 additions & 5 deletions ModConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,22 @@ internal class ModConfig
{
public bool PetToo { get; set; } = true;

public KeySetting Keys { get; set; } = new KeySetting();
public bool ShowIsHarvestable { get; set; } = true;

public byte ShowIsHarvestableTime { get; set; } = 5;

public KeySetting Keys { get; set; } = new KeySetting();

public EntitiesSetting Entities { get; set; } = new EntitiesSetting();

public OffsetSetting Offsets { get; set; } = new OffsetSetting();
}

internal class EntitiesSetting
{
public EntitiesConfig Bubble { get; set; } = new EntitiesConfig()
{
X = 141,
X = 141,
Y = 465,
Width = 20,
Height = 24,
Expand All @@ -26,14 +37,42 @@ internal class ModConfig
Y = 514,
Width = 9,
Height = 10,
Offset = new Vector2(7f, 7f),
Scale = 3f,
Offset = new Vector2(7f, 7f)
};

public Offset Offsets { get; set; } = new Offset();
public EntitiesConfig CowMilk { get; set; } = new EntitiesConfig()
{
X = 256,
Y = 112,
Width = 16,
Height = 16,
Scale = 2f,
Offset = new Vector2(4f, 4f)
};

public EntitiesConfig GoatMilk { get; set; } = new EntitiesConfig()
{
X = 64,
Y = 288,
Width = 16,
Height = 16,
Scale = 2f,
Offset = new Vector2(4f, 4f)
};

public EntitiesConfig Wool { get; set; } = new EntitiesConfig()
{
X = 128,
Y = 288,
Width = 16,
Height = 16,
Scale = 2f,
Offset = new Vector2(4f, 4f)
};
}

internal class Offset
internal class OffsetSetting
{
public Vector2 BarnAnimal { get; set; } = new Vector2(32f, -64f);

Expand Down
105 changes: 82 additions & 23 deletions ModEntry.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Netcode;
using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;
using StardewValley.Buildings;
using StardewValley.Characters;
using StardewValley.Network;
using StardewValley.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -18,6 +20,8 @@ public class ModEntry : Mod

private ModConfig Config;

private EntitiesSetting Entities;


public override void Entry(IModHelper helper)
{
Expand All @@ -31,6 +35,8 @@ public override void Entry(IModHelper helper)
private void LoadConfig()
{
Config = Helper.ReadConfig<ModConfig>();

Entities = Config.Entities;
}

private void OnButtonsChanged(object sender, ButtonsChangedEventArgs e)
Expand Down Expand Up @@ -118,30 +124,84 @@ private void RegisterEvent()
if (!m_Registered)
{
m_Registered = true;
Helper.Events.Display.RenderedWorld += OnRenderingHud;
Helper.Events.Display.RenderedWorld += OnRenderedWorld;

}
else
{
m_Registered = false;
Helper.Events.Display.RenderedWorld -= OnRenderingHud;
Helper.Events.Display.RenderedWorld -= OnRenderedWorld;
}
}

private double m_ShowProductTimeout;
private double m_ShowProductTime;

private void OnRenderingHud(object sender, EventArgs e)
private void SetupTime(ref double ms)
{
m_ShowProductTime = ms + Config.ShowIsHarvestableTime * 1000;
m_ShowProductTimeout = ms + Config.ShowIsHarvestableTime * 2000;
}

private void OnRenderedWorld(object sender, EventArgs e)
{
if (!Context.IsWorldReady)
return;

GameLocation currentLocation = Game1.currentLocation;
List<FarmAnimal> animals = GetAnimals(currentLocation);

double ms = Game1.currentGameTime.TotalGameTime.TotalMilliseconds;
float yOffsetFactor = 4f * (float)Math.Round(Math.Sin(ms / 250.0), 2);

if (animals != null)
{
foreach (FarmAnimal farmAnimal in animals)
bool showProduct = false;
if (Config.ShowIsHarvestable)
{
if (!farmAnimal.wasPet)
if (m_ShowProductTime == default)
{
DrawEntity(farmAnimal, farmAnimal.home);
SetupTime(ref ms);
}

showProduct = ms <= m_ShowProductTime;
}

foreach (FarmAnimal animal in animals)
{
if (Config.ShowIsHarvestable)
{
if (showProduct && animal.currentProduce > 0 && animal.age >= animal.ageWhenMature)
{
switch (animal.currentProduce)
{
case 184://Milk Cow ID
case 186://Milk Cow ID
DrawEntity(Game1.objectSpriteSheet, Entities.CowMilk, animal, animal.home, ref yOffsetFactor);
break;

case 436://Milk Goat ID
case 438://Milk Goat ID
DrawEntity(Game1.objectSpriteSheet, Entities.GoatMilk, animal, animal.home, ref yOffsetFactor);
break;

case 440://Wool ID
DrawEntity(Game1.objectSpriteSheet, Entities.Wool, animal, animal.home, ref yOffsetFactor);
break;
}

continue;
}

if (ms >= m_ShowProductTimeout)
{
SetupTime(ref ms);
}
}

if (!animal.wasPet && !animal.wasAutoPet)
{
DrawEntity(Game1.mouseCursors, Entities.Heart, animal, animal.home, ref yOffsetFactor);
}
}
}
Expand All @@ -151,40 +211,39 @@ private void OnRenderingHud(object sender, EventArgs e)
Pet pet = GetPet(currentLocation);
if (pet != null && !pet.lastPetDay.Values.Any(x => x == Game1.Date.TotalDays))
{
DrawEntity(pet);
DrawEntity(Game1.mouseCursors, Entities.Heart, pet, null, ref yOffsetFactor);
}
}



}


private void DrawEntity(Character animal, Building home = null)

private void DrawEntity(Texture2D spriteSheet, EntitiesConfig config, Character character, Building building, ref float yOffsetFactor)
{
float factor = 4f * (float)Math.Round(Math.Sin(Game1.currentGameTime.TotalGameTime.TotalMilliseconds / 250.0), 2);
Vector2 offset = GetOffsetForAnimal(building);
offset += character.position;

Vector2 offsetForAnimal = GetOffsetForAnimal(home);
offset += new Vector2(character.Sprite.getWidth() / 2, yOffsetFactor);

DrawEntity(Config.Bubble, animal, factor, offsetForAnimal);
DrawEntity(Config.Heart, animal, factor, offsetForAnimal);
}
DrawEntity(Game1.mouseCursors, Entities.Bubble, ref offset);

private void DrawEntity(EntitiesConfig config, Character animal, float factor, Vector2 offset)
{
offset += animal.position;
offset += config.Offset;
offset += new Vector2(animal.Sprite.getWidth() / 2, factor);

DrawSprite(config, Game1.GlobalToLocal(Game1.uiViewport, offset));
DrawEntity(spriteSheet, config, ref offset);
}

private void DrawSprite(EntitiesConfig config, Vector2 position)
{
private void DrawEntity(Texture2D spriteSheet, EntitiesConfig config, ref Vector2 offset)
{
Game1.spriteBatch.Draw(
Game1.mouseCursors,
position,
spriteSheet,
Game1.GlobalToLocal(Game1.uiViewport, offset),
new Rectangle(config.X, config.Y, config.Width, config.Height),
config.Color,
config.Rotation,
config.Origin,
config.Origin,
config.Scale,
config.SpriteEffects,
0f);
Expand Down
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"Name": "AnimalObserver",
"Author": "nofilenamed",
"Version": "1.0.1",
"Description": "Visual status of your animals.",
"Version": "1.0.2",
"Description": "Visual status and product of your animals.",
"UniqueID": "NFN.ANIMOBS",
"EntryDll": "AnimalObserver.dll",
"MinimumApiVersion": "3.9.5",
Expand Down

0 comments on commit cd86e37

Please sign in to comment.