Changed drinks to use ECS (#4948)
Co-authored-by: Paul Ritter <ritter.paul1@googlemail.com> Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
@@ -1,14 +1,23 @@
|
||||
using System.Linq;
|
||||
using Content.Server.Body.Behavior;
|
||||
using Content.Server.Chemistry.Components.SolutionManager;
|
||||
using Content.Server.Chemistry.EntitySystems;
|
||||
using Content.Server.Fluids.Components;
|
||||
using Content.Server.Nutrition.Components;
|
||||
using Content.Shared.Body.Components;
|
||||
using Content.Shared.Chemistry.Reagent;
|
||||
using Content.Shared.Examine;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Interaction.Helpers;
|
||||
using Content.Shared.Nutrition.Components;
|
||||
using Content.Shared.Popups;
|
||||
using Content.Shared.Throwing;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
@@ -27,6 +36,97 @@ namespace Content.Server.Nutrition.EntitySystems
|
||||
SubscribeLocalEvent<DrinkComponent, SolutionChangedEvent>(OnSolutionChange);
|
||||
SubscribeLocalEvent<DrinkComponent, ComponentInit>(OnDrinkInit);
|
||||
SubscribeLocalEvent<DrinkComponent, LandEvent>(HandleLand);
|
||||
SubscribeLocalEvent<DrinkComponent, UseInHandEvent>(OnUse);
|
||||
SubscribeLocalEvent<DrinkComponent, AfterInteractEvent>(AfterInteract);
|
||||
SubscribeLocalEvent<DrinkComponent, ExaminedEvent>(OnExamined);
|
||||
}
|
||||
|
||||
public bool IsEmpty(EntityUid uid, DrinkComponent? component = null)
|
||||
{
|
||||
if(!Resolve(uid, ref component))
|
||||
return true;
|
||||
|
||||
return _solutionContainerSystem.DrainAvailable(uid) <= 0;
|
||||
}
|
||||
|
||||
private void OnExamined(EntityUid uid, DrinkComponent component, ExaminedEvent args)
|
||||
{
|
||||
if (!component.Opened || !args.IsInDetailsRange)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var color = IsEmpty(uid, component) ? "gray" : "yellow";
|
||||
var openedText =
|
||||
Loc.GetString(IsEmpty(uid, component) ? "drink-component-on-examine-is-empty" : "drink-component-on-examine-is-opened");
|
||||
args.Message.AddMarkup(Loc.GetString("drink-component-on-examine-details-text", ("colorName", color), ("text", openedText)));
|
||||
}
|
||||
|
||||
private void SetOpen(EntityUid uid, bool opened = false, DrinkComponent? component = null)
|
||||
{
|
||||
if(!Resolve(uid, ref component))
|
||||
return;
|
||||
|
||||
if (opened == component.Opened)
|
||||
return;
|
||||
|
||||
component.Opened = opened;
|
||||
|
||||
if (!_solutionContainerSystem.TryGetSolution(uid, component.SolutionName, out _))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (EntityManager.TryGetComponent<AppearanceComponent>(uid, out var appearance))
|
||||
{
|
||||
appearance.SetData(DrinkCanStateVisual.Opened, opened);
|
||||
}
|
||||
|
||||
if (opened)
|
||||
{
|
||||
EntityManager.EnsureComponent<RefillableSolutionComponent>(uid).Solution= component.SolutionName;
|
||||
EntityManager.EnsureComponent<DrainableSolutionComponent>(uid).Solution= component.SolutionName;
|
||||
}
|
||||
else
|
||||
{
|
||||
EntityManager.RemoveComponent<RefillableSolutionComponent>(uid);
|
||||
EntityManager.RemoveComponent<DrainableSolutionComponent>(uid);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void AfterInteract(EntityUid uid, DrinkComponent component, AfterInteractEvent args)
|
||||
{
|
||||
if (args.Handled)
|
||||
return;
|
||||
|
||||
if (args.Target == null)
|
||||
return;
|
||||
|
||||
if (TryUseDrink(uid, args.User, args.Target, true, component))
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
private void OnUse(EntityUid uid, DrinkComponent component, UseInHandEvent args)
|
||||
{
|
||||
if (args.Handled) return;
|
||||
if (!component.Opened)
|
||||
{
|
||||
//Do the opening stuff like playing the sounds.
|
||||
SoundSystem.Play(Filter.Pvs(args.User), component.OpenSounds.GetSound(), args.User, AudioParams.Default);
|
||||
|
||||
SetOpen(uid, true, component);
|
||||
return;
|
||||
}
|
||||
|
||||
if (_solutionContainerSystem.DrainAvailable(uid) <= 0)
|
||||
{
|
||||
args.User.PopupMessage(Loc.GetString("drink-component-on-use-is-empty", ("owner", EntityManager.GetEntity(uid))));
|
||||
return;
|
||||
}
|
||||
|
||||
if (TryUseDrink(uid, args.User, args.User, false, component))
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
private void HandleLand(EntityUid uid, DrinkComponent component, LandEvent args)
|
||||
@@ -50,17 +150,16 @@ namespace Content.Server.Nutrition.EntitySystems
|
||||
|
||||
private void OnDrinkInit(EntityUid uid, DrinkComponent component, ComponentInit args)
|
||||
{
|
||||
component.Opened = component.DefaultToOpened;
|
||||
SetOpen(uid, component.DefaultToOpened, component);
|
||||
|
||||
var owner = EntityManager.GetEntity(uid);
|
||||
if (owner.TryGetComponent(out DrainableSolutionComponent? existingDrainable))
|
||||
if (EntityManager.TryGetComponent(uid, out DrainableSolutionComponent? existingDrainable))
|
||||
{
|
||||
// Beakers have Drink component but they should use the existing Drainable
|
||||
component.SolutionName = existingDrainable.Solution;
|
||||
}
|
||||
else
|
||||
{
|
||||
_solutionContainerSystem.EnsureSolution(owner, component.SolutionName);
|
||||
_solutionContainerSystem.EnsureSolution(uid, component.SolutionName);
|
||||
}
|
||||
|
||||
UpdateAppearance(component);
|
||||
@@ -73,15 +172,81 @@ namespace Content.Server.Nutrition.EntitySystems
|
||||
|
||||
public void UpdateAppearance(DrinkComponent component)
|
||||
{
|
||||
if (!component.Owner.TryGetComponent(out AppearanceComponent? appearance) ||
|
||||
!component.Owner.HasComponent<SolutionContainerManagerComponent>())
|
||||
if (!EntityManager.TryGetComponent(component.OwnerUid, out AppearanceComponent? appearance) ||
|
||||
!EntityManager.HasComponent<SolutionContainerManagerComponent>(component.OwnerUid))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var drainAvailable = Get<SolutionContainerSystem>().DrainAvailable(component.Owner);
|
||||
var drainAvailable = _solutionContainerSystem.DrainAvailable(component.OwnerUid);
|
||||
appearance.SetData(FoodVisuals.Visual, drainAvailable.Float());
|
||||
appearance.SetData(DrinkCanStateVisual.Opened, component.Opened);
|
||||
}
|
||||
|
||||
private bool TryUseDrink(EntityUid uid, IEntity user, IEntity target, bool forced, DrinkComponent? component = null)
|
||||
{
|
||||
if(!Resolve(uid, ref component))
|
||||
return false;
|
||||
|
||||
var owner = component.Owner;
|
||||
|
||||
if (!component.Opened)
|
||||
{
|
||||
target.PopupMessage(Loc.GetString("drink-component-try-use-drink-not-open", ("owner", owner)));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!_solutionContainerSystem.TryGetDrainableSolution(component.OwnerUid, out var interactions) ||
|
||||
interactions.DrainAvailable <= 0)
|
||||
{
|
||||
if (!forced)
|
||||
{
|
||||
target.PopupMessage(Loc.GetString("drink-component-try-use-drink-is-empty", ("entity", owner)));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!EntityManager.TryGetComponent(target.Uid, out SharedBodyComponent? body) ||
|
||||
!body.TryGetMechanismBehaviors<StomachBehavior>(out var stomachs))
|
||||
{
|
||||
target.PopupMessage(Loc.GetString("drink-component-try-use-drink-cannot-drink", ("owner", owner)));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (user != target && !user.InRangeUnobstructed(target, popup: true))
|
||||
return false;
|
||||
|
||||
var transferAmount = ReagentUnit.Min(component.TransferAmount, interactions.DrainAvailable);
|
||||
var drain = _solutionContainerSystem.Drain(owner.Uid, interactions, transferAmount);
|
||||
var firstStomach = stomachs.FirstOrDefault(stomach => stomach.CanTransferSolution(drain));
|
||||
|
||||
// All stomach are full or can't handle whatever solution we have.
|
||||
if (firstStomach == null)
|
||||
{
|
||||
target.PopupMessage(Loc.GetString("drink-component-try-use-drink-had-enough", ("owner", owner)));
|
||||
|
||||
if (EntityManager.HasComponent<RefillableSolutionComponent>(uid))
|
||||
{
|
||||
drain.SpillAt(target, "PuddleSmear");
|
||||
return false;
|
||||
}
|
||||
|
||||
_solutionContainerSystem.Refill(owner.Uid, interactions, drain);
|
||||
return false;
|
||||
}
|
||||
|
||||
SoundSystem.Play(Filter.Pvs(target), component.UseSound.GetSound(), target, AudioParams.Default.WithVolume(-2f));
|
||||
|
||||
target.PopupMessage(Loc.GetString("drink-component-try-use-drink-success-slurp"));
|
||||
|
||||
// TODO: Account for partial transfer.
|
||||
|
||||
drain.DoEntityReaction(target, ReactionMethod.Ingestion);
|
||||
|
||||
firstStomach.TryTransferSolution(drain);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user