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