Skip to content

Commit

Permalink
Звук сердцебиение в крите (#1114)
Browse files Browse the repository at this point in the history
* add: crit heartbeat sound

* fix: fix strange things happening on second crit

* add: disable option for children in prototypes

* add: shitty implementation of health-relative pitch scale

* clean up

* review requested changes

* review requested changes

* changes 2
  • Loading branch information
ThereDrD0 authored Jan 15, 2025
1 parent 8bbc68e commit 6206a0b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Content.Server/_Sunrise/CritHeartbeat/CritHeartbeatComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Robust.Shared.Audio;

namespace Content.Server._Sunrise.CritHeartbeat;

[RegisterComponent]
public sealed partial class CritHeartbeatComponent : Component
{
[DataField]
public SoundSpecifier HeartbeatSound = new SoundPathSpecifier("/Audio/_Sunrise/Effects/heartbeat.ogg");

/// <summary>
/// Чтобы выключать это для наследников в прототипах
/// </summary>
[DataField]
public bool Enabled = true;

public EntityUid? AudioStream;
}
45 changes: 45 additions & 0 deletions Content.Server/_Sunrise/CritHeartbeat/CritHeartbeatSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Content.Shared.Damage;
using Content.Shared.Mobs;
using Robust.Server.Audio;
using Robust.Shared.Audio;

namespace Content.Server._Sunrise.CritHeartbeat;

public sealed class CritHeartbeatSystem : EntitySystem
{
[Dependency] private readonly AudioSystem _audio = default!;

public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<CritHeartbeatComponent, MobStateChangedEvent>(OnMobStateChanged);
SubscribeLocalEvent<CritHeartbeatComponent, DamageChangedEvent>(OnDamage);
}

private void OnMobStateChanged(Entity<CritHeartbeatComponent> ent, ref MobStateChangedEvent args)
{
if (!ent.Comp.Enabled)
return;

ent.Comp.AudioStream = args.NewMobState == MobState.Critical
? _audio.PlayEntity(ent.Comp.HeartbeatSound, ent, ent)?.Entity
: _audio.Stop(ent.Comp.AudioStream);
}

private void OnDamage(Entity<CritHeartbeatComponent> ent, ref DamageChangedEvent args)
{
if (!ent.Comp.Enabled)
return;

if (!Exists(ent.Comp.AudioStream))
return;

var pitch = Math.Min(1, 100 / args.Damageable.TotalDamage.Float());

// Потому что игра говно, тут нельзя изменять аудиопарамс уже существующего звука. Поэтому я пересоздаю его заново
// Это приводит к проигрыванию звука через неравномерные промежутки времени, но зато работает и не очень заметно
_audio.Stop(ent.Comp.AudioStream);
ent.Comp.AudioStream = _audio.PlayEntity(ent.Comp.HeartbeatSound, ent, ent, AudioParams.Default.WithPitchScale(pitch))?.Entity;
}
}
Binary file added Resources/Audio/_Sunrise/Effects/heartbeat.ogg
Binary file not shown.
6 changes: 6 additions & 0 deletions Resources/Prototypes/Entities/Mobs/Species/base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,12 @@
- type: Carriable
- type: CanEscapeInventory
- type: Mood
- type: CritHeartbeat
heartbeatSound:
path: /Audio/_Sunrise/Effects/heartbeat.ogg
params:
volume: -3
loop: True
# Sunrise-End
- type: Barotrauma
damage:
Expand Down

0 comments on commit 6206a0b

Please sign in to comment.