Cache PlayerResources in PlayerStatistics

and remove a couple of redundant fields.
This commit is contained in:
Oliver Brakmann
2016-06-18 12:57:08 +02:00
parent da3aaf4318
commit 5157ac917f

View File

@@ -20,10 +20,9 @@ namespace OpenRA.Mods.Common.Traits
public object Create(ActorInitializer init) { return new PlayerStatistics(init.Self); }
}
public class PlayerStatistics : ITick, IResolveOrder
public class PlayerStatistics : ITick, IResolveOrder, INotifyCreated
{
World world;
Player player;
PlayerResources resources;
public int OrderCount;
@@ -31,7 +30,7 @@ namespace OpenRA.Mods.Common.Traits
{
get
{
return player.PlayerActor.Trait<PlayerResources>().Earned - earnedAtBeginningOfMinute;
return resources != null ? resources.Earned - earnedAtBeginningOfMinute : 0;
}
}
@@ -47,23 +46,24 @@ namespace OpenRA.Mods.Common.Traits
public int BuildingsKilled;
public int BuildingsDead;
public PlayerStatistics(Actor self)
public PlayerStatistics(Actor self) { }
void INotifyCreated.Created(Actor self)
{
world = self.World;
player = self.Owner;
resources = self.TraitOrDefault<PlayerResources>();
}
void UpdateEarnedThisMinute()
{
EarnedSamples.Enqueue(EarnedThisMinute);
earnedAtBeginningOfMinute = player.PlayerActor.Trait<PlayerResources>().Earned;
earnedAtBeginningOfMinute = resources != null ? resources.Earned : 0;
if (EarnedSamples.Count > 100)
EarnedSamples.Dequeue();
}
public void Tick(Actor self)
{
if (world.WorldTick % 1500 == 1)
if (self.World.WorldTick % 1500 == 1)
UpdateEarnedThisMinute();
}