Bar signs implemented.

This commit is contained in:
Pieter-Jan Briers
2020-04-23 00:58:43 +02:00
parent f5a9a953a9
commit 20481a0bb1
5 changed files with 372 additions and 0 deletions

View File

@@ -0,0 +1,115 @@
using System.Linq;
using Content.Server.GameObjects.Components.Power;
using Robust.Server.GameObjects;
using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.Random;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Log;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.BarSign
{
[RegisterComponent]
public class BarSignComponent : Component, IMapInit
{
public override string Name => "BarSign";
#pragma warning disable 649
[Dependency] private readonly IPrototypeManager _prototypeManager;
[Dependency] private readonly IRobustRandom _robustRandom;
#pragma warning restore 649
private string _currentSign;
private PowerDeviceComponent _power;
private SpriteComponent _sprite;
[ViewVariables(VVAccess.ReadWrite)]
public string CurrentSign
{
get => _currentSign;
set
{
_currentSign = value;
UpdateSignInfo();
}
}
private void UpdateSignInfo()
{
if (_currentSign == null)
{
return;
}
if (!_prototypeManager.TryIndex(_currentSign, out BarSignPrototype prototype))
{
Logger.ErrorS("barSign", $"Invalid bar sign prototype: \"{_currentSign}\"");
return;
}
if (!_power.Powered)
{
_sprite.LayerSetState(0, "empty");
}
else
{
_sprite.LayerSetState(0, prototype.Icon);
}
if (!string.IsNullOrEmpty(prototype.Name))
{
Owner.Name = prototype.Name;
}
else
{
Owner.Name = Loc.GetString("bar sign");
}
Owner.Description = prototype.Description;
}
public override void Initialize()
{
base.Initialize();
_power = Owner.GetComponent<PowerDeviceComponent>();
_sprite = Owner.GetComponent<SpriteComponent>();
_power.OnPowerStateChanged += PowerOnOnPowerStateChanged;
UpdateSignInfo();
}
private void PowerOnOnPowerStateChanged(object sender, PowerStateEventArgs e)
{
UpdateSignInfo();
}
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _currentSign, "current", null);
}
public void MapInit()
{
if (_currentSign != null)
{
return;
}
var prototypes = _prototypeManager.EnumeratePrototypes<BarSignPrototype>().Where(p => !p.Hidden)
.ToList();
var prototype = _robustRandom.Pick(prototypes);
CurrentSign = prototype.ID;
}
}
}

View File

@@ -0,0 +1,40 @@
using Robust.Shared.Localization;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
using YamlDotNet.RepresentationModel;
namespace Content.Server.GameObjects.Components.BarSign
{
[Prototype("barSign")]
public class BarSignPrototype : IPrototype, IIndexedPrototype
{
public string ID { get; private set; }
public string Icon { get; private set; }
public string Name { get; private set; }
public string Description { get; private set; }
public bool RenameArea { get; private set; } = true;
public bool Hidden { get; private set; }
public void LoadFrom(YamlMappingNode mapping)
{
ID = mapping.GetNode("id").AsString();
Name = Loc.GetString(mapping.GetNode("name").AsString());
Icon = mapping.GetNode("icon").AsString();
if (mapping.TryGetNode("hidden", out var node))
{
Hidden = node.AsBool();
}
if (mapping.TryGetNode("renameArea", out node))
{
RenameArea = node.AsBool();
}
if (mapping.TryGetNode("description", out node))
{
Description = Loc.GetString(node.AsString());
}
}
}
}