resources moved off player onto a synced trait

This commit is contained in:
Chris Forbes
2010-05-12 18:29:25 +12:00
parent a4592e7019
commit 46e465cf33
24 changed files with 186 additions and 152 deletions

View File

@@ -16,9 +16,8 @@
* You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System;
#endregion
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
@@ -43,23 +42,9 @@ namespace OpenRA
public readonly CountryInfo Country;
public readonly int Index;
public int Cash = 10000;
public int Ore = 0;
public int OreCapacity;
public int DisplayCash = 0;
public int PowerProvided = 0;
public int PowerDrained = 0;
public ShroudRenderer Shroud;
public World World { get; private set; }
public static List<PlayerColorPaletteInfo> PlayerColors(World world)
{
return world.WorldActor.Info.Traits.WithInterface<PlayerColorPaletteInfo>()
.Where(p => p.Playable)
.ToList();
}
public Player( World world, Session.Client client )
{
World = world;
@@ -69,9 +54,9 @@ namespace OpenRA
if (client != null)
{
Index = client.Index;
Palette = PlayerColors(world)[client.PaletteIndex % PlayerColors(world).Count()].Name;
Color = PlayerColors(world)[client.PaletteIndex % PlayerColors(world).Count()].Color;
Index = client.Index;
Palette = world.PlayerColors()[client.PaletteIndex % world.PlayerColors().Count()].Name;
Color = world.PlayerColors()[client.PaletteIndex % world.PlayerColors().Count()].Color;
PlayerName = client.Name;
InternalName = "Multi{0}".F(client.Index);
}
@@ -88,101 +73,13 @@ namespace OpenRA
?? world.GetCountries().Random(world.SharedRandom);
}
void UpdatePower()
{
var oldBalance = PowerProvided - PowerDrained;
PowerProvided = 0;
PowerDrained = 0;
var myBuildings = World.Queries.OwnedBy[this]
.WithTrait<Building>();
foreach (var a in myBuildings)
{
var p = a.Trait.GetPowerUsage();
if (p > 0)
PowerProvided += p;
else
PowerDrained -= p;
}
if (PowerProvided - PowerDrained < 0)
if (PowerProvided - PowerDrained != oldBalance)
GiveAdvice(World.WorldActor.Info.Traits.Get<EvaAlertsInfo>().LowPower);
}
public float GetSiloFullness() { return (float)Ore / OreCapacity; }
public PowerState GetPowerState()
{
if (PowerProvided >= PowerDrained) return PowerState.Normal;
if (PowerProvided > PowerDrained / 2) return PowerState.Low;
return PowerState.Critical;
}
void GiveAdvice(string advice)
public void GiveAdvice(string advice)
{
// todo: store the condition or something.
// repeat after World.Defaults.SpeakDelay, as long as the condition holds.
Sound.PlayToPlayer(this, advice);
}
public void GiveCash( int num ) { Cash += num; }
public void GiveOre(int num)
{
Ore += num;
if (Ore > OreCapacity)
Ore = OreCapacity; // trim off the overflow.
if (Ore > .8 * OreCapacity)
GiveAdvice(World.WorldActor.Info.Traits.Get<EvaAlertsInfo>().SilosNeeded);
}
public bool TakeCash( int num )
{
if (Cash + Ore < num) return false;
if (Ore <= num)
{
num -= Ore;
Ore = 0;
Cash -= num;
}
else
Ore -= num;
return true;
}
const float displayCashFracPerFrame = .07f;
const int displayCashDeltaPerFrame = 37;
public void Tick()
{
UpdatePower();
OreCapacity = World.Queries.OwnedBy[this].WithTrait<StoresOre>()
.Sum(a => a.Actor.Info.Traits.Get<StoresOreInfo>().Capacity);
var totalMoney = Cash + Ore;
var diff = Math.Abs(totalMoney - DisplayCash);
var move = Math.Min(Math.Max((int)(diff * displayCashFracPerFrame),
displayCashDeltaPerFrame), diff);
var eva = World.WorldActor.Info.Traits.Get<EvaAlertsInfo>();
if (DisplayCash < totalMoney)
{
DisplayCash += move;
Sound.PlayToPlayer(this, eva.CashTickUp);
}
else if (DisplayCash > totalMoney)
{
DisplayCash -= move;
Sound.PlayToPlayer(this, eva.CashTickDown);
}
}
public Dictionary<Player, Stance> Stances = new Dictionary<Player, Stance>();
}
}