Files
OldThink/Content.Server/GameObjects/Components/Stack/StackComponent.cs

144 lines
4.3 KiB
C#
Raw Normal View History

#nullable enable
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
2020-01-09 00:27:52 +01:00
using Content.Shared.GameObjects.Components;
using Content.Shared.GameObjects.EntitySystems;
using Content.Shared.Interfaces;
using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Shared.GameObjects;
using Robust.Shared.Localization;
using Robust.Shared.Map;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Stack
{
// TODO: Naming and presentation and such could use some improvement.
2019-07-31 15:02:36 +02:00
[RegisterComponent]
[ComponentReference(typeof(SharedStackComponent))]
2020-05-23 17:23:25 +02:00
public class StackComponent : SharedStackComponent, IInteractUsing, IExamine
{
private bool _throwIndividually = false;
[ViewVariables(VVAccess.ReadWrite)]
public bool ThrowIndividually
{
get => _throwIndividually;
private set
{
_throwIndividually = value;
Dirty();
}
}
public void Add(int amount)
{
Count += amount;
}
/// <summary>
/// Try to use an amount of items on this stack.
/// </summary>
/// <param name="amount"></param>
/// <returns>True if there were enough items to remove, false if not in which case nothing was changed.</returns>
public bool Use(int amount)
{
if (Count >= amount)
{
Count -= amount;
return true;
}
return false;
}
/// <summary>
/// Attempts to split this stack in two.
/// </summary>
/// <param name="amount">amount the new stack will have</param>
/// <param name="spawnPosition">the position the new stack will spawn at</param>
/// <param name="stack">the new stack</param>
/// <returns></returns>
public bool Split(int amount, EntityCoordinates spawnPosition, [NotNullWhen(true)] out IEntity? stack)
{
if (Count >= amount)
{
Count -= amount;
Removed EntityManager member variable from Components and EntitySystems (#2502) * Removed EntityManager member variable from Components and EntitySystems * Removed EntityManager with minor corecctions * Update PathfindingSystem.cs * Update InteractionSystem.cs * Update Content.Server/GameObjects/EntitySystems/Click/ExamineSystem.cs Co-authored-by: Víctor Aguilera Puerto <6766154+Zumorica@users.noreply.github.com> * Update Content.Client/GameObjects/Components/Suspicion/SuspicionRoleComponent.cs Co-authored-by: Clyybber <darkmine956@gmail.com> * Update Content.Client/GameObjects/Components/Suspicion/TraitorOverlay.cs Co-authored-by: Clyybber <darkmine956@gmail.com> * Update Content.Server/GameObjects/Components/Buckle/BuckleComponent.cs Co-authored-by: Clyybber <darkmine956@gmail.com> * Update Content.Server/GameObjects/Components/Buckle/BuckleComponent.cs Co-authored-by: Clyybber <darkmine956@gmail.com> * Update Content.Server/GameObjects/Components/PDA/PDAComponent.cs Co-authored-by: Clyybber <darkmine956@gmail.com> * Update Content.Server/GameObjects/Components/Singularity/SingularityComponent.cs Co-authored-by: Clyybber <darkmine956@gmail.com> * Update Content.Server/GameObjects/Components/Singularity/SingularityComponent.cs Co-authored-by: Clyybber <darkmine956@gmail.com> * Update Content.Server/GameObjects/EntitySystems/AI/Pathfinding/PathfindingSystem.cs Co-authored-by: Clyybber <darkmine956@gmail.com> * Update Content.Server/GameObjects/EntitySystems/Click/ExamineSystem.cs Co-authored-by: Clyybber <darkmine956@gmail.com> * Update Content.Server/GameObjects/EntitySystems/Click/InteractionSystem.cs Co-authored-by: Clyybber <darkmine956@gmail.com> * Update Content.Server/GameObjects/EntitySystems/Click/InteractionSystem.cs Co-authored-by: Clyybber <darkmine956@gmail.com> * Update Content.Server/GameObjects/Components/Stack/StackComponent.cs Co-authored-by: Clyybber <darkmine956@gmail.com> Co-authored-by: Víctor Aguilera Puerto <6766154+Zumorica@users.noreply.github.com> Co-authored-by: Clyybber <darkmine956@gmail.com> Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com>
2020-11-18 15:45:53 +01:00
stack = Owner.EntityManager.SpawnEntity(Owner.Prototype?.ID, spawnPosition);
if (stack.TryGetComponent(out StackComponent? stackComp))
{
stackComp.Count = amount;
}
return true;
}
stack = null;
return false;
}
2021-02-04 17:44:49 +01:00
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
{
if (!eventArgs.Using.TryGetComponent<StackComponent>(out var stack))
return false;
if (!stack.StackTypeId.Equals(StackTypeId))
{
return false;
}
var toTransfer = Math.Min(Count, stack.AvailableSpace);
Count -= toTransfer;
stack.Add(toTransfer);
var popupPos = eventArgs.ClickLocation;
if (popupPos == EntityCoordinates.Invalid)
{
popupPos = eventArgs.User.Transform.Coordinates;
}
if (toTransfer > 0)
{
popupPos.PopupMessage(eventArgs.User, $"+{toTransfer}");
if (stack.AvailableSpace == 0)
{
eventArgs.Using.SpawnTimer(
300,
() => popupPos.PopupMessage(
eventArgs.User,
2021-02-28 20:02:03 -04:00
Loc.GetString("comp-stack-becomes-full")
)
);
}
}
else if (toTransfer == 0 && stack.AvailableSpace == 0)
{
popupPos.PopupMessage(
eventArgs.User,
2021-02-28 20:02:03 -04:00
Loc.GetString("comp-stack-already-full")
);
}
return true;
}
void IExamine.Examine(FormattedMessage message, bool inDetailsRange)
{
if (inDetailsRange)
{
message.AddMarkup(
Loc.GetString(
2021-02-28 20:02:03 -04:00
"comp-stack-examine-detail-count",
("count", Count),
("markupCountColor", "lightgray")
)
);
}
}
}
}