Move resource tick logic into chrome classes.

This commit is contained in:
Alexis Hunt
2016-08-21 15:04:31 -04:00
parent 84d58f78d2
commit becbee8388
4 changed files with 57 additions and 60 deletions

View File

@@ -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<PlayerResources>();
var cash = widget.Get<LabelWithTooltipWidget>("CASH");
var label = cash.Text;
cash.GetText = () => label.F(playerResources.DisplayCash + playerResources.DisplayResources);
this.world = world;
player = world.LocalPlayer;
playerResources = player.PlayerActor.Trait<PlayerResources>();
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);
}
}
}

View File

@@ -234,7 +234,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
var stats = player.PlayerActor.TraitOrDefault<PlayerStatistics>();
if (stats == null) return template;
template.Get<LabelWidget>("CASH").GetText = () => "$" + (res.DisplayCash + res.DisplayResources);
template.Get<LabelWidget>("CASH").GetText = () => "$" + (res.Cash + res.Resources);
template.Get<LabelWidget>("EARNED_MIN").GetText = () => AverageEarnedPerMinute(res.Earned);
template.Get<LabelWidget>("EARNED_THIS_MIN").GetText = () => "$" + stats.EarnedThisMinute;
template.Get<LabelWidget>("EARNED").GetText = () => "$" + res.Earned;
@@ -259,7 +259,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
LobbyUtils.AddPlayerFlagAndName(template, player);
var res = player.PlayerActor.Trait<PlayerResources>();
template.Get<LabelWidget>("CASH").GetText = () => "$" + (res.DisplayCash + res.DisplayResources);
template.Get<LabelWidget>("CASH").GetText = () => "$" + (res.Cash + res.Resources);
template.Get<LabelWidget>("EARNED_MIN").GetText = () => AverageEarnedPerMinute(res.Earned);
var powerRes = player.PlayerActor.Trait<PowerManager>();

View File

@@ -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");