remove trait lookup spam everywhere for IOccupySpace.

This commit is contained in:
Chris Forbes
2011-08-02 19:43:02 +12:00
parent aff3111eac
commit 663a323f29
11 changed files with 35 additions and 37 deletions

View File

@@ -25,25 +25,25 @@ namespace OpenRA
public readonly World World; public readonly World World;
public readonly uint ActorID; public readonly uint ActorID;
IOccupySpace OccupiesSpace; Lazy<IOccupySpace> occupySpace;
IHasLocation HasLocation; IHasLocation HasLocation;
Lazy<IMove> Move; Lazy<IMove> Move;
public Cached<Rectangle> Bounds; public Cached<Rectangle> Bounds;
public Cached<Rectangle> ExtendedBounds; public Cached<Rectangle> ExtendedBounds;
public int2 Location public IOccupySpace OccupiesSpace { get { return occupySpace.Value; } }
{ get {
if (OccupiesSpace == null) public int2 Location { get { return occupySpace.Value.TopLeft; } }
OccupiesSpace = Trait<IOccupySpace>();
return OccupiesSpace.TopLeft;
}}
public int2 CenterLocation public int2 CenterLocation
{ get { {
get
{
if (HasLocation == null) if (HasLocation == null)
HasLocation = Trait<IHasLocation>(); HasLocation = Trait<IHasLocation>();
return HasLocation.PxPosition; return HasLocation.PxPosition;
}} }
}
[Sync] [Sync]
public Player Owner; public Player Owner;
@@ -60,6 +60,8 @@ namespace OpenRA
if( initDict.Contains<OwnerInit>() ) if( initDict.Contains<OwnerInit>() )
Owner = init.Get<OwnerInit,Player>(); Owner = init.Get<OwnerInit,Player>();
occupySpace = Lazy.New( () => TraitOrDefault<IOccupySpace>() );
if (name != null) if (name != null)
{ {
if (!Rules.Info.ContainsKey(name.ToLowerInvariant())) if (!Rules.Info.ContainsKey(name.ToLowerInvariant()))

View File

@@ -44,8 +44,8 @@ namespace OpenRA
map = world.Map; map = world.Map;
influence = new InfluenceNode[world.Map.MapSize.X, world.Map.MapSize.Y]; influence = new InfluenceNode[world.Map.MapSize.X, world.Map.MapSize.Y];
world.ActorAdded += a => Add( a, a.TraitOrDefault<IOccupySpace>() ); world.ActorAdded += a => Add( a, a.OccupiesSpace );
world.ActorRemoved += a => Remove( a, a.TraitOrDefault<IOccupySpace>() ); world.ActorRemoved += a => Remove( a, a.OccupiesSpace );
} }
public IEnumerable<Actor> GetUnitsAt( int2 a ) public IEnumerable<Actor> GetUnitsAt( int2 a )
@@ -71,8 +71,8 @@ namespace OpenRA
if (!AnyUnitsAt(a)) if (!AnyUnitsAt(a))
return true; return true;
return new[]{SubCell.TopLeft, SubCell.TopRight, SubCell.Center, return new[]{ SubCell.TopLeft, SubCell.TopRight, SubCell.Center,
SubCell.BottomLeft, SubCell.BottomRight}.Any(b => !AnyUnitsAt(a,b)); SubCell.BottomLeft, SubCell.BottomRight }.Any(b => !AnyUnitsAt(a,b));
} }
public bool AnyUnitsAt(int2 a) public bool AnyUnitsAt(int2 a)

View File

@@ -132,7 +132,7 @@ namespace OpenRA.Traits
public static IEnumerable<int2> GetVisOrigins(Actor a) public static IEnumerable<int2> GetVisOrigins(Actor a)
{ {
var ios = a.TraitOrDefault<IOccupySpace>(); var ios = a.OccupiesSpace;
if (ios != null) if (ios != null)
{ {
var cells = ios.OccupiedCells(); var cells = ios.OccupiedCells();

View File

@@ -26,7 +26,7 @@ namespace OpenRA.Mods.RA.Activities
if (target == null || !target.IsInWorld || target.IsDead()) return NextActivity; if (target == null || !target.IsInWorld || target.IsDead()) return NextActivity;
if (target.Owner == self.Owner) return NextActivity; if (target.Owner == self.Owner) return NextActivity;
if( !target.Trait<IOccupySpace>().OccupiedCells().Any( x => x.First == self.Location ) ) if( !target.OccupiesSpace.OccupiedCells().Any( x => x.First == self.Location ) )
return NextActivity; return NextActivity;
// todo: clean this up // todo: clean this up

View File

@@ -31,7 +31,7 @@ namespace OpenRA.Mods.RA.Activities
if (IsCanceled) return NextActivity; if (IsCanceled) return NextActivity;
if (target == null || !target.IsInWorld || target.IsDead()) return NextActivity; if (target == null || !target.IsInWorld || target.IsDead()) return NextActivity;
if( !target.Trait<IOccupySpace>().OccupiedCells().Any( x => x.First == self.Location ) ) if( !target.OccupiesSpace.OccupiedCells().Any( x => x.First == self.Location ) )
return NextActivity; return NextActivity;
self.World.AddFrameEndTask(w => w.Add(new DelayedAction(delay, () => self.World.AddFrameEndTask(w => w.Add(new DelayedAction(delay, () =>

View File

@@ -30,7 +30,7 @@ namespace OpenRA.Mods.RA.Activities
{ {
if (IsCanceled) return NextActivity; if (IsCanceled) return NextActivity;
if (target == null || !target.IsInWorld || target.IsDead()) return NextActivity; if (target == null || !target.IsInWorld || target.IsDead()) return NextActivity;
if (!target.Trait<IOccupySpace>().OccupiedCells().Any(x => x.First == self.Location)) if (!target.OccupiesSpace.OccupiedCells().Any(x => x.First == self.Location))
return NextActivity; return NextActivity;
target.Owner.PlayerActor.Trait<PlayerResources>().GiveCash(payload); target.Owner.PlayerActor.Trait<PlayerResources>().GiveCash(payload);

View File

@@ -28,7 +28,7 @@ namespace OpenRA.Mods.RA.Activities
return NextActivity; return NextActivity;
var mobile = self.Trait<Mobile>(); var mobile = self.Trait<Mobile>();
var nearest = target.Trait<IOccupySpace>().NearestCellTo( mobile.toCell ); var nearest = target.OccupiesSpace.NearestCellTo( mobile.toCell );
if( ( nearest - mobile.toCell ).LengthSquared > 2 ) if( ( nearest - mobile.toCell ).LengthSquared > 2 )
return Util.SequenceActivities( new MoveAdjacentTo( target ), this ); return Util.SequenceActivities( new MoveAdjacentTo( target ), this );

View File

@@ -25,7 +25,7 @@ namespace OpenRA.Mods.RA.Activities
if (target == null || !target.IsInWorld || target.IsDead()) return NextActivity; if (target == null || !target.IsInWorld || target.IsDead()) return NextActivity;
if (target.Owner == self.Owner) return NextActivity; if (target.Owner == self.Owner) return NextActivity;
if( !target.Trait<IOccupySpace>().OccupiedCells().Any( x => x.First == self.Location ) ) if( !target.OccupiesSpace.OccupiedCells().Any( x => x.First == self.Location ) )
return NextActivity; return NextActivity;
foreach (var t in target.TraitsImplementing<IAcceptSpy>()) foreach (var t in target.TraitsImplementing<IAcceptSpy>())

View File

@@ -35,12 +35,12 @@ namespace OpenRA.Mods.RA.Activities
if( IsCanceled || !target.IsValid) return NextActivity; if( IsCanceled || !target.IsValid) return NextActivity;
var mobile = self.Trait<Mobile>(); var mobile = self.Trait<Mobile>();
Pair<int2, SubCell>[] cells = new Pair<int2, SubCell>[] {}; var cells = new Pair<int2, SubCell>[] {};
if (target.IsActor) if (target.IsActor)
cells = target.Actor.Trait<IOccupySpace>().OccupiedCells().ToArray(); cells = target.Actor.OccupiesSpace.OccupiedCells().ToArray();
if (cells.Length == 0) if (cells.Length == 0)
cells = new OpenRA.FileFormats.Pair<int2, SubCell>[] { cells = new [] {
Pair.New(Util.CellContaining(target.CenterLocation), SubCell.FullCell) }; Pair.New(Util.CellContaining(target.CenterLocation), SubCell.FullCell) };
var ps1 = new PathSearch( self.World, mobile.Info, self.Owner ) var ps1 = new PathSearch( self.World, mobile.Info, self.Owner )

View File

@@ -24,7 +24,7 @@ namespace OpenRA.Mods.RA.Activities
{ {
if (IsCanceled) return NextActivity; if (IsCanceled) return NextActivity;
if (target == null || !target.IsInWorld || target.IsDead()) return NextActivity; if (target == null || !target.IsInWorld || target.IsDead()) return NextActivity;
if( !target.Trait<IOccupySpace>().OccupiedCells().Any( x => x.First == self.Location ) ) if( !target.OccupiesSpace.OccupiedCells().Any( x => x.First == self.Location ) )
return NextActivity; return NextActivity;
var health = target.Trait<Health>(); var health = target.Trait<Health>();

View File

@@ -15,29 +15,25 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA namespace OpenRA.Mods.RA
{ {
public class AppearsOnRadarInfo : TraitInfo<AppearsOnRadar> public class AppearsOnRadarInfo : ITraitInfo
{ {
public readonly bool UseLocation = false; public readonly bool UseLocation = false;
public object Create(ActorInitializer init) { return new AppearsOnRadar(this); }
} }
public class AppearsOnRadar : IRadarSignature public class AppearsOnRadar : IRadarSignature
{ {
AppearsOnRadarInfo info; AppearsOnRadarInfo info;
IOccupySpace Space;
public AppearsOnRadar(AppearsOnRadarInfo info) { this.info = info; }
public IEnumerable<int2> RadarSignatureCells(Actor self) public IEnumerable<int2> RadarSignatureCells(Actor self)
{ {
if (info == null)
info = self.Info.Traits.Get<AppearsOnRadarInfo>();
if (info.UseLocation) if (info.UseLocation)
return new int2[] { self.Location }; return new int2[] { self.Location };
else else
{ return self.OccupiesSpace.OccupiedCells().Select(c => c.First);
if (Space == null)
Space = self.Trait<IOccupySpace>();
return Space.OccupiedCells().Select(c => c.First);
}
} }
public Color RadarSignatureColor(Actor self) public Color RadarSignatureColor(Actor self)