diff --git a/OpenRA.Game/Traits/Player/PlayerResources.cs b/OpenRA.Game/Traits/Player/PlayerResources.cs index d8801772f3..92ab6adcfb 100644 --- a/OpenRA.Game/Traits/Player/PlayerResources.cs +++ b/OpenRA.Game/Traits/Player/PlayerResources.cs @@ -45,8 +45,6 @@ namespace OpenRA.Traits public class PlayerResources : ITick, ISync { - const float DisplayCashFracPerFrame = .07f; - const int DisplayCashDeltaPerFrame = 37; readonly PlayerResourcesInfo info; readonly Player owner; @@ -67,9 +65,6 @@ namespace OpenRA.Traits [Sync] public int Resources; [Sync] public int ResourceCapacity; - public int DisplayCash; - public int DisplayResources; - public int Earned; public int Spent; @@ -160,63 +155,14 @@ namespace OpenRA.Traits return true; } - int nextCashTickTime = 0; - public void Tick(Actor self) { - if (nextCashTickTime > 0) - nextCashTickTime--; - ResourceCapacity = self.World.ActorsWithTrait() .Where(a => a.Actor.Owner == owner) .Sum(a => a.Trait.Capacity); if (Resources > ResourceCapacity) Resources = ResourceCapacity; - - var diff = Math.Abs(Cash - DisplayCash); - var move = Math.Min(Math.Max((int)(diff * DisplayCashFracPerFrame), DisplayCashDeltaPerFrame), diff); - - if (DisplayCash < Cash) - { - DisplayCash += move; - PlayCashTickUp(self); - } - else if (DisplayCash > Cash) - { - DisplayCash -= move; - PlayCashTickDown(self); - } - - diff = Math.Abs(Resources - DisplayResources); - move = Math.Min(Math.Max((int)(diff * DisplayCashFracPerFrame), - DisplayCashDeltaPerFrame), diff); - - if (DisplayResources < Resources) - { - DisplayResources += move; - PlayCashTickUp(self); - } - else if (DisplayResources > Resources) - { - DisplayResources -= move; - PlayCashTickDown(self); - } - } - - public void PlayCashTickUp(Actor self) - { - if (Game.Settings.Sound.CashTicks) - Game.Sound.PlayNotification(self.World.Map.Rules, self.Owner, "Sounds", "CashTickUp", self.Owner.Faction.InternalName); - } - - public void PlayCashTickDown(Actor self) - { - if (Game.Settings.Sound.CashTicks && nextCashTickTime == 0) - { - Game.Sound.PlayNotification(self.World.Map.Rules, self.Owner, "Sounds", "CashTickDown", self.Owner.Faction.InternalName); - nextCashTickTime = 2; - } } } } diff --git a/OpenRA.Mods.Common/Widgets/Logic/Ingame/IngameCashCounterLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/Ingame/IngameCashCounterLogic.cs index 9d112a2c18..91aac336eb 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/Ingame/IngameCashCounterLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/Ingame/IngameCashCounterLogic.cs @@ -9,6 +9,9 @@ */ #endregion +using System; +using System.Linq; +using OpenRA.Mods.Common.Traits; using OpenRA.Traits; using OpenRA.Widgets; @@ -16,15 +19,63 @@ namespace OpenRA.Mods.Common.Widgets.Logic { public class IngameCashCounterLogic : ChromeLogic { + const float DisplayFracPerFrame = .07f; + const int DisplayDeltaPerFrame = 37; + + readonly World world; + readonly Player player; + readonly PlayerResources playerResources; + readonly string cashLabel; + + int nextCashTickTime = 0; + int displayResources; + string displayLabel; + [ObjectCreator.UseCtor] public IngameCashCounterLogic(Widget widget, World world) { - var playerResources = world.LocalPlayer.PlayerActor.Trait(); var cash = widget.Get("CASH"); - var label = cash.Text; - cash.GetText = () => label.F(playerResources.DisplayCash + playerResources.DisplayResources); + this.world = world; + player = world.LocalPlayer; + playerResources = player.PlayerActor.Trait(); + displayResources = playerResources.Cash + playerResources.Resources; + cashLabel = cash.Text; + displayLabel = cashLabel.F(displayResources); + + cash.GetText = () => displayLabel; cash.GetTooltipText = () => "Silo Usage: {0}/{1}".F(playerResources.Resources, playerResources.ResourceCapacity); } + + public override void Tick() + { + if (nextCashTickTime > 0) + nextCashTickTime--; + + var actual = playerResources.Cash + playerResources.Resources; + + var diff = Math.Abs(actual - displayResources); + var move = Math.Min(Math.Max((int)(diff * DisplayFracPerFrame), DisplayDeltaPerFrame), diff); + + if (displayResources < actual) + { + displayResources += move; + + if (Game.Settings.Sound.CashTicks) + Game.Sound.PlayNotification(world.Map.Rules, player, "Sounds", "CashTickUp", player.Faction.InternalName); + } + else if (displayResources > actual) + { + displayResources -= move; + + if (Game.Settings.Sound.CashTicks && nextCashTickTime == 0) + { + Game.Sound.PlayNotification(world.Map.Rules, player, "Sounds", "CashTickDown", player.Faction.InternalName); + nextCashTickTime = 2; + } + } + + displayLabel = cashLabel.F(displayResources); + } } } diff --git a/OpenRA.Mods.Common/Widgets/Logic/Ingame/ObserverStatsLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/Ingame/ObserverStatsLogic.cs index b5d4469059..5a6dc4eb8e 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/Ingame/ObserverStatsLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/Ingame/ObserverStatsLogic.cs @@ -234,7 +234,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic var stats = player.PlayerActor.TraitOrDefault(); if (stats == null) return template; - template.Get("CASH").GetText = () => "$" + (res.DisplayCash + res.DisplayResources); + template.Get("CASH").GetText = () => "$" + (res.Cash + res.Resources); template.Get("EARNED_MIN").GetText = () => AverageEarnedPerMinute(res.Earned); template.Get("EARNED_THIS_MIN").GetText = () => "$" + stats.EarnedThisMinute; template.Get("EARNED").GetText = () => "$" + res.Earned; @@ -259,7 +259,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic LobbyUtils.AddPlayerFlagAndName(template, player); var res = player.PlayerActor.Trait(); - template.Get("CASH").GetText = () => "$" + (res.DisplayCash + res.DisplayResources); + template.Get("CASH").GetText = () => "$" + (res.Cash + res.Resources); template.Get("EARNED_MIN").GetText = () => AverageEarnedPerMinute(res.Earned); var powerRes = player.PlayerActor.Trait(); diff --git a/OpenRA.Mods.Common/Widgets/Logic/Ingame/ProductionTooltipLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/Ingame/ProductionTooltipLogic.cs index 01f9f0f9df..381d986f53 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/Ingame/ProductionTooltipLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/Ingame/ProductionTooltipLogic.cs @@ -90,7 +90,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic var costString = cost.ToString(); costLabel.GetText = () => costString; - costLabel.GetColor = () => pr.DisplayCash + pr.DisplayResources >= cost + costLabel.GetColor = () => pr.Cash + pr.Resources >= cost ? Color.White : Color.Red; var descString = tooltip.Description.Replace("\\n", "\n");