remove most of the crap from queries

This commit is contained in:
Chris Forbes
2011-03-17 21:48:50 +13:00
parent eba7641125
commit 149324ea92
21 changed files with 113 additions and 129 deletions

View File

@@ -83,9 +83,8 @@ namespace OpenRA.Orders
public override void Tick(World world)
{
var hasStructure = world.Queries.OwnedBy[world.LocalPlayer]
.WithTrait<T>()
.Any();
var hasStructure = world.Queries.WithTrait<T>()
.Any( a => a.Actor.Owner == world.LocalPlayer );
if (!hasStructure)
world.CancelInputMode();

View File

@@ -132,7 +132,8 @@ namespace OpenRA.Traits
{
var eva = self.World.WorldActor.Info.Traits.Get<EvaAlertsInfo>();
OreCapacity = self.World.Queries.OwnedBy[Owner].WithTrait<IStoreOre>()
OreCapacity = self.World.Queries.WithTrait<IStoreOre>()
.Where(a => a.Actor.Owner == Owner)
.Sum(a => a.Trait.Capacity);
if (Ore > OreCapacity)

View File

@@ -126,7 +126,7 @@ namespace OpenRA.Traits
}
// Is now our ally; add unit vis
if (newStance == Stance.Ally)
foreach (var a in w.Queries.OwnedBy[player])
foreach (var a in w.Actors.Where( a => a.Owner == player ))
AddActor(a);
}

View File

@@ -208,46 +208,15 @@ namespace OpenRA
{
readonly World world;
public readonly Cache<Player, OwnedByCachedView> OwnedBy;
public AllQueries( World world )
{
this.world = world;
OwnedBy = new Cache<Player, OwnedByCachedView>(p => new OwnedByCachedView(world, world.actors, x => x.Owner == p));
}
public IEnumerable<TraitPair<T>> WithTrait<T>()
{
return world.traitDict.ActorsWithTraitMultiple<T>( world );
}
static CachedView<Actor, TraitPair<T>> WithTraitInner<T>( Set<Actor> set, TypeDictionary hasTrait )
{
var ret = hasTrait.GetOrDefault<CachedView<Actor, TraitPair<T>>>();
if( ret != null )
return ret;
ret = new CachedView<Actor, TraitPair<T>>(
set,
x => x.HasTrait<T>(),
x => new TraitPair<T> { Actor = x, Trait = x.Trait<T>() } );
hasTrait.Add( ret );
return ret;
}
public class OwnedByCachedView : CachedView<Actor, Actor>
{
readonly TypeDictionary hasTrait = new TypeDictionary();
public OwnedByCachedView( World world, Set<Actor> set, Func<Actor, bool> include )
: base( set, include, a => a )
{
}
public CachedView<Actor, TraitPair<T>> WithTrait<T>()
{
return WithTraitInner<T>( this, hasTrait );
}
}
}
public AllQueries Queries;