Introduce PlugsInit

This commit is contained in:
Pavel Penev
2015-08-25 03:00:23 +03:00
parent c4bf92870b
commit 8f42cff550

View File

@@ -9,40 +9,44 @@
#endregion #endregion
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using OpenRA.Traits; using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
{ {
public class PluggableInfo : ITraitInfo, Requires<UpgradeManagerInfo> public class PluggableInfo : ITraitInfo, Requires<UpgradeManagerInfo>, UsesInit<PlugsInit>
{ {
[Desc("Footprint cell offset where a plug can be placed.")] [Desc("Footprint cell offset where a plug can be placed.")]
public readonly CVec Offset = CVec.Zero; public readonly CVec Offset = CVec.Zero;
[FieldLoader.LoadUsing("LoadUpgrades", true)] [FieldLoader.Require, Desc("Upgrades to grant for each accepted plug type.")]
[Desc("Upgrades to grant for each accepted plug type.")]
public readonly Dictionary<string, string[]> Upgrades = null; public readonly Dictionary<string, string[]> Upgrades = null;
static object LoadUpgrades(MiniYaml y) public object Create(ActorInitializer init) { return new Pluggable(init, this); }
{
return y.ToDictionary()["Upgrades"].Nodes.ToDictionary(
kv => kv.Key,
kv => FieldLoader.GetValue<string[]>("(value)", kv.Value.Value));
} }
public object Create(ActorInitializer init) { return new Pluggable(init.Self, this); } public class Pluggable : INotifyCreated
}
public class Pluggable
{ {
public readonly PluggableInfo Info; public readonly PluggableInfo Info;
readonly string initialPlug;
readonly UpgradeManager upgradeManager; readonly UpgradeManager upgradeManager;
string active; string active;
public Pluggable(Actor self, PluggableInfo info) public Pluggable(ActorInitializer init, PluggableInfo info)
{ {
Info = info; Info = info;
upgradeManager = self.Trait<UpgradeManager>(); upgradeManager = init.Self.Trait<UpgradeManager>();
var plugInit = init.Contains<PlugsInit>() ? init.Get<PlugsInit, Dictionary<CVec, string>>() : new Dictionary<CVec, string>();
if (plugInit.ContainsKey(Info.Offset))
initialPlug = plugInit[Info.Offset];
}
public void Created(Actor self)
{
if (!string.IsNullOrEmpty(initialPlug))
EnablePlug(self, initialPlug);
} }
public bool AcceptsPlug(Actor self, string type) public bool AcceptsPlug(Actor self, string type)
@@ -71,4 +75,13 @@ namespace OpenRA.Mods.Common.Traits
upgradeManager.RevokeUpgrade(self, u, this); upgradeManager.RevokeUpgrade(self, u, this);
} }
} }
public class PlugsInit : IActorInit<Dictionary<CVec, string>>
{
[DictionaryFromYamlKey]
readonly Dictionary<CVec, string> value = new Dictionary<CVec, string>();
public PlugsInit() { }
public PlugsInit(Dictionary<CVec, string> init) { value = init; }
public Dictionary<CVec, string> Value(World world) { return value; }
}
} }