Pause refactor (#1314)

* Refactor pausing

* Sprite as well woops

* Bring PauseManagerExt back with [Obsolete] so that content compiles.

Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
This commit is contained in:
metalgearsloth
2020-10-13 03:16:20 +11:00
committed by GitHub
parent 6102ee80ec
commit bc87f2ea9c
13 changed files with 89 additions and 29 deletions

View File

@@ -1849,6 +1849,7 @@ namespace Robust.Client.GameObjects
public bool Initialized { get; } = false;
public bool Initializing { get; } = false;
public bool Deleted { get; } = true;
public bool Paused { get; set; }
public EntityPrototype? Prototype { get; set; }
public string Description { get; set; } = string.Empty;
public bool IsValid()

View File

@@ -1,9 +1,26 @@
using Robust.Server.Interfaces.Timing;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
namespace Robust.Server.GameObjects.Components.Markers
{
public class IgnorePauseComponent : Component
{
public override string Name => "IgnorePause";
public override void OnAdd()
{
base.OnAdd();
Owner.Paused = false;
}
public override void OnRemove()
{
base.OnRemove();
if (IoCManager.Resolve<IPauseManager>().IsMapPaused(Owner.Transform.MapID))
{
Owner.Paused = true;
}
}
}
}

View File

@@ -18,7 +18,7 @@ namespace Robust.Server.Interfaces.GameObjects
{
public static void RunMapInit(this IEntity entity)
{
foreach (var init in entity.GetAllComponents<IMapInit>().ToList())
foreach (var init in entity.GetAllComponents<IMapInit>())
{
init.MapInit();
}

View File

@@ -1,7 +1,7 @@
using JetBrains.Annotations;
using System;
using JetBrains.Annotations;
using Robust.Server.GameObjects.Components.Markers;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.Map;
namespace Robust.Server.Interfaces.Timing
@@ -33,9 +33,10 @@ namespace Robust.Server.Interfaces.Timing
public static class PauseManagerExt
{
[Pure]
[Obsolete("Use IEntity.Paused directly")]
public static bool IsEntityPaused(this IPauseManager manager, IEntity entity)
{
return !entity.HasComponent<IgnorePauseComponent>() && manager.IsGridPaused(entity.Transform.GridID);
return entity.Paused;
}
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Robust.Server.GameObjects.Components.Markers;
using Robust.Server.Interfaces.GameObjects;
using Robust.Server.Interfaces.Timing;
using Robust.Shared.Interfaces.GameObjects;
@@ -23,10 +24,19 @@ namespace Robust.Server.Timing
if (paused)
{
_pausedMaps.Add(mapId);
foreach (var entity in _entityManager.GetEntitiesInMap(mapId))
{
if (entity.HasComponent<IgnorePauseComponent>()) continue;
entity.Paused = true;
}
}
else
{
_pausedMaps.Remove(mapId);
foreach (var entity in _entityManager.GetEntitiesInMap(mapId))
{
entity.Paused = false;
}
}
}
@@ -39,28 +49,25 @@ namespace Robust.Server.Timing
_unInitializedMaps.Remove(mapId);
foreach (var entity in _entityManager.GetEntities())
foreach (var entity in _entityManager.GetEntitiesInMap(mapId))
{
if (entity.Transform.MapID != mapId)
{
continue;
}
entity.RunMapInit();
entity.Paused = false;
}
}
public void DoGridMapInitialize(IMapGrid grid) => DoGridMapInitialize(grid.Index);
public void DoGridMapInitialize(GridId gridId)
{
foreach (var entity in _entityManager.GetEntities())
var mapId = _mapManager.GetGrid(gridId).ParentMapId;
foreach (var entity in _entityManager.GetEntitiesInMap(mapId))
{
if (entity.Transform.GridID != gridId)
{
continue;
}
entity.RunMapInit();
entity.Paused = false;
}
}

View File

@@ -38,6 +38,10 @@ namespace Robust.Shared.GameObjects
[ViewVariables]
public IEntity Owner { get; set; } = default!;
/// <inheritdoc />
[ViewVariables]
public bool Paused => Owner.Paused;
/// <summary>
/// True if this entity is a client-only entity.
/// That is, it does not exist on the server, only THIS client.

View File

@@ -460,19 +460,19 @@ namespace Robust.Shared.GameObjects
#region Join Functions
/// <inheritdoc />
public IEnumerable<T> EntityQuery<T>()
public IEnumerable<T> EntityQuery<T>(bool paused = false)
{
var comps = _entTraitDict[typeof(T)];
foreach (var comp in comps.Values)
{
if (comp.Deleted) continue;
if (comp.Deleted || paused && comp.Paused) continue;
yield return (T) (object) comp;
}
}
/// <inheritdoc />
public IEnumerable<(TComp1, TComp2)> EntityQuery<TComp1, TComp2>()
public IEnumerable<(TComp1, TComp2)> EntityQuery<TComp1, TComp2>(bool paused = false)
where TComp1 : IComponent
where TComp2 : IComponent
{
@@ -485,7 +485,7 @@ namespace Robust.Shared.GameObjects
{
var uid = kvComp.Key;
if (!trait2.TryGetValue(uid, out var t2Comp) || t2Comp.Deleted)
if (!trait2.TryGetValue(uid, out var t2Comp) || t2Comp.Deleted || paused && kvComp.Value.Paused)
continue;
yield return ((TComp1) (object) kvComp.Value, (TComp2) (object) t2Comp);
@@ -493,7 +493,7 @@ namespace Robust.Shared.GameObjects
}
/// <inheritdoc />
public IEnumerable<(TComp1, TComp2, TComp3)> EntityQuery<TComp1, TComp2, TComp3>()
public IEnumerable<(TComp1, TComp2, TComp3)> EntityQuery<TComp1, TComp2, TComp3>(bool paused = false)
where TComp1 : IComponent
where TComp2 : IComponent
where TComp3 : IComponent
@@ -506,7 +506,7 @@ namespace Robust.Shared.GameObjects
{
var uid = kvComp.Key;
if (!trait2.TryGetValue(uid, out var t2Comp) || t2Comp.Deleted)
if (!trait2.TryGetValue(uid, out var t2Comp) || t2Comp.Deleted || paused && kvComp.Value.Paused)
continue;
if (!trait3.TryGetValue(uid, out var t3Comp) || t3Comp.Deleted)
@@ -519,7 +519,7 @@ namespace Robust.Shared.GameObjects
}
/// <inheritdoc />
public IEnumerable<(TComp1, TComp2, TComp3, TComp4)> EntityQuery<TComp1, TComp2, TComp3, TComp4>()
public IEnumerable<(TComp1, TComp2, TComp3, TComp4)> EntityQuery<TComp1, TComp2, TComp3, TComp4>(bool paused = false)
where TComp1 : IComponent
where TComp2 : IComponent
where TComp3 : IComponent
@@ -534,7 +534,7 @@ namespace Robust.Shared.GameObjects
{
var uid = kvComp.Key;
if (!trait2.TryGetValue(uid, out var t2Comp) || t2Comp.Deleted)
if (!trait2.TryGetValue(uid, out var t2Comp) || t2Comp.Deleted || paused && kvComp.Value.Paused)
continue;
if (!trait3.TryGetValue(uid, out var t3Comp) || t3Comp.Deleted)
@@ -553,12 +553,12 @@ namespace Robust.Shared.GameObjects
#endregion
/// <inheritdoc />
public IEnumerable<IComponent> GetAllComponents(Type type)
public IEnumerable<IComponent> GetAllComponents(Type type, bool paused = false)
{
var comps = _entTraitDict[type];
foreach (var comp in comps.Values)
{
if (comp.Deleted) continue;
if (comp.Deleted || paused && comp.Paused) continue;
yield return comp;
}

View File

@@ -76,6 +76,9 @@ namespace Robust.Shared.GameObjects
/// <inheritdoc />
[ViewVariables]
public bool Deleted { get; private set; }
[ViewVariables]
public bool Paused { get; set; }
private ITransformComponent? _transform;

View File

@@ -166,6 +166,18 @@ namespace Robust.Shared.GameObjects
return query.Match(this);
}
public IEnumerable<IEntity> GetEntitiesInMap(MapId mapId)
{
if (!_entityTreesPerMap.TryGetValue(mapId, out var trees))
yield break;
foreach (var entity in trees)
{
if (!entity.Deleted)
yield return entity;
}
}
/// <inheritdoc />
public IEnumerable<IEntity> GetEntitiesAt(MapId mapId, Vector2 position, bool approximate = false)
{

View File

@@ -30,6 +30,11 @@ namespace Robust.Shared.Interfaces.GameObjects
/// <seealso cref="IComponentRegistration.Name" />
string Name { get; }
/// <summary>
/// Whether the Owner has been paused.
/// </summary>
bool Paused { get; }
/// <summary>
/// Whether the client should synchronize component additions and removals.
/// If this is false and the component gets added or removed server side, the client will not do the same.

View File

@@ -201,7 +201,7 @@ namespace Robust.Shared.Interfaces.GameObjects
/// </summary>
/// <typeparam name="T">A trait or type of a component to retrieve.</typeparam>
/// <returns>All components that have the specified type.</returns>
IEnumerable<T> EntityQuery<T>();
IEnumerable<T> EntityQuery<T>(bool paused = true);
/// <summary>
/// Returns the relevant components from all entities that contain the two required components.
@@ -209,7 +209,7 @@ namespace Robust.Shared.Interfaces.GameObjects
/// <typeparam name="TComp1">First required component.</typeparam>
/// <typeparam name="TComp2">Second required component.</typeparam>
/// <returns>The pairs of components from each entity that has the two required components.</returns>
IEnumerable<(TComp1, TComp2)> EntityQuery<TComp1, TComp2>()
IEnumerable<(TComp1, TComp2)> EntityQuery<TComp1, TComp2>(bool paused = true)
where TComp1 : IComponent
where TComp2 : IComponent;
@@ -220,7 +220,7 @@ namespace Robust.Shared.Interfaces.GameObjects
/// <typeparam name="TComp2">Second required component.</typeparam>
/// <typeparam name="TComp3">Third required component.</typeparam>
/// <returns>The pairs of components from each entity that has the three required components.</returns>
IEnumerable<(TComp1, TComp2, TComp3)> EntityQuery<TComp1, TComp2, TComp3>()
IEnumerable<(TComp1, TComp2, TComp3)> EntityQuery<TComp1, TComp2, TComp3>(bool paused = true)
where TComp1 : IComponent
where TComp2 : IComponent
where TComp3 : IComponent;
@@ -233,7 +233,7 @@ namespace Robust.Shared.Interfaces.GameObjects
/// <typeparam name="TComp3">Third required component.</typeparam>
/// <typeparam name="TComp4">Fourth required component.</typeparam>
/// <returns>The pairs of components from each entity that has the four required components.</returns>
IEnumerable<(TComp1, TComp2, TComp3, TComp4)> EntityQuery<TComp1, TComp2, TComp3, TComp4>()
IEnumerable<(TComp1, TComp2, TComp3, TComp4)> EntityQuery<TComp1, TComp2, TComp3, TComp4>(bool paused = true)
where TComp1 : IComponent
where TComp2 : IComponent
where TComp3 : IComponent
@@ -243,8 +243,9 @@ namespace Robust.Shared.Interfaces.GameObjects
/// Returns ALL component instances of a specified type.
/// </summary>
/// <param name="type">A trait or component type to check for.</param>
/// <param name="paused"></param>
/// <returns>All components that are the specified type.</returns>
IEnumerable<IComponent> GetAllComponents(Type type);
IEnumerable<IComponent> GetAllComponents(Type type, bool paused = true);
/// <summary>
/// Culls all components from the collection that are marked as deleted. This needs to be called often.

View File

@@ -41,6 +41,8 @@ namespace Robust.Shared.Interfaces.GameObjects
/// True if the entity has been deleted.
/// </summary>
bool Deleted { get; }
bool Paused { get; set; }
/// <summary>
/// The prototype that was used to create this entity.

View File

@@ -89,6 +89,13 @@ namespace Robust.Shared.Interfaces.GameObjects
IEnumerable<IEntity> GetEntities();
/// <summary>
/// Yields all of the entities on a particular map. faster than GetEntities()
/// </summary>
/// <param name="mapId"></param>
/// <returns></returns>
IEnumerable<IEntity> GetEntitiesInMap(MapId mapId);
/// <summary>
/// Shuts-down and removes given <see cref="IEntity"/>. This is also broadcast to all clients.
/// </summary>
@@ -171,7 +178,7 @@ namespace Robust.Shared.Interfaces.GameObjects
/// <param name="range"></param>
/// <param name="approximate">If true, will not recalculate precise entity AABBs, resulting in a perf increase. </param>
IEnumerable<IEntity> GetEntitiesInRange(EntityCoordinates position, float range, bool approximate = false);
/// <summary>
/// Gets entities within a certain *square* range of this entity
/// </summary>