2021-01-06 22:49:19 +11:00
|
|
|
using System;
|
2020-12-17 10:45:04 +03:00
|
|
|
using System.Threading.Tasks;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Shared.Interaction;
|
|
|
|
|
using Content.Shared.Interaction.Helpers;
|
2021-07-10 17:35:33 +02:00
|
|
|
using Content.Shared.Sound;
|
2020-06-19 15:20:59 +02:00
|
|
|
using Robust.Shared.Audio;
|
|
|
|
|
using Robust.Shared.GameObjects;
|
|
|
|
|
using Robust.Shared.IoC;
|
2021-03-21 09:12:03 -07:00
|
|
|
using Robust.Shared.Player;
|
2020-06-19 15:20:59 +02:00
|
|
|
using Robust.Shared.Random;
|
2021-03-05 01:08:38 +01:00
|
|
|
using Robust.Shared.Serialization.Manager.Attributes;
|
2020-06-19 15:20:59 +02:00
|
|
|
using Robust.Shared.ViewVariables;
|
|
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.Nutrition.Components
|
2020-06-19 15:20:59 +02:00
|
|
|
{
|
|
|
|
|
[RegisterComponent]
|
2021-01-06 22:49:19 +11:00
|
|
|
public class UtensilComponent : Component, IAfterInteract
|
2020-06-19 15:20:59 +02:00
|
|
|
{
|
2021-01-06 22:49:19 +11:00
|
|
|
public override string Name => "Utensil";
|
2020-06-19 15:20:59 +02:00
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("types")]
|
2021-01-06 22:49:19 +11:00
|
|
|
private UtensilType _types = UtensilType.None;
|
2020-06-19 15:20:59 +02:00
|
|
|
|
|
|
|
|
[ViewVariables]
|
2021-01-06 22:49:19 +11:00
|
|
|
public UtensilType Types
|
2020-06-19 15:20:59 +02:00
|
|
|
{
|
|
|
|
|
get => _types;
|
|
|
|
|
set
|
|
|
|
|
{
|
2021-01-06 22:49:19 +11:00
|
|
|
if (_types.Equals(value))
|
|
|
|
|
return;
|
|
|
|
|
|
2020-06-19 15:20:59 +02:00
|
|
|
_types = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The chance that the utensil has to break with each use.
|
|
|
|
|
/// A value of 0 means that it is unbreakable.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ViewVariables]
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("breakChance")]
|
2020-06-19 15:20:59 +02:00
|
|
|
private float _breakChance;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The sound to be played if the utensil breaks.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ViewVariables]
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("breakSound")]
|
2021-07-10 17:35:33 +02:00
|
|
|
private SoundSpecifier _breakSound = new SoundPathSpecifier("/Audio/Items/snap.ogg");
|
2020-06-19 15:20:59 +02:00
|
|
|
|
|
|
|
|
public void AddType(UtensilType type)
|
|
|
|
|
{
|
|
|
|
|
Types |= type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool HasAnyType(UtensilType type)
|
|
|
|
|
{
|
|
|
|
|
return (_types & type) != UtensilType.None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool HasType(UtensilType type)
|
|
|
|
|
{
|
2021-01-06 22:49:19 +11:00
|
|
|
return (_types & type) != 0;
|
2020-06-19 15:20:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemoveType(UtensilType type)
|
|
|
|
|
{
|
|
|
|
|
Types &= ~type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void TryBreak(IEntity user)
|
|
|
|
|
{
|
2021-07-31 19:52:33 +02:00
|
|
|
if (IoCManager.Resolve<IRobustRandom>().Prob(_breakChance))
|
2020-06-19 15:20:59 +02:00
|
|
|
{
|
2021-07-31 19:52:33 +02:00
|
|
|
SoundSystem.Play(Filter.Pvs(user), _breakSound.GetSound(), user, AudioParams.Default.WithVolume(-2f));
|
2020-06-19 15:20:59 +02:00
|
|
|
Owner.Delete();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-03 14:05:31 +01:00
|
|
|
async Task<bool> IAfterInteract.AfterInteract(AfterInteractEventArgs eventArgs)
|
2020-06-19 15:20:59 +02:00
|
|
|
{
|
2021-03-26 16:00:03 +01:00
|
|
|
return TryUseUtensil(eventArgs.User, eventArgs.Target);
|
2020-06-19 15:20:59 +02:00
|
|
|
}
|
|
|
|
|
|
2021-03-26 16:00:03 +01:00
|
|
|
private bool TryUseUtensil(IEntity user, IEntity? target)
|
2020-06-19 15:20:59 +02:00
|
|
|
{
|
2021-01-11 09:36:21 +01:00
|
|
|
if (target == null || !target.TryGetComponent(out FoodComponent? food))
|
2020-06-19 15:20:59 +02:00
|
|
|
{
|
2021-03-26 16:00:03 +01:00
|
|
|
return false;
|
2020-06-19 15:20:59 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-30 11:37:06 +02:00
|
|
|
if (!user.InRangeUnobstructed(target, popup: true))
|
2020-06-19 15:20:59 +02:00
|
|
|
{
|
2021-03-26 16:00:03 +01:00
|
|
|
return false;
|
2020-06-19 15:20:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
food.TryUseFood(user, null, this);
|
2021-03-26 16:00:03 +01:00
|
|
|
return true;
|
2020-06-19 15:20:59 +02:00
|
|
|
}
|
|
|
|
|
}
|
2021-01-06 22:49:19 +11:00
|
|
|
|
|
|
|
|
[Flags]
|
|
|
|
|
public enum UtensilType : byte
|
|
|
|
|
{
|
|
|
|
|
None = 0,
|
|
|
|
|
Fork = 1,
|
|
|
|
|
Spoon = 1 << 1,
|
|
|
|
|
Knife = 1 << 2
|
|
|
|
|
}
|
2020-06-19 15:20:59 +02:00
|
|
|
}
|