2021-11-21 10:35:09 +03:00
|
|
|
using Content.Server.Nutrition.Components;
|
|
|
|
|
using Content.Server.Popups;
|
|
|
|
|
using Content.Shared.Interaction;
|
|
|
|
|
using Robust.Shared.Audio;
|
|
|
|
|
using Robust.Shared.Player;
|
|
|
|
|
using Robust.Shared.Random;
|
|
|
|
|
|
|
|
|
|
namespace Content.Server.Nutrition.EntitySystems
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles usage of the utensils on the food items
|
|
|
|
|
/// </summary>
|
2022-02-16 00:23:23 -07:00
|
|
|
internal sealed class UtensilSystem : EntitySystem
|
2021-11-21 10:35:09 +03:00
|
|
|
{
|
|
|
|
|
[Dependency] private readonly IRobustRandom _robustRandom = default!;
|
|
|
|
|
[Dependency] private readonly FoodSystem _foodSystem = default!;
|
|
|
|
|
[Dependency] private readonly PopupSystem _popupSystem = default!;
|
2022-02-17 15:40:03 +13:00
|
|
|
[Dependency] private readonly SharedInteractionSystem _interactionSystem = default!;
|
2021-11-21 10:35:09 +03:00
|
|
|
|
|
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
|
|
|
|
SubscribeLocalEvent<UtensilComponent, AfterInteractEvent>(OnAfterInteract);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Clicked with utensil
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void OnAfterInteract(EntityUid uid, UtensilComponent component, AfterInteractEvent ev)
|
|
|
|
|
{
|
2022-02-05 15:39:01 +13:00
|
|
|
if (ev.Target == null || !ev.CanReach)
|
2021-11-21 10:35:09 +03:00
|
|
|
return;
|
|
|
|
|
|
2023-05-07 14:58:20 +12:00
|
|
|
var result = TryUseUtensil(ev.User, ev.Target.Value, component);
|
|
|
|
|
ev.Handled = result.Handled;
|
2021-11-21 10:35:09 +03:00
|
|
|
}
|
|
|
|
|
|
2023-05-07 14:58:20 +12:00
|
|
|
public (bool Success, bool Handled) TryUseUtensil(EntityUid user, EntityUid target, UtensilComponent component)
|
2021-11-21 10:35:09 +03:00
|
|
|
{
|
2022-06-04 19:17:48 +12:00
|
|
|
if (!EntityManager.TryGetComponent(target, out FoodComponent? food))
|
2023-05-07 14:58:20 +12:00
|
|
|
return (false, true);
|
2021-11-21 10:35:09 +03:00
|
|
|
|
|
|
|
|
//Prevents food usage with a wrong utensil
|
|
|
|
|
if ((food.Utensil & component.Types) == 0)
|
|
|
|
|
{
|
2023-02-24 19:01:25 -05:00
|
|
|
_popupSystem.PopupEntity(Loc.GetString("food-system-wrong-utensil", ("food", target), ("utensil", component.Owner)), user, user);
|
2023-05-07 14:58:20 +12:00
|
|
|
return (false, true);
|
2021-11-21 10:35:09 +03:00
|
|
|
}
|
|
|
|
|
|
2022-02-17 15:40:03 +13:00
|
|
|
if (!_interactionSystem.InRangeUnobstructed(user, target, popup: true))
|
2023-05-07 14:58:20 +12:00
|
|
|
return (false, true);
|
2021-11-21 10:35:09 +03:00
|
|
|
|
2023-02-24 19:01:25 -05:00
|
|
|
return _foodSystem.TryFeed(user, user, target, food);
|
2021-11-21 10:35:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Attempt to break the utensil after interaction.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="uid">Utensil.</param>
|
2021-12-03 15:53:09 +01:00
|
|
|
/// <param name="userUid">User of the utensil.</param>
|
2021-11-21 10:35:09 +03:00
|
|
|
public void TryBreak(EntityUid uid, EntityUid userUid, UtensilComponent? component = null)
|
|
|
|
|
{
|
|
|
|
|
if (!Resolve(uid, ref component))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (_robustRandom.Prob(component.BreakChance))
|
|
|
|
|
{
|
2022-06-12 19:45:47 -04:00
|
|
|
SoundSystem.Play(component.BreakSound.GetSound(), Filter.Pvs(userUid), userUid, AudioParams.Default.WithVolume(-2f));
|
2021-12-08 13:00:43 +01:00
|
|
|
EntityManager.DeleteEntity(component.Owner);
|
2021-11-21 10:35:09 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|