Красивое УИ телепорта для призрака (#378)

* add: nice ghost teleport ui

* fix: fix unused import

* wtf

* fuck you, search bar

* fix-add: finally
This commit is contained in:
ThereDrD0
2024-06-26 05:13:42 +03:00
committed by GitHub
parent 331eb0ece5
commit e35098f7dd
27 changed files with 835 additions and 239 deletions

View File

@@ -27,12 +27,16 @@ using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Server.Player;
using Content.Shared._White;
using Content.Shared._White.Antag;
using Content.Shared.Humanoid;
using Content.Shared.Roles;
using Content.Shared.SSDIndicator;
using Robust.Shared.Configuration;
using Robust.Shared.Network;
using Robust.Shared.Map;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Systems;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing;
using InvisibilityComponent = Content.Shared._White.Administration.InvisibilityComponent;
@@ -56,19 +60,12 @@ namespace Content.Server.Ghost
[Dependency] private readonly VisibilitySystem _visibilitySystem = default!;
[Dependency] private readonly IChatManager _chatManager = default!;
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
[Dependency] private readonly MetaDataSystem _metaData = default!;
[Dependency] private readonly IMapManager _mapManager = default!;
private EntityQuery<GhostComponent> _ghostQuery;
private EntityQuery<PhysicsComponent> _physicsQuery;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
public override void Initialize()
{
base.Initialize();
_ghostQuery = GetEntityQuery<GhostComponent>();
_physicsQuery = GetEntityQuery<PhysicsComponent>();
SubscribeLocalEvent<GhostComponent, ComponentStartup>(OnGhostStartup);
SubscribeLocalEvent<GhostComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<GhostComponent, ComponentShutdown>(OnGhostShutdown);
@@ -84,7 +81,6 @@ namespace Content.Server.Ghost
SubscribeNetworkEvent<GhostWarpsRequestEvent>(OnGhostWarpsRequest);
SubscribeNetworkEvent<GhostReturnToBodyRequest>(OnGhostReturnToBodyRequest);
SubscribeNetworkEvent<GhostWarpToTargetRequestEvent>(OnGhostWarpToTargetRequest);
SubscribeNetworkEvent<GhostnadoRequestEvent>(OnGhostnadoRequest);
SubscribeNetworkEvent<GhostReturnToRoundRequest>(OnGhostReturnToRoundRequest);
@@ -329,7 +325,7 @@ namespace Content.Server.Ghost
private void OnGhostReturnToBodyRequest(GhostReturnToBodyRequest msg, EntitySessionEventArgs args)
{
if (args.SenderSession.AttachedEntity is not {Valid: true} attached
|| !_ghostQuery.TryComp(attached, out var ghost)
|| !TryComp(attached, out GhostComponent? ghost)
|| !ghost.CanReturnToBody
|| !TryComp(attached, out ActorComponent? actor))
{
@@ -345,20 +341,20 @@ namespace Content.Server.Ghost
private void OnGhostWarpsRequest(GhostWarpsRequestEvent msg, EntitySessionEventArgs args)
{
if (args.SenderSession.AttachedEntity is not {Valid: true} entity
|| !_ghostQuery.HasComp(entity))
|| !HasComp<GhostComponent>(entity))
{
Log.Warning($"User {args.SenderSession.Name} sent a {nameof(GhostWarpsRequestEvent)} without being a ghost.");
return;
}
var response = new GhostWarpsResponseEvent(GetPlayerWarps(entity).Concat(GetLocationWarps()).ToList());
var response = new GhostWarpsResponseEvent(GetPlayerWarps(), GetLocationWarps(), GetAntagonistWarps());
RaiseNetworkEvent(response, args.SenderSession.Channel);
}
private void OnGhostWarpToTargetRequest(GhostWarpToTargetRequestEvent msg, EntitySessionEventArgs args)
{
if (args.SenderSession.AttachedEntity is not {Valid: true} attached
|| !_ghostQuery.HasComp(attached))
|| !TryComp(attached, out GhostComponent? _))
{
Log.Warning($"User {args.SenderSession.Name} tried to warp to {msg.Target} without being a ghost.");
return;
@@ -372,66 +368,98 @@ namespace Content.Server.Ghost
return;
}
WarpTo(attached, target);
}
private void OnGhostnadoRequest(GhostnadoRequestEvent msg, EntitySessionEventArgs args)
{
if (args.SenderSession.AttachedEntity is not {} uid
|| !_ghostQuery.HasComp(uid))
{
Log.Warning($"User {args.SenderSession.Name} tried to ghostnado without being a ghost.");
return;
}
if (_followerSystem.GetMostFollowed() is not {} target)
return;
WarpTo(uid, target);
}
private void WarpTo(EntityUid uid, EntityUid target)
{
if ((TryComp(target, out WarpPointComponent? warp) && warp.Follow) || HasComp<MobStateComponent>(target))
{
_followerSystem.StartFollowingEntity(uid, target);
_followerSystem.StartFollowingEntity(attached, target);
return;
}
var xform = Transform(uid);
_transformSystem.SetCoordinates(uid, xform, Transform(target).Coordinates);
_transformSystem.AttachToGridOrMap(uid, xform);
if (_physicsQuery.TryComp(uid, out var physics))
_physics.SetLinearVelocity(uid, Vector2.Zero, body: physics);
var xform = Transform(attached);
_transformSystem.SetCoordinates(attached, xform, Transform(target).Coordinates);
_transformSystem.AttachToGridOrMap(attached, xform);
if (TryComp(attached, out PhysicsComponent? physics))
_physics.SetLinearVelocity(attached, Vector2.Zero, body: physics);
}
private IEnumerable<GhostWarp> GetLocationWarps()
private List<GhostWarpPlace> GetLocationWarps()
{
var warps = new List<GhostWarpPlace> { };
var allQuery = AllEntityQuery<WarpPointComponent>();
while (allQuery.MoveNext(out var uid, out var warp))
{
yield return new GhostWarp(GetNetEntity(uid), warp.Location ?? Name(uid), true);
var newWarp = new GhostWarpPlace(GetNetEntity(uid), warp.Location ?? Name(uid), warp.Location ?? Description(uid));
warps.Add(newWarp);
}
return warps;
}
private IEnumerable<GhostWarp> GetPlayerWarps(EntityUid except)
private List<GhostWarpPlayer> GetPlayerWarps()
{
foreach (var player in _playerManager.Sessions)
var warps = new List<GhostWarpPlayer> { };
foreach (var mindContainer in EntityQuery<MindContainerComponent>())
{
if (player.AttachedEntity is not {Valid: true} attached)
var entity = mindContainer.Owner;
if (!(HasComp<HumanoidAppearanceComponent>(entity) || HasComp<GhostComponent>(entity)) || HasComp<GlobalAntagonistComponent>(entity))
continue;
if (attached == except) continue;
var playerDepartmentId = _prototypeManager.Index<DepartmentPrototype>("Specific").ID;
var playerJobName = "Неизвестно";
TryComp<MindContainerComponent>(attached, out var mind);
if (_jobs.MindTryGetJob(mindContainer.Mind ?? mindContainer.LastMindStored, out _, out var jobPrototype))
{
playerJobName = Loc.GetString(jobPrototype.Name);
var jobName = _jobs.MindTryGetJobName(mind?.Mind);
var playerInfo = $"{Comp<MetaDataComponent>(attached).EntityName} ({jobName})";
if (_jobs.TryGetDepartment(jobPrototype.ID, out var departmentPrototype))
{
playerDepartmentId = departmentPrototype.ID;
}
}
var hasAnyMind = (mindContainer.Mind ?? mindContainer.LastMindStored) != null;
var isDead = _mobState.IsDead(entity);
var isLeft = TryComp<SSDIndicatorComponent>(entity, out var indicator) && indicator.IsSSD && !isDead && hasAnyMind;
if (_mobState.IsAlive(attached) || _mobState.IsCritical(attached))
yield return new GhostWarp(GetNetEntity(attached), playerInfo, false);
var warp = new GhostWarpPlayer(
GetNetEntity(entity),
Comp<MetaDataComponent>(entity).EntityName,
playerJobName,
playerDepartmentId,
HasComp<GhostComponent>(entity),
isLeft,
isDead,
_mobState.IsAlive(entity)
);
warps.Add(warp);
}
return warps;
}
private List<GhostWarpGlobalAntagonist> GetAntagonistWarps()
{
var warps = new List<GhostWarpGlobalAntagonist> { };
foreach (var antagonist in EntityQuery<GlobalAntagonistComponent>())
{
var entity = antagonist.Owner;
var prototype = _prototypeManager.Index<AntagonistPrototype>(antagonist.AntagonistPrototype ?? "globalAntagonistUnknown");
var warp = new GhostWarpGlobalAntagonist(
GetNetEntity(entity),
Comp<MetaDataComponent>(entity).EntityName,
prototype.Name,
prototype.Description,
prototype.ID
);
warps.Add(warp);
}
return warps;
}
#endregion
@@ -474,59 +502,5 @@ namespace Content.Server.Ghost
return ghostBoo.Handled;
}
public EntityUid? SpawnGhost(Entity<MindComponent?> mind, EntityUid targetEntity,
bool canReturn = false)
{
_transformSystem.TryGetMapOrGridCoordinates(targetEntity, out var spawnPosition);
return SpawnGhost(mind, spawnPosition, canReturn);
}
public EntityUid? SpawnGhost(Entity<MindComponent?> mind, EntityCoordinates? spawnPosition = null,
bool canReturn = false)
{
if (!Resolve(mind, ref mind.Comp))
return null;
// Test if the map is being deleted
var mapUid = spawnPosition?.GetMapUid(EntityManager);
if (mapUid == null || TerminatingOrDeleted(mapUid.Value))
spawnPosition = null;
spawnPosition ??= _ticker.GetObserverSpawnPoint();
if (!spawnPosition.Value.IsValid(EntityManager))
{
Log.Warning($"No spawn valid ghost spawn position found for {mind.Comp.CharacterName}"
+ " \"{ToPrettyString(mind)}\"");
_minds.TransferTo(mind.Owner, null, createGhost: false, mind: mind.Comp);
return null;
}
var ghost = SpawnAtPosition(GameTicker.ObserverPrototypeName, spawnPosition.Value);
var ghostComponent = Comp<GhostComponent>(ghost);
// Try setting the ghost entity name to either the character name or the player name.
// If all else fails, it'll default to the default entity prototype name, "observer".
// However, that should rarely happen.
if (!string.IsNullOrWhiteSpace(mind.Comp.CharacterName))
_metaData.SetEntityName(ghost, mind.Comp.CharacterName);
else if (!string.IsNullOrWhiteSpace(mind.Comp.Session?.Name))
_metaData.SetEntityName(ghost, mind.Comp.Session.Name);
if (mind.Comp.TimeOfDeath.HasValue)
{
SetTimeOfDeath(ghost, mind.Comp.TimeOfDeath!.Value, ghostComponent);
}
SetCanReturnToBody(ghostComponent, canReturn);
if (canReturn)
_minds.Visit(mind.Owner, ghost, mind.Comp);
else
_minds.TransferTo(mind.Owner, ghost, mind: mind.Comp);
Log.Debug($"Spawned ghost \"{ToPrettyString(ghost)}\" for {mind.Comp.CharacterName}.");
return ghost;
}
}
}