forked from Simple-Station/Einstein-Engines
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInteractionPopupSystem.cs
More file actions
134 lines (112 loc) · 5.04 KB
/
InteractionPopupSystem.cs
File metadata and controls
134 lines (112 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using Content.Server.Interaction.Components;
using Content.Server.Popups;
using Content.Shared.Bed.Sleep;
using Content.Shared.IdentityManagement;
using Content.Shared.Interaction;
using Content.Shared.Mobs.Components;
using Content.Shared.Mobs.Systems;
using Content.Shared.Mood;
using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Player;
using Robust.Shared.Random;
using Robust.Shared.Timing;
namespace Content.Server.Interaction;
public sealed class InteractionPopupSystem : EntitySystem
{
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly MobStateSystem _mobStateSystem = default!;
[Dependency] private readonly PopupSystem _popupSystem = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<InteractionPopupComponent, InteractHandEvent>(OnInteractHand);
SubscribeLocalEvent<InteractionPopupComponent, ActivateInWorldEvent>(OnActivateInWorld);
}
private void OnActivateInWorld(EntityUid uid, InteractionPopupComponent component, ActivateInWorldEvent args)
{
if (!component.OnActivate)
return;
SharedInteract(uid, component, args, args.Target, args.User);
}
private void OnInteractHand(EntityUid uid, InteractionPopupComponent component, InteractHandEvent args)
{
SharedInteract(uid, component, args, args.Target, args.User);
}
private void SharedInteract(
EntityUid uid,
InteractionPopupComponent component,
HandledEntityEventArgs args,
EntityUid target,
EntityUid user)
{
if (args.Handled || user == target)
return;
//Handling does nothing and this thing annoyingly plays way too often.
if (HasComp<SleepingComponent>(uid))
return;
args.Handled = true;
var curTime = _gameTiming.CurTime;
if (curTime < component.LastInteractTime + component.InteractDelay)
return;
if (TryComp<MobStateComponent>(uid, out var state)
&& !_mobStateSystem.IsAlive(uid, state))
{
return;
}
// TODO: Should be an attempt event
// TODO: Need to handle pausing with an accumulator.
string msg = ""; // Stores the text to be shown in the popup message
SoundSpecifier? sfx = null; // Stores the filepath of the sound to be played
if (_random.Prob(component.SuccessChance))
{
if (component.InteractSuccessString != null)
{
msg = Loc.GetString(component.InteractSuccessString, ("target", Identity.Entity(uid, EntityManager))); // Success message (localized).
if (component.InteractSuccessString == "hugging-success-generic")
{
var ev = new MoodEffectEvent("BeingHugged");
RaiseLocalEvent(uid, ev);
}
else if (component.InteractSuccessString.Contains("petting-success-"))
{
var ev = new MoodEffectEvent("PetAnimal");
RaiseLocalEvent(uid, ev);
}
}
if (component.InteractSuccessSound != null)
sfx = component.InteractSuccessSound;
if (component.InteractSuccessSpawn != null)
Spawn(component.InteractSuccessSpawn, _transform.GetMapCoordinates(uid));
}
else
{
if (component.InteractFailureString != null)
msg = Loc.GetString(component.InteractFailureString, ("target", Identity.Entity(uid, EntityManager))); // Failure message (localized).
if (component.InteractFailureSound != null)
sfx = component.InteractFailureSound;
if (component.InteractFailureSpawn != null)
Spawn(component.InteractFailureSpawn, _transform.GetMapCoordinates(uid));
}
if (component.MessagePerceivedByOthers != null)
{
var msgOthers = Loc.GetString(component.MessagePerceivedByOthers,
("user", Identity.Entity(user, EntityManager)), ("target", Identity.Entity(uid, EntityManager)));
_popupSystem.PopupEntity(msg, uid, user);
_popupSystem.PopupEntity(msgOthers, uid, Filter.PvsExcept(user, entityManager: EntityManager), true);
}
else
_popupSystem.PopupEntity(msg, uid, user); //play only for the initiating entity.
if (sfx is not null) //not all cases will have sound.
{
if (component.SoundPerceivedByOthers)
_audio.PlayPvs(sfx, target); //play for everyone in range
else
_audio.PlayEntity(sfx, Filter.Entities(user, target), target, true); //play only for the initiating entity and its target.
}
component.LastInteractTime = curTime;
}
}