Files
OldThink/Content.Server/GameObjects/Components/Nutrition/DrinkComponent.cs

149 lines
5.2 KiB
C#
Raw Normal View History

using System;
2020-04-22 04:23:12 +10:00
using System.Linq;
using Content.Server.GameObjects.Components.Chemistry;
using Content.Server.GameObjects.Components.Sound;
using Content.Server.GameObjects.EntitySystems;
using Content.Server.Utility;
using Content.Shared.Chemistry;
using Content.Shared.GameObjects.Components.Nutrition;
using Content.Shared.Interfaces;
using Content.Shared.Maths;
using Robust.Server.GameObjects;
2020-04-22 04:23:12 +10:00
using Robust.Server.GameObjects.EntitySystems;
2020-05-22 16:23:18 -05:00
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
using Robust.Shared.Utility;
namespace Content.Server.GameObjects.Components.Nutrition
{
[RegisterComponent]
public class DrinkComponent : Component, IAfterAttack, IUse
{
#pragma warning disable 649
[Dependency] private readonly ILocalizationManager _localizationManager;
#pragma warning restore 649
public override string Name => "Drink";
[ViewVariables]
private SolutionComponent _contents;
private AppearanceComponent _appearanceComponent;
[ViewVariables]
private string _useSound;
[ViewVariables]
private string _finishPrototype;
2020-04-05 11:36:12 +02:00
public ReagentUnit TransferAmount => _transferAmount;
[ViewVariables]
2020-04-05 11:36:12 +02:00
private ReagentUnit _transferAmount = ReagentUnit.New(2);
2020-04-05 11:36:12 +02:00
public ReagentUnit MaxVolume
{
get => _contents.MaxVolume;
set => _contents.MaxVolume = value;
}
private bool _despawnOnFinish;
private bool _drinking;
public int UsesLeft()
{
// In case transfer amount exceeds volume left
if (_contents.CurrentVolume == 0)
{
return 0;
}
2020-04-05 11:36:12 +02:00
return Math.Max(1, (int)Math.Ceiling((_contents.CurrentVolume / _transferAmount).Float()));
}
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _useSound, "use_sound", "/Audio/items/drink.ogg");
// E.g. cola can when done or clear bottle, whatever
// Currently this will enforce it has the same volume but this may change. - TODO: this should be implemented in a separate component
serializer.DataField(ref _despawnOnFinish, "despawn_empty", false);
serializer.DataField(ref _finishPrototype, "spawn_on_finish", null);
}
protected override void Startup()
{
base.Startup();
_contents = Owner.GetComponent<SolutionComponent>();
_contents.Capabilities = SolutionCaps.PourIn
| SolutionCaps.PourOut
| SolutionCaps.Injectable;
_drinking = false;
Owner.TryGetComponent(out AppearanceComponent appearance);
_appearanceComponent = appearance;
_appearanceComponent?.SetData(SharedFoodComponent.FoodVisuals.MaxUses, MaxVolume.Float());
_updateAppearance();
}
private void _updateAppearance()
{
_appearanceComponent?.SetData(SharedFoodComponent.FoodVisuals.Visual, _contents.CurrentVolume.Float());
}
bool IUse.UseEntity(UseEntityEventArgs eventArgs)
{
UseDrink(eventArgs.User);
return true;
}
void IAfterAttack.AfterAttack(AfterAttackEventArgs eventArgs)
{
if (!InteractionChecks.InRangeUnobstructed(eventArgs)) return;
UseDrink(eventArgs.Attacked);
}
private void UseDrink(IEntity targetEntity)
{
if (targetEntity == null)
{
return;
}
if (UsesLeft() == 0 && !_despawnOnFinish)
{
targetEntity.PopupMessage(targetEntity, _localizationManager.GetString("Empty"));
return;
}
if (targetEntity.TryGetComponent(out StomachComponent stomachComponent))
{
_drinking = true;
2020-04-05 11:36:12 +02:00
var transferAmount = ReagentUnit.Min(_transferAmount, _contents.CurrentVolume);
var split = _contents.SplitSolution(transferAmount);
if (stomachComponent.TryTransferSolution(split))
{
2020-04-22 04:23:12 +10:00
// When we split Finish gets called which may delete the can so need to use the entity system for sound
if (_useSound != null)
{
var audioSystem = EntitySystem.Get<AudioSystem>();
2020-05-22 16:23:18 -05:00
audioSystem.Play(_useSound, Owner, AudioParams.Default.WithVolume(-2f));
targetEntity.PopupMessage(targetEntity, _localizationManager.GetString("Slurp"));
}
}
else
{
// Add it back in
_contents.TryAddSolution(split);
targetEntity.PopupMessage(targetEntity, _localizationManager.GetString("Can't drink"));
}
_drinking = false;
}
Add solution pouring / click-transfer (#574) * Add click-based solution transfer For example, clicking on a beaker with a soda can to transfer the soda to the beaker. Works on plain solution containers like beakers and also on open drink containers like soda cans as long as they have the `PourIn` and `PourOut` solution capabilities. If no `SolutionComponent` is added to a drink entity, the `DrinkComponent` will give the entity one. This PR extends that behavior slightly by also giving these default `SolutionComponent`'s the proper capabilities for pouring in/out. * Improve fix for poured drinks not immediately disappearing Instead of making `DrinkComponent.Use` public this splits out the code important to both users and made that function public, leaving `Use` private. * Shorten solution transfer popup * Make code review changes - Move pouring code from SolutionComponent to new PourableComponent. Added PourableComponent to client ignore list and added to existing container prototypes. - Added EmptyVolume property to shared SolutionComponent for convenience. - Removed DrinkComponent fix from pouring AttackBy code. Instead DrinkComponent subscribes to the SolutionChanged action and updates its self when necessary. - Fixed pouring being able to add more than a containers max volume and sometimes deleting reagents. - Added message for when a container is full. * More code review changes - Remove IAttackBy ComponentReference attribute in PourableComponent - Remove _transferAmount from shared SolutionComponent. Left over var from previous commit not being used anymore.
2020-01-28 20:07:02 -05:00
}
}
}