Files
RobustToolbox/Robust.Shared/GameObjects/Components/CollisionWakeComponent.cs
T
2021-11-30 15:07:08 +01:00

75 lines
2.0 KiB
C#

using System;
using System.Linq;
using Robust.Shared.GameStates;
using Robust.Shared.Physics;
using Robust.Shared.Players;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Robust.Shared.GameObjects
{
/// <summary>
/// An optimisation component for stuff that should be set as collidable when it's awake and non-collidable when asleep.
/// </summary>
[NetworkedComponent()]
public sealed class CollisionWakeComponent : Component
{
public override string Name => "CollisionWake";
[DataField("enabled")]
private bool _enabled = true;
[ViewVariables(VVAccess.ReadWrite)]
public bool Enabled
{
get => _enabled;
set
{
if (value == _enabled) return;
_enabled = value;
Dirty();
RaiseStateChange();
}
}
internal void RaiseStateChange()
{
Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, new CollisionWakeStateMessage(), false);
}
protected override void OnRemove()
{
base.OnRemove();
if (Owner.TryGetComponent(out IPhysBody? body) && Owner.LifeStage < EntityLifeStage.Terminating)
{
body.CanCollide = true;
}
}
public override ComponentState GetComponentState()
{
return new CollisionWakeState(Enabled);
}
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
if (curState is not CollisionWakeState state) return;
Enabled = state.Enabled;
}
[Serializable, NetSerializable]
public class CollisionWakeState : ComponentState
{
public bool Enabled { get; }
public CollisionWakeState(bool enabled)
{
Enabled = enabled;
}
}
}
}