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

@@ -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<IStoreResources>()
.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;
}
}
}
}

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