ECS-ify Construction[Ghost]Component IExamine.

This commit is contained in:
Pieter-Jan Briers
2021-09-15 16:36:43 +02:00
parent 9733d35de7
commit ce6e37bdd8
12 changed files with 121 additions and 108 deletions

View File

@@ -1,40 +1,15 @@
using Content.Shared.Construction.Prototypes;
using Content.Shared.Examine;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
namespace Content.Client.Construction
{
[RegisterComponent]
public class ConstructionGhostComponent : Component, IExamine
public class ConstructionGhostComponent : Component
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
public override string Name => "ConstructionGhost";
[ViewVariables] public ConstructionPrototype? Prototype { get; set; }
[ViewVariables] public int GhostId { get; set; }
void IExamine.Examine(FormattedMessage message, bool inDetailsRange)
{
if (Prototype == null) return;
message.AddMarkup(Loc.GetString("construction-ghost-examine-message", ("name", Prototype.Name)));
if (!_prototypeManager.TryIndex(Prototype.Graph, out ConstructionGraphPrototype? graph)) return;
var startNode = graph.Nodes[Prototype.StartNode];
if (!graph.TryPath(Prototype.StartNode, Prototype.TargetNode, out var path) ||
!startNode.TryGetEdge(path[0].Name, out var edge))
{
return;
}
edge.Steps[0].DoExamine(message, inDetailsRange);
}
}
}

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using Content.Shared.Construction;
using Content.Shared.Construction.Prototypes;
using Content.Shared.Examine;
using Content.Shared.Input;
using Content.Shared.Interaction.Helpers;
using JetBrains.Annotations;
@@ -11,8 +12,10 @@ using Robust.Shared.GameObjects;
using Robust.Shared.Input;
using Robust.Shared.Input.Binding;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
namespace Content.Client.Construction
@@ -24,6 +27,8 @@ namespace Content.Client.Construction
public class ConstructionSystem : SharedConstructionSystem
{
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
private readonly Dictionary<int, ConstructionGhostComponent> _ghosts = new();
private int _nextId;
@@ -44,6 +49,8 @@ namespace Content.Client.Construction
.Bind(EngineKeyFunctions.Use,
new PointerInputCmdHandler(HandleUse))
.Register<ConstructionSystem>();
SubscribeLocalEvent<ConstructionGhostComponent, ExaminedEvent>(HandleConstructionGhostExamined);
}
/// <inheritdoc />
@@ -54,6 +61,28 @@ namespace Content.Client.Construction
CommandBinds.Unregister<ConstructionSystem>();
}
private void HandleConstructionGhostExamined(EntityUid uid, ConstructionGhostComponent component, ExaminedEvent args)
{
if (component.Prototype == null) return;
args.Message.AddMarkup(Loc.GetString(
"construction-ghost-examine-message",
("name", component.Prototype.Name)));
if (!_prototypeManager.TryIndex(component.Prototype.Graph, out ConstructionGraphPrototype? graph))
return;
var startNode = graph.Nodes[component.Prototype.StartNode];
if (!graph.TryPath(component.Prototype.StartNode, component.Prototype.TargetNode, out var path) ||
!startNode.TryGetEdge(path[0].Name, out var edge))
{
return;
}
edge.Steps[0].DoExamine(args);
}
public event EventHandler<CraftingAvailabilityChangedArgs>? CraftingAvailabilityChanged;
public event EventHandler? ToggleCraftingWindow;

View File

@@ -9,24 +9,21 @@ using Content.Server.Tools.Components;
using Content.Shared.Construction;
using Content.Shared.Construction.Prototypes;
using Content.Shared.Construction.Steps;
using Content.Shared.Examine;
using Content.Shared.Interaction;
using Content.Shared.Tool;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Log;
using Robust.Shared.Physics;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
namespace Content.Server.Construction.Components
{
[RegisterComponent]
public partial class ConstructionComponent : Component, IExamine, IInteractUsing
public partial class ConstructionComponent : Component, IInteractUsing
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
@@ -45,7 +42,7 @@ namespace Content.Server.Construction.Components
[ViewVariables]
private HashSet<string> _containers = new();
[ViewVariables]
private List<List<ConstructionGraphStep>>? _edgeNestedStepProgress = null;
public List<List<ConstructionGraphStep>>? EdgeNestedStepProgress = null;
private ConstructionGraphNode? _target = null;
@@ -298,14 +295,14 @@ namespace Content.Server.Construction.Components
break;
case NestedConstructionGraphStep nestedStep:
if(_edgeNestedStepProgress == null)
_edgeNestedStepProgress = new List<List<ConstructionGraphStep>>(nestedStep.Steps);
if(EdgeNestedStepProgress == null)
EdgeNestedStepProgress = new List<List<ConstructionGraphStep>>(nestedStep.Steps);
foreach (var list in _edgeNestedStepProgress.ToArray())
foreach (var list in EdgeNestedStepProgress.ToArray())
{
if (list.Count == 0)
{
_edgeNestedStepProgress.Remove(list);
EdgeNestedStepProgress.Remove(list);
continue;
}
@@ -315,10 +312,10 @@ namespace Content.Server.Construction.Components
// We check again...
if (list.Count == 0)
_edgeNestedStepProgress.Remove(list);
EdgeNestedStepProgress.Remove(list);
}
if (_edgeNestedStepProgress.Count == 0)
if (EdgeNestedStepProgress.Count == 0)
handled = true;
break;
@@ -390,7 +387,7 @@ namespace Content.Server.Construction.Components
public void ResetEdge()
{
_edgeNestedStepProgress = null;
EdgeNestedStepProgress = null;
TargetNextEdge = null;
Edge = null;
EdgeStep = 0;
@@ -531,51 +528,5 @@ namespace Content.Server.Construction.Components
await HandleEntityChange(graphNode);
}
void IExamine.Examine(FormattedMessage message, bool inDetailsRange)
{
if(Target != null)
message.AddMarkup(Loc.GetString("construction-component-to-create-header",("targetName", Target.Name)) + "\n");
if (Edge == null && TargetNextEdge != null)
{
var preventStepExamine = false;
foreach (var condition in TargetNextEdge.Conditions)
{
preventStepExamine |= condition.DoExamine(Owner, message, inDetailsRange);
}
if(!preventStepExamine)
TargetNextEdge.Steps[0].DoExamine(message, inDetailsRange);
return;
}
if (Edge != null)
{
var preventStepExamine = false;
foreach (var condition in Edge.Conditions)
{
preventStepExamine |= condition.DoExamine(Owner, message, inDetailsRange);
}
if (preventStepExamine) return;
}
if (_edgeNestedStepProgress == null)
{
if(EdgeStep < Edge?.Steps.Count)
Edge.Steps[EdgeStep].DoExamine(message, inDetailsRange);
return;
}
foreach (var list in _edgeNestedStepProgress)
{
if(list.Count == 0) continue;
list[0].DoExamine(message, inDetailsRange);
}
}
}
}

View File

@@ -14,6 +14,7 @@ using Content.Shared.Construction;
using Content.Shared.Construction.Prototypes;
using Content.Shared.Construction.Steps;
using Content.Shared.Coordinates;
using Content.Shared.Examine;
using Content.Shared.Interaction.Events;
using Content.Shared.Interaction.Helpers;
using Content.Shared.Notification.Managers;
@@ -50,6 +51,59 @@ namespace Content.Server.Construction
SubscribeNetworkEvent<TryStartStructureConstructionMessage>(HandleStartStructureConstruction);
SubscribeNetworkEvent<TryStartItemConstructionMessage>(HandleStartItemConstruction);
SubscribeLocalEvent<ConstructionComponent, ExaminedEvent>(HandleConstructionExamined);
}
private void HandleConstructionExamined(EntityUid uid, ConstructionComponent component, ExaminedEvent args)
{
if (component.Target != null)
{
args.Message.AddMarkup(
Loc.GetString(
"construction-component-to-create-header",
("targetName", component.Target.Name)) + "\n");
}
if (component.Edge == null && component.TargetNextEdge != null)
{
var preventStepExamine = false;
foreach (var condition in component.TargetNextEdge.Conditions)
{
preventStepExamine |= condition.DoExamine(args);
}
if (!preventStepExamine)
component.TargetNextEdge.Steps[0].DoExamine(args);
return;
}
if (component.Edge != null)
{
var preventStepExamine = false;
foreach (var condition in component.Edge.Conditions)
{
preventStepExamine |= condition.DoExamine(args);
}
if (preventStepExamine) return;
}
if (component.EdgeNestedStepProgress == null)
{
if (component.EdgeStep < component.Edge?.Steps.Count)
component.Edge.Steps[component.EdgeStep].DoExamine(args);
return;
}
foreach (var list in component.EdgeNestedStepProgress)
{
if(list.Count == 0) continue;
list[0].DoExamine(args);
}
}
private IEnumerable<IEntity> EnumerateNearby(IEntity user)

View File

@@ -1,12 +1,12 @@
using System.Threading.Tasks;
using Content.Shared.Examine;
using Robust.Shared.GameObjects;
using Robust.Shared.Utility;
namespace Content.Shared.Construction
{
public interface IGraphCondition
{
Task<bool> Condition(IEntity entity);
bool DoExamine(IEntity entity, FormattedMessage message, bool inExamineRange) { return false; }
bool DoExamine(ExaminedEvent examinedEvent) { return false; }
}
}

View File

@@ -1,4 +1,5 @@
using Robust.Shared.Localization;
using Content.Shared.Examine;
using Robust.Shared.Localization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
@@ -10,10 +11,12 @@ namespace Content.Shared.Construction.Steps
[DataField("icon")] public SpriteSpecifier Icon { get; private set; } = SpriteSpecifier.Invalid;
public override void DoExamine(FormattedMessage message, bool inDetailsRange)
public override void DoExamine(ExaminedEvent examinedEvent)
{
if (string.IsNullOrEmpty(Name)) return;
message.AddMarkup(Loc.GetString("construction-insert-arbitrary-entity", ("stepName", Name)));
if (string.IsNullOrEmpty(Name))
return;
examinedEvent.Message.AddMarkup(Loc.GetString("construction-insert-arbitrary-entity", ("stepName", Name)));
}
}
}

View File

@@ -1,7 +1,7 @@
using Robust.Shared.GameObjects;
using Content.Shared.Examine;
using Robust.Shared.GameObjects;
using Robust.Shared.Localization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
namespace Content.Shared.Construction.Steps
{
@@ -21,9 +21,9 @@ namespace Content.Shared.Construction.Steps
return false;
}
public override void DoExamine(FormattedMessage message, bool inDetailsRange)
public override void DoExamine(ExaminedEvent examinedEvent)
{
message.AddMarkup(string.IsNullOrEmpty(Name)
examinedEvent.Message.AddMarkup(string.IsNullOrEmpty(Name)
? Loc.GetString(
"construction-insert-entity-with-component",
("componentName", Component))// Terrible.

View File

@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using Content.Shared.Examine;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
namespace Content.Shared.Construction.Steps
{
@@ -15,6 +15,6 @@ namespace Content.Shared.Construction.Steps
public IReadOnlyList<IGraphAction> Completed => _completed;
public abstract void DoExamine(FormattedMessage message, bool inDetailsRange);
public abstract void DoExamine(ExaminedEvent examinedEvent);
}
}

View File

@@ -1,11 +1,11 @@
using System.Diagnostics.CodeAnalysis;
using Content.Shared.Examine;
using Content.Shared.Stacks;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
namespace Content.Shared.Construction.Steps
{
@@ -21,9 +21,9 @@ namespace Content.Shared.Construction.Steps
public StackPrototype MaterialPrototype =>
IoCManager.Resolve<IPrototypeManager>().Index<StackPrototype>(MaterialPrototypeId);
public override void DoExamine(FormattedMessage message, bool inDetailsRange)
public override void DoExamine(ExaminedEvent examinedEvent)
{
message.AddMarkup(Loc.GetString("construction-insert-material-entity", ("amount", Amount), ("materialName", MaterialPrototype.Name)));
examinedEvent.Message.AddMarkup(Loc.GetString("construction-insert-material-entity", ("amount", Amount), ("materialName", MaterialPrototype.Name)));
}
public override bool EntityValid(IEntity entity)

View File

@@ -1,9 +1,9 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Content.Shared.Examine;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
namespace Content.Shared.Construction.Steps
{
@@ -20,7 +20,7 @@ namespace Content.Shared.Construction.Steps
}
}
public override void DoExamine(FormattedMessage message, bool inDetailsRange)
public override void DoExamine(ExaminedEvent examinedEvent)
{
}
}

View File

@@ -1,7 +1,7 @@
using Robust.Shared.GameObjects;
using Content.Shared.Examine;
using Robust.Shared.GameObjects;
using Robust.Shared.Localization;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
namespace Content.Shared.Construction.Steps
{
@@ -15,9 +15,9 @@ namespace Content.Shared.Construction.Steps
return entity.Prototype?.ID == Prototype;
}
public override void DoExamine(FormattedMessage message, bool inDetailsRange)
public override void DoExamine(ExaminedEvent examinedEvent)
{
message.AddMarkup(string.IsNullOrEmpty(Name)
examinedEvent.Message.AddMarkup(string.IsNullOrEmpty(Name)
? Loc.GetString(
"construction-insert-prototype-no-name",
("prototypeName", Prototype) // Terrible.

View File

@@ -1,3 +1,4 @@
using Content.Shared.Examine;
using Content.Shared.Tool;
using Robust.Shared.Localization;
using Robust.Shared.Serialization.Manager.Attributes;
@@ -14,15 +15,15 @@ namespace Content.Shared.Construction.Steps
[DataField("examine")] public string ExamineOverride { get; } = string.Empty;
public override void DoExamine(FormattedMessage message, bool inDetailsRange)
public override void DoExamine(ExaminedEvent examinedEvent)
{
if (!string.IsNullOrEmpty(ExamineOverride))
{
message.AddMarkup(Loc.GetString(ExamineOverride));
examinedEvent.Message.AddMarkup(Loc.GetString(ExamineOverride));
return;
}
message.AddMarkup(Loc.GetString("construction-use-tool-entity", ("toolName", Tool.GetToolName())));
examinedEvent.Message.AddMarkup(Loc.GetString("construction-use-tool-entity", ("toolName", Tool.GetToolName())));
}
}
}