Skip to content

Commit

Permalink
Рефактор
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitosAseev committed Sep 1, 2024
1 parent 20ccb5e commit be1ba4a
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 169 deletions.
81 changes: 0 additions & 81 deletions Content.Server/Stories/Reflectors/EmitterRefrectorSystem.cs

This file was deleted.

97 changes: 97 additions & 0 deletions Content.Server/Stories/Reflectors/RefrectorSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System.Numerics;
using Content.Server.Weapons.Ranged.Systems;
using Content.Shared.Popups;
using Content.Shared.Projectiles;
using Direction = Robust.Shared.Maths.Direction;
using Content.Shared.Stories.Reflectors;
using Content.Shared.Whitelist;
using Content.Shared.Weapons.Ranged.Components;
using Robust.Shared.Map;
using Robust.Shared.Network;

namespace Content.Server.Stories.Reflectors;
public sealed class ReflectorSystem : EntitySystem
{
[Dependency] private readonly EntityWhitelistSystem _whitelistSystem = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
[Dependency] private readonly INetManager _netManager = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] private readonly GunSystem _gun = default!;

public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ReflectorComponent, ProjectileReflectAttemptEvent>(OnReflectCollide);
}

private void OnReflectCollide(EntityUid uid, ReflectorComponent component, ref ProjectileReflectAttemptEvent args)
{
if (args.Cancelled)
return;

var collisionDirection = CalculateCollisionDirection(uid, args.ProjUid);
if (component.BlockedDirections.Contains(collisionDirection.ToString()))
return;

if (TryReflectProjectile(uid, component, args.ProjUid , collisionDirection))
args.Cancelled = true;
}
private Direction CalculateCollisionDirection(EntityUid uid, EntityUid projectile)
{
var projWorldPos = _transform.GetWorldPosition(projectile);
var uidWorldMatrix = _transform.GetInvWorldMatrix(Transform(uid));
var localCollisionPoint = Vector2.Transform(projWorldPos, uidWorldMatrix);

return localCollisionPoint.ToAngle().GetCardinalDir();

}

private bool TryReflectProjectile(EntityUid user, ReflectorComponent component , EntityUid projectile, Direction collisionDirection)
{
if (!TryComp<ReflectiveComponent>(projectile, out var reflective) ||
reflective.Reflective == 0x0 ||
!TryComp<GunComponent>(user, out var gunComponent) ||
_whitelistSystem.IsBlacklistPass(component.Blacklist, projectile) ||
_whitelistSystem.IsWhitelistFail(component.Whitelist, projectile))
{
return false;
}

var targetOffset = ReflectBasedOnType(component, collisionDirection);
if (!targetOffset.HasValue)
return false;

var xform = Transform(user);
var targetPos = new EntityCoordinates(user, targetOffset.Value);

_transform.SetLocalPosition(projectile, xform.LocalPosition + xform.LocalRotation.RotateVec(targetOffset.Value));

_gun.Shoot(user, gunComponent, projectile, xform.Coordinates, targetPos, out _);

if (_netManager.IsServer)
_popup.PopupEntity(Loc.GetString("reflect-shot"), user);

return true;
}

private Vector2 ReflectAngular(Direction collisionDirection)
{
return collisionDirection switch
{
Direction.North => Vector2.UnitY,
Direction.South => -Vector2.UnitY,
Direction.East => -Vector2.UnitX,
Direction.West => Vector2.UnitX,
_ => throw new ArgumentOutOfRangeException(nameof(collisionDirection)),
};
}
private Vector2? ReflectBasedOnType(ReflectorComponent component, Direction collisionDirection)
{
return component.State switch
{
ReflectorType.Simple => component.ReflectionDirection?.ToVec(),
ReflectorType.Angular => ReflectAngular(collisionDirection),
_ => throw new ArgumentOutOfRangeException(nameof(component.State), component.State, "Invalid ReflectorType encountered."),
};
}
}
23 changes: 0 additions & 23 deletions Content.Shared/Stories/Reflectors/EmitterRefrectorComponent.cs

This file was deleted.

14 changes: 0 additions & 14 deletions Content.Shared/Stories/Reflectors/ReflectCountComponent.cs

This file was deleted.

31 changes: 31 additions & 0 deletions Content.Shared/Stories/Reflectors/RefrectorComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Content.Shared.Whitelist;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;

namespace Content.Shared.Stories.Reflectors;

[RegisterComponent, NetworkedComponent, AutoGenerateComponentState(true)]
public sealed partial class ReflectorComponent : Component
{
[DataField]
public EntityWhitelist? Whitelist;

[DataField]
public EntityWhitelist? Blacklist;

[DataField]
public List<string> BlockedDirections = new();

[DataField]
public Direction? ReflectionDirection;

[DataField, AutoNetworkedField]
public ReflectorType State = ReflectorType.Simple;
}

[Serializable, NetSerializable]
public enum ReflectorType
{
Simple,
Angular,
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@
description: Машинная плата отражателя, тип "Коробка"
components:
- type: Sprite
sprite: Objects/Misc/module.rsi
state: power_mod
sprite: Stories/Objects/Misc/module.rsi
state: reflectorbox_circuitboard
- type: MachineBoard
prototype: ReflectorBox
stackRequirements:
Capacitor: 1
Capacitor: 3
CableHV: 10
componentRequirements:
PowerCell:
amount: 4
defaultPrototype: PowerCellSmall
amount: 1
defaultPrototype: PowerCellMedium

- type: entity
id: ReflectorCornerMachineCircuitboard
Expand All @@ -38,17 +38,17 @@
description: Машинная плата отражателя, тип "Угловой"
components:
- type: Sprite
sprite: Objects/Misc/module.rsi
state: power_mod
sprite: Stories/Objects/Misc/module.rsi
state: reflectorcorner_circuitboard
- type: MachineBoard
prototype: ReflectorCorner
stackRequirements:
Capacitor: 1
Capacitor: 3
CableHV: 10
componentRequirements:
PowerCell:
amount: 4
defaultPrototype: PowerCellSmall
amount: 1
defaultPrototype: PowerCellMedium

- type: entity
id: ReflectorCornerTsideMachineCircuitboard
Expand All @@ -57,15 +57,15 @@
description: Машинная плата отражателя, тип "Двойной-Угловой"
components:
- type: Sprite
sprite: Objects/Misc/module.rsi
state: power_mod
sprite: Stories/Objects/Misc/module.rsi
state: reflectortsidecorner_circuitboard
- type: MachineBoard
prototype: ReflectoCornerTside
stackRequirements:
Capacitor: 1
Capacitor: 3
CableHV: 10
componentRequirements:
PowerCell:
amount: 4
defaultPrototype: PowerCellSmall
amount: 1
defaultPrototype: PowerCellMedium

Loading

0 comments on commit be1ba4a

Please sign in to comment.