2021-02-22 04:08:17 +00:00
|
|
|
using System;
|
2021-05-26 10:20:57 +02:00
|
|
|
using Content.Shared.GameObjects.EntitySystems;
|
2021-02-25 06:18:29 +01:00
|
|
|
using Content.Shared.Stacks;
|
2020-01-09 00:27:52 +01:00
|
|
|
using Robust.Shared.GameObjects;
|
2021-02-18 09:09:07 +01:00
|
|
|
using Robust.Shared.Players;
|
2020-01-09 00:27:52 +01:00
|
|
|
using Robust.Shared.Serialization;
|
2021-03-05 01:08:38 +01:00
|
|
|
using Robust.Shared.Serialization.Manager.Attributes;
|
2021-05-26 10:20:57 +02:00
|
|
|
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
2020-04-12 01:15:16 +02:00
|
|
|
using Robust.Shared.ViewVariables;
|
2020-01-09 00:27:52 +01:00
|
|
|
|
|
|
|
|
namespace Content.Shared.GameObjects.Components
|
|
|
|
|
{
|
2021-03-05 01:08:38 +01:00
|
|
|
public abstract class SharedStackComponent : Component, ISerializationHooks
|
2020-01-09 00:27:52 +01:00
|
|
|
{
|
|
|
|
|
public sealed override string Name => "Stack";
|
|
|
|
|
public sealed override uint? NetID => ContentNetIDs.STACK;
|
|
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("max")]
|
|
|
|
|
private int _maxCount = 30;
|
2020-04-12 01:15:16 +02:00
|
|
|
|
|
|
|
|
[ViewVariables(VVAccess.ReadWrite)]
|
2021-05-26 10:20:57 +02:00
|
|
|
[DataField("stackType", required:true, customTypeSerializer:typeof(PrototypeIdSerializer<StackPrototype>))]
|
|
|
|
|
public string StackTypeId { get; private set; } = string.Empty;
|
2020-04-12 01:15:16 +02:00
|
|
|
|
2021-05-26 10:20:57 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Current stack count.
|
|
|
|
|
/// Do NOT set this directly, raise the <see cref="StackChangeCountEvent"/> event instead.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ViewVariables(VVAccess.ReadWrite)]
|
|
|
|
|
[DataField("count")]
|
|
|
|
|
public int Count { get; set; } = 30;
|
2020-04-12 01:15:16 +02:00
|
|
|
|
|
|
|
|
[ViewVariables]
|
|
|
|
|
public int MaxCount
|
|
|
|
|
{
|
|
|
|
|
get => _maxCount;
|
|
|
|
|
private set
|
|
|
|
|
{
|
2021-05-26 10:20:57 +02:00
|
|
|
if (_maxCount == value)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-04-12 01:15:16 +02:00
|
|
|
_maxCount = value;
|
|
|
|
|
Dirty();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
[ViewVariables]
|
2021-05-26 10:20:57 +02:00
|
|
|
public int AvailableSpace => MaxCount - Count;
|
2020-04-12 01:15:16 +02:00
|
|
|
|
2021-02-18 09:09:07 +01:00
|
|
|
public override ComponentState GetComponentState(ICommonSession player)
|
2020-04-12 01:15:16 +02:00
|
|
|
{
|
|
|
|
|
return new StackComponentState(Count, MaxCount);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-15 21:55:49 +01:00
|
|
|
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
|
2020-04-12 01:15:16 +02:00
|
|
|
{
|
2020-11-26 14:33:31 +01:00
|
|
|
if (curState is not StackComponentState cast)
|
2020-04-12 01:15:16 +02:00
|
|
|
return;
|
|
|
|
|
|
2021-05-26 10:20:57 +02:00
|
|
|
// This will change the count and call events.
|
|
|
|
|
Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, new StackChangeCountEvent(cast.Count));
|
2020-04-12 01:15:16 +02:00
|
|
|
MaxCount = cast.MaxCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-01-09 00:27:52 +01:00
|
|
|
[Serializable, NetSerializable]
|
2020-04-12 01:15:16 +02:00
|
|
|
private sealed class StackComponentState : ComponentState
|
2020-01-09 00:27:52 +01:00
|
|
|
{
|
|
|
|
|
public int Count { get; }
|
|
|
|
|
public int MaxCount { get; }
|
|
|
|
|
|
2020-06-08 16:49:05 -04:00
|
|
|
public StackComponentState(int count, int maxCount) : base(ContentNetIDs.STACK)
|
2020-01-09 00:27:52 +01:00
|
|
|
{
|
|
|
|
|
Count = count;
|
|
|
|
|
MaxCount = maxCount;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|