* 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.
94 lines
3.7 KiB
C#
94 lines
3.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using Content.Server.GameObjects.Components.Nutrition;
|
|
using Content.Server.GameObjects.EntitySystems;
|
|
using Content.Server.Interfaces;
|
|
using Robust.Shared.GameObjects;
|
|
using Robust.Shared.IoC;
|
|
using Robust.Shared.Localization;
|
|
using Robust.Shared.Serialization;
|
|
using Robust.Shared.ViewVariables;
|
|
|
|
namespace Content.Server.GameObjects.Components.Chemistry
|
|
{
|
|
/// <summary>
|
|
/// Gives an entity click behavior for pouring reagents into
|
|
/// other entities and being poured into. The entity must have
|
|
/// a SolutionComponent or DrinkComponent for this to work.
|
|
/// (DrinkComponent adds a SolutionComponent if one isn't present).
|
|
/// </summary>
|
|
[RegisterComponent]
|
|
class PourableComponent : Component, IAttackBy
|
|
{
|
|
#pragma warning disable 649
|
|
[Dependency] private readonly IServerNotifyManager _notifyManager;
|
|
[Dependency] private readonly ILocalizationManager _localizationManager;
|
|
#pragma warning restore 649
|
|
|
|
public override string Name => "Pourable";
|
|
|
|
private int _transferAmount;
|
|
|
|
/// <summary>
|
|
/// The amount of solution to be transferred from this solution when clicking on other solutions with it.
|
|
/// </summary>
|
|
[ViewVariables]
|
|
public int TransferAmount
|
|
{
|
|
get => _transferAmount;
|
|
set => _transferAmount = value;
|
|
}
|
|
|
|
public override void ExposeData(ObjectSerializer serializer)
|
|
{
|
|
base.ExposeData(serializer);
|
|
serializer.DataField(ref _transferAmount, "transferAmount", 5);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called when the owner of this component is clicked on with another entity.
|
|
/// The owner of this component is the target.
|
|
/// The entity used to click on this one is the attacker.
|
|
/// </summary>
|
|
/// <param name="eventArgs">Attack event args</param>
|
|
/// <returns></returns>
|
|
bool IAttackBy.AttackBy(AttackByEventArgs eventArgs)
|
|
{
|
|
//Get target and check if it can be poured into
|
|
if (!Owner.TryGetComponent<SolutionComponent>(out var targetSolution))
|
|
return false;
|
|
if (!targetSolution.CanPourIn)
|
|
return false;
|
|
|
|
//Get attack entity and check if it can pour out.
|
|
var attackEntity = eventArgs.AttackWith;
|
|
if (!attackEntity.TryGetComponent<SolutionComponent>(out var attackSolution) || !attackSolution.CanPourOut)
|
|
return false;
|
|
if (!attackEntity.TryGetComponent<PourableComponent>(out var attackPourable))
|
|
return false;
|
|
|
|
//Get transfer amount. May be smaller than _transferAmount if not enough room
|
|
int realTransferAmount = Math.Min(attackPourable.TransferAmount, targetSolution.EmptyVolume);
|
|
if (realTransferAmount <= 0) //Special message if container is full
|
|
{
|
|
_notifyManager.PopupMessage(Owner.Transform.GridPosition, eventArgs.User,
|
|
_localizationManager.GetString("Container is full"));
|
|
return false;
|
|
}
|
|
//Remove transfer amount from attacker
|
|
if (!attackSolution.TryRemoveSolution(realTransferAmount, out var removedSolution))
|
|
return false;
|
|
|
|
//Add poured solution to this solution
|
|
if (!targetSolution.TryAddSolution(removedSolution))
|
|
return false;
|
|
|
|
_notifyManager.PopupMessage(Owner.Transform.GridPosition, eventArgs.User,
|
|
_localizationManager.GetString("Transferred {0}u", removedSolution.TotalVolume));
|
|
|
|
return true;
|
|
}
|
|
}
|
|
}
|