using Content.Shared.Chemistry.EntitySystems;
using Content.Shared.Chemistry.Reaction;
using Content.Shared.Kitchen.Components;
using Robust.Shared.Containers;
namespace Content.Shared.Kitchen.EntitySystems;
public abstract partial class MicrowaveSystem
{
///
/// Adjusts a microwave's visuals, audio, and power draw when activated.
///
[SubscribeLocalEvent]
private void OnCookStart(Entity ent, ref ComponentStartup args)
{
if (!MicrowaveQuery.TryComp(ent, out var microwaveComponent))
return;
ent.Comp.NextCookUpdate = Timing.CurTime + microwaveComponent.UpdateInterval;
ent.Comp.LastCookUpdated = Timing.CurTime;
DirtyFields(ent.AsNullable(), null, nameof(ent.Comp.NextCookUpdate), nameof(ent.Comp.LastCookUpdated));
ActivateMicrowaveCycle((ent, microwaveComponent));
}
///
/// Adjusts a microwave's visuals, audio, and power draw when activated.
///
[SubscribeLocalEvent]
private void OnCookEnd(Entity ent, ref ComponentShutdown args)
{
if (!MicrowaveQuery.TryComp(ent, out var microwaveComponent))
return;
DeactivateMicrowaveCycle((ent, microwaveComponent));
}
///
/// Adds ActivelyMicrowavedComponent to entities inserted into an active microwave.
///
[SubscribeLocalEvent]
private void OnActiveMicrowaveInsert(Entity ent, ref EntInsertedIntoContainerMessage args)
{
if (Timing.ApplyingState)
return;
BeginActivelyMicrowaving(args.Entity, ent.Owner);
}
///
/// Removes ActivelyMicrowavedComponent from entities removed from an active microwave.
///
[SubscribeLocalEvent]
private void OnActiveMicrowaveRemove(Entity ent, ref EntRemovedFromContainerMessage args)
{
if (Timing.ApplyingState)
return;
RemCompDeferred(args.Entity);
}
///
/// Prevents reagent reactions in entitites that are actively being microwaved.
///
///
/// For example, raw egg would otherwise turn into cooked egg during the process, preventing it from being
/// "spent" when the microwave is finished cooking.
///
[SubscribeLocalEvent]
private void OnReactionAttempt(Entity ent, ref SolutionRelayEvent args)
{
if (!ActiveMicrowaveQuery.TryComp(ent.Comp.Microwave, out var activeMicrowaveComp))
return;
var portionedRecipe = activeMicrowaveComp.PortionedRecipe;
if (portionedRecipe == null // no recipe selected
|| !ProtoMan.TryIndex(portionedRecipe.Value.Recipe, out var recipe))
return;
var recipeReagents = recipe.Ingredients.Reagents.Keys;
foreach (var reagent in recipeReagents)
{
if (args.Event.Reaction.Reactants.ContainsKey(reagent))
{
args.Event.Cancelled = true;
return;
}
}
}
///
/// Adjusts a microwave's visuals, audio, and power draw when activated.
///
protected virtual void ActivateMicrowaveCycle(Entity ent)
{
SetAppearance(ent, MicrowaveVisualState.Cooking);
_powerState.SetWorkingState(ent.Owner, true);
}
///
/// Adjusts a microwave's visuals, audio, and power draw when deactivated.
///
protected virtual void DeactivateMicrowaveCycle(Entity ent)
{
SetAppearance(ent, MicrowaveVisualState.Idle);
_powerState.SetWorkingState(ent.Owner, false);
foreach (var uid in GetMicrowaveContents(ent.AsNullable()))
{
RemCompDeferred(uid);
}
}
///
/// Add ActivelyMicrowavedComponent to items that are being actively microwaved.
///
/// The entity being microwaved.
/// That dastardly microwave
private void BeginActivelyMicrowaving(EntityUid uid, EntityUid microwave)
{
var comp = new ActivelyMicrowavedComponent { Microwave = microwave };
AddComp(uid, comp);
}
}