Finish refactoring tools. Add multitools. (as in multiple tools in one)

This commit is contained in:
zumorica
2020-04-29 13:43:07 +02:00
parent ca5638badf
commit ff5549a0d1
37 changed files with 840 additions and 467 deletions

View File

@@ -0,0 +1,114 @@
using System.Collections.Generic;
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.GameObjects.Components.Interactable;
using Robust.Server.GameObjects;
using Robust.Server.GameObjects.EntitySystems;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Serialization;
using Robust.Shared.IoC;
using Robust.Shared.Serialization;
namespace Content.Server.GameObjects.Components.Interactable
{
/// <summary>
/// Not to be confused with Multitool (power)
/// </summary>
[RegisterComponent]
public class MultiToolComponent : Component, IUse
{
public class ToolEntry : IExposeData
{
private string _state;
private string _sound;
private string _soundCollection;
private string _texture;
private string _sprite;
private string _changeSound;
public Tool Behavior { get; private set; }
public string State => _state;
public string Texture => _texture;
public string Sprite => _sprite;
public string Sound => _sound;
public string SoundCollection => _soundCollection;
public string ChangeSound => _changeSound;
public void ExposeData(ObjectSerializer serializer)
{
Behavior = (Tool)serializer.ReadStringEnumKey("behavior");
serializer.DataField(ref _state, "state", string.Empty);
serializer.DataField(ref _sprite, "sprite", string.Empty);
serializer.DataField(ref _texture, "texture", string.Empty);
serializer.DataField(ref _sound, "useSound", string.Empty);
serializer.DataField(ref _soundCollection, "useSoundCollection", string.Empty);
serializer.DataField(ref _changeSound, "changeSound", string.Empty);
}
}
#pragma warning disable 649
[Dependency] private IEntitySystemManager _entitySystemManager;
#pragma warning restore 649
public override string Name => "MultiTool";
private List<ToolEntry> _tools;
private int _currentTool = 0;
private AudioSystem _audioSystem;
private ToolComponent _tool;
private SpriteComponent _sprite;
public override void Initialize()
{
base.Initialize();
Owner.TryGetComponent(out _tool);
Owner.TryGetComponent(out _sprite);
_audioSystem = _entitySystemManager.GetEntitySystem<AudioSystem>();
SetTool();
}
public void Cycle()
{
_currentTool = (_currentTool + 1) % _tools.Count;
SetTool();
var current = _tools[_currentTool];
if(!string.IsNullOrEmpty(current.ChangeSound))
_audioSystem.Play(current.ChangeSound, Owner);
}
private void SetTool()
{
if (_tool == null) return;
var current = _tools[_currentTool];
_tool.UseSound = current.Sound;
_tool.UseSoundCollection = current.SoundCollection;
_tool.Behavior = current.Behavior;
if (_sprite == null) return;
if (string.IsNullOrEmpty(current.Texture))
if (!string.IsNullOrEmpty(current.Sprite))
_sprite.LayerSetState(0, current.State, current.Sprite);
else
_sprite.LayerSetState(0, current.State);
else
_sprite.LayerSetTexture(0, current.Texture);
}
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _tools, "tools", new List<ToolEntry>());
}
public bool UseEntity(UseEntityEventArgs eventArgs)
{
Cycle();
return true;
}
}
}

View File

@@ -1,96 +1,37 @@
// Only unused on .NET Core due to KeyValuePair.Deconstruct
// ReSharper disable once RedundantUsingDirective
using System;
using Content.Server.GameObjects.Components.Chemistry;
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.Audio;
using Content.Shared.Chemistry;
using Content.Shared.GameObjects;
using Content.Shared.GameObjects.Components;
using Content.Shared.GameObjects.Components.Interactable;
using Content.Shared.Maps;
using Robust.Server.GameObjects;
using Robust.Server.GameObjects.EntitySystems;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.Interfaces.Random;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Interactable
{
public enum Tool : byte
[RegisterComponent]
public class ToolComponent : SharedToolComponent, IExamine, IAfterAttack, IUse, IAttack
{
Wrench,
Crowbar,
Screwdriver,
Wirecutters,
Welder,
Multitool,
}
public abstract class ToolComponent : Component, IExamine, IAfterAttack
{
#pragma warning disable 649
[Dependency] private IEntitySystemManager _entitySystemManager;
[Dependency] private readonly ITileDefinitionManager _tileDefinitionManager;
[Dependency] private readonly IMapManager _mapManager;
#pragma warning restore 649
private AudioSystem _audioSystem;
private InteractionSystem _interactionSystem;
private SolutionComponent
private Tool _behavior;
private bool _activated = false;
public override string Name => "Tool";
public Tool Behavior
{
get => _behavior;
set => _behavior = value;
}
public override void Initialize()
{
base.Initialize();
_audioSystem = _entitySystemManager.GetEntitySystem<AudioSystem>();
_interactionSystem = _entitySystemManager.GetEntitySystem<InteractionSystem>();
}
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _behavior, "behavior", Tool.Wrench);
serializer.DataField(ref _speedModifier, "Speed", 1);
}
public void Examine(FormattedMessage message)
{
throw new System.NotImplementedException();
}
/// <summary>
/// For tool interactions that have a delay before action this will modify the rate, time to wait is divided by this value
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public float SpeedModifier
{
get => _speedModifier;
set => _speedModifier = value;
}
private float _speedModifier = 1;
/// <summary>
/// Status modifier which determines whether or not we can act as a tool at this time
/// </summary>
/// <returns></returns>
public virtual bool CanUse()
{
if (_behavior != Tool.Welder)
return true;
}
/// <summary>
/// Default Cost of using the welder fuel for an action
/// </summary>
@@ -99,22 +40,197 @@ namespace Content.Server.GameObjects.Components.Interactable
/// <summary>
/// Rate at which we expunge fuel from ourselves when activated
/// </summary>
public const float FuelLossRate = 0.2f;
public const float FuelLossRate = 5f; // 0.2f
#pragma warning disable 649
[Dependency] private IEntitySystemManager _entitySystemManager;
[Dependency] private readonly ITileDefinitionManager _tileDefinitionManager;
[Dependency] private readonly IMapManager _mapManager;
[Dependency] private readonly IPrototypeManager _prototypeManager;
[Dependency] private readonly IRobustRandom _robustRandom;
#pragma warning restore 649
private AudioSystem _audioSystem;
private InteractionSystem _interactionSystem;
private ToolSystem _toolSystem;
private SolutionComponent _solutionComponent;
private SpriteComponent _spriteComponent;
private Tool _behavior = Tool.Wrench;
private float _speedModifier = 1;
private bool _welderLit = false;
private string _useSound;
private string _useSoundCollection;
[ViewVariables]
public override Tool Behavior
{
get => _behavior;
set
{
_behavior = value;
Dirty();
}
}
[ViewVariables]
public float Fuel => _solutionComponent?.Solution.GetReagentQuantity("chem.WeldingFuel").Float() ?? 0f;
[ViewVariables]
public float FuelCapacity => _solutionComponent?.MaxVolume.Float() ?? 0f;
/// <summary>
/// For tool interactions that have a delay before action this will modify the rate, time to wait is divided by this value
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public float SpeedModifier
{
get => _speedModifier;
set => _speedModifier = value;
}
/// <summary>
/// Status of welder, whether it is ignited
/// </summary>
[ViewVariables]
public bool Activated
public bool WelderLit
{
get => _activated;
get => _welderLit;
private set
{
_activated = value;
_welderLit = value;
Dirty();
}
}
public string UseSound
{
get => _useSound;
set => _useSound = value;
}
public string UseSoundCollection
{
get => _useSoundCollection;
set => _useSoundCollection = value;
}
public override void Initialize()
{
base.Initialize();
_audioSystem = _entitySystemManager.GetEntitySystem<AudioSystem>();
_interactionSystem = _entitySystemManager.GetEntitySystem<InteractionSystem>();
_toolSystem = _entitySystemManager.GetEntitySystem<ToolSystem>();
Owner.TryGetComponent(out _solutionComponent);
Owner.TryGetComponent(out _spriteComponent);
}
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _speedModifier, "speed", 1);
serializer.DataField(ref _useSound, "useSound", string.Empty);
serializer.DataField(ref _useSoundCollection, "useSoundCollection", string.Empty);
_behavior = (Tool)serializer.ReadStringEnumKey("behavior");
}
/// <summary>
/// Status modifier which determines whether or not we can act as a tool at this time
/// </summary>
public bool CanUse()
{
return _behavior != Tool.Welder || CanWeld(DefaultFuelCost);
}
public bool TryWeld(float value)
{
if (!WelderLit || !CanWeld(value) || _solutionComponent == null)
{
return false;
}
return _solutionComponent.TryRemoveReagent("chem.WeldingFuel", ReagentUnit.New(value));
}
public bool CanWeld(float value)
{
return Fuel > value || Behavior != Tool.Welder;
}
public bool CanLitWelder()
{
return Fuel > 0 || Behavior != Tool.Welder;
}
/// <summary>
/// Deactivates welding tool if active, activates welding tool if possible
/// </summary>
/// <returns></returns>
public bool ToggleWelderStatus()
{
if (WelderLit)
{
WelderLit = false;
// Layer 1 is the flame.
_spriteComponent.LayerSetVisible(1, false);
PlaySoundCollection("WelderOff", -5);
_toolSystem.Unsubscribe(this);
return true;
}
if (!CanLitWelder()) return false;
WelderLit = true;
_spriteComponent.LayerSetVisible(1, true);
PlaySoundCollection("WelderOn", -5);
_toolSystem.Subscribe(this);
return true;
}
public void OnUpdate(float frameTime)
{
if (Behavior != Tool.Welder || !WelderLit)
{
return;
}
_solutionComponent.TryRemoveReagent("chem.WeldingFuel", ReagentUnit.New(FuelLossRate * frameTime));
Logger.Info(_solutionComponent.Solution.GetReagentQuantity("chem.WeldingFuel").ToString());
if (Fuel == 0)
{
ToggleWelderStatus();
}
Dirty();
}
private void PlaySoundCollection(string name, float volume=-5f)
{
var soundCollection = _prototypeManager.Index<SoundCollectionPrototype>(name);
var file = _robustRandom.Pick(soundCollection.PickFiles);
_entitySystemManager.GetEntitySystem<AudioSystem>()
.Play(file, Owner, AudioParams.Default.WithVolume(volume));
}
public void PlayUseSound()
{
if(string.IsNullOrEmpty(UseSoundCollection))
_audioSystem.Play(UseSound, Owner);
else
PlaySoundCollection(UseSoundCollection, 0f);
}
public override ComponentState GetComponentState()
{
return Behavior == Tool.Welder ? new ToolComponentState(FuelCapacity, Fuel, WelderLit) : new ToolComponentState(Behavior);
}
public void AfterAttack(AfterAttackEventArgs eventArgs)
{
if (Behavior != Tool.Crowbar)
@@ -134,11 +250,49 @@ namespace Content.Server.GameObjects.Components.Interactable
var underplating = _tileDefinitionManager["underplating"];
mapGrid.SetTile(eventArgs.ClickLocation, new Tile(underplating.TileId));
_audioSystem.Play("/Audio/items/crowbar.ogg", Owner);
PlayUseSound();
//Actually spawn the relevant tile item at the right position and give it some offset to the corner.
var tileItem = Owner.EntityManager.SpawnEntity(tileDef.ItemDropPrototypeName, coordinates);
tileItem.Transform.WorldPosition += (0.2f, 0.2f);
}
public bool UseEntity(UseEntityEventArgs eventArgs)
{
Logger.Info(Behavior.ToString());
switch (Behavior)
{
case Tool.Welder:
return ToggleWelderStatus();
}
return false;
}
public void Examine(FormattedMessage message)
{
switch (Behavior)
{
case Tool.Welder:
if (WelderLit)
{
message.AddMarkup(Loc.GetString("[color=orange]Lit[/color]\n"));
}
else
{
message.AddText(Loc.GetString("Not lit\n"));
}
message.AddMarkup(Loc.GetString("Fuel: [color={0}]{1}/{2}[/color].",
Fuel < FuelCapacity / 4f ? "darkorange" : "orange", Math.Round(Fuel), FuelCapacity));
break;
}
}
public void Attack(AttackEventArgs eventArgs)
{
}
}
}

View File

@@ -1,29 +0,0 @@
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.Maps;
using Robust.Server.GameObjects.EntitySystems;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.IoC;
using Robust.Shared.Map;
namespace Content.Server.GameObjects.Components.Interactable.Tools
{
[RegisterComponent]
public class CrowbarComponent : ToolComponent, IAfterAttack
{
#pragma warning disable 649
#pragma warning restore 649
/// <summary>
/// Tool that can be used to crowbar things apart, such as deconstructing
/// </summary>
public override string Name => "Crowbar";
public void AfterAttack(AfterAttackEventArgs eventArgs)
{
}
}
}

View File

@@ -1,217 +0,0 @@
using System;
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.Audio;
using Content.Shared.GameObjects;
using Content.Shared.GameObjects.Components;
using Robust.Server.GameObjects;
using Robust.Server.GameObjects.EntitySystems;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Random;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Interactable.Tools
{
/// <summary>
/// Tool used to weld metal together, light things on fire, or melt into constituent parts
/// </summary>
[RegisterComponent]
class WelderComponent : ToolComponent, IUse, IExamine
{
SpriteComponent spriteComponent;
#pragma warning disable 649
[Dependency] private readonly IPrototypeManager _prototypeManager;
[Dependency] private readonly IRobustRandom _robustRandom;
[Dependency] private readonly IEntitySystemManager _entitySystemManager;
#pragma warning restore 649
public override string Name => "Welder";
public override uint? NetID => ContentNetIDs.WELDER;
/// <summary>
/// Maximum fuel capacity the welder can hold
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public float FuelCapacity
{
get => _fuelCapacity;
set
{
_fuelCapacity = value;
Dirty();
}
}
private float _fuelCapacity = 50;
/// <summary>
/// Fuel the welder has to do tasks
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public float Fuel
{
get => _fuel;
set
{
_fuel = value;
Dirty();
}
}
private float _fuel = 0;
private bool _activated = false;
/// <summary>
/// Default Cost of using the welder fuel for an action
/// </summary>
public const float DefaultFuelCost = 5;
/// <summary>
/// Rate at which we expunge fuel from ourselves when activated
/// </summary>
public const float FuelLossRate = 0.2f;
/// <summary>
/// Status of welder, whether it is ignited
/// </summary>
[ViewVariables]
public bool Activated
{
get => _activated;
private set
{
_activated = value;
Dirty();
}
}
//private string OnSprite { get; set; }
//private string OffSprite { get; set; }
public override void Initialize()
{
base.Initialize();
spriteComponent = Owner.GetComponent<SpriteComponent>();
}
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _fuelCapacity, "Capacity", 50);
serializer.DataField(ref _fuel, "Fuel", FuelCapacity);
serializer.DataField(ref _activated, "Activated", false);
}
public void OnUpdate(float frameTime)
{
if (!Activated)
{
return;
}
Fuel = Math.Max(Fuel - (FuelLossRate * frameTime), 0);
if (Fuel == 0)
{
ToggleStatus();
}
}
public bool TryUse(float value)
{
if (!Activated || !CanUse(value))
{
return false;
}
Fuel -= value;
return true;
}
public bool CanUse(float value)
{
return Fuel > value;
}
public override bool CanUse()
{
return CanUse(DefaultFuelCost);
}
public bool CanActivate()
{
return Fuel > 0;
}
public bool UseEntity(UseEntityEventArgs eventArgs)
{
return ToggleStatus();
}
/// <summary>
/// Deactivates welding tool if active, activates welding tool if possible
/// </summary>
/// <returns></returns>
public bool ToggleStatus()
{
if (Activated)
{
Activated = false;
// Layer 1 is the flame.
spriteComponent.LayerSetVisible(1, false);
PlaySoundCollection("welder_off", -5);
return true;
}
else if (CanActivate())
{
Activated = true;
spriteComponent.LayerSetVisible(1, true);
PlaySoundCollection("welder_on", -5);
return true;
}
else
{
return false;
}
}
void IExamine.Examine(FormattedMessage message)
{
var loc = IoCManager.Resolve<ILocalizationManager>();
if (Activated)
{
message.AddMarkup(loc.GetString("[color=orange]Lit[/color]\n"));
}
else
{
message.AddText(loc.GetString("Not lit\n"));
}
message.AddMarkup(loc.GetString("Fuel: [color={0}]{1}/{2}[/color].",
Fuel < FuelCapacity / 4f ? "darkorange" : "orange", Math.Round(Fuel), FuelCapacity));
}
private void PlaySoundCollection(string name, float volume)
{
var soundCollection = _prototypeManager.Index<SoundCollectionPrototype>(name);
var file = _robustRandom.Pick(soundCollection.PickFiles);
_entitySystemManager.GetEntitySystem<AudioSystem>()
.Play(file, AudioParams.Default.WithVolume(volume));
}
public override ComponentState GetComponentState()
{
return new WelderComponentState(FuelCapacity, Fuel, Activated);
}
}
}