Adds eating with utensils (#1136)
* Add Utensil component For eating. With utensils. Added to fork, plastic fork, spoon, plastic spoon and plastic knife. Ignored component on the client. * Add break chance to utensils Set to 20% for plastic ones * Add break sound to utensils * Add utensil kinds None, fork, spoon and knife. For sporks, forknifes and sporknifes, of course. * Add restricting foods by utensils needed * Fix utensils breaking when food isn't eaten * Moved getting held utensils to FoodComponent * Add breaking a clicking utensil even if its not necessary to eat the food * Move use utensil code to a separate method * Add telling a handless entity when they need an utensil to eat The immersion is off the charts * Change food trash to only be held when the food was also being held * Fix Wi-Fi utensils * Remove unnecessary utensil ItemGroup * Made TryUseFood public, removed redundant trash position update * Renamed UtensilKind to UtensilType * Remove eating food when clicking with it on nothing * Disable eating food when clicked directly if it requires an untensil to eat
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.GameObjects.Components.Chemistry;
|
||||
using Content.Server.GameObjects.Components.Sound;
|
||||
using Content.Server.GameObjects.Components.Utensil;
|
||||
using Content.Server.GameObjects.EntitySystems;
|
||||
using Content.Server.Utility;
|
||||
using Content.Shared.Chemistry;
|
||||
using Content.Shared.GameObjects.Components.Utensil;
|
||||
using Content.Shared.Interfaces;
|
||||
using Robust.Server.GameObjects.EntitySystems;
|
||||
using Robust.Shared.Audio;
|
||||
@@ -10,6 +13,7 @@ using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
@@ -32,18 +36,28 @@ namespace Content.Server.GameObjects.Components.Nutrition
|
||||
private SolutionComponent _contents;
|
||||
[ViewVariables]
|
||||
private ReagentUnit _transferAmount;
|
||||
private UtensilType _utensilsNeeded;
|
||||
|
||||
public int UsesRemaining => _contents.CurrentVolume == 0
|
||||
?
|
||||
0 : Math.Max(1, (int)Math.Ceiling((_contents.CurrentVolume / _transferAmount).Float()));
|
||||
|
||||
|
||||
public override void ExposeData(ObjectSerializer serializer)
|
||||
{
|
||||
base.ExposeData(serializer);
|
||||
serializer.DataField(ref _useSound, "useSound", "/Audio/items/eatfood.ogg");
|
||||
serializer.DataField(ref _transferAmount, "transferAmount", ReagentUnit.New(5));
|
||||
serializer.DataField(ref _trashPrototype, "trash", "TrashPlate");
|
||||
|
||||
if (serializer.Reading)
|
||||
{
|
||||
var utensils = serializer.ReadDataField("utensils", new List<UtensilType>());
|
||||
foreach (var utensil in utensils)
|
||||
{
|
||||
_utensilsNeeded |= utensil;
|
||||
Dirty();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
@@ -55,15 +69,27 @@ namespace Content.Server.GameObjects.Components.Nutrition
|
||||
|
||||
bool IUse.UseEntity(UseEntityEventArgs eventArgs)
|
||||
{
|
||||
if (_utensilsNeeded != UtensilType.None)
|
||||
{
|
||||
eventArgs.User.PopupMessage(eventArgs.User, Loc.GetString("You need to use a {0} to eat that!", _utensilsNeeded));
|
||||
return false;
|
||||
}
|
||||
|
||||
return TryUseFood(eventArgs.User, null);
|
||||
}
|
||||
|
||||
// Feeding someone else
|
||||
void IAfterInteract.AfterInteract(AfterInteractEventArgs eventArgs)
|
||||
{
|
||||
if (eventArgs.Target == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
TryUseFood(eventArgs.User, eventArgs.Target);
|
||||
}
|
||||
|
||||
private bool TryUseFood(IEntity user, IEntity target)
|
||||
public bool TryUseFood(IEntity user, IEntity target, UtensilComponent utensilUsed = null)
|
||||
{
|
||||
if (user == null)
|
||||
{
|
||||
@@ -78,20 +104,65 @@ namespace Content.Server.GameObjects.Components.Nutrition
|
||||
|
||||
var trueTarget = target ?? user;
|
||||
|
||||
if (trueTarget.TryGetComponent(out StomachComponent stomachComponent))
|
||||
if (!trueTarget.TryGetComponent(out StomachComponent stomach))
|
||||
{
|
||||
var transferAmount = ReagentUnit.Min(_transferAmount, _contents.CurrentVolume);
|
||||
var split = _contents.SplitSolution(transferAmount);
|
||||
if (stomachComponent.TryTransferSolution(split))
|
||||
return false;
|
||||
}
|
||||
|
||||
var utensils = utensilUsed != null
|
||||
? new List<UtensilComponent> {utensilUsed}
|
||||
: null;
|
||||
|
||||
if (_utensilsNeeded != UtensilType.None)
|
||||
{
|
||||
utensils = new List<UtensilComponent>();
|
||||
var types = UtensilType.None;
|
||||
|
||||
if (user.TryGetComponent(out HandsComponent hands))
|
||||
{
|
||||
_entitySystem.GetEntitySystem<AudioSystem>()
|
||||
.PlayFromEntity(_useSound, trueTarget, AudioParams.Default.WithVolume(-1f));
|
||||
trueTarget.PopupMessage(user, Loc.GetString("Nom"));
|
||||
foreach (var item in hands.GetAllHeldItems())
|
||||
{
|
||||
if (!item.Owner.TryGetComponent(out UtensilComponent utensil))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
utensils.Add(utensil);
|
||||
types |= utensil.Types;
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
if (!types.HasFlag(_utensilsNeeded))
|
||||
{
|
||||
_contents.TryAddSolution(split);
|
||||
trueTarget.PopupMessage(user, Loc.GetString("You can't eat any more!"));
|
||||
trueTarget.PopupMessage(user, Loc.GetString("You need to be holding a {0} to eat that!", _utensilsNeeded));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!InteractionChecks.InRangeUnobstructed(user, trueTarget.Transform.MapPosition))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var transferAmount = ReagentUnit.Min(_transferAmount, _contents.CurrentVolume);
|
||||
var split = _contents.SplitSolution(transferAmount);
|
||||
if (!stomach.TryTransferSolution(split))
|
||||
{
|
||||
_contents.TryAddSolution(split);
|
||||
trueTarget.PopupMessage(user, Loc.GetString("You can't eat any more!"));
|
||||
return false;
|
||||
}
|
||||
|
||||
_entitySystem.GetEntitySystem<AudioSystem>()
|
||||
.PlayFromEntity(_useSound, trueTarget, AudioParams.Default.WithVolume(-1f));
|
||||
trueTarget.PopupMessage(user, Loc.GetString("Nom"));
|
||||
|
||||
// If utensils were used
|
||||
if (utensils != null)
|
||||
{
|
||||
foreach (var utensil in utensils)
|
||||
{
|
||||
utensil.TryBreak(user);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,19 +173,27 @@ namespace Content.Server.GameObjects.Components.Nutrition
|
||||
|
||||
//We're empty. Become trash.
|
||||
var position = Owner.Transform.GridPosition;
|
||||
Owner.Delete();
|
||||
var finisher = Owner.EntityManager.SpawnEntity(_trashPrototype, position);
|
||||
if (user.TryGetComponent(out HandsComponent handsComponent) && finisher.TryGetComponent(out ItemComponent itemComponent))
|
||||
|
||||
// If the user is holding the item
|
||||
if (user.TryGetComponent(out HandsComponent handsComponent) &&
|
||||
handsComponent.IsHolding(Owner))
|
||||
{
|
||||
if (handsComponent.CanPutInHand(itemComponent))
|
||||
Owner.Delete();
|
||||
|
||||
// Put the trash in the user's hand
|
||||
if (finisher.TryGetComponent(out ItemComponent item) &&
|
||||
handsComponent.CanPutInHand(item))
|
||||
{
|
||||
handsComponent.PutInHand(itemComponent);
|
||||
return true;
|
||||
handsComponent.PutInHand(item);
|
||||
}
|
||||
}
|
||||
finisher.Transform.GridPosition = user.Transform.GridPosition;
|
||||
return true;
|
||||
else
|
||||
{
|
||||
Owner.Delete();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user