Skip to content

Commit

Permalink
Move viewsubscriber to shared + handle eye targets
Browse files Browse the repository at this point in the history
Need this stuff for AI.
  • Loading branch information
metalgearsloth committed Aug 15, 2024
1 parent 2178707 commit 2eb9937
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Robust.Shared.GameObjects;

namespace Robust.Client.GameObjects;

public sealed class ViewSubscriberSystem : SharedViewSubscriberSystem
{

}

This file was deleted.

19 changes: 6 additions & 13 deletions Robust.Server/GameObjects/EntitySystems/ViewSubscriberSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,15 @@ namespace Robust.Server.GameObjects
/// <summary>
/// Entity System that handles subscribing and unsubscribing to PVS views.
/// </summary>
public sealed class ViewSubscriberSystem : EntitySystem
public sealed class ViewSubscriberSystem : SharedViewSubscriberSystem
{
public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<ViewSubscriberComponent, ComponentShutdown>(OnViewSubscriberShutdown);
}

/// <summary>
/// Subscribes the session to get PVS updates from the point of view of the specified entity.
/// </summary>
public void AddViewSubscriber(EntityUid uid, ICommonSession session)
public override void AddViewSubscriber(EntityUid uid, ICommonSession session)
{
// If the entity doesn't have the component, it will be added.
var viewSubscriber = EntityManager.EnsureComponent<ViewSubscriberComponent>(uid);
var viewSubscriber = EntityManager.EnsureComponent<Shared.GameObjects.ViewSubscriberComponent>(uid);

if (viewSubscriber.SubscribedSessions.Contains(session))
return; // Already subscribed, do nothing else.
Expand All @@ -35,9 +28,9 @@ public void AddViewSubscriber(EntityUid uid, ICommonSession session)
/// <summary>
/// Unsubscribes the session from getting PVS updates from the point of view of the specified entity.
/// </summary>
public void RemoveViewSubscriber(EntityUid uid, ICommonSession session)
public override void RemoveViewSubscriber(EntityUid uid, ICommonSession session)
{
if(!EntityManager.TryGetComponent(uid, out ViewSubscriberComponent? viewSubscriber))
if(!EntityManager.TryGetComponent(uid, out Shared.GameObjects.ViewSubscriberComponent? viewSubscriber))
return; // Entity didn't have any subscriptions, do nothing.

if (!viewSubscriber.SubscribedSessions.Remove(session))
Expand All @@ -47,7 +40,7 @@ public void RemoveViewSubscriber(EntityUid uid, ICommonSession session)
RaiseLocalEvent(uid, new ViewSubscriberRemovedEvent(uid, session), true);
}

private void OnViewSubscriberShutdown(EntityUid uid, ViewSubscriberComponent component, ComponentShutdown _)
protected override void OnViewSubscriberShutdown(EntityUid uid, ViewSubscriberComponent component, ComponentShutdown _)
{
foreach (var session in component.SubscribedSessions)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using Robust.Shared.Player;

namespace Robust.Shared.GameObjects;

// Not networked because doesn't do anything on client.
[RegisterComponent]
public sealed partial class ViewSubscriberComponent : Component
{
internal readonly HashSet<ICommonSession> SubscribedSessions = new();
}
48 changes: 48 additions & 0 deletions Robust.Shared/GameObjects/Systems/SharedEyeSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,41 @@
using System.Numerics;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Player;

namespace Robust.Shared.GameObjects;

public abstract class SharedEyeSystem : EntitySystem
{
[Dependency] private readonly SharedViewSubscriberSystem _views = default!;

public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<EyeComponent, PlayerAttachedEvent>(OnEyePlayerAttached);
SubscribeLocalEvent<EyeComponent, PlayerDetachedEvent>(OnEyePlayerDetached);
}

private void OnEyePlayerAttached(Entity<EyeComponent> ent, ref PlayerAttachedEvent args)
{
var value = ent.Comp.Target;

if (value != null && TryComp(ent.Owner, out ActorComponent? actorComp))
{
_views.AddViewSubscriber(value.Value, actorComp.PlayerSession);
}
}

private void OnEyePlayerDetached(Entity<EyeComponent> ent, ref PlayerDetachedEvent args)
{
var value = ent.Comp.Target;

if (value == null && TryComp(ent.Owner, out ActorComponent? actorComp))
{
_views.RemoveViewSubscriber(ent.Comp.Target!.Value, actorComp.PlayerSession);
}
}

/// <summary>
/// Refreshes all values for IEye with the component.
/// </summary>
Expand Down Expand Up @@ -74,6 +104,10 @@ public void SetRotation(EntityUid uid, Angle rotation, EyeComponent? eyeComponen
eyeComponent.Eye.Rotation = rotation;
}

/// <summary>
/// Sets the eye component as tracking another entity.
/// Will also add the target to view subscribers so they can leave range and still work with PVS.
/// </summary>
public void SetTarget(EntityUid uid, EntityUid? value, EyeComponent? eyeComponent = null)
{
if (!Resolve(uid, ref eyeComponent))
Expand All @@ -82,6 +116,20 @@ public void SetTarget(EntityUid uid, EntityUid? value, EyeComponent? eyeComponen
if (eyeComponent.Target.Equals(value))
return;

// Automatically handle view subs.
if (TryComp(uid, out ActorComponent? actorComp))
{
if (value != null)
{
_views.AddViewSubscriber(value.Value, actorComp.PlayerSession);
}
else
{
// Should never be null here
_views.RemoveViewSubscriber(eyeComponent.Target!.Value, actorComp.PlayerSession);
}
}

eyeComponent.Target = value;
Dirty(uid, eyeComponent);
}
Expand Down
26 changes: 26 additions & 0 deletions Robust.Shared/GameObjects/Systems/SharedViewSubscriberSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Robust.Shared.Player;

namespace Robust.Shared.GameObjects;

public abstract class SharedViewSubscriberSystem : EntitySystem
{
// NOOP on client

public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ViewSubscriberComponent, ComponentShutdown>(OnViewSubscriberShutdown);
}

/// <summary>
/// Subscribes the session to get PVS updates from the point of view of the specified entity.
/// </summary>
public virtual void AddViewSubscriber(EntityUid uid, ICommonSession session) {}

/// <summary>
/// Unsubscribes the session from getting PVS updates from the point of view of the specified entity.
/// </summary>
public virtual void RemoveViewSubscriber(EntityUid uid, ICommonSession session) {}

protected virtual void OnViewSubscriberShutdown(EntityUid uid, ViewSubscriberComponent component, ComponentShutdown _) {}
}

0 comments on commit 2eb9937

Please sign in to comment.