Files

288 lines
11 KiB
C#
Raw Permalink Normal View History

2024-02-27 18:22:15 +03:00
using System.Diagnostics.CodeAnalysis;
using System.Linq;
2024-02-26 21:43:11 +03:00
using Content.Server.Chat.Managers;
2024-02-23 18:52:03 +03:00
using Content.Server.Chat.Systems;
2024-02-26 21:43:11 +03:00
using Content.Server.DoAfter;
2024-02-23 18:52:03 +03:00
using Content.Server.EUI;
using Content.Shared._Amour.InteractionPanel;
2024-02-27 18:22:15 +03:00
using Content.Shared._Amour.InteractionPanel.Checks;
using Content.Shared.ActionBlocker;
2024-02-26 21:43:11 +03:00
using Content.Shared.Chat;
using Content.Shared.DoAfter;
2024-02-24 20:54:20 +03:00
using Content.Shared.Humanoid;
2024-02-27 18:22:15 +03:00
using Content.Shared.Interaction.Events;
2024-02-24 20:54:20 +03:00
using Content.Shared.Mind;
using Content.Shared.Mobs.Systems;
2024-02-27 18:22:15 +03:00
using Content.Shared.Movement.Events;
2024-02-20 14:28:01 +03:00
using Content.Shared.Verbs;
2024-02-23 18:52:03 +03:00
using Robust.Server.Audio;
2024-02-20 14:28:01 +03:00
using Robust.Server.Player;
2024-02-24 20:54:20 +03:00
using Robust.Shared.Enums;
2024-02-23 18:52:03 +03:00
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Timing;
2024-02-20 14:28:01 +03:00
namespace Content.Server._Amour.InteractionPanel;
public sealed class InteractionPanelSystem : EntitySystem
{
[Dependency] private readonly EuiManager _eui = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
2024-02-23 18:52:03 +03:00
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly ChatSystem _chatSystem = default!;
[Dependency] private readonly AudioSystem _audioSystem = default!;
[Dependency] private readonly IRobustRandom _robustRandom = default!;
2024-02-26 21:43:11 +03:00
[Dependency] private readonly IChatManager _chatManager = default!;
[Dependency] private readonly DoAfterSystem _doAfterSystem = default!;
2024-02-27 18:22:15 +03:00
[Dependency] private readonly ActionBlockerSystem _actionBlockerSystem = default!;
[Dependency] private readonly MobStateSystem _mobStateSystem = default!;
2024-02-23 18:52:03 +03:00
2024-02-20 14:28:01 +03:00
public override void Initialize()
{
2025-02-23 22:36:27 +03:00
SubscribeLocalEvent<InteractionPanelComponent, GetVerbsEvent<Verb>>(OnVerb);
SubscribeLocalEvent<InteractionPanelComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<InteractionPanelComponent, PanelDoAfterEvent>(OnPanel);
SubscribeLocalEvent<InteractionPanelComponent, ChangeDirectionAttemptEvent>(OnCancelable);
SubscribeLocalEvent<InteractionPanelComponent, UpdateCanMoveEvent>(OnCancelable);
2024-02-27 18:22:15 +03:00
}
private void OnCancelable(EntityUid uid, InteractionPanelComponent component, CancellableEntityEventArgs args)
{
if (component.IsActive || component.IsBlocked)
{
args.Cancel();
}
2024-02-26 21:43:11 +03:00
}
private void OnPanel(EntityUid uid, InteractionPanelComponent component, PanelDoAfterEvent args)
{
component.IsBlocked = false;
2025-02-23 22:36:27 +03:00
if (args.Cancelled
2024-02-26 21:43:11 +03:00
|| !_prototypeManager.TryIndex<InteractionPrototype>(args.Prototype, out var prototype)
|| !TryComp<InteractionPanelComponent>(args.Target, out var targetInteractionPanelComponent))
return;
2025-02-23 22:36:27 +03:00
Interact(new Entity<InteractionPanelComponent>(uid, component), new Entity<InteractionPanelComponent>(args.Target.Value, targetInteractionPanelComponent), prototype, false);
2024-02-20 14:28:01 +03:00
}
2024-02-23 18:52:03 +03:00
private void OnInit(EntityUid uid, InteractionPanelComponent component, ComponentInit args)
{
component.Timeout = _gameTiming.CurTime;
2024-02-24 20:54:20 +03:00
component.EndTime = _gameTiming.CurTime;
2024-02-26 21:43:11 +03:00
if (_prototypeManager.TryIndex(component.ActionListPrototype, out var prototype))
{
component.ActionPrototypes.AddRange(prototype.Prototypes);
}
2024-02-23 18:52:03 +03:00
}
private void OnVerb(EntityUid uid, InteractionPanelComponent component, GetVerbsEvent<Verb> args)
2024-02-20 14:28:01 +03:00
{
if (!_mobStateSystem.IsAlive(args.User) || !_mobStateSystem.IsAlive(uid))
return;
2024-02-20 14:28:01 +03:00
args.Verbs.Add(new Verb()
{
2024-02-27 18:22:15 +03:00
Text = Loc.GetString("interaction-open"),
2025-02-23 22:36:27 +03:00
Act = () => OpenPanel(args.User, args.User, uid)
2024-02-20 14:28:01 +03:00
});
}
2024-02-23 18:52:03 +03:00
public void OpenPanel(EntityUid panelOpener, Entity<InteractionPanelComponent?> user,
Entity<InteractionPanelComponent?> target)
{
2025-02-23 22:36:27 +03:00
if (!Resolve(user, ref user.Comp) || !Resolve(target, ref target.Comp)
2024-02-23 18:52:03 +03:00
|| !_playerManager.TryGetSessionByEntity(panelOpener, out var session))
return;
if (!_mobStateSystem.IsAlive(user) || !_mobStateSystem.IsAlive(target))
return;
2024-02-23 18:52:03 +03:00
_eui.OpenEui(new InteractionPanelEui(
2025-02-23 22:36:27 +03:00
new Entity<InteractionPanelComponent>(user, user.Comp),
new Entity<InteractionPanelComponent>(target, target.Comp)),
2024-02-23 18:52:03 +03:00
session);
}
public void Interact(Entity<InteractionPanelComponent?> user,
Entity<InteractionPanelComponent?> target, ProtoId<InteractionPrototype> protoId)
{
//TODO: Пиздец... пиздец.... пиздец....
2025-02-23 22:36:27 +03:00
if (!Resolve(user, ref user.Comp)
|| !Resolve(target, ref target.Comp)
2024-02-23 18:52:03 +03:00
|| user.Comp.IsActive || user.Comp.IsBlocked
|| target.Comp.IsActive || target.Comp.IsBlocked
2024-02-24 20:54:20 +03:00
|| user.Comp.Timeout > _gameTiming.CurTime
|| target.Comp.Timeout > _gameTiming.CurTime
2024-02-26 21:43:11 +03:00
|| !_prototypeManager.TryIndex(protoId, out var prototype))
return;
if (!_mobStateSystem.IsAlive(user) || !_mobStateSystem.IsAlive(target))
return;
2025-02-23 22:36:27 +03:00
if (!Check(user!, target!, prototype, out var check))
2024-02-26 21:43:11 +03:00
{
2025-02-23 22:36:27 +03:00
if (_playerManager.TryGetSessionByEntity(user, out var session) || session is null)
2024-02-26 21:43:11 +03:00
return;
var message = ParseMessage(target, $"interaction-fail-{check.GetType().Name.ToLower()}");
2025-02-23 22:36:27 +03:00
_chatManager.ChatMessageToOne(ChatChannel.Emotes, message, message, EntityUid.Invalid, false, session.Channel);
2024-02-26 21:43:11 +03:00
return;
}
if (prototype.BeginningTimeout == TimeSpan.Zero)
{
2025-02-23 22:36:27 +03:00
Interact(user!, target!, prototype);
2024-02-23 18:52:03 +03:00
return;
2024-02-26 21:43:11 +03:00
}
user.Comp.IsBlocked = true;
2025-02-23 22:36:27 +03:00
if (prototype.PreBeginMessages.Count > 0)
2024-02-26 21:43:11 +03:00
{
_chatSystem.TrySendInGameICMessage(user,
2025-02-23 22:36:27 +03:00
ParseMessage(target, _robustRandom.Pick(prototype.PreBeginMessages)),
2024-02-26 21:43:11 +03:00
InGameICChatType.Emote,
false);
}
_doAfterSystem.TryStartDoAfter(new DoAfterArgs(
EntityManager,
user,
prototype.BeginningTimeout,
2025-02-23 22:36:27 +03:00
new PanelDoAfterEvent(prototype.ID), user, target
2024-02-26 21:43:11 +03:00
)
{
BreakOnDamage = true,
2024-05-03 14:56:23 +03:00
BreakOnMove = true,
2024-02-26 21:43:11 +03:00
BreakOnHandChange = true
});
}
private void Interact(Entity<InteractionPanelComponent> user,
Entity<InteractionPanelComponent> target, InteractionPrototype prototype, bool hasChecked = true)
{
2025-02-23 22:36:27 +03:00
if (!hasChecked && !Check(user, target, prototype, out var check))
2024-02-26 21:43:11 +03:00
{
2025-02-23 22:36:27 +03:00
if (_playerManager.TryGetSessionByEntity(user, out var session) || session is null)
2024-02-26 21:43:11 +03:00
return;
2024-02-27 18:22:15 +03:00
var message = ParseMessage(target, $"interaction-fail-{check.GetType().Name.ToLower()}");
2025-02-23 22:36:27 +03:00
_chatManager.ChatMessageToOne(ChatChannel.Emotes, message, message, EntityUid.Invalid, false, session.Channel);
2024-02-27 18:22:15 +03:00
return;
2024-02-26 21:43:11 +03:00
}
2024-02-23 18:52:03 +03:00
2024-02-27 18:22:15 +03:00
user.Comp.Timeout = _gameTiming.CurTime + prototype.Timeout + prototype.EndTime;
2024-02-24 20:54:20 +03:00
user.Comp.EndTime = _gameTiming.CurTime + prototype.EndTime;
2024-02-23 18:52:03 +03:00
user.Comp.IsActive = true;
2024-02-26 21:43:11 +03:00
user.Comp.CurrentAction = prototype.ID;
2025-02-23 22:36:27 +03:00
user.Comp.CurrentPartner = new Entity<InteractionPanelComponent>(target, target.Comp);
2024-02-23 18:52:03 +03:00
2025-02-23 22:36:27 +03:00
if (prototype.BeginningMessages.Count > 0)
2024-02-24 20:54:20 +03:00
{
_chatSystem.TrySendInGameICMessage(user,
2025-02-23 22:36:27 +03:00
ParseMessage(target, _robustRandom.Pick(prototype.BeginningMessages)),
2024-02-24 20:54:20 +03:00
InGameICChatType.Emote,
false);
}
2024-02-23 18:52:03 +03:00
if (prototype.BeginningSound is not null)
_audioSystem.PlayPvs(prototype.BeginningSound, user);
2024-02-26 21:43:11 +03:00
foreach (var action in prototype.BeginningActions)
{
2025-02-23 22:36:27 +03:00
action.Run(user!, target!, EntityManager);
2024-02-26 21:43:11 +03:00
}
2024-02-27 18:22:15 +03:00
_actionBlockerSystem.UpdateCanMove(user);
_actionBlockerSystem.UpdateCanMove(target);
2025-02-23 22:36:27 +03:00
RaiseLocalEvent(user, new InteractionBeginningEvent(prototype.ID, user, target));
2024-02-23 18:52:03 +03:00
}
2024-02-24 20:54:20 +03:00
private string GetName(EntityUid target)
{
2025-02-23 22:36:27 +03:00
if (!TryComp<MindComponent>(target, out var mind) || mind.CharacterName is null)
2024-02-24 20:54:20 +03:00
return MetaData(target).EntityName;
return mind.CharacterName;
}
private Gender GetGender(EntityUid target)
{
if (!TryComp<HumanoidAppearanceComponent>(target, out var humanoidAppearanceComponent))
return Gender.Male;
return humanoidAppearanceComponent.Gender;
}
private string ParseMessage(EntityUid target, string message)
{
return Loc.GetString(message,
("target", GetName(target)), ("gender", GetGender(target)));
}
2024-02-27 18:22:15 +03:00
public bool Check(Entity<InteractionPanelComponent> user,
2025-02-23 22:36:27 +03:00
Entity<InteractionPanelComponent> target, InteractionPrototype prototype, [NotNullWhen(false)] out IInteractionCheck? check)
2024-02-27 18:22:15 +03:00
{
check = null;
foreach (var checkout in prototype.Checks.Where(check => !check.IsAvailable(user!, target!, EntityManager)))
{
check = checkout;
return false;
}
return true;
}
2024-02-23 18:52:03 +03:00
public override void Update(float frameTime)
{
base.Update(frameTime);
var query = EntityQueryEnumerator<InteractionPanelComponent>();
2024-02-26 21:43:11 +03:00
2024-02-23 18:52:03 +03:00
while (query.MoveNext(out var uid, out var component))
{
2025-02-23 22:36:27 +03:00
if (component.EndTime > _gameTiming.CurTime || !component.IsActive)
2024-02-23 18:52:03 +03:00
continue;
if (component.CurrentPartner is null)
{
continue;
}
2024-02-26 21:43:11 +03:00
var user = new Entity<InteractionPanelComponent>(uid, component);
2024-02-27 18:22:15 +03:00
var target = component.CurrentPartner.Value;
2024-02-26 21:43:11 +03:00
2024-02-23 18:52:03 +03:00
if (_prototypeManager.TryIndex(component.CurrentAction, out var prototype))
{
2024-02-24 20:54:20 +03:00
if (prototype.EndingMessages.Count > 0)
{
_chatSystem.TrySendInGameICMessage(uid,
ParseMessage(component.CurrentPartner.Value,
_robustRandom.Pick(prototype.EndingMessages)),
InGameICChatType.Emote,
false);
}
2024-02-23 18:52:03 +03:00
if (prototype.EndingSound is not null)
_audioSystem.PlayPvs(prototype.EndingSound, uid);
2024-02-26 21:43:11 +03:00
foreach (var action in prototype.EndingActions)
{
2025-02-23 22:36:27 +03:00
action.Run(user, component.CurrentPartner.Value, EntityManager);
2024-02-26 21:43:11 +03:00
}
2024-02-23 18:52:03 +03:00
}
component.IsActive = false;
2024-02-27 18:22:15 +03:00
_actionBlockerSystem.UpdateCanMove(user);
_actionBlockerSystem.UpdateCanMove(target);
2024-02-23 18:52:03 +03:00
RaiseLocalEvent(uid, new InteractionEndingEvent(component.CurrentAction,
2024-02-26 21:43:11 +03:00
user,
2024-02-27 18:22:15 +03:00
target));
2024-02-23 18:52:03 +03:00
}
}
2024-02-20 14:28:01 +03:00
}