Files
RobustToolbox/Robust.Shared/GameObjects/Components/MetaDataComponent.cs
Leon Friedrich 557de01532 Dirty PVS chunk when visibility mask changes (#3045)
* Dirty PVS chunk when visibility mask changes

* fix tests?

* aaaaaa

* maybe sorta finally fix tests

* directly return vis mask,

Also defaults to 1 only if visibility component does not exist.

* make sure everything has first bit set
2022-07-17 06:23:35 +10:00

198 lines
6.4 KiB
C#

using System;
using JetBrains.Annotations;
using Robust.Shared.GameStates;
using Robust.Shared.IoC;
using Robust.Shared.Players;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Timing;
using Robust.Shared.ViewVariables;
namespace Robust.Shared.GameObjects
{
/// <summary>
/// Serialized state of a <see cref="MetaDataComponent"/>.
/// </summary>
[Serializable, NetSerializable]
public sealed class MetaDataComponentState : ComponentState
{
/// <summary>
/// The in-game name of this entity.
/// </summary>
public string? Name { get; }
/// <summary>
/// The in-game description of this entity.
/// </summary>
public string? Description { get; }
/// <summary>
/// The prototype this entity was created from, if any.
/// </summary>
public string? PrototypeId { get; }
/// <summary>
/// Constructs a new instance of <see cref="MetaDataComponentState"/>.
/// </summary>
/// <param name="name">The in-game name of this entity.</param>
/// <param name="description">The in-game description of this entity.</param>
/// <param name="prototypeId">The prototype this entity was created from, if any.</param>
public MetaDataComponentState(string? name, string? description, string? prototypeId)
{
Name = name;
Description = description;
PrototypeId = prototypeId;
}
}
/// <summary>
/// Contains meta data about this entity that isn't component specific.
/// </summary>
[NetworkedComponent]
public sealed class MetaDataComponent : Component
{
[DataField("name")] internal string? _entityName;
[DataField("desc")] internal string? _entityDescription;
internal EntityPrototype? _entityPrototype;
internal bool _entityPaused;
// Every entity starts at tick 1, because they are conceptually created in the time between 0->1
[ViewVariables]
public GameTick EntityLastModifiedTick { get; internal set; } = new(1);
/// <summary>
/// The in-game name of this entity.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public string EntityName
{
get
{
if (_entityName == null)
return _entityPrototype != null ? _entityPrototype.Name : string.Empty;
return _entityName;
}
set
{
string? newValue = value;
if (_entityPrototype != null && _entityPrototype.Name == newValue)
newValue = null;
if (_entityName == newValue)
return;
_entityName = newValue;
Dirty();
}
}
/// <summary>
/// The in-game description of this entity.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public string EntityDescription
{
get
{
if (_entityDescription == null)
return _entityPrototype != null ? _entityPrototype.Description : string.Empty;
return _entityDescription;
}
set
{
string? newValue = value;
if (_entityPrototype != null && _entityPrototype.Description == newValue)
newValue = null;
if(_entityDescription == newValue)
return;
_entityDescription = newValue;
Dirty();
}
}
/// <summary>
/// The prototype this entity was created from, if any.
/// </summary>
[ViewVariables]
public EntityPrototype? EntityPrototype
{
get => _entityPrototype;
set
{
_entityPrototype = value;
Dirty();
}
}
/// <summary>
/// The current lifetime stage of this entity. You can use this to check
/// if the entity is initialized or being deleted.
/// </summary>
[ViewVariables]
public EntityLifeStage EntityLifeStage { get; internal set; }
[ViewVariables]
public MetaDataFlags Flags { get; internal set; }
/// <summary>
/// The sum of our visibility layer and our parent's visibility layers.
/// </summary>
/// <remarks>
/// Every entity will always have the first bit set to true.
/// </remarks>
[Access(typeof(MetaDataSystem))]
public int VisibilityMask = 1;
[UsedImplicitly, ViewVariables(VVAccess.ReadWrite)]
private int VVVisibilityMask
{
get => VisibilityMask;
set => IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<MetaDataSystem>().SetVisibilityMask(Owner, value, this);
}
[ViewVariables]
public bool EntityPaused
{
get => _entityPaused;
[Obsolete("Call MetaDataSystem to change this.")]
set
{
if (_entityPaused == value)
return;
_entityPaused = value;
IoCManager.Resolve<IEntityManager>().EventBus.RaiseLocalEvent(Owner, new EntityPausedEvent(Owner, value));
}
}
public bool EntityInitialized => EntityLifeStage >= EntityLifeStage.Initialized;
public bool EntityInitializing => EntityLifeStage == EntityLifeStage.Initializing;
public bool EntityDeleted => EntityLifeStage >= EntityLifeStage.Deleted;
internal override void ClearTicks()
{
// Do not clear modified ticks.
// MetaDataComponent is used in the game state system to carry initial data like prototype ID.
// So it ALWAYS has to be sent.
// (Creation can still be cleared though)
ClearCreationTick();
}
}
[Flags]
public enum MetaDataFlags : byte
{
None = 0,
/// <summary>
/// Whether the entity has states specific to a particular player.
/// </summary>
EntitySpecific = 1 << 0,
/// <summary>
/// Whether the entity is currently inside of a container.
/// </summary>
InContainer = 1 << 1,
}
}