Removes the ConstructorComponents and moves the construction blueprint feature into a new ECS system. (#1114)

This commit is contained in:
Acruid
2020-06-15 12:30:11 -07:00
committed by GitHub
parent 548b91df61
commit 189ed9309f
11 changed files with 302 additions and 330 deletions

View File

@@ -10,7 +10,6 @@ namespace Content.Client.GameObjects.Components.Construction
public override string Name => "ConstructionGhost";
[ViewVariables] public ConstructionPrototype Prototype { get; set; }
[ViewVariables] public ConstructorComponent Master { get; set; }
[ViewVariables] public int GhostID { get; set; }
}
}

View File

@@ -1,132 +0,0 @@
using System.Collections.Generic;
using Content.Client.Construction;
using Content.Client.UserInterface;
using Content.Shared.Construction;
using Content.Shared.GameObjects.Components.Construction;
using Robust.Client.GameObjects;
using Robust.Client.Interfaces.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.GameObjects.Components;
using Robust.Shared.Interfaces.Network;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Players;
namespace Content.Client.GameObjects.Components.Construction
{
[RegisterComponent]
public class ConstructorComponent : SharedConstructorComponent
{
#pragma warning disable 649
[Dependency] private readonly IGameHud _gameHud;
#pragma warning restore 649
private int nextId;
private readonly Dictionary<int, ConstructionGhostComponent> Ghosts = new Dictionary<int, ConstructionGhostComponent>();
public ConstructionMenu ConstructionMenu { get; private set; }
public override void Initialize()
{
base.Initialize();
Owner.GetComponent<ITransformComponent>();
}
public override void HandleMessage(ComponentMessage message, IComponent component)
{
base.HandleMessage(message, component);
switch (message)
{
case PlayerAttachedMsg _:
if (ConstructionMenu == null)
{
ConstructionMenu = new ConstructionMenu {Owner = this};
ConstructionMenu.OnClose += () => _gameHud.CraftingButtonDown = false;
}
_gameHud.CraftingButtonVisible = true;
_gameHud.CraftingButtonToggled = b =>
{
if (b)
{
ConstructionMenu.Open();
}
else
{
ConstructionMenu.Close();
}
};
break;
case PlayerDetachedMsg _:
_gameHud.CraftingButtonVisible = false;
break;
default:
break;
}
}
public override void HandleNetworkMessage(ComponentMessage message, INetChannel channel, ICommonSession session = null)
{
base.HandleNetworkMessage(message, channel, session);
switch (message)
{
case AckStructureConstructionMessage ackMsg:
ClearGhost(ackMsg.Ack);
break;
default:
break;
}
}
public override void OnRemove()
{
ConstructionMenu?.Dispose();
}
public void SpawnGhost(ConstructionPrototype prototype, GridCoordinates loc, Direction dir)
{
var entMgr = IoCManager.Resolve<IClientEntityManager>();
var ghost = entMgr.SpawnEntity("constructionghost", loc);
var comp = ghost.GetComponent<ConstructionGhostComponent>();
comp.Prototype = prototype;
comp.Master = this;
comp.GhostID = nextId++;
ghost.GetComponent<ITransformComponent>().LocalRotation = dir.ToAngle();
var sprite = ghost.GetComponent<SpriteComponent>();
sprite.LayerSetSprite(0, prototype.Icon);
sprite.LayerSetVisible(0, true);
Ghosts.Add(comp.GhostID, comp);
}
public void TryStartConstruction(int ghostId)
{
var ghost = Ghosts[ghostId];
var transform = ghost.Owner.GetComponent<ITransformComponent>();
var msg = new TryStartStructureConstructionMessage(transform.GridPosition, ghost.Prototype.ID, transform.LocalRotation, ghostId);
SendNetworkMessage(msg);
}
public void TryStartItemConstruction(string prototypeName)
{
var msg = new TryStartItemConstructionMessage(prototypeName);
SendNetworkMessage(msg);
}
public void ClearGhost(int ghostId)
{
if (Ghosts.TryGetValue(ghostId, out var ghost))
{
ghost.Owner.Delete();
Ghosts.Remove(ghostId);
}
}
}
}

View File

@@ -0,0 +1,196 @@
using System.Collections.Generic;
using Content.Client.Construction;
using Content.Client.GameObjects.Components.Construction;
using Content.Client.UserInterface;
using Content.Shared.Construction;
using Content.Shared.Input;
using JetBrains.Annotations;
using Robust.Client.GameObjects;
using Robust.Client.GameObjects.EntitySystems;
using Robust.Client.Player;
using Robust.Shared.Input;
using Robust.Shared.Input.Binding;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
namespace Content.Client.GameObjects.EntitySystems
{
/// <summary>
/// The client-side implementation of the construction system, which is used for constructing entities in game.
/// </summary>
[UsedImplicitly]
public class ConstructionSystem : Shared.GameObjects.EntitySystems.ConstructionSystem
{
#pragma warning disable 649
[Dependency] private readonly IGameHud _gameHud;
[Dependency] private readonly IPlayerManager _playerManager;
[Dependency] private readonly IEntityManager _entityManager;
#pragma warning restore 649
private int _nextId;
private readonly Dictionary<int, ConstructionGhostComponent> _ghosts = new Dictionary<int, ConstructionGhostComponent>();
private ConstructionMenu _constructionMenu;
/// <inheritdoc />
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<PlayerAttachSysMessage>(HandlePlayerAttached);
SubscribeNetworkEvent<AckStructureConstructionMessage>(HandleAckStructure);
CommandBinds.Builder
.Bind(ContentKeyFunctions.OpenCraftingMenu,
new PointerInputCmdHandler(HandleOpenCraftingMenu))
.Bind(EngineKeyFunctions.Use,
new PointerInputCmdHandler(HandleUse))
.Register<ConstructionSystem>();
}
private void HandleAckStructure(AckStructureConstructionMessage msg)
{
ClearGhost(msg.GhostId);
}
private void HandlePlayerAttached(PlayerAttachSysMessage msg)
{
if (msg.AttachedEntity == null)
{
_gameHud.CraftingButtonVisible = false;
return;
}
if (_constructionMenu == null)
{
_constructionMenu = new ConstructionMenu();
_constructionMenu.OnClose += () => _gameHud.CraftingButtonDown = false;
}
_gameHud.CraftingButtonVisible = true;
_gameHud.CraftingButtonToggled = b =>
{
if (b)
{
_constructionMenu.Open();
}
else
{
_constructionMenu.Close();
}
};
}
/// <inheritdoc />
public override void Shutdown()
{
_constructionMenu?.Dispose();
CommandBinds.Unregister<ConstructionSystem>();
base.Shutdown();
}
private bool HandleOpenCraftingMenu(in PointerInputCmdHandler.PointerInputCmdArgs args)
{
if (_playerManager.LocalPlayer.ControlledEntity == null)
{
return false;
}
var menu = _constructionMenu;
if (menu.IsOpen)
{
if (menu.IsAtFront())
{
SetOpenValue(menu, false);
}
else
{
menu.MoveToFront();
}
}
else
{
SetOpenValue(menu, true);
}
return true;
}
private bool HandleUse(in PointerInputCmdHandler.PointerInputCmdArgs args)
{
if (!args.EntityUid.IsValid() || !args.EntityUid.IsClientSide())
return false;
var entity = _entityManager.GetEntity(args.EntityUid);
if (!entity.TryGetComponent(out ConstructionGhostComponent ghostComp))
return false;
TryStartConstruction(ghostComp.GhostID);
return true;
}
private void SetOpenValue(ConstructionMenu menu, bool value)
{
if (value)
{
_gameHud.CraftingButtonDown = true;
menu.OpenCentered();
}
else
{
_gameHud.CraftingButtonDown = false;
menu.Close();
}
}
/// <summary>
/// Creates a construction ghost at the given location.
/// </summary>
public void SpawnGhost(ConstructionPrototype prototype, GridCoordinates loc, Direction dir)
{
var ghost = _entityManager.SpawnEntity("constructionghost", loc);
var comp = ghost.GetComponent<ConstructionGhostComponent>();
comp.Prototype = prototype;
comp.GhostID = _nextId++;
ghost.Transform.LocalRotation = dir.ToAngle();
var sprite = ghost.GetComponent<SpriteComponent>();
sprite.LayerSetSprite(0, prototype.Icon);
sprite.LayerSetVisible(0, true);
_ghosts.Add(comp.GhostID, comp);
}
private void TryStartConstruction(int ghostId)
{
var ghost = _ghosts[ghostId];
var transform = ghost.Owner.Transform;
var msg = new TryStartStructureConstructionMessage(transform.GridPosition, ghost.Prototype.ID, transform.LocalRotation, ghostId);
RaiseNetworkEvent(msg);
}
/// <summary>
/// Starts constructing an item underneath the attached entity.
/// </summary>
public void TryStartItemConstruction(string prototypeName)
{
RaiseNetworkEvent(new TryStartItemConstructionMessage(prototypeName));
}
/// <summary>
/// Removes a construction ghost entity with the given ID.
/// </summary>
public void ClearGhost(int ghostId)
{
if (_ghosts.TryGetValue(ghostId, out var ghost))
{
ghost.Owner.Delete();
_ghosts.Remove(ghostId);
}
}
}
}

View File

@@ -1,101 +0,0 @@
using Content.Client.Construction;
using Content.Client.GameObjects.Components.Construction;
using Content.Client.UserInterface;
using Content.Shared.Input;
using Robust.Client.GameObjects.EntitySystems;
using Robust.Client.Player;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Input;
using Robust.Shared.Input.Binding;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC;
namespace Content.Client.GameObjects.EntitySystems
{
public sealed class ConstructorSystem : EntitySystem
{
#pragma warning disable 649
[Dependency] private readonly IGameHud _gameHud;
[Dependency] private readonly IPlayerManager _playerManager;
[Dependency] private readonly IEntityManager _entityManager;
#pragma warning restore 649
public override void Initialize()
{
base.Initialize();
var inputSys = EntitySystemManager.GetEntitySystem<InputSystem>();
CommandBinds.Builder
.Bind(ContentKeyFunctions.OpenCraftingMenu,
new PointerInputCmdHandler(HandleOpenCraftingMenu))
.Bind(EngineKeyFunctions.Use,
new PointerInputCmdHandler(HandleUse))
.Register<ConstructorSystem>();
}
public override void Shutdown()
{
CommandBinds.Unregister<ConstructorSystem>();
base.Shutdown();
}
private bool HandleOpenCraftingMenu(in PointerInputCmdHandler.PointerInputCmdArgs args)
{
if (_playerManager.LocalPlayer.ControlledEntity == null
|| !_playerManager.LocalPlayer.ControlledEntity.TryGetComponent(out ConstructorComponent constructor))
{
return false;
}
var menu = constructor.ConstructionMenu;
if (menu.IsOpen)
{
if (menu.IsAtFront())
{
_setOpenValue(menu, false);
}
else
{
menu.MoveToFront();
}
}
else
{
_setOpenValue(menu, true);
}
return true;
}
private bool HandleUse(in PointerInputCmdHandler.PointerInputCmdArgs args)
{
if (!args.EntityUid.IsValid() || !args.EntityUid.IsClientSide())
return false;
var entity = _entityManager.GetEntity(args.EntityUid);
if (!entity.TryGetComponent(out ConstructionGhostComponent ghostComp))
return false;
ghostComp.Master.TryStartConstruction(ghostComp.GhostID);
return true;
}
private void _setOpenValue(ConstructionMenu menu, bool value)
{
if (value)
{
_gameHud.CraftingButtonDown = true;
menu.OpenCentered();
}
else
{
_gameHud.CraftingButtonDown = false;
menu.Close();
}
}
}
}