Files
tbd-station-14/Content.Server/Projectiles/Components/HitscanComponent.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

165 lines
5.7 KiB
C#

using System;
using Content.Shared.Damage;
using Content.Shared.Physics;
using Content.Shared.Sound;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Player;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Timing;
using Robust.Shared.ViewVariables;
namespace Content.Server.Projectiles.Components
{
/// <summary>
/// Lasers etc.
/// </summary>
[RegisterComponent]
public class HitscanComponent : Component
{
[Dependency] private readonly IGameTiming _gameTiming = default!;
public override string Name => "Hitscan";
public CollisionGroup CollisionMask => (CollisionGroup) _collisionMask;
[DataField("layers")] //todo WithFormat.Flags<CollisionLayer>()
private int _collisionMask = (int) CollisionGroup.Opaque;
[DataField("damage", required: true)]
[ViewVariables(VVAccess.ReadWrite)]
public DamageSpecifier Damage = default!;
public float MaxLength => 20.0f;
private TimeSpan _startTime;
private TimeSpan _deathTime;
public float ColorModifier { get; set; } = 1.0f;
[DataField("spriteName")]
private string _spriteName = "Objects/Weapons/Guns/Projectiles/laser.png";
[DataField("muzzleFlash")]
private string? _muzzleFlash;
[DataField("impactFlash")]
private string? _impactFlash;
[DataField("soundHitWall")]
private SoundSpecifier _soundHitWall = new SoundPathSpecifier("/Audio/Weapons/Guns/Hits/laser_sear_wall.ogg");
public void FireEffects(IEntity user, float distance, Angle angle, IEntity? hitEntity = null)
{
var effectSystem = EntitySystem.Get<EffectSystem>();
_startTime = _gameTiming.CurTime;
_deathTime = _startTime + TimeSpan.FromSeconds(1);
var afterEffect = AfterEffects(user.Transform.Coordinates, angle, distance, 1.0f);
if (afterEffect != null)
{
effectSystem.CreateParticle(afterEffect);
}
// if we're too close we'll stop the impact and muzzle / impact sprites from clipping
if (distance > 1.0f)
{
var impactEffect = ImpactFlash(distance, angle);
if (impactEffect != null)
{
effectSystem.CreateParticle(impactEffect);
}
var muzzleEffect = MuzzleFlash(user.Transform.Coordinates, angle);
if (muzzleEffect != null)
{
effectSystem.CreateParticle(muzzleEffect);
}
}
if (hitEntity != null && _soundHitWall != null)
{
// TODO: No wall component so ?
var offset = angle.ToVec().Normalized / 2;
var coordinates = user.Transform.Coordinates.Offset(offset);
SoundSystem.Play(Filter.Pvs(coordinates), _soundHitWall.GetSound(), coordinates);
}
Owner.SpawnTimer((int) _deathTime.TotalMilliseconds, () =>
{
if (!Owner.Deleted)
{
Owner.Delete();
}
});
}
private EffectSystemMessage? MuzzleFlash(EntityCoordinates grid, Angle angle)
{
if (_muzzleFlash == null)
{
return null;
}
var offset = angle.ToVec().Normalized / 2;
var message = new EffectSystemMessage
{
EffectSprite = _muzzleFlash,
Born = _startTime,
DeathTime = _deathTime,
Coordinates = grid.Offset(offset),
//Rotated from east facing
Rotation = (float) angle.Theta,
Color = Vector4.Multiply(new Vector4(255, 255, 255, 750), ColorModifier),
ColorDelta = new Vector4(0, 0, 0, -1500f),
Shaded = false
};
return message;
}
private EffectSystemMessage AfterEffects(EntityCoordinates origin, Angle angle, float distance, float offset = 0.0f)
{
var midPointOffset = angle.ToVec() * distance / 2;
var message = new EffectSystemMessage
{
EffectSprite = _spriteName,
Born = _startTime,
DeathTime = _deathTime,
Size = new Vector2(distance - offset, 1f),
Coordinates = origin.Offset(midPointOffset),
//Rotated from east facing
Rotation = (float) angle.Theta,
Color = Vector4.Multiply(new Vector4(255, 255, 255, 750), ColorModifier),
ColorDelta = new Vector4(0, 0, 0, -1500f),
Shaded = false
};
return message;
}
private EffectSystemMessage? ImpactFlash(float distance, Angle angle)
{
if (_impactFlash == null)
{
return null;
}
var message = new EffectSystemMessage
{
EffectSprite = _impactFlash,
Born = _startTime,
DeathTime = _deathTime,
Coordinates = Owner.Transform.Coordinates.Offset(angle.ToVec() * distance),
//Rotated from east facing
Rotation = (float) angle.FlipPositive(),
Color = Vector4.Multiply(new Vector4(255, 255, 255, 750), ColorModifier),
ColorDelta = new Vector4(0, 0, 0, -1500f),
Shaded = false
};
return message;
}
}
}