Refactor a bunch of stuff.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.GameObjects.EntitySystems;
|
||||
using Content.Shared.GameObjects;
|
||||
using Content.Shared.GameObjects.Components.Interactable;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Server.GameObjects.EntitySystems;
|
||||
@@ -26,7 +27,7 @@ namespace Content.Server.GameObjects.Components.Interactable
|
||||
private string _sprite;
|
||||
private string _changeSound;
|
||||
|
||||
public Tool Behavior { get; private set; }
|
||||
public ToolQuality Behavior { get; private set; }
|
||||
public string State => _state;
|
||||
public string Texture => _texture;
|
||||
public string Sprite => _sprite;
|
||||
@@ -37,7 +38,7 @@ namespace Content.Server.GameObjects.Components.Interactable
|
||||
public void ExposeData(ObjectSerializer serializer)
|
||||
{
|
||||
if(serializer.Reading)
|
||||
Behavior = (Tool)serializer.ReadStringEnumKey("behavior");
|
||||
Behavior = (ToolQuality)serializer.ReadStringEnumKey("behavior");
|
||||
serializer.DataField(ref _state, "state", string.Empty);
|
||||
serializer.DataField(ref _sprite, "sprite", string.Empty);
|
||||
serializer.DataField(ref _texture, "texture", string.Empty);
|
||||
@@ -52,6 +53,7 @@ namespace Content.Server.GameObjects.Components.Interactable
|
||||
#pragma warning restore 649
|
||||
|
||||
public override string Name => "MultiTool";
|
||||
public override uint? NetID => ContentNetIDs.MULTITOOLS;
|
||||
private List<ToolEntry> _tools;
|
||||
private int _currentTool = 0;
|
||||
|
||||
@@ -87,7 +89,7 @@ namespace Content.Server.GameObjects.Components.Interactable
|
||||
|
||||
_tool.UseSound = current.Sound;
|
||||
_tool.UseSoundCollection = current.SoundCollection;
|
||||
_tool.Behavior = current.Behavior;
|
||||
_tool.Qualities = current.Behavior;
|
||||
|
||||
if (_sprite == null) return;
|
||||
|
||||
@@ -98,6 +100,8 @@ namespace Content.Server.GameObjects.Components.Interactable
|
||||
_sprite.LayerSetState(0, current.State);
|
||||
else
|
||||
_sprite.LayerSetTexture(0, current.Texture);
|
||||
|
||||
Dirty();
|
||||
}
|
||||
|
||||
public override void ExposeData(ObjectSerializer serializer)
|
||||
@@ -111,5 +115,10 @@ namespace Content.Server.GameObjects.Components.Interactable
|
||||
Cycle();
|
||||
return true;
|
||||
}
|
||||
|
||||
public override ComponentState GetComponentState()
|
||||
{
|
||||
return new MultiToolComponentState(_tool.Qualities);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// ReSharper disable once RedundantUsingDirective
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection.Metadata.Ecma335;
|
||||
using Content.Server.GameObjects.Components.Chemistry;
|
||||
@@ -46,18 +47,18 @@ namespace Content.Server.GameObjects.Components.Interactable
|
||||
private InteractionSystem _interactionSystem;
|
||||
private SpriteComponent _spriteComponent;
|
||||
|
||||
protected Tool _behavior = Tool.Wrench;
|
||||
protected ToolQuality _qualities = ToolQuality.Anchoring;
|
||||
private string _useSound;
|
||||
private string _useSoundCollection;
|
||||
private float _speedModifier = 1;
|
||||
|
||||
[ViewVariables]
|
||||
public override Tool Behavior
|
||||
public override ToolQuality Qualities
|
||||
{
|
||||
get => _behavior;
|
||||
get => _qualities;
|
||||
set
|
||||
{
|
||||
_behavior = value;
|
||||
_qualities = value;
|
||||
Dirty();
|
||||
}
|
||||
}
|
||||
@@ -84,6 +85,23 @@ namespace Content.Server.GameObjects.Components.Interactable
|
||||
set => _useSoundCollection = value;
|
||||
}
|
||||
|
||||
public void AddQuality(ToolQuality quality)
|
||||
{
|
||||
_qualities |= quality;
|
||||
Dirty();
|
||||
}
|
||||
|
||||
public void RemoveQuality(ToolQuality quality)
|
||||
{
|
||||
_qualities &= ~quality;
|
||||
Dirty();
|
||||
}
|
||||
|
||||
public bool HasQuality(ToolQuality quality)
|
||||
{
|
||||
return _qualities.HasFlag(quality);
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
@@ -99,13 +117,10 @@ namespace Content.Server.GameObjects.Components.Interactable
|
||||
|
||||
if (serializer.Reading)
|
||||
{
|
||||
try
|
||||
var qualities = serializer.ReadDataField("qualities", new List<ToolQuality>());
|
||||
foreach (var quality in qualities)
|
||||
{
|
||||
_behavior = (Tool)serializer.ReadStringEnumKey("behavior");
|
||||
}
|
||||
catch
|
||||
{
|
||||
// ignored
|
||||
AddQuality(quality);
|
||||
}
|
||||
}
|
||||
serializer.DataField(ref _speedModifier, "speed", 1);
|
||||
@@ -113,22 +128,11 @@ namespace Content.Server.GameObjects.Components.Interactable
|
||||
serializer.DataField(ref _useSoundCollection, "useSoundCollection", string.Empty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Status modifier which determines whether or not we can act as a tool at this time
|
||||
/// </summary>
|
||||
public virtual bool CanUse(float resource)
|
||||
public virtual bool UseTool(IEntity user, IEntity target, ToolQuality toolQualityNeeded)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
PlayUseSound();
|
||||
|
||||
/// <summary>
|
||||
/// Method which will try to use the current tool and consume resources/power if applicable.
|
||||
/// </summary>
|
||||
/// <param name="resource">The amount of resource</param>
|
||||
/// <returns>Whether the tool could be used or not.</returns>
|
||||
public virtual bool TryUse(float resource = 0f)
|
||||
{
|
||||
return true;
|
||||
return ActionBlockerSystem.CanInteract(user) && HasQuality(toolQualityNeeded);
|
||||
}
|
||||
|
||||
protected void PlaySoundCollection(string name, float volume=-5f)
|
||||
@@ -147,17 +151,9 @@ namespace Content.Server.GameObjects.Components.Interactable
|
||||
PlaySoundCollection(UseSoundCollection, 0f);
|
||||
}
|
||||
|
||||
public override ComponentState GetComponentState()
|
||||
{
|
||||
return new ToolComponentState(Behavior);
|
||||
}
|
||||
|
||||
public void AfterAttack(AfterAttackEventArgs eventArgs)
|
||||
{
|
||||
if (eventArgs.Attacked != null && RaiseToolAct(eventArgs))
|
||||
return;
|
||||
|
||||
if (Behavior != Tool.Crowbar)
|
||||
if (Qualities != ToolQuality.Prying)
|
||||
return;
|
||||
|
||||
var mapGrid = _mapManager.GetGrid(eventArgs.ClickLocation.GridID);
|
||||
@@ -180,101 +176,5 @@ namespace Content.Server.GameObjects.Components.Interactable
|
||||
var tileItem = Owner.EntityManager.SpawnEntity(tileDef.ItemDropPrototypeName, coordinates);
|
||||
tileItem.Transform.WorldPosition += (0.2f, 0.2f);
|
||||
}
|
||||
|
||||
private bool RaiseToolAct(AfterAttackEventArgs eventArgs)
|
||||
{
|
||||
var attacked = eventArgs.Attacked;
|
||||
var clickLocation = eventArgs.ClickLocation;
|
||||
var user = eventArgs.User;
|
||||
|
||||
switch (Behavior)
|
||||
{
|
||||
case Tool.Wrench:
|
||||
var wrenchList = attacked.GetAllComponents<IWrenchAct>().ToList();
|
||||
var wrenchAttackBy = new WrenchActEventArgs()
|
||||
{ User = user, ClickLocation = clickLocation, AttackWith = Owner };
|
||||
|
||||
foreach (var comp in wrenchList)
|
||||
{
|
||||
if (comp.WrenchAct(wrenchAttackBy))
|
||||
return true;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case Tool.Crowbar:
|
||||
var crowbarList = attacked.GetAllComponents<ICrowbarAct>().ToList();
|
||||
var crowbarAttackBy = new CrowbarActEventArgs()
|
||||
{ User = user, ClickLocation = clickLocation, AttackWith = Owner };
|
||||
|
||||
foreach (var comp in crowbarList)
|
||||
{
|
||||
if (comp.CrowbarAct(crowbarAttackBy))
|
||||
return true;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case Tool.Screwdriver:
|
||||
var screwdriverList = attacked.GetAllComponents<IScrewdriverAct>().ToList();
|
||||
var screwdriverAttackBy = new ScrewdriverActEventArgs()
|
||||
{ User = user, ClickLocation = clickLocation, AttackWith = Owner };
|
||||
|
||||
foreach (var comp in screwdriverList)
|
||||
{
|
||||
if (comp.ScrewdriverAct(screwdriverAttackBy))
|
||||
return true;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case Tool.Wirecutter:
|
||||
var wirecutterList = attacked.GetAllComponents<IWirecutterAct>().ToList();
|
||||
var wirecutterAttackBy = new WirecutterActEventArgs()
|
||||
{ User = user, ClickLocation = clickLocation, AttackWith = Owner };
|
||||
|
||||
foreach (var comp in wirecutterList)
|
||||
{
|
||||
if (comp.WirecutterAct(wirecutterAttackBy))
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
|
||||
case Tool.Welder:
|
||||
var welderList = attacked.GetAllComponents<IWelderAct>().ToList();
|
||||
var welder = (WelderComponent) this;
|
||||
var welderAttackBy = new WelderActEventArgs()
|
||||
{
|
||||
User = user, ClickLocation = clickLocation, AttackWith = Owner,
|
||||
Fuel = welder.Fuel, FuelCapacity = welder.FuelCapacity
|
||||
};
|
||||
|
||||
foreach (var comp in welderList)
|
||||
{
|
||||
if (comp.WelderAct(welderAttackBy))
|
||||
return true;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case Tool.Multitool:
|
||||
var multitoolList = attacked.GetAllComponents<IMultitoolAct>().ToList();
|
||||
var multitoolAttackBy = new MultitoolActEventArgs()
|
||||
{ User = user, ClickLocation = clickLocation, AttackWith = Owner };
|
||||
|
||||
foreach (var comp in multitoolList)
|
||||
{
|
||||
if (comp.MultitoolAct(multitoolAttackBy))
|
||||
return true;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ namespace Content.Server.GameObjects.Components.Interactable
|
||||
{
|
||||
#pragma warning disable 649
|
||||
[Dependency] private IEntitySystemManager _entitySystemManager;
|
||||
[Dependency] private readonly IRobustRandom _robustRandom;
|
||||
#pragma warning restore 649
|
||||
|
||||
public override string Name => "Welder";
|
||||
@@ -38,7 +37,6 @@ namespace Content.Server.GameObjects.Components.Interactable
|
||||
public const float FuelLossRate = 0.5f;
|
||||
|
||||
private bool _welderLit = false;
|
||||
|
||||
private WelderSystem _welderSystem;
|
||||
private SpriteComponent _spriteComponent;
|
||||
private SolutionComponent _solutionComponent;
|
||||
@@ -67,7 +65,7 @@ namespace Content.Server.GameObjects.Components.Interactable
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
_behavior = Tool.Welder;
|
||||
AddQuality(ToolQuality.Welding);
|
||||
|
||||
_welderSystem = _entitySystemManager.GetEntitySystem<WelderSystem>();
|
||||
|
||||
@@ -80,41 +78,40 @@ namespace Content.Server.GameObjects.Components.Interactable
|
||||
return new WelderComponentState(FuelCapacity, Fuel, WelderLit);
|
||||
}
|
||||
|
||||
public bool CanUse()
|
||||
public override bool UseTool(IEntity user, IEntity target, ToolQuality toolQualityNeeded)
|
||||
{
|
||||
return CanWeld(DefaultFuelCost);
|
||||
var canUse = base.UseTool(user, target, toolQualityNeeded);
|
||||
|
||||
return toolQualityNeeded.HasFlag(ToolQuality.Welding) ? canUse && TryWeld(DefaultFuelCost) : canUse;
|
||||
}
|
||||
|
||||
public bool TryUse()
|
||||
public bool UseTool(IEntity user, IEntity target, ToolQuality toolQualityNeeded, float fuelConsumed)
|
||||
{
|
||||
return TryWeld(DefaultFuelCost);
|
||||
return base.UseTool(user, target, toolQualityNeeded) && TryWeld(fuelConsumed);
|
||||
}
|
||||
|
||||
public bool TryWeld(float value)
|
||||
private 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)
|
||||
private bool CanWeld(float value)
|
||||
{
|
||||
return Fuel > value || Behavior != Tool.Welder;
|
||||
return Fuel > value || Qualities != ToolQuality.Welding;
|
||||
}
|
||||
|
||||
public bool CanLitWelder()
|
||||
private bool CanLitWelder()
|
||||
{
|
||||
return Fuel > 0 || Behavior != Tool.Welder;
|
||||
return Fuel > 0 || Qualities != ToolQuality.Welding;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deactivates welding tool if active, activates welding tool if possible
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public bool ToggleWelderStatus()
|
||||
private bool ToggleWelderStatus()
|
||||
{
|
||||
if (WelderLit)
|
||||
{
|
||||
@@ -157,17 +154,13 @@ namespace Content.Server.GameObjects.Components.Interactable
|
||||
|
||||
public void OnUpdate(float frameTime)
|
||||
{
|
||||
if (Behavior != Tool.Welder || !WelderLit)
|
||||
{
|
||||
if (!HasQuality(ToolQuality.Welding) || !WelderLit)
|
||||
return;
|
||||
}
|
||||
|
||||
_solutionComponent.TryRemoveReagent("chem.WeldingFuel", ReagentUnit.New(FuelLossRate * frameTime));
|
||||
|
||||
if (Fuel == 0)
|
||||
{
|
||||
ToggleWelderStatus();
|
||||
}
|
||||
|
||||
Dirty();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user