using System.Runtime.InteropServices;
using Content.Shared.Damage;
using Content.Shared.Doors.Systems;
using Content.Shared.Tools;
using JetBrains.Annotations;
using Robust.Shared.Audio;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
using Robust.Shared.Timing;
using DrawDepthTag = Robust.Shared.GameObjects.DrawDepth;
namespace Content.Shared.Doors.Components;
[NetworkedComponent]
[RegisterComponent]
public sealed partial class DoorComponent : Component
{
///
/// The current state of the door -- whether it is open, closed, opening, or closing.
///
///
/// This should never be set directly, use instead.
///
[ViewVariables(VVAccess.ReadWrite)]
[DataField("state")]
[Access(typeof(SharedDoorSystem))]
public DoorState State = DoorState.Closed;
#region Timing
// if you want do dynamically adjust these times, you need to add networking for them. So for now, they are all
// read-only.
///
/// Closing time until impassable. Total time is this plus .
///
[DataField("closeTimeOne")]
public TimeSpan CloseTimeOne = TimeSpan.FromSeconds(0.4f);
///
/// Closing time until fully closed. Total time is this plus .
///
[DataField("closeTimeTwo")]
public TimeSpan CloseTimeTwo = TimeSpan.FromSeconds(0.2f);
///
/// Opening time until passable. Total time is this plus .
///
[DataField("openTimeOne")]
public TimeSpan OpenTimeOne = TimeSpan.FromSeconds(0.4f);
///
/// Opening time until fully open. Total time is this plus .
///
[DataField("openTimeTwo")]
public TimeSpan OpenTimeTwo = TimeSpan.FromSeconds(0.2f);
///
/// Interval between deny sounds & visuals;
///
[DataField("denyDuration")]
public TimeSpan DenyDuration = TimeSpan.FromSeconds(0.45f);
[DataField("emagDuration")]
public TimeSpan EmagDuration = TimeSpan.FromSeconds(0.8f);
///
/// When the door is active, this is the time when the state will next update.
///
public TimeSpan? NextStateChange;
///
/// Whether the door is currently partially closed or open. I.e., when the door is "closing" and is already opaque,
/// but not yet actually closed.
///
[DataField("partial")]
public bool Partial;
#endregion
#region Sounds
///
/// Sound to play when the door opens.
///
[DataField("openSound")]
public SoundSpecifier? OpenSound;
///
/// Sound to play when the door closes.
///
[DataField("closeSound")]
public SoundSpecifier? CloseSound;
///
/// Sound to play if the door is denied.
///
[DataField("denySound")]
public SoundSpecifier? DenySound;
///
/// Sound to play when a disarmed (hands comp with 0 hands) entity opens the door. What?
///
[DataField("tryOpenDoorSound")]
public SoundSpecifier TryOpenDoorSound = new SoundPathSpecifier("/Audio/Effects/bang.ogg");
///
/// Sound to play when door has been emagged or possibly electrically tampered
///
[DataField("sparkSound")]
public SoundSpecifier SparkSound = new SoundCollectionSpecifier("sparks");
#endregion
#region Crushing
///
/// This is how long a door-crush will stun you. This also determines how long it takes the door to open up
/// again. Total stun time is actually given by this plus .
///
[DataField("doorStunTime")]
public TimeSpan DoorStunTime = TimeSpan.FromSeconds(2f);
[DataField("crushDamage")]
public DamageSpecifier? CrushDamage;
///
/// If false, this door is incapable of crushing entities. This just determines whether it will apply damage and
/// stun, not whether it can close despite entities being in the way.
///
[DataField("canCrush")]
public bool CanCrush = true;
///
/// Whether to check for colliding entities before closing. This may be overridden by other system by subscribing to
/// . For example, hacked airlocks will set this to false.
///
[DataField("performCollisionCheck")]
public bool PerformCollisionCheck = true;
///
/// List of EntityUids of entities we're currently crushing. Cleared in OnPartialOpen().
///
[DataField("currentlyCrushing")]
public HashSet CurrentlyCrushing = new();
#endregion
#region Graphics
///
/// The key used when playing door opening/closing/emagging/deny animations.
///
public const string AnimationKey = "door_animation";
///
/// The sprite state used for the door when it's open.
///
[DataField("openSpriteState")]
[ViewVariables(VVAccess.ReadWrite)]
public string OpenSpriteState = "open";
///
/// The sprite states used for the door while it's open.
///
[ViewVariables(VVAccess.ReadOnly)]
public List<(DoorVisualLayers, string)> OpenSpriteStates = default!;
///
/// The sprite state used for the door when it's closed.
///
[DataField("closedSpriteState")]
[ViewVariables(VVAccess.ReadWrite)]
public string ClosedSpriteState = "closed";
///
/// The sprite states used for the door while it's closed.
///
[ViewVariables(VVAccess.ReadOnly)]
public List<(DoorVisualLayers, string)> ClosedSpriteStates = default!;
///
/// The sprite state used for the door when it's opening.
///
[DataField("openingSpriteState")]
public string OpeningSpriteState = "opening";
///
/// The sprite state used for the door when it's closing.
///
[DataField("closingSpriteState")]
public string ClosingSpriteState = "closing";
///
/// The sprite state used for the door when it's being emagged.
///
[DataField("emaggingSpriteState")]
public string EmaggingSpriteState = "emagging";
///
/// The sprite state used for the door when it's open.
///
[DataField("openingAnimationTime")]
public float OpeningAnimationTime = 0.8f;
///
/// The sprite state used for the door when it's open.
///
[DataField("closingAnimationTime")]
public float ClosingAnimationTime = 0.8f;
///
/// The sprite state used for the door when it's open.
///
[DataField("emaggingAnimationTime")]
public float EmaggingAnimationTime = 1.5f;
///
/// The animation used when the door opens.
///
public object OpeningAnimation = default!;
///
/// The animation used when the door closes.
///
public object ClosingAnimation = default!;
///
/// The animation used when the door denies access.
///
public object DenyingAnimation = default!;
///
/// The animation used when the door is emagged.
///
public object EmaggingAnimation = default!;
#endregion Graphics
#region Serialization
///
/// Time until next state change. Because apparently might not get saved/restored.
///
[DataField("SecondsUntilStateChange")]
private float? SecondsUntilStateChange
{
[UsedImplicitly]
get
{
if (NextStateChange == null)
{
return null;
}
var curTime = IoCManager.Resolve().CurTime;
return (float) (NextStateChange.Value - curTime).TotalSeconds;
}
set
{
if (value == null || value.Value > 0)
return;
NextStateChange = IoCManager.Resolve().CurTime + TimeSpan.FromSeconds(value.Value);
}
}
#endregion
[DataField("canPry"), ViewVariables(VVAccess.ReadWrite)]
public bool CanPry = true;
[DataField("pryingQuality", customTypeSerializer: typeof(PrototypeIdSerializer))]
public string PryingQuality = "Prying";
///
/// Default time that the door should take to pry open.
///
[DataField("pryTime"), ViewVariables(VVAccess.ReadWrite)]
public float PryTime = 1.5f;
[DataField("changeAirtight")]
public bool ChangeAirtight = true;
///
/// Whether the door blocks light.
///
[ViewVariables(VVAccess.ReadWrite)]
[DataField("occludes")]
public bool Occludes = true;
///
/// Whether the door will open when it is bumped into.
///
[ViewVariables(VVAccess.ReadWrite)]
[DataField("bumpOpen")]
public bool BumpOpen = true;
///
/// Whether the door will open when it is activated or clicked.
///
[ViewVariables(VVAccess.ReadWrite)]
[DataField("clickOpen")]
public bool ClickOpen = true;
[DataField("openDrawDepth", customTypeSerializer: typeof(ConstantSerializer))]
public int OpenDrawDepth = (int) DrawDepth.DrawDepth.Doors;
[DataField("closedDrawDepth", customTypeSerializer: typeof(ConstantSerializer))]
public int ClosedDrawDepth = (int) DrawDepth.DrawDepth.Doors;
}
[Serializable, NetSerializable]
public enum DoorState : byte
{
Closed,
Closing,
Open,
Opening,
Welded,
Denying,
Emagging
}
[Serializable, NetSerializable]
public enum DoorVisuals : byte
{
State,
Powered,
BoltLights,
EmergencyLights,
ClosedLights,
BaseRSI,
}
public enum DoorVisualLayers : byte
{
Base,
BaseUnlit,
BaseBolted,
BaseEmergencyAccess,
}
[Serializable, NetSerializable]
public sealed class DoorComponentState : ComponentState
{
public readonly DoorState DoorState;
public readonly HashSet CurrentlyCrushing;
public readonly TimeSpan? NextStateChange;
public readonly bool Partial;
public DoorComponentState(DoorComponent door)
{
DoorState = door.State;
CurrentlyCrushing = door.CurrentlyCrushing;
NextStateChange = door.NextStateChange;
Partial = door.Partial;
}
}