Skip to content

Commit

Permalink
отсоси
Browse files Browse the repository at this point in the history
  • Loading branch information
babaevlsdd committed Dec 20, 2024
1 parent e59f4c5 commit 73bdebb
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ private void OnAfterNVGUpdateVisualsEvent(EntityUid uid, NightVisionDeviceCompon
if (TryComp<SpriteComponent>(uid, out var sprite))
{
if (sprite.LayerMapTryGet(NVDVisuals.Light, out var layer))
sprite.LayerSetVisible(layer, !component.Activated);
sprite.LayerSetVisible(layer, component.Activated);
}
}
}
22 changes: 20 additions & 2 deletions Content.Client/_Sunrise/Eye/NightVision/NightVisionSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
using Robust.Client.Graphics;
using Robust.Client.Player;
using Robust.Shared.Player;
using Content.Shared.Mobs;

namespace Content.Client._Sunrise.Eye.NightVision;
public sealed class NightVisionSystem : EntitySystem
{
[Dependency] private readonly IPlayerManager _player = default!;
[Dependency] private readonly IOverlayManager _overlayMan = default!;
[Dependency] private readonly ILightManager _lightManager = default!;

private NightVisionOverlay _overlay = default!;

Expand All @@ -20,10 +22,26 @@ public override void Initialize()

SubscribeLocalEvent<NightVisionComponent, PlayerAttachedEvent>(OnPlayerAttached);
SubscribeLocalEvent<NightVisionComponent, PlayerDetachedEvent>(OnPlayerDetached);
SubscribeLocalEvent<NightVisionComponent, MobStateChangedEvent>(OnMobStateChanged);

_overlay = new();
}

private void OnMobStateChanged(EntityUid uid, NightVisionComponent component, MobStateChangedEvent args)
{
if (args.NewMobState == MobState.Dead)
{
ResetLighting();
}
}

private void ResetLighting()
{
if (_overlayMan.HasOverlay<NightVisionOverlay>())
_overlayMan.RemoveOverlay(_overlay);
_lightManager.DrawLighting = true;
}

private void OnPlayerAttached(EntityUid uid, NightVisionComponent component, PlayerAttachedEvent args)
{
if (_overlay == default!)
Expand All @@ -35,7 +53,7 @@ private void OnPlayerDetached(EntityUid uid, NightVisionComponent component, Pla
{
if (_overlay == default!)
return;
_overlayMan.RemoveOverlay(_overlay);
ResetLighting();
}

private void OnNightVisionInit(EntityUid uid, NightVisionComponent component, ComponentInit args)
Expand All @@ -51,6 +69,6 @@ private void OnNightVisionShutdown(EntityUid uid, NightVisionComponent component
return;
if (_overlay == default!)
return;
_overlayMan.RemoveOverlay(_overlay);
ResetLighting();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
using Content.Shared.PowerCell;
using Content.Shared.PowerCell.Components;
using Content.Shared.Popups;
using Content.Shared.Containers.ItemSlots;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Network;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Content.Shared.Examine;

namespace Content.Shared._Sunrise.Eye.NightVision.Systems;

Expand All @@ -23,11 +27,12 @@ public sealed class NightVisionDeviceSystem : EntitySystem
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] private readonly SharedPowerCellSystem _cell = default!;
[Dependency] private readonly ItemSlotsSystem _itemSlots = default!;

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

SubscribeLocalEvent<NightVisionDeviceComponent, InventoryRelayedEvent<CanVisionAttemptEvent>>(OnNVDTrySee);
SubscribeLocalEvent<NightVisionDeviceComponent, NightVisionDeviceUpdateVisualsEvent>(OnNightVisionDeviceUpdateVisuals);

Expand All @@ -39,6 +44,17 @@ public override void Initialize()
SubscribeLocalEvent<NightVisionDeviceComponent, ComponentShutdown>(OnShutdown);
}

private bool HasPowerAndBattery(EntityUid uid)
{
if (!TryComp<PowerCellSlotComponent>(uid, out var slot))
return false;

if (!_itemSlots.TryGetSlot(uid, slot.CellSlotId, out var itemSlot))
return false;

return itemSlot.Item != null && _powerCell.HasDrawCharge(uid);
}

private void OnNVDTrySee(EntityUid uid, NightVisionDeviceComponent component, InventoryRelayedEvent<CanVisionAttemptEvent> args)
{
args.Args.Cancel();
Expand All @@ -48,6 +64,8 @@ private void OnNightVisionDeviceUpdateVisuals(EntityUid uid, NightVisionDeviceCo
{
var updVisEv = new AfterNvdUpdateVisualsEvent();
RaiseLocalEvent(uid, ref updVisEv);

_appearance.SetData(uid, NVDVisuals.Light, component.Activated);
}

private void OnGetActions(EntityUid uid, NightVisionDeviceComponent component, GetItemActionsEvent args)
Expand All @@ -65,37 +83,64 @@ private void OnShutdown(EntityUid uid, NightVisionDeviceComponent component, Com

private void OnPowerCellSlotEmpty(Entity<NightVisionDeviceComponent> ent, ref PowerCellSlotEmptyEvent args)
{
if (ent.Comp.isPowered)
Toggle(ent);
if (ent.Comp.Activated)
ForceDisable(ent);
}

private void OnPowerCellChanged(Entity<NightVisionDeviceComponent> ent, ref PowerCellChangedEvent args)
{
if (args.Ejected || !_powerCell.HasDrawCharge(ent))
if (ent.Comp.isPowered)
Toggle(ent);
if (args.Ejected || !HasPowerAndBattery(ent.Owner))
{
if (ent.Comp.Activated)
ForceDisable(ent);
}
}

private void OnToggleAction(Entity<NightVisionDeviceComponent> ent, ref ToggleActionEvent args)
{
if (args.Handled)
return;

if (ent.Comp.isPowered && !_powerCell.HasDrawCharge(ent.Owner))
if (!HasPowerAndBattery(ent.Owner))
{
_popup.PopupClient(Loc.GetString("base-computer-ui-component-not-powered", ("machine", ent.Owner)), args.Performer, args.Performer);
return;
}

Toggle(ent);

args.Handled = true;
}
public void Toggle(Entity<NightVisionDeviceComponent> ent)

private void ForceDisable(Entity<NightVisionDeviceComponent> ent)
{
ent.Comp.Activated = false;

if (!_light.TryGetLight(ent.Owner, out var light))
return;

var draw = Comp<PowerCellDrawComponent>(ent.Owner);
_cell.SetDrawEnabled((ent.Owner, draw), false);

_appearance.SetData(ent, ToggleableLightVisuals.Enabled, false);
_light.SetEnabled(ent.Owner, false, comp: light);

var updVisEv = new NightVisionDeviceUpdateVisualsEvent();
RaiseLocalEvent(ent, ref updVisEv);

if (TryComp<TransformComponent>(ent.Owner, out var transform))
{
var equipped = transform.ParentUid;
var changeEv = new NightVisionDeviceToggledEvent(equipped);
RaiseLocalEvent(ent.Owner, ref changeEv);
}

Dirty(ent);
}

public void Toggle(Entity<NightVisionDeviceComponent> ent)
{
if (!HasPowerAndBattery(ent.Owner))
return;

ent.Comp.Activated = !ent.Comp.Activated;

Expand All @@ -115,13 +160,16 @@ public void Toggle(Entity<NightVisionDeviceComponent> ent)
_appearance.SetData(ent, ToggleableLightVisuals.Enabled, ent.Comp.Activated);
_light.SetEnabled(ent.Owner, ent.Comp.Activated, comp: light);

var updVisEv = new NightVisionDeviceUpdateVisualsEvent();
RaiseLocalEvent(ent, ref updVisEv);

if (TryComp<TransformComponent>(ent.Owner, out var transform))
{
var equipped = transform.ParentUid;

var changeEv = new NightVisionDeviceToggledEvent(equipped);
RaiseLocalEvent(ent.Owner, ref changeEv);
}

Dirty(ent);
}
}
Expand All @@ -134,11 +182,9 @@ public NightVisionDeviceToggledEvent(EntityUid equipped)
{
Equipped = equipped;
}

};

[PublicAPI, ByRefEvent]
public sealed class AfterNvdUpdateVisualsEvent : EntityEventArgs
{

}

0 comments on commit 73bdebb

Please sign in to comment.