Files
tbd-station-14/Content.Server/Nutrition/Components/UtensilComponent.cs
Galactic Chimp f531c65a0b Merge branch 'master' into replace-sounds-with-sound-specifier
# Conflicts:
#	Content.Server/Actions/Actions/DisarmAction.cs
#	Content.Server/Actions/Actions/ScreamAction.cs
#	Content.Server/Arcade/Components/SpaceVillainArcadeComponent.cs
#	Content.Server/Damage/Components/DamageOnHighSpeedImpactComponent.cs
#	Content.Server/Explosion/Components/FlashExplosiveComponent.cs
#	Content.Server/Physics/Controllers/MoverController.cs
#	Content.Server/Portal/Components/PortalComponent.cs
#	Content.Server/Portal/Components/TeleporterComponent.cs
#	Content.Server/Projectiles/Components/ProjectileComponent.cs
#	Content.Server/Singularity/Components/EmitterComponent.cs
#	Content.Server/Sound/EmitSoundSystem.cs
#	Content.Server/Stunnable/Components/StunbatonComponent.cs
#	Content.Server/Tools/Components/MultitoolComponent.cs
#	Content.Server/Weapon/Ranged/Barrels/Components/ServerBatteryBarrelComponent.cs
#	Content.Shared/Gravity/GravityComponent.cs
#	Content.Shared/Light/Component/SharedExpendableLightComponent.cs
#	Content.Shared/Maps/ContentTileDefinition.cs
#	Content.Shared/Slippery/SlipperyComponent.cs
#	Content.Shared/Standing/StandingStateComponent.cs
#	Resources/Prototypes/Entities/Objects/Fun/bike_horn.yml
2021-07-25 14:12:00 +02:00

112 lines
2.9 KiB
C#

using System;
using System.Threading.Tasks;
using Content.Shared.Interaction;
using Content.Shared.Interaction.Helpers;
using Content.Shared.Sound;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Player;
using Robust.Shared.Random;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Nutrition.Components
{
[RegisterComponent]
public class UtensilComponent : Component, IAfterInteract
{
public override string Name => "Utensil";
[DataField("types")]
private UtensilType _types = UtensilType.None;
[ViewVariables]
public UtensilType Types
{
get => _types;
set
{
if (_types.Equals(value))
return;
_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]
[DataField("breakChance")]
private float _breakChance;
/// <summary>
/// The sound to be played if the utensil breaks.
/// </summary>
[ViewVariables]
[DataField("breakSound")]
private SoundSpecifier _breakSound = new SoundPathSpecifier("/Audio/Items/snap.ogg");
public void AddType(UtensilType type)
{
Types |= type;
}
public bool HasAnyType(UtensilType type)
{
return (_types & type) != UtensilType.None;
}
public bool HasType(UtensilType type)
{
return (_types & type) != 0;
}
public void RemoveType(UtensilType type)
{
Types &= ~type;
}
internal void TryBreak(IEntity user)
{
if (_breakSound.TryGetSound(out var breakSound) && IoCManager.Resolve<IRobustRandom>().Prob(_breakChance))
{
SoundSystem.Play(Filter.Pvs(user), breakSound, user, AudioParams.Default.WithVolume(-2f));
Owner.Delete();
}
}
async Task<bool> IAfterInteract.AfterInteract(AfterInteractEventArgs eventArgs)
{
return TryUseUtensil(eventArgs.User, eventArgs.Target);
}
private bool TryUseUtensil(IEntity user, IEntity? target)
{
if (target == null || !target.TryGetComponent(out FoodComponent? food))
{
return false;
}
if (!user.InRangeUnobstructed(target, popup: true))
{
return false;
}
food.TryUseFood(user, null, this);
return true;
}
}
[Flags]
public enum UtensilType : byte
{
None = 0,
Fork = 1,
Spoon = 1 << 1,
Knife = 1 << 2
}
}