diff --git a/SDVModTest/UIElements/ShowWhenAnimalNeedsPet.cs b/SDVModTest/UIElements/ShowWhenAnimalNeedsPet.cs index 48bec474..257d97dd 100644 --- a/SDVModTest/UIElements/ShowWhenAnimalNeedsPet.cs +++ b/SDVModTest/UIElements/ShowWhenAnimalNeedsPet.cs @@ -12,84 +12,40 @@ namespace UIInfoSuite.UIElements { class ShowWhenAnimalNeedsPet : IDisposable { + #region Properties private float _yMovementPerDraw = 0f; private float _alpha = 1f; + private readonly IModHelper _helper; + #endregion + + #region Lifecycle public ShowWhenAnimalNeedsPet(IModHelper helper) { _helper = helper; } + public void Dispose() + { + ToggleOption(false); + } + public void ToggleOption(bool showWhenAnimalNeedsPet) { _helper.Events.Player.Warped -= OnWarped; - _helper.Events.Display.RenderingHud -= OnRenderingHud_DrawAnimalHasProduct; + _helper.Events.Display.RenderingHud -= OnRenderingHud; if (showWhenAnimalNeedsPet) { _helper.Events.Player.Warped += OnWarped; - _helper.Events.Display.RenderingHud += OnRenderingHud_DrawAnimalHasProduct; + _helper.Events.Display.RenderingHud += OnRenderingHud; } } + #endregion - public void Dispose() - { - ToggleOption(false); - } - /// Raised before drawing the HUD (item toolbar, clock, etc) to the screen. The vanilla HUD may be hidden at this point (e.g. because a menu is open). - /// The event sender. - /// The event arguments. - private void OnRenderingHud_DrawAnimalHasProduct(object sender, RenderingHudEventArgs e) - { - if (!Game1.eventUp && - Game1.activeClickableMenu == null && - Game1.currentLocation != null) - { - var animalsInCurrentLocation = GetAnimalsInCurrentLocation(); - if (animalsInCurrentLocation != null) - { - foreach (var animal in animalsInCurrentLocation.Pairs) - { - if (!animal.Value.IsEmoting && - animal.Value.currentProduce.Value != 430 && - animal.Value.currentProduce.Value > 0 && - animal.Value.age.Value >= animal.Value.ageWhenMature.Value) - { - Vector2 positionAboveAnimal = GetPetPositionAboveAnimal(animal.Value); - positionAboveAnimal.Y += (float)(Math.Sin(Game1.currentGameTime.TotalGameTime.TotalMilliseconds / 300.0 + (double)animal.Value.Name.GetHashCode()) * 5.0); - Game1.spriteBatch.Draw( - Game1.emoteSpriteSheet, - new Vector2(positionAboveAnimal.X + 14f, positionAboveAnimal.Y), - new Rectangle(3 * (Game1.tileSize / 4) % Game1.emoteSpriteSheet.Width, 3 * (Game1.tileSize / 4) / Game1.emoteSpriteSheet.Width * (Game1.tileSize / 4), Game1.tileSize / 4, Game1.tileSize / 4), - Color.White * 0.9f, - 0.0f, - Vector2.Zero, - 4f, - SpriteEffects.None, - 1f); - - Rectangle sourceRectangle = GameLocation.getSourceRectForObject(animal.Value.currentProduce.Value); - Game1.spriteBatch.Draw( - Game1.objectSpriteSheet, - new Vector2(positionAboveAnimal.X + 28f, positionAboveAnimal.Y + 8f), - sourceRectangle, - Color.White * 0.9f, - 0.0f, - Vector2.Zero, - 2.2f, - SpriteEffects.None, - 1f); - } - } - } - } - } - - /// Raised after a player warps to a new location. - /// The event sender. - /// The event arguments. + #region Event subscriptions private void OnWarped(object sender, WarpedEventArgs e) { if (e.IsLocalPlayer) @@ -105,18 +61,6 @@ private void OnWarped(object sender, WarpedEventArgs e) } } - /// Raised before drawing the HUD (item toolbar, clock, etc) to the screen. The vanilla HUD may be hidden at this point (e.g. because a menu is open). - /// The event sender. - /// The event arguments. - private void OnRenderingHud_DrawNeedsPetTooltip(object sender, RenderingHudEventArgs e) - { - if (!Game1.eventUp && Game1.activeClickableMenu == null) - { - DrawIconForFarmAnimals(); - DrawIconForPets(); - } - } - private void StartDrawingPetNeeds() { _helper.Events.Display.RenderingHud += OnRenderingHud_DrawNeedsPetTooltip; @@ -129,16 +73,73 @@ private void StopDrawingPetNeeds() _helper.Events.GameLoop.UpdateTicked -= UpdateTicked; } - /// Raised after the game state is updated (≈60 times per second). - /// The event sender. - /// The event arguments. + private void OnRenderingHud_DrawNeedsPetTooltip(object sender, RenderingHudEventArgs e) + { + if (!Game1.eventUp && Game1.activeClickableMenu == null) + { + DrawIconForFarmAnimals(); + DrawIconForPets(); + } + } + + private void OnRenderingHud(object sender, RenderingHudEventArgs e) + { + if (!Game1.eventUp && Game1.activeClickableMenu == null && Game1.currentLocation != null) + { + DrawAnimalHasProduct(); + } + } + private void UpdateTicked(object sender, UpdateTickedEventArgs e) { - // Bob up and down in a sin wave each draw float sine = (float)Math.Sin(e.Ticks / 20.0); _yMovementPerDraw = -6f + 6f * sine; _alpha = 0.8f + 0.2f * sine; } + #endregion + + + #region Logic + private void DrawAnimalHasProduct() + { + var animalsInCurrentLocation = GetAnimalsInCurrentLocation(); + if (animalsInCurrentLocation != null) + { + foreach (var animal in animalsInCurrentLocation.Pairs) + { + if (!animal.Value.IsEmoting && + animal.Value.currentProduce.Value != 430 && + animal.Value.currentProduce.Value > 0 && + animal.Value.age.Value >= animal.Value.ageWhenMature.Value) + { + Vector2 positionAboveAnimal = GetPetPositionAboveAnimal(animal.Value); + positionAboveAnimal.Y += (float)(Math.Sin(Game1.currentGameTime.TotalGameTime.TotalMilliseconds / 300.0 + (double)animal.Value.Name.GetHashCode()) * 5.0); + Game1.spriteBatch.Draw( + Game1.emoteSpriteSheet, + new Vector2(positionAboveAnimal.X + 14f, positionAboveAnimal.Y), + new Rectangle(3 * (Game1.tileSize / 4) % Game1.emoteSpriteSheet.Width, 3 * (Game1.tileSize / 4) / Game1.emoteSpriteSheet.Width * (Game1.tileSize / 4), Game1.tileSize / 4, Game1.tileSize / 4), + Color.White * 0.9f, + 0.0f, + Vector2.Zero, + 4f, + SpriteEffects.None, + 1f); + + Rectangle sourceRectangle = GameLocation.getSourceRectForObject(animal.Value.currentProduce.Value); + Game1.spriteBatch.Draw( + Game1.objectSpriteSheet, + new Vector2(positionAboveAnimal.X + 28f, positionAboveAnimal.Y + 8f), + sourceRectangle, + Color.White * 0.9f, + 0.0f, + Vector2.Zero, + 2.2f, + SpriteEffects.None, + 1f); + } + } + } + } private void DrawIconForFarmAnimals() { @@ -227,6 +228,7 @@ private Vector2 GetPetPositionAboveAnimal(Character animal) } return animals; - } + } + #endregion } }