-
Notifications
You must be signed in to change notification settings - Fork 410
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move viewsubscriber to shared + handle eye targets (#5362)
* Move viewsubscriber to shared + handle eye targets Need this stuff for AI. * Fix dumb * review
- Loading branch information
1 parent
36a5b67
commit 140767c
Showing
6 changed files
with
145 additions
and
77 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
Robust.Client/GameObjects/EntitySystems/ViewSubscriberSystem.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
using Robust.Shared.GameObjects; | ||
|
||
namespace Robust.Client.GameObjects; | ||
|
||
public sealed class ViewSubscriberSystem : SharedViewSubscriberSystem; |
12 changes: 0 additions & 12 deletions
12
Robust.Server/GameObjects/Components/Eye/ViewSubscriberComponent.cs
This file was deleted.
Oops, something went wrong.
128 changes: 63 additions & 65 deletions
128
Robust.Server/GameObjects/EntitySystems/ViewSubscriberSystem.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,87 @@ | ||
using Robust.Shared.GameObjects; | ||
using Robust.Shared.Player; | ||
|
||
namespace Robust.Server.GameObjects | ||
namespace Robust.Server.GameObjects; | ||
|
||
/// <summary> | ||
/// Entity System that handles subscribing and unsubscribing to PVS views. | ||
/// </summary> | ||
public sealed class ViewSubscriberSystem : SharedViewSubscriberSystem | ||
{ | ||
public override void Initialize() | ||
{ | ||
base.Initialize(); | ||
SubscribeLocalEvent<ViewSubscriberComponent, ComponentShutdown>(OnViewSubscriberShutdown); | ||
} | ||
|
||
/// <summary> | ||
/// Entity System that handles subscribing and unsubscribing to PVS views. | ||
/// Subscribes the session to get PVS updates from the point of view of the specified entity. | ||
/// </summary> | ||
public sealed class ViewSubscriberSystem : EntitySystem | ||
public override void AddViewSubscriber(EntityUid uid, ICommonSession session) | ||
{ | ||
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) | ||
{ | ||
// If the entity doesn't have the component, it will be added. | ||
var viewSubscriber = EntityManager.EnsureComponent<ViewSubscriberComponent>(uid); | ||
// If the entity doesn't have the component, it will be added. | ||
var viewSubscriber = EntityManager.EnsureComponent<Shared.GameObjects.ViewSubscriberComponent>(uid); | ||
|
||
if (viewSubscriber.SubscribedSessions.Contains(session)) | ||
return; // Already subscribed, do nothing else. | ||
if (viewSubscriber.SubscribedSessions.Contains(session)) | ||
return; // Already subscribed, do nothing else. | ||
|
||
viewSubscriber.SubscribedSessions.Add(session); | ||
session.ViewSubscriptions.Add(uid); | ||
viewSubscriber.SubscribedSessions.Add(session); | ||
session.ViewSubscriptions.Add(uid); | ||
|
||
RaiseLocalEvent(uid, new ViewSubscriberAddedEvent(uid, session), true); | ||
} | ||
|
||
/// <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) | ||
{ | ||
if(!EntityManager.TryGetComponent(uid, out ViewSubscriberComponent? viewSubscriber)) | ||
return; // Entity didn't have any subscriptions, do nothing. | ||
|
||
if (!viewSubscriber.SubscribedSessions.Remove(session)) | ||
return; // Session wasn't subscribed, do nothing. | ||
|
||
session.ViewSubscriptions.Remove(uid); | ||
RaiseLocalEvent(uid, new ViewSubscriberRemovedEvent(uid, session), true); | ||
} | ||
|
||
private void OnViewSubscriberShutdown(EntityUid uid, ViewSubscriberComponent component, ComponentShutdown _) | ||
{ | ||
foreach (var session in component.SubscribedSessions) | ||
{ | ||
session.ViewSubscriptions.Remove(uid); | ||
} | ||
} | ||
RaiseLocalEvent(uid, new ViewSubscriberAddedEvent(uid, session), true); | ||
} | ||
|
||
/// <summary> | ||
/// Raised when a session subscribes to an entity's PVS view. | ||
/// Unsubscribes the session from getting PVS updates from the point of view of the specified entity. | ||
/// </summary> | ||
public sealed class ViewSubscriberAddedEvent : EntityEventArgs | ||
public override void RemoveViewSubscriber(EntityUid uid, ICommonSession session) | ||
{ | ||
public EntityUid View { get; } | ||
public ICommonSession Subscriber { get; } | ||
if(!EntityManager.TryGetComponent(uid, out Shared.GameObjects.ViewSubscriberComponent? viewSubscriber)) | ||
return; // Entity didn't have any subscriptions, do nothing. | ||
|
||
if (!viewSubscriber.SubscribedSessions.Remove(session)) | ||
return; // Session wasn't subscribed, do nothing. | ||
|
||
public ViewSubscriberAddedEvent(EntityUid view, ICommonSession subscriber) | ||
session.ViewSubscriptions.Remove(uid); | ||
RaiseLocalEvent(uid, new ViewSubscriberRemovedEvent(uid, session), true); | ||
} | ||
|
||
private void OnViewSubscriberShutdown(EntityUid uid, ViewSubscriberComponent component, ComponentShutdown _) | ||
{ | ||
foreach (var session in component.SubscribedSessions) | ||
{ | ||
View = view; | ||
Subscriber = subscriber; | ||
session.ViewSubscriptions.Remove(uid); | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Raised when a session is unsubscribed from an entity's PVS view. | ||
/// Not raised when sessions are unsubscribed due to the component being removed. | ||
/// </summary> | ||
public sealed class ViewSubscriberRemovedEvent : EntityEventArgs | ||
/// <summary> | ||
/// Raised when a session subscribes to an entity's PVS view. | ||
/// </summary> | ||
public sealed class ViewSubscriberAddedEvent : EntityEventArgs | ||
{ | ||
public EntityUid View { get; } | ||
public ICommonSession Subscriber { get; } | ||
|
||
public ViewSubscriberAddedEvent(EntityUid view, ICommonSession subscriber) | ||
{ | ||
public EntityUid View { get; } | ||
public ICommonSession Subscriber { get; } | ||
View = view; | ||
Subscriber = subscriber; | ||
} | ||
} | ||
|
||
public ViewSubscriberRemovedEvent(EntityUid view, ICommonSession subscriber) | ||
{ | ||
View = view; | ||
Subscriber = subscriber; | ||
} | ||
/// <summary> | ||
/// Raised when a session is unsubscribed from an entity's PVS view. | ||
/// Not raised when sessions are unsubscribed due to the component being removed. | ||
/// </summary> | ||
public sealed class ViewSubscriberRemovedEvent : EntityEventArgs | ||
{ | ||
public EntityUid View { get; } | ||
public ICommonSession Subscriber { get; } | ||
|
||
public ViewSubscriberRemovedEvent(EntityUid view, ICommonSession subscriber) | ||
{ | ||
View = view; | ||
Subscriber = subscriber; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Robust.Shared/GameObjects/Components/Eye/ViewSubscriberComponent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
internal sealed partial class ViewSubscriberComponent : Component | ||
{ | ||
internal readonly HashSet<ICommonSession> SubscribedSessions = new(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
Robust.Shared/GameObjects/Systems/SharedViewSubscriberSystem.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Robust.Shared.Player; | ||
|
||
namespace Robust.Shared.GameObjects; | ||
|
||
public abstract class SharedViewSubscriberSystem : EntitySystem | ||
{ | ||
// NOOP on client | ||
|
||
/// <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) {} | ||
} |