Skip to content
This repository has been archived by the owner on Apr 6, 2024. It is now read-only.

Commit

Permalink
Further tweaking towards Nightvision component having custom actions …
Browse files Browse the repository at this point in the history
…per instance.
  • Loading branch information
Pspritechologist committed Apr 6, 2023
1 parent 88c5e1a commit aa8f1e5
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 23 deletions.
107 changes: 98 additions & 9 deletions Content.Client/SimpleStation14/Overlays/Systems/NightvisionSystem.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Client.Player;
using Robust.Shared.Network;
using Content.Shared.SimpleStation14.Overlays;
using Robust.Shared.Prototypes;
using Content.Shared.Actions.ActionTypes;
using Content.Shared.Actions;

namespace Content.Client.SimpleStation14.Overlays;
public sealed class NightvisionSystem : EntitySystem
{
[Dependency] private readonly IPlayerManager _player = default!;
[Dependency] private readonly IOverlayManager _overlayMan = default!;
[Dependency] private readonly INetManager _net = default!;
[Dependency] private readonly IEntityManager _entityManager = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly SharedActionsSystem _action = default!;

private NightvisionOverlay _overlay = default!;

Expand All @@ -24,30 +27,116 @@ public override void Initialize()
SubscribeLocalEvent<NightvisionComponent, PlayerAttachedEvent>(OnPlayerAttached);
SubscribeLocalEvent<NightvisionComponent, PlayerDetachedEvent>(OnPlayerDetached);

SubscribeLocalEvent<NightvisionComponent, NightvisionToggleActionEvent>(OnNightvisionToggle);

_overlay = new();
}

private void OnNightvisionStartup(EntityUid uid, NightvisionComponent component, ComponentStartup args)

private void OnNightvisionToggle(EntityUid uid, NightvisionComponent component, NightvisionToggleActionEvent args)
{
if (_player.LocalPlayer?.ControlledEntity == uid)
if (_player.LocalPlayer?.ControlledEntity == null)
{
Logger.Warning("Tried to toggle nightvision without a player!");
return;
}

// var uid = _player.LocalPlayer.ControlledEntity.Value;
// var component = _entityManager.GetComponent<NightvisionComponent>(uid);

Logger.Warning("Toggled nightvision!");

if (!component.Enabled)
{
component.Enabled = true;
_overlayMan.AddOverlay(_overlay);
}
else
{
component.Enabled = false;
_overlayMan.RemoveOverlay(_overlay);
}
}

private void OnNightvisionShutdown(EntityUid uid, NightvisionComponent component, ComponentShutdown args)
/// <summary>
/// Handles adding the toggle Nightvision action to a player, based on the <see cref="NightvisionComponent"/>.
/// </summary>
/// <param name="uid">The entity to add the action to.</param>
/// <param name="component">The component to use for the action.</param>
/// <param name="provider">The entity that provides the action. Defaults to the entity itself.</param>
/// <param name="enabled">Whether or not the action is enabled immediately.</param>
private void AddNightvisionAction(EntityUid uid, NightvisionComponent component, EntityUid? provider = null, bool enabled = false)
{
if (_player.LocalPlayer?.ControlledEntity == uid)
if (!_prototypeManager.TryIndex<InstantActionPrototype>("NightvisionToggleAction", out var toggleAction))
return;

var action = new InstantAction(toggleAction);

action.DisplayName = component.ActionName;
action.Description = component.ActionDescription;

if (component.Icon != null)
{
_overlayMan.RemoveOverlay(_overlay);
action.Icon = component.Icon;

if (component.IconOn != null)
action.IconOn = component.IconOn;
}
else

if (component.ToggleSound != null)
{
action.Sound = component.ToggleSound;
}

_action.AddAction(uid, action, provider ?? uid);

if (enabled)
{
_action.SetToggled(action, true);
}
}

private void AddNightvision(EntityUid uid, NightvisionComponent component, EntityUid? provider = null)
{
if (component.Enabled)
{
_overlayMan.AddOverlay(_overlay);
}

if (component.Toggle)
{
AddNightvisionAction(uid, component, uid, component.Enabled);
}
}

private void RemoveOverlay(EntityUid uid, NightvisionComponent component)
{
_overlayMan.RemoveOverlay(_overlay);
}


private void OnNightvisionStartup(EntityUid uid, NightvisionComponent component, ComponentStartup args)
{
if (component.Granted && _player.LocalPlayer?.ControlledEntity == uid)
AddNightvision(uid, component);
}

private void OnNightvisionShutdown(EntityUid uid, NightvisionComponent component, ComponentShutdown args)
{
if (component.Granted && _player.LocalPlayer?.ControlledEntity == uid)
RemoveOverlay(uid, component);
}

private void OnPlayerAttached(EntityUid uid, NightvisionComponent component, PlayerAttachedEvent args)
{
_overlayMan.AddOverlay(_overlay);
if (component.Granted)
AddNightvision(uid, component);
}

private void OnPlayerDetached(EntityUid uid, NightvisionComponent component, PlayerDetachedEvent args)
{
_overlayMan.RemoveOverlay(_overlay);
if (component.Granted)
RemoveOverlay(uid, component);
}
}
67 changes: 55 additions & 12 deletions Content.Shared/SimpleStation14/Shaders/NightvisionComponent.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,53 @@
using Content.Shared.Actions;
using Robust.Shared.Audio;
using Robust.Shared.Utility;

namespace Content.Shared.SimpleStation14.Overlays;


public sealed class NightvisionToggleActionEvent : InstantActionEvent { }


[RegisterComponent]
public sealed class NightvisionComponent : Component
{
/// <summary>
/// The tint color to apply to the screen, in RGB. Defaults to green.
/// </summary>
[DataField("tint"), ViewVariables(VVAccess.ReadWrite)]
public Vector3 Tint = new(0.0f, 1f, 0.1f);
public Vector3 Tint = new(0.5f, 0.5f, 0.5f);

/// <summary>
/// The amount to boost the brightness of the screen.
/// </summary>
[DataField("strength"), ViewVariables(VVAccess.ReadWrite)]
public float Strength = 0.7f;
public float Strength = 5f;

/// <summary>
/// The intensity of the noise effect.
/// </summary>
[DataField("noise"), ViewVariables(VVAccess.ReadWrite)]
public float Noise = 0.5f;
public float Noise = 0.0f;

/// <summary>
/// Whether or not the nightvision is inherient to the entity.
/// Whether or not the nightvision applies to the owner.
/// Would be false for equipment that grants nightvision.
/// </summary>
[DataField("inherint"), ViewVariables(VVAccess.ReadWrite)]
public bool Inherint;
/// <remarks>
/// USEINHAND EQUIPCLOTHING
/// </remarks>
[DataField("granted"), ViewVariables(VVAccess.ReadWrite)]
public bool Granted = true;

/// <summary>
/// The entity that granted this nightvision, if valid
/// </summary>
[ViewVariables(VVAccess.ReadOnly)]
public EntityUid? GranterUid;


/// <summary>
/// Can the nightvision be toggled on and off via action?
/// If true, grants an action. Action details can be defined in the YAML.
/// </summary>
[DataField("toggle"), ViewVariables(VVAccess.ReadOnly)]
public bool Toggle = false;
Expand All @@ -44,12 +56,43 @@ public sealed class NightvisionComponent : Component
/// Is the nightvision currently enabled?
/// </summary>
[DataField("enabled"), ViewVariables(VVAccess.ReadWrite)]
public bool Enabled = false;
public bool Enabled = true;


[DataField("sprite")]
public string rsi = "Objects/Weapons/Guns/Projectiles/Projectiles.rsi";
// For customizing the action.

[DataField("state")]
public string state = "bolt";
/// <summary>
/// The name of the action.
/// </summary>
[DataField("actionName")]
public string ActionName = "actions-nightvision-toggle-name";

/// <summary>
/// The description of the action.
/// </summary>
[DataField("actionDescription")]
public string ActionDescription = "actions-nightvision-toggle-description";

/// <summary>
/// Icon state for the action.
/// If null, the entity's icon will be used.
/// </summary>
[DataField("icon")]
public SpriteSpecifier? Icon;

/// <summary>
/// State for when the night vision is toggled on.
/// If null, the regular state will be used.
/// </summary>
/// <remarks>
/// This *must* be used with <see cref="IconOn"/>, not by itself.
/// </remarks>
[DataField("iconOn")]
public SpriteSpecifier? IconOn;

/// <summary>
/// Sound to play when the nightvision is toggled.
/// </summary>
[DataField("toggleSound")]
public SoundSpecifier? ToggleSound;
}
Binary file not shown.
2 changes: 1 addition & 1 deletion Resources/Prototypes/CM-SS14/Actions/Xeno.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
name: xeno-action-rest-name
description: xeno-action-rest-description
serverEvent: !type:XenoRestActionEvent
useDelay: 20
useDelay: 10
9 changes: 9 additions & 0 deletions Resources/Prototypes/CM-SS14/Actions/misc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- type: instantAction
id: NightvisionToggleAction
icon: Interface/Actions/web.png
name: misc-action-nightvision-name
description: misc-action-nightvision-description
serverEvent: !type:NightvisionToggleActionEvent
useDelay: 20
clientExclusive: true

9 changes: 8 additions & 1 deletion Resources/Prototypes/Entities/Mobs/NPCs/xeno.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,17 @@
- type: Xeno
- type: Nightvision
enabled: true
tint: 1, 1, 1
tint: 1, 0.90, 0.95
strength: 16
noise: 0
toggle: true
toggleSound: /Audio/CM-SS14/Effects/Items/nightvision.ogg
icon:
sprite: Actions/Implants/implants.rsi
state: explosive
iconOn:
sprite: Actions/Implants/implants.rsi
state: freedom

- type: entity
name: Praetorian
Expand Down

0 comments on commit aa8f1e5

Please sign in to comment.