Add EmoteOnDamage comp/system for zombies (#14371)

This commit is contained in:
0x6273
2023-03-23 15:52:46 +01:00
committed by GitHub
parent 35c237b9e1
commit 21e5aea8ca
5 changed files with 162 additions and 62 deletions

View File

@@ -0,0 +1,51 @@
namespace Content.Server.Chat;
using Content.Server.Chat.Systems;
using Content.Shared.Chat.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.Set;
/// <summary>
/// Causes an entity to automatically emote when taking damage.
/// </summary>
[RegisterComponent, Access(typeof(EmoteOnDamageSystem))]
public sealed class EmoteOnDamageComponent : Component
{
/// <summary>
/// Chance of preforming an emote when taking damage and not on cooldown.
/// </summary>
[DataField("emoteChance"), ViewVariables(VVAccess.ReadWrite)]
public float EmoteChance = 0.5f;
/// <summary>
/// A set of emotes that will be randomly picked from.
/// <see cref="EmotePrototype"/>
/// </summary>
[DataField("emotes", customTypeSerializer: typeof(PrototypeIdHashSetSerializer<EmotePrototype>)), ViewVariables(VVAccess.ReadWrite)]
public HashSet<string> Emotes = new();
/// <summary>
/// Also send the emote in chat.
/// <summary>
[DataField("withChat"), ViewVariables(VVAccess.ReadWrite)]
public bool WithChat = false;
/// <summary>
/// Hide the chat message from the chat window, only showing the popup.
/// This does nothing if WithChat is false.
/// <summary>
[DataField("hiddenFromChatWindow")]
public bool HiddenFromChatWindow = false;
/// <summary>
/// The simulation time of the last emote preformed due to taking damage.
/// </summary>
[DataField("lastEmoteTime", customTypeSerializer: typeof(TimeOffsetSerializer)), ViewVariables(VVAccess.ReadWrite)]
public TimeSpan LastEmoteTime = TimeSpan.Zero;
/// <summary>
/// The cooldown between emotes.
/// </summary>
[DataField("emoteCooldown"), ViewVariables(VVAccess.ReadWrite)]
public TimeSpan EmoteCooldown = TimeSpan.FromSeconds(2);
}

View File

@@ -0,0 +1,82 @@
namespace Content.Server.Chat.Systems;
using Content.Shared.Chat.Prototypes;
using Content.Shared.Damage;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
public sealed class EmoteOnDamageSystem : EntitySystem
{
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly ChatSystem _chatSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<EmoteOnDamageComponent, EntityUnpausedEvent>(OnUnpaused);
SubscribeLocalEvent<EmoteOnDamageComponent, DamageChangedEvent>(OnDamage);
}
private void OnUnpaused(EntityUid uid, EmoteOnDamageComponent emoteOnDamage, ref EntityUnpausedEvent args)
{
emoteOnDamage.LastEmoteTime += args.PausedTime;
}
private void OnDamage(EntityUid uid, EmoteOnDamageComponent emoteOnDamage, DamageChangedEvent args)
{
if (!args.DamageIncreased)
return;
if (emoteOnDamage.LastEmoteTime + emoteOnDamage.EmoteCooldown > _gameTiming.CurTime)
return;
if (emoteOnDamage.Emotes.Count == 0)
return;
if (!_random.Prob(emoteOnDamage.EmoteChance))
return;
var emote = _random.Pick(emoteOnDamage.Emotes);
if (emoteOnDamage.WithChat)
{
_chatSystem.TryEmoteWithChat(uid, emote, emoteOnDamage.HiddenFromChatWindow);
}
else
{
_chatSystem.TryEmoteWithoutChat(uid,emote);
}
emoteOnDamage.LastEmoteTime = _gameTiming.CurTime;
}
/// <summary>
/// Try to add an emote to the entity, which will be performed at an interval.
/// </summary>
public bool AddEmote(EntityUid uid, string emotePrototypeId, EmoteOnDamageComponent? emoteOnDamage = null)
{
if (!Resolve(uid, ref emoteOnDamage, logMissing: false))
return false;
DebugTools.Assert(_prototypeManager.HasIndex<EmotePrototype>(emotePrototypeId), "Prototype not found. Did you make a typo?");
return emoteOnDamage.Emotes.Add(emotePrototypeId);
}
/// <summary>
/// Stop preforming an emote.
/// </summary>
public bool RemoveEmote(EntityUid uid, string emotePrototypeId, EmoteOnDamageComponent? emoteOnDamage = null)
{
if (!Resolve(uid, ref emoteOnDamage, logMissing: false))
return false;
DebugTools.Assert(_prototypeManager.HasIndex<EmotePrototype>(emotePrototypeId), "Prototype not found. Did you make a typo?");
return emoteOnDamage.Emotes.Remove(emotePrototypeId);
}
}