Files
crystall-punk-14/Content.Shared/_CP14/Cooking/CP14SharedCookingSystem.DoAfter.cs
Red 8721f736e1 Cooking simulator 3: Pies (#1540)
* enum -> prototype

* move sprites

* more dirt sprites

* some food data stucking fixing

* pumpkin fix

* pie move sprites

* refactor components

* pie refactor

* remove outdated proto

* new pie types

* Update SliceableFoodSystem.cs

* Refactor food cooking system and add fat flavor

Added 'Fat' to the flavor profile. Refactored food cooking logic to use CreateFoodData and UpdateFoodDataVisuals instead of CookFood and ApplyFoodVisuals. Introduced RenameCooker property to CP14FoodCookerComponent to control entity renaming during cooking. Improved separation of food data creation and visual updates.

* Update migration.yml

* Refactor food visual and sliceable logic in cooking system

Moved sliceable food logic from OnCookFinished to UpdateFoodDataVisuals for better encapsulation. Made UpdateFoodDataVisuals overridable and updated its usage in random food initialization. Added Rename field to CP14RandomFoodDataComponent and cleaned up unused BecomeSliceable field in CP14FoodCookerComponent. Updated pie_pan.yml to add SliceableFood and a new random food entity.

* Update pie_pan.yml

* fill levels
2025-07-20 15:45:59 +03:00

177 lines
5.3 KiB
C#

/*
* This file is sublicensed under MIT License
* https://github.com/space-wizards/space-station-14/blob/master/LICENSE.TXT
*/
using Content.Shared._CP14.Cooking.Components;
using Content.Shared._CP14.Cooking.Prototypes;
using Content.Shared.DoAfter;
using Content.Shared.Temperature;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.Timing;
namespace Content.Shared._CP14.Cooking;
public abstract partial class CP14SharedCookingSystem
{
[Dependency] private readonly IGameTiming _timing = default!;
private void InitDoAfter()
{
SubscribeLocalEvent<CP14FoodCookerComponent, OnTemperatureChangeEvent>(OnTemperatureChange);
SubscribeLocalEvent<CP14FoodCookerComponent, EntParentChangedMessage>(OnParentChanged);
SubscribeLocalEvent<CP14FoodCookerComponent, CP14CookingDoAfter>(OnCookFinished);
SubscribeLocalEvent<CP14FoodCookerComponent, CP14BurningDoAfter>(OnCookBurned);
}
private void UpdateDoAfter(float frameTime)
{
var query = EntityQueryEnumerator<CP14FoodCookerComponent>();
while(query.MoveNext(out var uid, out var cooker))
{
if (_timing.CurTime > cooker.LastHeatingTime + cooker.HeatingFrequencyRequired && _doAfter.IsRunning(cooker.DoAfterId))
_doAfter.Cancel(cooker.DoAfterId);
}
}
protected virtual void OnCookBurned(Entity<CP14FoodCookerComponent> ent, ref CP14BurningDoAfter args)
{
StopCooking(ent);
if (args.Cancelled || args.Handled)
return;
BurntFood(ent);
args.Handled = true;
}
protected virtual void OnCookFinished(Entity<CP14FoodCookerComponent> ent, ref CP14CookingDoAfter args)
{
StopCooking(ent);
if (args.Cancelled || args.Handled)
return;
if (!TryComp<CP14FoodHolderComponent>(ent, out var holder))
return;
if (!_proto.TryIndex(args.Recipe, out var indexedRecipe))
return;
CreateFoodData(ent, indexedRecipe);
UpdateFoodDataVisuals((ent, holder), ent.Comp.RenameCooker);
args.Handled = true;
}
private void StartCooking(Entity<CP14FoodCookerComponent> ent, CP14CookingRecipePrototype recipe)
{
if (_doAfter.IsRunning(ent.Comp.DoAfterId))
return;
_appearance.SetData(ent, CP14CookingVisuals.Cooking, true);
var doAfterArgs = new DoAfterArgs(EntityManager, ent, recipe.CookingTime, new CP14CookingDoAfter(recipe.ID), ent)
{
NeedHand = false,
BreakOnWeightlessMove = false,
RequireCanInteract = false,
};
_doAfter.TryStartDoAfter(doAfterArgs, out var doAfterId);
ent.Comp.DoAfterId = doAfterId;
_ambientSound.SetAmbience(ent, true);
}
private void StartBurning(Entity<CP14FoodCookerComponent> ent)
{
if (_doAfter.IsRunning(ent.Comp.DoAfterId))
return;
_appearance.SetData(ent, CP14CookingVisuals.Burning, true);
var doAfterArgs = new DoAfterArgs(EntityManager, ent, 20, new CP14BurningDoAfter(), ent)
{
NeedHand = false,
BreakOnWeightlessMove = false,
RequireCanInteract = false,
};
_doAfter.TryStartDoAfter(doAfterArgs, out var doAfterId);
ent.Comp.DoAfterId = doAfterId;
_ambientSound.SetAmbience(ent, true);
}
protected void StopCooking(Entity<CP14FoodCookerComponent> ent)
{
if (_doAfter.IsRunning(ent.Comp.DoAfterId))
_doAfter.Cancel(ent.Comp.DoAfterId);
_appearance.SetData(ent, CP14CookingVisuals.Cooking, false);
_appearance.SetData(ent, CP14CookingVisuals.Burning, false);
_ambientSound.SetAmbience(ent, false);
}
private void OnTemperatureChange(Entity<CP14FoodCookerComponent> ent, ref OnTemperatureChangeEvent args)
{
if (!_container.TryGetContainer(ent, ent.Comp.ContainerId, out var container))
return;
if (!TryComp<CP14FoodHolderComponent>(ent, out var holder))
return;
if (container.ContainedEntities.Count <= 0 && holder.FoodData is null)
{
StopCooking(ent);
return;
}
if (args.TemperatureDelta > 0)
{
ent.Comp.LastHeatingTime = _timing.CurTime;
DirtyField(ent.Owner,ent.Comp, nameof(CP14FoodCookerComponent.LastHeatingTime));
if (!_doAfter.IsRunning(ent.Comp.DoAfterId) && holder.FoodData is null)
{
var recipe = GetRecipe(ent);
if (recipe is not null)
StartCooking(ent, recipe);
}
else
{
StartBurning(ent);
}
}
else
{
StopCooking(ent);
}
}
private void OnParentChanged(Entity<CP14FoodCookerComponent> ent, ref EntParentChangedMessage args)
{
StopCooking(ent);
}
}
[Serializable, NetSerializable]
public sealed partial class CP14CookingDoAfter : DoAfterEvent
{
[DataField]
public ProtoId<CP14CookingRecipePrototype> Recipe;
public CP14CookingDoAfter(ProtoId<CP14CookingRecipePrototype> recipe)
{
Recipe = recipe;
}
public override DoAfterEvent Clone() => this;
}
[Serializable, NetSerializable]
public sealed partial class CP14BurningDoAfter : SimpleDoAfterEvent;