moths can eat goat wool (#21704)

* add Wooly system

* add RequireDead to Food

* minor fix+cleanup and fix repeating

* minor fix+cleanup and fix repeating

* make goat wooly

---------

Co-authored-by: deltanedas <@deltanedas:kde.org>
This commit is contained in:
deltanedas
2023-11-17 08:51:51 +00:00
committed by GitHub
parent 6e8ed007b8
commit 1ba6cad880
5 changed files with 131 additions and 12 deletions

View File

@@ -0,0 +1,42 @@
using Content.Server.Animals.Systems;
using Content.Shared.Chemistry.Reagent;
using Content.Shared.FixedPoint;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
/// <summary>
/// Lets an animal grow a wool solution when not hungry.
/// </summary>
[RegisterComponent, Access(typeof(WoolySystem))]
public sealed partial class WoolyComponent : Component
{
/// <summary>
/// What reagent to grow.
/// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)]
public ProtoId<ReagentPrototype> ReagentId = "Fiber";
/// <summary>
/// How much wool to grow at every growth cycle.
/// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)]
public FixedPoint2 Quantity = 25;
/// <summary>
/// What solution to add the wool reagent to.
/// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)]
public string Solution = "wool";
/// <summary>
/// How long to wait before growing wool.
/// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)]
public TimeSpan GrowthDelay = TimeSpan.FromMinutes(1);
/// <summary>
/// When to next try growing wool.
/// </summary>
[DataField(customTypeSerializer: typeof(TimeOffsetSerializer)), ViewVariables(VVAccess.ReadWrite)]
public TimeSpan NextGrowth = TimeSpan.FromSeconds(0);
}

View File

@@ -0,0 +1,56 @@
using Content.Server.Animals.Components;
using Content.Server.Nutrition;
using Content.Shared.Chemistry.EntitySystems;
using Content.Shared.Nutrition.Components;
using Content.Shared.Nutrition.EntitySystems;
using Robust.Shared.Timing;
namespace Content.Server.Animals.Systems;
/// <summary>
/// Handles regeneration of an animal's wool solution when not hungry.
/// Shearing is not currently possible so the only use is for moths to eat.
/// </summary>
public sealed class WoolySystem : EntitySystem
{
[Dependency] private readonly HungerSystem _hunger = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly SolutionContainerSystem _solutionContainer = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<WoolyComponent, BeforeFullyEatenEvent>(OnBeforeFullyEaten);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var query = EntityQueryEnumerator<WoolyComponent, HungerComponent>();
var now = _timing.CurTime;
while (query.MoveNext(out var uid, out var comp, out var hunger))
{
if (now < comp.NextGrowth)
continue;
comp.NextGrowth = now + comp.GrowthDelay;
// Is there enough nutrition to produce reagent?
if (_hunger.GetHungerThreshold(hunger) < HungerThreshold.Peckish)
continue;
if (!_solutionContainer.TryGetSolution(uid, comp.Solution, out var solution))
continue;
_solutionContainer.TryAddReagent(uid, solution, comp.ReagentId, comp.Quantity, out _);
}
}
private void OnBeforeFullyEaten(Entity<WoolyComponent> ent, ref BeforeFullyEatenEvent args)
{
// don't want moths to delete goats after eating them
args.Cancel();
}
}