Moved Food and Utensil to ECS (#5380)
* Food and Utensil to ECS, Made utensil less restrictive, no more soup eating with a knife... * AltVerb "can eat" check * removed HasFlag calls * AltActionVerb -> InteractionVerb * "required utensil" filed
This commit is contained in:
77
Content.Server/Nutrition/EntitySystems/UtensilSystem.cs
Normal file
77
Content.Server/Nutrition/EntitySystems/UtensilSystem.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using Content.Server.Nutrition.Components;
|
||||
using Content.Server.Popups;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Interaction.Helpers;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.Nutrition.EntitySystems
|
||||
{
|
||||
/// <summary>
|
||||
/// Handles usage of the utensils on the food items
|
||||
/// </summary>
|
||||
internal class UtensilSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IRobustRandom _robustRandom = default!;
|
||||
[Dependency] private readonly FoodSystem _foodSystem = default!;
|
||||
[Dependency] private readonly PopupSystem _popupSystem = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<UtensilComponent, AfterInteractEvent>(OnAfterInteract);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clicked with utensil
|
||||
/// </summary>
|
||||
private void OnAfterInteract(EntityUid uid, UtensilComponent component, AfterInteractEvent ev)
|
||||
{
|
||||
if (ev.Target == null)
|
||||
return;
|
||||
|
||||
if (TryUseUtensil(ev.UserUid, ev.Target.Uid, component))
|
||||
ev.Handled = true;
|
||||
}
|
||||
|
||||
private bool TryUseUtensil(EntityUid userUid, EntityUid targetUid, UtensilComponent component)
|
||||
{
|
||||
if (!EntityManager.TryGetComponent(targetUid, out FoodComponent food))
|
||||
return false;
|
||||
|
||||
//Prevents food usage with a wrong utensil
|
||||
if ((food.Utensil & component.Types) == 0)
|
||||
{
|
||||
_popupSystem.PopupEntity(Loc.GetString("food-system-wrong-utensil", ("food", food.Owner), ("utensil", component.Owner)), userUid, Filter.Entities(userUid));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!userUid.InRangeUnobstructed(targetUid, popup: true))
|
||||
return false;
|
||||
|
||||
return _foodSystem.TryUseFood(targetUid, userUid, userUid);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempt to break the utensil after interaction.
|
||||
/// </summary>
|
||||
/// <param name="uid">Utensil.</param>
|
||||
/// <param name="userUid">User of the utensil.</param>
|
||||
public void TryBreak(EntityUid uid, EntityUid userUid, UtensilComponent? component = null)
|
||||
{
|
||||
if (!Resolve(uid, ref component))
|
||||
return;
|
||||
|
||||
if (_robustRandom.Prob(component.BreakChance))
|
||||
{
|
||||
SoundSystem.Play(Filter.Pvs(userUid), component.BreakSound.GetSound(), userUid, AudioParams.Default.WithVolume(-2f));
|
||||
component.Owner.Delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user