-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModEntry.cs
More file actions
269 lines (220 loc) · 8.53 KB
/
Copy pathModEntry.cs
File metadata and controls
269 lines (220 loc) · 8.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
using System;
using System.Collections.Generic;
using System.Linq;
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;
namespace AnimalObserver
{
public class ModEntry : Mod
{
private bool m_Registered;
private ModConfig Config;
private EntitiesSetting Entities;
public override void Entry(IModHelper helper)
{
LoadConfig();
helper.Events.Input.ButtonsChanged += OnButtonsChanged;
RegisterEvent();
}
private void LoadConfig()
{
Config = Helper.ReadConfig<ModConfig>();
Entities = Config.Entities;
}
private void OnButtonsChanged(object sender, ButtonsChangedEventArgs e)
{
if (Config.Keys.Reload.JustPressed())
{
LoadConfig();
}
if (Config.Keys.Toggle.JustPressed())
{
RegisterEvent();
}
}
private static List<FarmAnimal> GetAnimals(GameLocation currentLocation)
{
List<FarmAnimal> list = null;
if (currentLocation is Farm farm)
{
if (farm.animals == null || farm.animals.Count() <= 0)
{
return list;
}
list = new List<FarmAnimal>();
using (NetDictionary<long, FarmAnimal, NetRef<FarmAnimal>, SerializableDictionary<long, FarmAnimal>, NetLongDictionary<FarmAnimal, NetRef<FarmAnimal>>>.ValuesCollection.Enumerator enumerator = farm.animals.Values.GetEnumerator())
{
while (enumerator.MoveNext())
{
FarmAnimal item = enumerator.Current;
list.Add(item);
}
return list;
}
}
if (currentLocation is AnimalHouse animalHouse)
{
if (animalHouse.animals != null && animalHouse.animals.Count() > 0)
{
list = new List<FarmAnimal>();
foreach (FarmAnimal item2 in animalHouse.animals.Values)
{
list.Add(item2);
}
}
}
return list;
}
private static Pet GetPet(GameLocation currentLocation)
{
foreach (NPC npc in currentLocation.characters)
{
if (npc is Pet pet)
{
return pet;
}
}
return null;
}
public Vector2 GetOffsetForAnimal(Building building)
{
if (building == null)
return Config.Offsets.Pet;
var data = building.GetData();
if (data.ValidOccupantTypes[0] == "Coop")
{
return Config.Offsets.CoopAnimal;
}
else if (data.ValidOccupantTypes[0] == "Barn")
{
return Config.Offsets.BarnAnimal;
}
return default;
}
private void RegisterEvent()
{
if (!m_Registered)
{
m_Registered = true;
Helper.Events.Display.RenderedWorld += OnRenderedWorld;
}
else
{
m_Registered = false;
Helper.Events.Display.RenderedWorld -= OnRenderedWorld;
}
}
private double m_ShowProductTimeout;
private double m_ShowProductTime;
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)
{
bool showProduct = false;
if (Config.ShowIsHarvestable)
{
if (m_ShowProductTime == default)
{
SetupTime(ref ms);
}
showProduct = ms <= m_ShowProductTime;
}
foreach (FarmAnimal animal in animals)
{
if (Config.ShowIsHarvestable)
{
if (showProduct && !string.IsNullOrEmpty(animal.currentProduce.Value) && animal.isAdult())
{
//Monitor.LogOnce($"{animal.displayName} {animal.currentProduce.Value}", LogLevel.Debug);
switch (animal.currentProduce.Value)
{
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 "430": //Truffle ID
{
if (Config.ShowTruffle)
{
DrawEntity(Game1.objectSpriteSheet, Entities.Truffle, 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.Value && !animal.wasAutoPet.Value)
{
DrawEntity(Game1.mouseCursors, Entities.Heart, animal, animal.home, ref yOffsetFactor);
}
}
}
if (Config.PetToo)
{
Pet pet = GetPet(currentLocation);
if (pet != null && !pet.lastPetDay.Values.Any(x => x == Game1.Date.TotalDays))
{
DrawEntity(Game1.mouseCursors, Entities.Heart, pet, null, ref yOffsetFactor);
}
}
}
private void DrawEntity(Texture2D spriteSheet, EntitiesConfig config, Character character, Building building, ref float yOffsetFactor)
{
Vector2 offset = GetOffsetForAnimal(building);
offset += character.position.Value;
offset += new Vector2(character.Sprite.getWidth() / 2, yOffsetFactor);
DrawEntity(Game1.mouseCursors, Entities.Bubble, ref offset);
offset += config.Offset;
DrawEntity(spriteSheet, config, ref offset);
}
private static void DrawEntity(Texture2D spriteSheet, EntitiesConfig config, ref Vector2 offset)
{
Game1.spriteBatch.Draw(
spriteSheet,
Game1.GlobalToLocal(Game1.uiViewport, offset),
new Rectangle(config.X, config.Y, config.Width, config.Height),
config.Color,
config.Rotation,
config.Origin,
config.Scale,
config.SpriteEffects,
0f);
}
}
}