Re-organize all projects (#4166)
This commit is contained in:
90
Content.Client/MobState/DamageStateVisualizer.cs
Normal file
90
Content.Client/MobState/DamageStateVisualizer.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
#nullable enable
|
||||
using System.Collections.Generic;
|
||||
using Content.Shared.DrawDepth;
|
||||
using Content.Shared.MobState;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
|
||||
namespace Content.Client.MobState
|
||||
{
|
||||
[UsedImplicitly]
|
||||
public sealed class DamageStateVisualizer : AppearanceVisualizer, ISerializationHooks
|
||||
{
|
||||
private DamageState _data = DamageState.Alive;
|
||||
private Dictionary<DamageState, string> _stateMap = new();
|
||||
private int? _originalDrawDepth;
|
||||
|
||||
[DataField("normal")]
|
||||
private string? normal;
|
||||
[DataField("crit")]
|
||||
private string? crit;
|
||||
[DataField("dead")]
|
||||
private string? dead;
|
||||
|
||||
void ISerializationHooks.BeforeSerialization()
|
||||
{
|
||||
_stateMap.TryGetValue(DamageState.Alive, out normal);
|
||||
_stateMap.TryGetValue(DamageState.Critical, out crit);
|
||||
_stateMap.TryGetValue(DamageState.Dead, out dead);
|
||||
}
|
||||
|
||||
void ISerializationHooks.AfterDeserialization()
|
||||
{
|
||||
if (normal != null)
|
||||
{
|
||||
_stateMap.Add(DamageState.Alive, normal);
|
||||
}
|
||||
|
||||
if (crit != null)
|
||||
{
|
||||
_stateMap.Add(DamageState.Critical, crit);
|
||||
}
|
||||
|
||||
if (dead != null)
|
||||
{
|
||||
_stateMap.Add(DamageState.Dead, dead);
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnChangeData(AppearanceComponent component)
|
||||
{
|
||||
base.OnChangeData(component);
|
||||
var sprite = component.Owner.GetComponent<ISpriteComponent>();
|
||||
if (!component.TryGetData(DamageStateVisuals.State, out DamageState data))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_data == data)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_data = data;
|
||||
|
||||
if (_stateMap.TryGetValue(_data, out var state))
|
||||
{
|
||||
sprite.LayerSetState(DamageStateVisualLayers.Base, state);
|
||||
}
|
||||
|
||||
// So they don't draw over mobs anymore
|
||||
if (_data == DamageState.Dead)
|
||||
{
|
||||
_originalDrawDepth = sprite.DrawDepth;
|
||||
sprite.DrawDepth = (int) DrawDepth.FloorObjects;
|
||||
}
|
||||
else if (_originalDrawDepth != null)
|
||||
{
|
||||
sprite.DrawDepth = _originalDrawDepth.Value;
|
||||
_originalDrawDepth = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum DamageStateVisualLayers : byte
|
||||
{
|
||||
Base
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user