diff --git a/OpenRA.Mods.Common/Traits/Player/PlayerResources.cs b/OpenRA.Mods.Common/Traits/Player/PlayerResources.cs index fa66f59d6e..c8ff936bca 100644 --- a/OpenRA.Mods.Common/Traits/Player/PlayerResources.cs +++ b/OpenRA.Mods.Common/Traits/Player/PlayerResources.cs @@ -66,7 +66,7 @@ namespace OpenRA.Mods.Common.Traits public object Create(ActorInitializer init) { return new PlayerResources(init.Self, this); } } - public class PlayerResources : ITick, ISync + public class PlayerResources : ISync { public readonly PlayerResourcesInfo Info; readonly Player owner; @@ -197,13 +197,14 @@ namespace OpenRA.Mods.Common.Traits return true; } - void ITick.Tick(Actor self) + public void AddStorage(int capacity) { - // PERF: Avoid LINQ. - ResourceCapacity = 0; - foreach (var tp in self.World.ActorsWithTrait()) - if (tp.Actor.Owner == owner) - ResourceCapacity += tp.Trait.Capacity; + ResourceCapacity += capacity; + } + + public void RemoveStorage(int capacity) + { + ResourceCapacity -= capacity; if (Resources > ResourceCapacity) Resources = ResourceCapacity; diff --git a/OpenRA.Mods.Common/Traits/StoresResources.cs b/OpenRA.Mods.Common/Traits/StoresResources.cs index 09ae4221d0..06cd12e3fb 100644 --- a/OpenRA.Mods.Common/Traits/StoresResources.cs +++ b/OpenRA.Mods.Common/Traits/StoresResources.cs @@ -31,7 +31,7 @@ namespace OpenRA.Mods.Common.Traits public object Create(ActorInitializer init) { return new StoresResources(init.Self, this); } } - public class StoresResources : IPips, INotifyOwnerChanged, INotifyCapture, IStoreResources, ISync, INotifyKilled + public class StoresResources : IPips, INotifyOwnerChanged, INotifyCapture, IStoreResources, ISync, INotifyKilled, INotifyAddedToWorld, INotifyRemovedFromWorld { readonly StoresResourcesInfo info; PlayerResources player; @@ -71,5 +71,15 @@ namespace OpenRA.Mods.Common.Traits player.Resources * info.PipCount > i * player.ResourceCapacity ? info.PipColor : PipType.Transparent); } + + void INotifyAddedToWorld.AddedToWorld(Actor self) + { + player.AddStorage(info.Capacity); + } + + void INotifyRemovedFromWorld.RemovedFromWorld(Actor self) + { + player.RemoveStorage(info.Capacity); + } } }