Files
crystall-punk-14/Content.Server/GameObjects/Components/Weapon/Melee/StunbatonComponent.cs

225 lines
7.9 KiB
C#
Raw Normal View History

#nullable enable
using System.Collections.Generic;
using System.Threading.Tasks;
Add changing the amount of hands on the GUI depending on your body parts (#1406) * Multiple hands in gui first pass * Remove IHandsComponent interface * Create hand class and more hand textures * Refactor ServerHandsComponent to use a single list of hands * Seal SharedHand * Fix picked up items not showing on top of the hand buttons * Remove HandsGui buttons and panels dictionaries * Fix items in hands rendering * Fix wrong hand container comparison * Fix not updating the location of duplicate hands * Change ClientHandsComponent to use a SortedList instead of a dictionary * More merge conflict fixes * Change SortedList to List * Fix hand button order * Add item tooltip for more than 2 hands and updating when removing hands * Add add hand and remove hand command * Merge conflict fixes * Remove nullable reference type from ContainerSlot * Fix texture errors * Fix error when reaching 0 hands * Fix error when swapping hands with no hands * Merged remove hand methods * Fix item panel texture errors * Merge conflict fixes * Fix addhand and removehand command descriptions * Add properly displaying tooltips for 2 hands * Make hand indexes and locations consistent across the client and server * Add dropping held entity if a hand is removed * Change hand location to be calculated by index * Made different hand gui updates more consistent * Remove human body yml testing changes * Sanitize addhand and removehand commands * Merge conflict fixes * Remove testing changes * Revert body system changes * Add missing imports * Remove obsolete hands parameter in yml files * Fix broken import * Fix startup error and adding and removing hands on the same tick * Make hand container id use an uint In case someone gets more than 2 billion hands * Rename hand component files * Make hands state use an array
2020-07-25 15:11:16 +02:00
using Content.Server.GameObjects.Components.GUI;
using Content.Server.GameObjects.Components.Items.Storage;
2020-05-13 20:21:03 +02:00
using Content.Server.GameObjects.Components.Mobs;
2020-05-25 14:02:59 +01:00
using Content.Server.GameObjects.Components.Power;
Add changing the amount of hands on the GUI depending on your body parts (#1406) * Multiple hands in gui first pass * Remove IHandsComponent interface * Create hand class and more hand textures * Refactor ServerHandsComponent to use a single list of hands * Seal SharedHand * Fix picked up items not showing on top of the hand buttons * Remove HandsGui buttons and panels dictionaries * Fix items in hands rendering * Fix wrong hand container comparison * Fix not updating the location of duplicate hands * Change ClientHandsComponent to use a SortedList instead of a dictionary * More merge conflict fixes * Change SortedList to List * Fix hand button order * Add item tooltip for more than 2 hands and updating when removing hands * Add add hand and remove hand command * Merge conflict fixes * Remove nullable reference type from ContainerSlot * Fix texture errors * Fix error when reaching 0 hands * Fix error when swapping hands with no hands * Merged remove hand methods * Fix item panel texture errors * Merge conflict fixes * Fix addhand and removehand command descriptions * Add properly displaying tooltips for 2 hands * Make hand indexes and locations consistent across the client and server * Add dropping held entity if a hand is removed * Change hand location to be calculated by index * Made different hand gui updates more consistent * Remove human body yml testing changes * Sanitize addhand and removehand commands * Merge conflict fixes * Remove testing changes * Revert body system changes * Add missing imports * Remove obsolete hands parameter in yml files * Fix broken import * Fix startup error and adding and removing hands on the same tick * Make hand container id use an uint In case someone gets more than 2 billion hands * Rename hand component files * Make hands state use an array
2020-07-25 15:11:16 +02:00
using Content.Server.Interfaces.GameObjects.Components.Items;
2020-05-14 16:13:25 +02:00
using Content.Shared.Audio;
using Content.Shared.GameObjects.EntitySystems;
using Content.Shared.GameObjects.Verbs;
2020-05-25 14:02:59 +01:00
using Content.Shared.Interfaces;
using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Server.GameObjects;
2020-05-25 14:02:59 +01:00
using Robust.Server.GameObjects.Components.Container;
2020-05-14 16:13:25 +02:00
using Robust.Server.GameObjects.EntitySystems;
2020-05-25 14:02:59 +01:00
using Robust.Server.Interfaces.GameObjects;
2020-05-13 20:21:03 +02:00
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Systems;
2020-05-13 20:21:03 +02:00
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Random;
using Robust.Shared.IoC;
2020-05-25 14:02:59 +01:00
using Robust.Shared.Localization;
using Robust.Shared.Random;
2020-05-13 20:21:03 +02:00
using Robust.Shared.Serialization;
2020-05-25 14:02:59 +01:00
using Robust.Shared.Utility;
2020-05-14 16:13:25 +02:00
using Robust.Shared.ViewVariables;
2020-05-13 20:21:03 +02:00
namespace Content.Server.GameObjects.Components.Weapon.Melee
{
[RegisterComponent]
public class StunbatonComponent : MeleeWeaponComponent, IUse, IExamine, IInteractUsing, IThrowCollide
2020-05-13 20:21:03 +02:00
{
[Dependency] private readonly IRobustRandom _robustRandom = default!;
2020-05-13 20:21:03 +02:00
public override string Name => "Stunbaton";
2020-05-14 16:13:25 +02:00
private bool _activated = false;
[ViewVariables] private PowerCellSlotComponent _cellSlot = default!;
private PowerCellComponent? Cell => _cellSlot.Cell;
2020-05-25 14:02:59 +01:00
2020-05-14 16:13:25 +02:00
[ViewVariables(VVAccess.ReadWrite)]
private float _paralyzeChanceNoSlowdown = 0.35f;
[ViewVariables(VVAccess.ReadWrite)]
private float _paralyzeChanceWithSlowdown = 0.85f;
2020-05-14 16:13:25 +02:00
[ViewVariables(VVAccess.ReadWrite)]
private float _paralyzeTime = 10f;
[ViewVariables(VVAccess.ReadWrite)]
private float _slowdownTime = 5f;
2020-07-07 23:12:12 +02:00
[ViewVariables(VVAccess.ReadWrite)] public float EnergyPerUse { get; set; } = 50;
2020-05-25 14:02:59 +01:00
2020-05-14 16:13:25 +02:00
[ViewVariables]
public bool Activated => _activated;
2020-05-13 20:21:03 +02:00
2020-05-25 14:02:59 +01:00
public override void Initialize()
{
base.Initialize();
_cellSlot = Owner.EnsureComponent<PowerCellSlotComponent>();
2020-05-25 14:02:59 +01:00
}
2020-05-13 20:21:03 +02:00
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _paralyzeChanceNoSlowdown, "paralyzeChanceNoSlowdown", 0.35f);
serializer.DataField(ref _paralyzeChanceWithSlowdown, "paralyzeChanceWithSlowdown", 0.85f);
2020-05-13 20:21:03 +02:00
serializer.DataField(ref _paralyzeTime, "paralyzeTime", 10f);
serializer.DataField(ref _slowdownTime, "slowdownTime", 5f);
2020-05-13 20:21:03 +02:00
}
public override void HandleMessage(ComponentMessage message, IComponent? component)
{
base.HandleMessage(message, component);
switch (message)
{
case PowerCellChangedMessage m:
if (component is PowerCellSlotComponent slotComponent && slotComponent == _cellSlot)
{
if (m.Ejected)
{
TurnOff();
}
}
break;
}
}
protected override bool OnHitEntities(IReadOnlyList<IEntity> entities, AttackEventArgs eventArgs)
2020-05-13 20:21:03 +02:00
{
2020-07-07 23:12:12 +02:00
if (!Activated || entities.Count == 0 || Cell == null)
return true;
if (!Cell.TryUseCharge(EnergyPerUse))
return true;
EntitySystem.Get<AudioSystem>().PlayAtCoords("/Audio/Weapons/egloves.ogg", Owner.Transform.Coordinates, AudioHelpers.WithVariation(0.25f));
2020-05-14 16:13:25 +02:00
2020-05-13 20:21:03 +02:00
foreach (var entity in entities)
{
if (!entity.TryGetComponent(out StunnableComponent? stunnable)) continue;
if(!stunnable.SlowedDown)
if(_robustRandom.Prob(_paralyzeChanceNoSlowdown))
stunnable.Paralyze(_paralyzeTime);
else
stunnable.Slowdown(_slowdownTime);
else
if(_robustRandom.Prob(_paralyzeChanceWithSlowdown))
stunnable.Paralyze(_paralyzeTime);
else
stunnable.Slowdown(_slowdownTime);
}
2020-07-07 23:12:12 +02:00
if (!(Cell.CurrentCharge < EnergyPerUse)) return true;
EntitySystem.Get<AudioSystem>().PlayAtCoords(AudioHelpers.GetRandomFileFromSoundCollection("sparks"), Owner.Transform.Coordinates, AudioHelpers.WithVariation(0.25f));
2020-07-07 23:12:12 +02:00
TurnOff();
2020-05-25 14:02:59 +01:00
return true;
}
2020-05-25 14:02:59 +01:00
private bool ToggleStatus(IEntity user)
{
if (!ActionBlockerSystem.CanUse(user)) return false;
2020-05-25 14:02:59 +01:00
if (Activated)
{
TurnOff();
}
else
{
TurnOn(user);
}
return true;
}
private void TurnOff()
{
if (!_activated)
{
return;
}
var sprite = Owner.GetComponent<SpriteComponent>();
var item = Owner.GetComponent<ItemComponent>();
EntitySystem.Get<AudioSystem>().PlayAtCoords(AudioHelpers.GetRandomFileFromSoundCollection("sparks"), Owner.Transform.Coordinates, AudioHelpers.WithVariation(0.25f));
2020-05-25 14:02:59 +01:00
item.EquippedPrefix = "off";
sprite.LayerSetState(0, "stunbaton_off");
_activated = false;
}
private void TurnOn(IEntity user)
{
if (_activated)
{
2020-05-25 14:02:59 +01:00
return;
2020-05-13 20:21:03 +02:00
}
2020-05-25 14:02:59 +01:00
var sprite = Owner.GetComponent<SpriteComponent>();
var item = Owner.GetComponent<ItemComponent>();
if (Cell == null)
{
EntitySystem.Get<AudioSystem>().PlayAtCoords("/Audio/Machines/button.ogg", Owner.Transform.Coordinates, AudioHelpers.WithVariation(0.25f));
2020-05-25 14:02:59 +01:00
Owner.PopupMessage(user, Loc.GetString("Cell missing..."));
2020-05-25 14:02:59 +01:00
return;
}
2020-05-14 16:13:25 +02:00
if (Cell.CurrentCharge < EnergyPerUse)
2020-05-25 14:02:59 +01:00
{
EntitySystem.Get<AudioSystem>().PlayAtCoords("/Audio/Machines/button.ogg", Owner.Transform.Coordinates, AudioHelpers.WithVariation(0.25f));
Owner.PopupMessage(user, Loc.GetString("Dead cell..."));
2020-05-25 14:02:59 +01:00
return;
}
EntitySystem.Get<AudioSystem>().PlayAtCoords(AudioHelpers.GetRandomFileFromSoundCollection("sparks"), Owner.Transform.Coordinates, AudioHelpers.WithVariation(0.25f));
2020-05-25 14:02:59 +01:00
item.EquippedPrefix = "on";
sprite.LayerSetState(0, "stunbaton_on");
_activated = true;
}
public bool UseEntity(UseEntityEventArgs eventArgs)
{
ToggleStatus(eventArgs.User);
return true;
2020-05-13 20:21:03 +02:00
}
2020-05-25 14:02:59 +01:00
public async Task<bool> InteractUsing(InteractUsingEventArgs eventArgs)
2020-05-25 14:02:59 +01:00
{
if (!ActionBlockerSystem.CanInteract(eventArgs.User)) return false;
if (!_cellSlot.InsertCell(eventArgs.Using)) return false;
2020-05-25 14:02:59 +01:00
Dirty();
return true;
}
public void Examine(FormattedMessage message, bool inDetailsRange)
2020-05-25 14:02:59 +01:00
{
if (Activated)
{
message.AddMarkup(Loc.GetString("The light is currently [color=darkgreen]on[/color]."));
2020-05-25 14:02:59 +01:00
}
}
public void DoHit(ThrowCollideEventArgs eventArgs)
{
if (!Activated || Cell == null || !Cell.TryUseCharge(EnergyPerUse) || !eventArgs.Target.TryGetComponent(out StunnableComponent? stunnable))
return;
EntitySystem.Get<AudioSystem>().PlayAtCoords("/Audio/Weapons/egloves.ogg", Owner.Transform.Coordinates, AudioHelpers.WithVariation(0.25f));
stunnable.Paralyze(_paralyzeTime);
}
2020-05-13 20:21:03 +02:00
}
}