Files
tbd-station-14/Content.Server/Window/WindowComponent.cs
Leon Friedrich df584ad446 ECS damageable (#4529)
* ECS and damage Data

* Comments and newlines

* Added Comments

* Make TryChangeDamageEvent immutable

* Remove SetAllDamage event

Use public SetAllDamage function instead

* Undo destructible mistakes

That was some shit code.

* Rename DamageData to DamageSpecifier

And misc small edits

misc

* Cache trigger prototypes.

* Renaming destructible classes & functions

* Revert "Cache trigger prototypes."

This reverts commit 86bae15ba6616884dba75f552dfdfbe2d1fb6586.

* Replace prototypes with prototype IDs.

* Split damage.yml into individual files

* move get/handle component state to system

* Update HealthChange doc

* Make godmode call Dirty() on damageable component

* Add Initialize() to fix damage test

* Make non-static

* uncache resistance set prototype and trim DamageableComponentState

* Remove unnecessary Dirty() calls during initialization

* RemoveTryChangeDamageEvent

* revert Dirty()

* Fix MobState relying on DamageableComponent.Dirty()

* Fix DisposalUnit Tests.

These were previously failing, but because the async was not await-ed, this never raised the exception.

After I fixed MobState component, this exception stopped happening and instead the assertions started being tested & failing

* Disposal test 2: electric boogaloo

* Fix typos/mistakes

also add comments and fix spacing.

* Use Uids instead of IEntity

* fix merge

* Comments, a merge issue, and making some damage ignore resistances

* Extend DamageSpecifier and use it for DamageableComponent

* fix master merge

* Fix Disposal unit test. Again.

Snapgrids were removed in master

* Execute Exectute
2021-09-14 10:07:37 -07:00

132 lines
4.5 KiB
C#

using System;
using Content.Server.Destructible;
using Content.Server.Destructible.Thresholds.Triggers;
using Content.Server.Notification;
using Content.Shared.Audio;
using Content.Shared.Damage;
using Content.Shared.Examine;
using Content.Shared.Interaction;
using Content.Shared.Rounding;
using Content.Shared.Sound;
using Content.Shared.Window;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Player;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
namespace Content.Server.Window
{
[RegisterComponent]
[ComponentReference(typeof(SharedWindowComponent))]
public class WindowComponent : SharedWindowComponent, IExamine, IInteractHand
{
[Dependency] private readonly IGameTiming _gameTiming = default!;
[ViewVariables(VVAccess.ReadWrite)] private TimeSpan _lastKnockTime;
[DataField("knockDelay")]
[ViewVariables(VVAccess.ReadWrite)]
private TimeSpan _knockDelay = TimeSpan.FromSeconds(0.5);
[DataField("rateLimitedKnocking")]
[ViewVariables(VVAccess.ReadWrite)] private bool _rateLimitedKnocking = true;
[DataField("knockSound")]
private SoundSpecifier _knockSound = new SoundPathSpecifier("/Audio/Effects/glass_knock.ogg");
public void UpdateVisuals(int currentDamage)
{
if (Owner.TryGetComponent(out AppearanceComponent? appearance) &&
Owner.TryGetComponent(out DestructibleComponent? destructible))
{
foreach (var threshold in destructible.Thresholds)
{
if (threshold.Trigger is not DamageTrigger trigger)
{
continue;
}
appearance.SetData(WindowVisuals.Damage, (float) currentDamage / trigger.Damage);
}
}
}
void IExamine.Examine(FormattedMessage message, bool inDetailsRange)
{
if (!Owner.TryGetComponent(out DamageableComponent? damageable) ||
!Owner.TryGetComponent(out DestructibleComponent? destructible))
{
return;
}
var damage = damageable.TotalDamage;
DamageTrigger? trigger = null;
// TODO: Pretend this does not exist until https://github.com/space-wizards/space-station-14/pull/2783 is merged
foreach (var threshold in destructible.Thresholds)
{
if ((trigger = threshold.Trigger as DamageTrigger) != null)
{
break;
}
}
if (trigger == null)
{
return;
}
var damageThreshold = trigger.Damage;
var fraction = damage == 0 || damageThreshold == 0
? 0f
: (float) damage / damageThreshold;
var level = Math.Min(ContentHelpers.RoundToLevels((double) fraction, 1, 7), 5);
switch (level)
{
case 0:
message.AddText(Loc.GetString("comp-window-damaged-1"));
break;
case 1:
message.AddText(Loc.GetString("comp-window-damaged-2"));
break;
case 2:
message.AddText(Loc.GetString("comp-window-damaged-3"));
break;
case 3:
message.AddText(Loc.GetString("comp-window-damaged-4"));
break;
case 4:
message.AddText(Loc.GetString("comp-window-damaged-5"));
break;
case 5:
message.AddText(Loc.GetString("comp-window-damaged-6"));
break;
}
}
bool IInteractHand.InteractHand(InteractHandEventArgs eventArgs)
{
if (_rateLimitedKnocking && _gameTiming.CurTime < _lastKnockTime + _knockDelay)
{
return false;
}
SoundSystem.Play(
Filter.Pvs(eventArgs.Target), _knockSound.GetSound(),
eventArgs.Target.Transform.Coordinates, AudioHelpers.WithVariation(0.05f));
eventArgs.Target.PopupMessageEveryone(Loc.GetString("comp-window-knock"));
_lastKnockTime = _gameTiming.CurTime;
return true;
}
}
}