Misc constructor caching

Cache trait look-ups in constructor for various other traits and
activities.
This commit is contained in:
reaperrr
2015-03-14 04:00:39 +01:00
parent 1e9d1a6cb7
commit b52d055eec
11 changed files with 70 additions and 48 deletions

View File

@@ -16,15 +16,19 @@ namespace OpenRA.Mods.Common.Activities
public class CaptureActor : Enter
{
readonly Actor actor;
readonly Building building;
readonly Capturable capturable;
readonly CapturesInfo capturesInfo;
readonly Health health;
public CaptureActor(Actor self, Actor target)
: base(self, target)
{
actor = target;
building = actor.TraitOrDefault<Building>();
capturesInfo = self.Info.Traits.Get<CapturesInfo>();
capturable = target.Trait<Capturable>();
health = actor.Trait<Health>();
}
protected override bool CanReserve(Actor self)
@@ -37,20 +41,17 @@ namespace OpenRA.Mods.Common.Activities
if (actor.IsDead || capturable.BeingCaptured)
return;
var b = actor.TraitOrDefault<Building>();
if (b != null && !b.Lock())
if (building != null && !building.Lock())
return;
self.World.AddFrameEndTask(w =>
{
if (b != null && b.Locked)
b.Unlock();
if (building != null && building.Locked)
building.Unlock();
if (actor.IsDead || capturable.BeingCaptured)
return;
var health = actor.Trait<Health>();
var lowEnoughHealth = health.HP <= capturable.Info.CaptureThreshold * health.MaxHP;
if (!capturesInfo.Sabotage || lowEnoughHealth || actor.Owner.NonCombatant)
{
@@ -61,8 +62,8 @@ namespace OpenRA.Mods.Common.Activities
foreach (var t in actor.TraitsImplementing<INotifyCapture>())
t.OnCapture(actor, self, oldOwner, self.Owner);
if (b != null && b.Locked)
b.Unlock();
if (building != null && building.Locked)
building.Unlock();
}
else
{

View File

@@ -17,17 +17,24 @@ namespace OpenRA.Mods.Common.Activities
{
class ExternalCaptureActor : Activity
{
Target target;
readonly ExternalCapturable capturable;
readonly ExternalCapturesInfo capturesInfo;
readonly Mobile mobile;
readonly Target target;
public ExternalCaptureActor(Target target) { this.target = target; }
public ExternalCaptureActor(Actor self, Target target)
{
this.target = target;
capturable = target.Actor.Trait<ExternalCapturable>();
capturesInfo = self.Info.Traits.Get<ExternalCapturesInfo>();
mobile = self.Trait<Mobile>();
}
public override Activity Tick(Actor self)
{
if (target.Type != TargetType.Actor)
return NextActivity;
var capturable = target.Actor.Trait<ExternalCapturable>();
if (IsCanceled || !self.IsInWorld || self.IsDead || !target.IsValidFor(self))
{
if (capturable.CaptureInProgress)
@@ -36,7 +43,6 @@ namespace OpenRA.Mods.Common.Activities
return NextActivity;
}
var mobile = self.Trait<Mobile>();
var nearest = target.Actor.OccupiesSpace.NearestCellTo(mobile.ToCell);
if ((nearest - mobile.ToCell).LengthSquared > 2)
@@ -56,8 +62,6 @@ namespace OpenRA.Mods.Common.Activities
if (capturable.CaptureProgressTime == capturable.Info.CaptureCompleteTime * 25)
{
var capturesInfo = self.Info.Traits.Get<ExternalCapturesInfo>();
self.World.AddFrameEndTask(w =>
{
if (target.Actor.IsDead)

View File

@@ -17,16 +17,23 @@ namespace OpenRA.Mods.Common.Activities
{
class Sell : Activity
{
readonly Health health;
readonly SellableInfo sellableInfo;
readonly PlayerResources playerResources;
public Sell(Actor self)
{
health = self.TraitOrDefault<Health>();
sellableInfo = self.Info.Traits.Get<SellableInfo>();
playerResources = self.Owner.PlayerActor.Trait<PlayerResources>();
}
public override Activity Tick(Actor self)
{
var h = self.TraitOrDefault<Health>();
var si = self.Info.Traits.Get<SellableInfo>();
var pr = self.Owner.PlayerActor.Trait<PlayerResources>();
var cost = self.GetSellValue();
var refund = (cost * si.RefundPercent * (h == null ? 1 : h.HP)) / (100 * (h == null ? 1 : h.MaxHP));
pr.GiveCash(refund);
var refund = (cost * sellableInfo.RefundPercent * (health == null ? 1 : health.HP)) / (100 * (health == null ? 1 : health.MaxHP));
playerResources.GiveCash(refund);
foreach (var ns in self.TraitsImplementing<INotifySold>())
ns.Sold(self);