Moved VisibilityFlags to EyeComponent (#3716)

Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
This commit is contained in:
Acruid
2021-03-27 17:37:19 -07:00
committed by GitHub
parent c8cb23e1fa
commit ac219e099d
4 changed files with 33 additions and 25 deletions

View File

@@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Content.Server.GameObjects.Components.Markers; using Content.Server.GameObjects.Components.Markers;
@@ -40,32 +40,40 @@ namespace Content.Server.GameObjects.Components.Observer
} }
} }
public override void Initialize() /// <inheritdoc />
protected override void Startup()
{ {
base.Initialize(); base.Startup();
// Allow this entity to be seen by other ghosts.
Owner.EnsureComponent<VisibilityComponent>().Layer = (int) VisibilityFlags.Ghost; Owner.EnsureComponent<VisibilityComponent>().Layer = (int) VisibilityFlags.Ghost;
// Allows this entity to see other ghosts.
Owner.EnsureComponent<EyeComponent>().VisibilityMask |= (uint) VisibilityFlags.Ghost;
_timeOfDeath = _gameTimer.RealTime; _timeOfDeath = _gameTimer.RealTime;
} }
public override ComponentState GetComponentState(ICommonSession player) => new GhostComponentState(CanReturnToBody); /// <inheritdoc />
protected override void Shutdown()
public override void HandleMessage(ComponentMessage message, IComponent? component)
{ {
base.HandleMessage(message, component); //Perf: If the entity is deleting itself, no reason to change these back.
if(Owner.LifeStage < EntityLifeStage.Terminating)
switch (message)
{ {
case PlayerAttachedMsg msg: // Entity can't be seen by ghosts anymore.
msg.NewPlayer.VisibilityMask |= (int) VisibilityFlags.Ghost; if (Owner.TryGetComponent<VisibilityComponent>(out var visComp))
Dirty(); visComp.Layer &= ~(int) VisibilityFlags.Ghost;
break;
case PlayerDetachedMsg msg: // Entity can't see ghosts anymore.
msg.OldPlayer.VisibilityMask &= ~(int) VisibilityFlags.Ghost; if (Owner.TryGetComponent<EyeComponent>(out var eyeComp))
break; eyeComp.VisibilityMask &= ~(uint) VisibilityFlags.Ghost;
} }
base.Shutdown();
} }
public override ComponentState GetComponentState(ICommonSession player) => new GhostComponentState(CanReturnToBody);
public override void HandleNetworkMessage(ComponentMessage message, INetChannel netChannel, ICommonSession? session = null!) public override void HandleNetworkMessage(ComponentMessage message, INetChannel netChannel, ICommonSession? session = null!)
{ {
base.HandleNetworkMessage(message, netChannel, session); base.HandleNetworkMessage(message, netChannel, session);

View File

@@ -131,13 +131,12 @@ namespace Content.Server.GameObjects.EntitySystems
// Get players that are in range and whose visibility layer matches the arrow's. // Get players that are in range and whose visibility layer matches the arrow's.
var viewers = _playerManager.GetPlayersBy((playerSession) => var viewers = _playerManager.GetPlayersBy((playerSession) =>
{ {
if ((playerSession.VisibilityMask & layer) == 0)
return false;
var ent = playerSession.ContentData()?.Mind?.CurrentEntity; var ent = playerSession.ContentData()?.Mind?.CurrentEntity;
return ent != null if (ent is null || (!ent.TryGetComponent<EyeComponent>(out var eyeComp) || (eyeComp.VisibilityMask & layer) != 0))
&& ent.Transform.MapPosition.InRange(player.Transform.MapPosition, PointingRange); return false;
return ent.Transform.MapPosition.InRange(player.Transform.MapPosition, PointingRange);
}); });
string selfMessage; string selfMessage;

View File

@@ -3,9 +3,10 @@ using System;
namespace Content.Server.GameObjects namespace Content.Server.GameObjects
{ {
[Flags] [Flags]
public enum VisibilityFlags public enum VisibilityFlags : uint
{ {
Normal = 1, None = 0,
Ghost = 2, Normal = 1 << 0,
Ghost = 1 << 1,
} }
} }