Cache trait lookups in MoveAdjacentTo.

This commit is contained in:
Paul Chote
2014-01-09 17:59:20 +13:00
parent 27ef3352f4
commit c333b59eb9
8 changed files with 16 additions and 10 deletions

View File

@@ -34,7 +34,7 @@ namespace OpenRA.Mods.RA.Activities
return NextActivity;
if (!Util.AdjacentCells(target).Any(c => c == self.Location))
return Util.SequenceActivities(new MoveAdjacentTo(target), this);
return Util.SequenceActivities(new MoveAdjacentTo(self, target), this);
// Move to the middle of the target, ignoring impassable tiles
var mobile = self.Trait<Mobile>();

View File

@@ -39,7 +39,7 @@ namespace OpenRA.Mods.RA.Activities
var nearest = target.Actor.OccupiesSpace.NearestCellTo(mobile.toCell);
if ((nearest - mobile.toCell).LengthSquared > 2)
return Util.SequenceActivities(new MoveAdjacentTo(target), this);
return Util.SequenceActivities(new MoveAdjacentTo(self, target), this);
if (!capturable.CaptureInProgress)
capturable.BeginCapture(self);

View File

@@ -37,7 +37,7 @@ namespace OpenRA.Mods.RA.Activities
return new Wait(20);
return Util.SequenceActivities(
new MoveAdjacentTo(Target.FromActor(rearmTarget)),
new MoveAdjacentTo(self, Target.FromActor(rearmTarget)),
mobile.MoveTo(rearmTarget.CenterPosition.ToCPos(), rearmTarget),
new Rearm(self),
new Repair(rearmTarget),

View File

@@ -16,15 +16,21 @@ namespace OpenRA.Mods.RA.Activities
public class MoveAdjacentTo : Activity
{
readonly Target target;
readonly Mobile mobile;
readonly PathFinder pathFinder;
public MoveAdjacentTo(Target target) { this.target = target; }
public MoveAdjacentTo(Actor self, Target target)
{
this.target = target;
mobile = self.Trait<Mobile>();
pathFinder = self.World.WorldActor.Trait<PathFinder>();
}
public override Activity Tick(Actor self)
{
if (IsCanceled || !target.IsValidFor(self))
return NextActivity;
var mobile = self.Trait<Mobile>();
var ps1 = new PathSearch(self.World, mobile.Info, self)
{
checkForBlocked = true,
@@ -42,7 +48,7 @@ namespace OpenRA.Mods.RA.Activities
ps1.heuristic = PathSearch.DefaultEstimator(mobile.toCell);
var ps2 = PathSearch.FromPoint(self.World, mobile.Info, self, mobile.toCell, target.CenterPosition.ToCPos(), true);
var ret = self.World.WorldActor.Trait<PathFinder>().FindBidiPath(ps1, ps2);
var ret = pathFinder.FindBidiPath(ps1, ps2);
return Util.SequenceActivities(mobile.MoveTo(() => ret), this);
}