Begin splitting power into its own trait; incomplete and non-working.

This commit is contained in:
Paul Chote
2010-09-18 14:12:38 +12:00
parent 0330ef2b9e
commit ce9caec291
13 changed files with 195 additions and 120 deletions

View File

@@ -45,17 +45,53 @@ namespace OpenRA.Traits
public int OreCapacity;
[Sync]
public int DisplayOre;
public float GetSiloFullness() { return (float)Ore / OreCapacity; }
public void GiveOre(int num)
{
Ore += num;
if (Ore > OreCapacity)
{
nextSiloAdviceTime = 0;
Ore = OreCapacity;
}
}
[Sync]
public int PowerProvided;
[Sync]
public int PowerDrained;
public bool TakeOre(int num)
{
if (Ore < num) return false;
Ore -= num;
return true;
}
public void GiveCash(int num)
{
Cash += num;
}
public bool TakeCash(int num)
{
if (Cash + Ore < num) return false;
// Spend ore before cash
Ore -= num;
if (Ore < 0)
{
Cash += Ore;
Ore = 0;
}
return true;
}
const float displayCashFracPerFrame = .07f;
const int displayCashDeltaPerFrame = 37;
int nextSiloAdviceTime = 0;
void TickOre(Actor self)
public void Tick(Actor self)
{
OreCapacity = self.World.Queries.OwnedBy[Owner].WithTrait<IStoreOre>()
.Sum(a => a.Trait.Capacity);
@@ -102,91 +138,5 @@ namespace OpenRA.Traits
Sound.PlayToPlayer(self.Owner, eva.CashTickDown);
}
}
int nextPowerAdviceTime = 0;
void TickPower()
{
var oldBalance = PowerProvided - PowerDrained;
PowerProvided = 0;
PowerDrained = 0;
var myBuildings = Owner.World.Queries.OwnedBy[Owner].WithTrait<Building>();
foreach (var a in myBuildings)
{
var q = a.Trait.GetPowerUsage();
if (q > 0)
PowerProvided += q;
else
PowerDrained -= q;
}
if (PowerProvided - PowerDrained < 0)
if (PowerProvided - PowerDrained != oldBalance)
nextPowerAdviceTime = 0;
if (--nextPowerAdviceTime <= 0)
{
if (PowerProvided - PowerDrained < 0)
Owner.GiveAdvice(Rules.Info["world"].Traits.Get<EvaAlertsInfo>().LowPower);
nextPowerAdviceTime = AdviceInterval;
}
}
public PowerState GetPowerState()
{
if (PowerProvided >= PowerDrained) return PowerState.Normal;
if (PowerProvided > PowerDrained / 2) return PowerState.Low;
return PowerState.Critical;
}
public float GetSiloFullness() { return (float)Ore / OreCapacity; }
public void GiveOre(int num)
{
Ore += num;
if (Ore > OreCapacity)
{
nextSiloAdviceTime = 0;
Ore = OreCapacity;
}
}
public bool TakeOre(int num)
{
if (Ore < num) return false;
Ore -= num;
return true;
}
public void GiveCash(int num)
{
Cash += num;
}
public bool TakeCash(int num)
{
if (Cash + Ore < num) return false;
// Spend ore before cash
Ore -= num;
if (Ore < 0)
{
Cash += Ore;
Ore = 0;
}
return true;
}
public void Tick(Actor self)
{
TickPower();
TickOre(self);
}
}
}