Skip to content

Commit

Permalink
Switched to using global overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
chromiumboy committed Jan 17, 2025
1 parent f951d2d commit 9b3ef33
Showing 1 changed file with 21 additions and 83 deletions.
104 changes: 21 additions & 83 deletions Content.Server/Holopad/HolopadSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
using Robust.Server.GameObjects;
using Robust.Server.GameStates;
using Robust.Shared.Containers;
using Robust.Shared.GameStates;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
using System.Linq;
Expand All @@ -35,17 +34,14 @@ public sealed class HolopadSystem : SharedHolopadSystem
[Dependency] private readonly SharedAmbientSoundSystem _ambientSoundSystem = default!;
[Dependency] private readonly SharedStationAiSystem _stationAiSystem = default!;
[Dependency] private readonly AccessReaderSystem _accessReaderSystem = default!;
[Dependency] private readonly InventorySystem _inventorySystem = default!;
[Dependency] private readonly ChatSystem _chatSystem = default!;
[Dependency] private readonly PopupSystem _popupSystem = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly PvsOverrideSystem _pvs = default!;

private float _updateTimer = 1.0f;
private const float UpdateTime = 1.0f;

// Data does not need to be saved
private Dictionary<Entity<HolopadUserComponent>, List<EntityUid>> _trackedHolopadUsers = new();

public override void Initialize()
{
base.Initialize();
Expand All @@ -67,18 +63,13 @@ public override void Initialize()

// Networked events
SubscribeNetworkEvent<HolopadUserTypingChangedEvent>(OnTypingChanged);
SubscribeLocalEvent<ExpandPvsEvent>(OnExpandPvs);

// Component start/shutdown events
SubscribeLocalEvent<HolopadComponent, ComponentInit>(OnHolopadInit);
SubscribeLocalEvent<HolopadComponent, ComponentShutdown>(OnHolopadShutdown);
SubscribeLocalEvent<HolopadUserComponent, ComponentInit>(OnHolopadUserInit);
SubscribeLocalEvent<HolopadUserComponent, ComponentShutdown>(OnHolopadUserShutdown);

// Equipment change events
SubscribeLocalEvent<HolopadUserComponent, EntInsertedIntoContainerMessage>(OnHolopadUserEquipped);
SubscribeLocalEvent<HolopadUserComponent, EntRemovedFromContainerMessage>(OnHolopadUserUnequipped);

// Misc events
SubscribeLocalEvent<HolopadUserComponent, EmoteEvent>(OnEmote);
SubscribeLocalEvent<HolopadUserComponent, JumpToCoreEvent>(OnJumpToCore);
Expand Down Expand Up @@ -321,24 +312,6 @@ private void OnTypingChanged(HolopadUserTypingChangedEvent ev, EntitySessionEven
}
}

private void OnExpandPvs(ref ExpandPvsEvent args)
{
if (_trackedHolopadUsers.Count == 0)
return;

if (args.Entities == null)
args.Entities = new();

// Add holopad users and their held/equipped items to PVS so that they can be rendered by remote holopads
foreach (var (holopadUser, inventoryItems) in _trackedHolopadUsers)
{
args.Entities.Add(holopadUser);

foreach (var item in inventoryItems)
args.Entities.Add(item);
}
}

#endregion

#region: Component start/shutdown events
Expand Down Expand Up @@ -372,20 +345,6 @@ private void OnHolopadUserShutdown(Entity<HolopadUserComponent> entity, ref Comp

#endregion

#region: Equipment change events

private void OnHolopadUserEquipped(Entity<HolopadUserComponent> entity, ref EntInsertedIntoContainerMessage args)
{
TrackHolopadUserAndSyncAppearance(entity);
}

private void OnHolopadUserUnequipped(Entity<HolopadUserComponent> entity, ref EntRemovedFromContainerMessage args)
{
TrackHolopadUserAndSyncAppearance(entity);
}

#endregion

#region: Misc events

private void OnEmote(Entity<HolopadUserComponent> entity, ref EmoteEvent args)
Expand Down Expand Up @@ -588,31 +547,16 @@ private void LinkHolopadToUser(Entity<HolopadComponent> entity, EntityUid? user)
entity.Comp.User = (user.Value, holopadUser);
}

// Track the new user and their inventory items for PVS expansion
TrackHolopadUserAndSyncAppearance(entity.Comp.User);
}

private void SyncHolopadHologramAppearanceWithTarget(Entity<HolopadComponent> entity, Entity<HolopadUserComponent>? user)
{
if (entity.Comp.Hologram == null)
return;

entity.Comp.Hologram.Value.Comp.LinkedEntity = user;
Dirty(entity.Comp.Hologram.Value);
// Add the new user to PVS and sync their appearance with any
// holopads connected to the one they are using
_pvs.AddGlobalOverride(user.Value);
SyncHolopadHologramAppearanceWithTarget(entity, entity.Comp.User);
}

private void UnlinkHolopadFromUser(Entity<HolopadComponent> entity, Entity<HolopadUserComponent>? user)
{
entity.Comp.User = null;

foreach (var linkedHolopad in GetLinkedHolopads(entity))
{
if (linkedHolopad.Comp.Hologram != null)
{
_appearanceSystem.SetData(linkedHolopad.Comp.Hologram.Value.Owner, TypingIndicatorVisuals.IsTyping, false);
SyncHolopadHologramAppearanceWithTarget(linkedHolopad, null);
}
}
SyncHolopadHologramAppearanceWithTarget(entity, null);

if (user == null)
return;
Expand All @@ -622,10 +566,24 @@ private void UnlinkHolopadFromUser(Entity<HolopadComponent> entity, Entity<Holop
if (!user.Value.Comp.LinkedHolopads.Any() &&
user.Value.Comp.LifeStage < ComponentLifeStage.Stopping)
{
_trackedHolopadUsers.Remove(user.Value);
_pvs.RemoveGlobalOverride(user.Value);
RemComp<HolopadUserComponent>(user.Value);
}
}
private void SyncHolopadHologramAppearanceWithTarget(Entity<HolopadComponent> entity, Entity<HolopadUserComponent>? user)
{
foreach (var linkedHolopad in GetLinkedHolopads(entity))
{
if (linkedHolopad.Comp.Hologram == null)
continue;

if (user == null)
_appearanceSystem.SetData(linkedHolopad.Comp.Hologram.Value.Owner, TypingIndicatorVisuals.IsTyping, false);

linkedHolopad.Comp.Hologram.Value.Comp.LinkedEntity = user;
Dirty(linkedHolopad.Comp.Hologram.Value);
}
}

private void ShutDownHolopad(Entity<HolopadComponent> entity)
{
Expand Down Expand Up @@ -796,24 +754,4 @@ private void SetHolopadAmbientState(Entity<HolopadComponent> entity, bool isEnab
if (TryComp<AmbientSoundComponent>(entity, out var ambientSound))
_ambientSoundSystem.SetAmbience(entity, isEnabled, ambientSound);
}

private void TrackHolopadUserAndSyncAppearance(Entity<HolopadUserComponent>? entity)
{
if (entity == null)
return;

var inventoryItems = new List<EntityUid>();

foreach (var item in _inventorySystem.GetHandOrInventoryEntities(entity.Value.Owner))
inventoryItems.Add(item);

_trackedHolopadUsers[entity.Value] = inventoryItems;

// Sync linked holopad hologram appearance with the user
foreach (var linkedHolopad in entity.Value.Comp.LinkedHolopads)
{
foreach (var receiver in GetLinkedHolopads(linkedHolopad))
SyncHolopadHologramAppearanceWithTarget(receiver, entity);
}
}
}

0 comments on commit 9b3ef33

Please sign in to comment.