This commit is contained in:
Bob
2010-01-31 13:55:01 +13:00
parent 73c16d5d9d
commit c4477455fe
7 changed files with 45 additions and 20 deletions

View File

@@ -41,14 +41,19 @@ namespace OpenRa.Collections
public class CachedView<T,U> : Set<U>
{
public CachedView( Set<T> set, Func<T,bool> include, Func<T,U> store )
public CachedView( Set<T> set, Func<T, bool> include, Func<T, U> store )
: this( set, include, x => new[] { store( x ) } )
{
}
public CachedView( Set<T> set, Func<T,bool> include, Func<T,IEnumerable<U>> store )
{
foreach( var t in set )
if( include( t ) )
Add( store( t ) );
store( t ).Do( x => Add( x ) );
set.OnAdd += obj => { if( include( obj ) ) Add( store( obj ) ); };
set.OnRemove += obj => { if( include( obj ) ) Remove( store( obj ) ); };
set.OnAdd += obj => { if( include( obj ) ) store( obj ).Do( x => Add( x ) ); };
set.OnRemove += obj => { if( include( obj ) ) store( obj ).Do( x => Remove( x ) ); };
}
}
}

View File

@@ -1,4 +1,6 @@

using System;
using System.Collections.Generic;
namespace OpenRa
{
public static class Exts
@@ -7,5 +9,11 @@ namespace OpenRa
{
return string.Format(fmt, args);
}
public static void Do<T>( this IEnumerable<T> e, Action<T> fn )
{
foreach( var ee in e )
fn( ee );
}
}
}

View File

@@ -52,9 +52,6 @@ namespace OpenRa
currentActivity = a.Tick(this) ?? new Idle();
if (a == currentActivity || currentActivity is Idle) break;
}
foreach (var tick in traits.WithInterface<ITick>())
tick.Tick(this);
}
public bool IsIdle

View File

@@ -18,8 +18,8 @@ namespace OpenRa
if (IsBridge(w, w.Map.MapTiles[i, j].tile))
ConvertBridgeToActor(w, i, j);
foreach (var br in w.Actors.SelectMany(a => a.traits.WithInterface<Bridge>()))
br.FinalizeBridges(w);
foreach (var br in w.Queries.WithTraitMultiple<Bridge>())
br.Trait.FinalizeBridges(w);
}
static void ConvertBridgeToActor(World w, int i, int j)

View File

@@ -45,8 +45,7 @@ namespace OpenRa.Graphics
public void DrawRegions( World world )
{
world.WorldRenderer.palette.Update(world.Actors.SelectMany(
a => a.traits.WithInterface<IPaletteModifier>()));
world.WorldRenderer.palette.Update(world.Queries.WithTraitMultiple<IPaletteModifier>().Select(t=>t.Trait));
float2 r1 = new float2(2, -2) / screenSize;
float2 r2 = new float2(-1, 1);

View File

@@ -62,11 +62,10 @@ namespace OpenRa.Traits
if (!a.traits.Get<IOccupySpace>().OccupiedCells().Contains( new int2( x, y ) ) )
throw new InvalidOperationException( "UIM: Sanity check failed A" );
foreach( Actor a in self.World.Actors )
foreach( var ios in a.traits.WithInterface<IOccupySpace>() )
foreach( var cell in ios.OccupiedCells() )
if (!influence[cell.X, cell.Y].Contains(a))
throw new InvalidOperationException( "UIM: Sanity check failed B" );
foreach( var t in self.World.Queries.WithTraitMultiple<IOccupySpace>() )
foreach( var cell in t.Trait.OccupiedCells() )
if (!influence[cell.X, cell.Y].Contains(t.Actor))
throw new InvalidOperationException( "UIM: Sanity check failed B" );
}
public IEnumerable<Actor> GetUnitsAt( int2 a )

View File

@@ -71,6 +71,9 @@ namespace OpenRa
players[i] = new Player(this, i, Game.LobbyInfo.Clients.FirstOrDefault(a => a.Index == i));
Timer.Time( "worldActor, players: {0}" );
Queries = new AllQueries( this );
Timer.Time( "queries: {0}" );
Bridges.MakeBridges(this);
PathFinder = new PathFinder(this);
Timer.Time( "bridge, pathing: {0}" );
@@ -79,8 +82,6 @@ namespace OpenRa
Minimap = new Minimap(this, Game.renderer);
Timer.Time( "renderer, minimap: {0}" );
Queries = new AllQueries( this );
Timer.Time( "queries: {0}" );
Timer.Time( "----end World.ctor" );
}
@@ -124,6 +125,8 @@ namespace OpenRa
}
foreach (var a in actors) a.Tick();
Queries.WithTraitMultiple<ITick>().Do( x => x.Trait.Tick( x.Actor ) );
foreach (var e in effects) e.Tick( this );
Game.viewport.Tick();
@@ -190,7 +193,21 @@ namespace OpenRa
x => x.traits.Contains<T>(),
x => new TraitPair<T> { Actor = x, Trait = x.traits.Get<T>() } );
hasTrait.Add( ret );
return ret; }
return ret;
}
public CachedView<Actor, TraitPair<T>> WithTraitMultiple<T>()
{
var ret = hasTrait.GetOrDefault<CachedView<Actor, TraitPair<T>>>();
if( ret != null )
return ret;
ret = new CachedView<Actor, TraitPair<T>>(
world.actors,
x => x.traits.Contains<T>(),
x => x.traits.WithInterface<T>().Select( t => new TraitPair<T> { Actor = x, Trait = t } ) );
hasTrait.Add( ret );
return ret;
}
public struct TraitPair<T>
{