Cache world INotifySelection traits in Selection.

This commit is contained in:
Paul Chote
2019-01-20 12:22:30 +00:00
parent 64c853a4e0
commit 0df159e73b
4 changed files with 16 additions and 6 deletions

View File

@@ -21,7 +21,14 @@ namespace OpenRA
{ {
public int Hash { get; private set; } public int Hash { get; private set; }
public IEnumerable<Actor> Actors { get { return actors; } } public IEnumerable<Actor> Actors { get { return actors; } }
readonly HashSet<Actor> actors = new HashSet<Actor>(); readonly HashSet<Actor> actors = new HashSet<Actor>();
readonly INotifySelection[] worldNotifySelection;
internal Selection(IEnumerable<INotifySelection> worldNotifySelection)
{
this.worldNotifySelection = worldNotifySelection.ToArray();
}
void UpdateHash() void UpdateHash()
{ {
@@ -31,14 +38,15 @@ namespace OpenRA
Hash += 1; Hash += 1;
} }
public void Add(World w, Actor a) public void Add(Actor a)
{ {
actors.Add(a); actors.Add(a);
UpdateHash(); UpdateHash();
foreach (var sel in a.TraitsImplementing<INotifySelected>()) foreach (var sel in a.TraitsImplementing<INotifySelected>())
sel.Selected(a); sel.Selected(a);
foreach (var ns in w.WorldActor.TraitsImplementing<INotifySelection>())
foreach (var ns in worldNotifySelection)
ns.SelectionChanged(); ns.SelectionChanged();
} }
@@ -77,7 +85,7 @@ namespace OpenRA
foreach (var sel in a.TraitsImplementing<INotifySelected>()) foreach (var sel in a.TraitsImplementing<INotifySelected>())
sel.Selected(a); sel.Selected(a);
foreach (var ns in world.WorldActor.TraitsImplementing<INotifySelection>()) foreach (var ns in worldNotifySelection)
ns.SelectionChanged(); ns.SelectionChanged();
if (world.IsGameOver) if (world.IsGameOver)

View File

@@ -142,7 +142,7 @@ namespace OpenRA
} }
} }
public readonly Selection Selection = new Selection(); public readonly Selection Selection;
public void CancelInputMode() { OrderGenerator = new UnitOrderGenerator(); } public void CancelInputMode() { OrderGenerator = new UnitOrderGenerator(); }
@@ -179,6 +179,8 @@ namespace OpenRA
ActorMap = WorldActor.Trait<IActorMap>(); ActorMap = WorldActor.Trait<IActorMap>();
ScreenMap = WorldActor.Trait<ScreenMap>(); ScreenMap = WorldActor.Trait<ScreenMap>();
Selection = new Selection(WorldActor.TraitsImplementing<INotifySelection>());
// Add players // Add players
foreach (var cmp in WorldActor.TraitsImplementing<ICreatePlayers>()) foreach (var cmp in WorldActor.TraitsImplementing<ICreatePlayers>())
cmp.CreatePlayers(this); cmp.CreatePlayers(this);

View File

@@ -193,7 +193,7 @@ namespace OpenRA.Mods.Cnc.Traits
nt.AfterTransform(a); nt.AfterTransform(a);
if (selected) if (selected)
self.World.Selection.Add(self.World, a); self.World.Selection.Add(a);
if (controlgroup.HasValue) if (controlgroup.HasValue)
self.World.Selection.AddToControlGroup(a, controlgroup.Value); self.World.Selection.AddToControlGroup(a, controlgroup.Value);

View File

@@ -135,7 +135,7 @@ namespace OpenRA.Mods.Common.Activities
self.ReplacedByActor = a; self.ReplacedByActor = a;
if (selected) if (selected)
w.Selection.Add(w, a); w.Selection.Add(a);
if (controlgroup.HasValue) if (controlgroup.HasValue)
w.Selection.AddToControlGroup(a, controlgroup.Value); w.Selection.AddToControlGroup(a, controlgroup.Value);