Safeguard CashTrickler negative values

By making sure to never remove more cash than the player has.
This commit is contained in:
reaperrr
2016-11-12 19:07:17 +01:00
parent 7dcda5db30
commit 8699d0fca3

View File

@@ -9,13 +9,14 @@
*/ */
#endregion #endregion
using System;
using OpenRA.Mods.Common.Effects; using OpenRA.Mods.Common.Effects;
using OpenRA.Traits; using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
{ {
[Desc("Lets the actor generate cash in a set periodic time.")] [Desc("Lets the actor generate cash in a set periodic time.")]
class CashTricklerInfo : ConditionalTraitInfo public class CashTricklerInfo : ConditionalTraitInfo
{ {
[Desc("Number of ticks to wait between giving money.")] [Desc("Number of ticks to wait between giving money.")]
public readonly int Interval = 50; public readonly int Interval = 50;
@@ -32,9 +33,10 @@ namespace OpenRA.Mods.Common.Traits
public override object Create(ActorInitializer init) { return new CashTrickler(this); } public override object Create(ActorInitializer init) { return new CashTrickler(this); }
} }
class CashTrickler : ConditionalTrait<CashTricklerInfo>, ITick, ISync public class CashTrickler : ConditionalTrait<CashTricklerInfo>, ITick, ISync, INotifyCreated, INotifyOwnerChanged
{ {
readonly CashTricklerInfo info; readonly CashTricklerInfo info;
PlayerResources resources;
[Sync] int ticks; [Sync] int ticks;
public CashTrickler(CashTricklerInfo info) public CashTrickler(CashTricklerInfo info)
@@ -43,6 +45,16 @@ namespace OpenRA.Mods.Common.Traits
this.info = info; this.info = info;
} }
void INotifyCreated.Created(Actor self)
{
resources = self.Owner.PlayerActor.Trait<PlayerResources>();
}
void INotifyOwnerChanged.OnOwnerChanged(Actor self, Player oldOwner, Player newOwner)
{
resources = newOwner.PlayerActor.Trait<PlayerResources>();
}
void ITick.Tick(Actor self) void ITick.Tick(Actor self)
{ {
if (IsTraitDisabled) if (IsTraitDisabled)
@@ -51,10 +63,7 @@ namespace OpenRA.Mods.Common.Traits
if (--ticks < 0) if (--ticks < 0)
{ {
ticks = info.Interval; ticks = info.Interval;
self.Owner.PlayerActor.Trait<PlayerResources>().GiveCash(info.Amount); ModifyCash(self, self.Owner, info.Amount);
if (info.ShowTicks)
AddCashTick(self, info.Amount);
} }
} }
@@ -63,5 +72,24 @@ namespace OpenRA.Mods.Common.Traits
self.World.AddFrameEndTask(w => w.Add( self.World.AddFrameEndTask(w => w.Add(
new FloatingText(self.CenterPosition, self.Owner.Color.RGB, FloatingText.FormatCashTick(amount), info.DisplayDuration))); new FloatingText(self.CenterPosition, self.Owner.Color.RGB, FloatingText.FormatCashTick(amount), info.DisplayDuration)));
} }
void ModifyCash(Actor self, Player newOwner, int amount)
{
if (amount < 0)
{
// Check whether the amount of cash to be removed would exceed available player cash, in that case only remove all the player cash
var drain = Math.Min(resources.Cash + resources.Resources, -amount);
resources.TakeCash(drain);
if (info.ShowTicks)
AddCashTick(self, -drain);
}
else
{
resources.GiveCash(amount);
if (info.ShowTicks)
AddCashTick(self, amount);
}
}
} }
} }