Implement equality on TraitPair.

This commit is contained in:
RoosterDragon
2015-12-31 01:38:09 +00:00
parent addbe6d564
commit 3026bdcea5
4 changed files with 17 additions and 10 deletions

View File

@@ -255,7 +255,7 @@ namespace OpenRA
public void Reset() { index = -1; } public void Reset() { index = -1; }
public bool MoveNext() { return ++index < actors.Count; } public bool MoveNext() { return ++index < actors.Count; }
public TraitPair<T> Current { get { return new TraitPair<T> { Actor = actors[index], Trait = traits[index] }; } } public TraitPair<T> Current { get { return new TraitPair<T>(actors[index], traits[index]); } }
object System.Collections.IEnumerator.Current { get { return Current; } } object System.Collections.IEnumerator.Current { get { return Current; } }
public void Dispose() { } public void Dispose() { }
} }

View File

@@ -408,14 +408,21 @@ namespace OpenRA
} }
} }
public struct TraitPair<T> public struct TraitPair<T> : IEquatable<TraitPair<T>>
{ {
public Actor Actor; public readonly Actor Actor;
public T Trait; public readonly T Trait;
public override string ToString() public TraitPair(Actor actor, T trait) { Actor = actor; Trait = trait; }
{
return "{0}->{1}".F(Actor.Info.Name, Trait.GetType().Name); public static bool operator ==(TraitPair<T> me, TraitPair<T> other) { return me.Actor == other.Actor && Equals(me.Trait, other.Trait); }
} public static bool operator !=(TraitPair<T> me, TraitPair<T> other) { return !(me == other); }
public override int GetHashCode() { return Actor.GetHashCode() ^ Trait.GetHashCode(); }
public bool Equals(TraitPair<T> other) { return this == other; }
public override bool Equals(object obj) { return obj is TraitPair<T> && Equals((TraitPair<T>)obj); }
public override string ToString() { return "{0}->{1}".F(Actor.Info.Name, Trait.GetType().Name); }
} }
} }

View File

@@ -236,7 +236,7 @@ namespace OpenRA.Mods.Common.Orders
var blockers = allTiles.SelectMany(world.ActorMap.GetActorsAt) var blockers = allTiles.SelectMany(world.ActorMap.GetActorsAt)
.Where(a => a.Owner == queue.Actor.Owner && a.IsIdle) .Where(a => a.Owner == queue.Actor.Owner && a.IsIdle)
.Select(a => new TraitPair<Mobile> { Actor = a, Trait = a.TraitOrDefault<Mobile>() }); .Select(a => new TraitPair<Mobile>(a, a.TraitOrDefault<Mobile>()));
foreach (var blocker in blockers.Where(x => x.Trait != null)) foreach (var blocker in blockers.Where(x => x.Trait != null))
{ {

View File

@@ -361,7 +361,7 @@ namespace OpenRA.Mods.Common.Traits
public virtual TraitPair<Production> MostLikelyProducer() public virtual TraitPair<Production> MostLikelyProducer()
{ {
var trait = self.TraitsImplementing<Production>().FirstOrDefault(p => p.Info.Produces.Contains(Info.Type)); var trait = self.TraitsImplementing<Production>().FirstOrDefault(p => p.Info.Produces.Contains(Info.Type));
return new TraitPair<Production> { Actor = self, Trait = trait }; return new TraitPair<Production>(self, trait);
} }
// Builds a unit from the actor that holds this queue (1 queue per building) // Builds a unit from the actor that holds this queue (1 queue per building)