Add priority levels to Exit.

This commit is contained in:
Paul Chote
2019-10-05 16:51:10 +01:00
committed by teinarss
parent 5ac9d2c2f1
commit 2146dd29bb
2 changed files with 24 additions and 4 deletions

View File

@@ -32,6 +32,9 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Number of ticks to wait before moving into the world.")] [Desc("Number of ticks to wait before moving into the world.")]
public readonly int ExitDelay = 0; public readonly int ExitDelay = 0;
[Desc("Exits with larger priorities will be used before lower priorities.")]
public readonly int Priority = 1;
public override object Create(ActorInitializer init) { return new Exit(init, this); } public override object Create(ActorInitializer init) { return new Exit(init, this); }
} }
@@ -46,7 +49,8 @@ namespace OpenRA.Mods.Common.Traits
public static Exit FirstExitOrDefault(this Actor actor, string productionType = null) public static Exit FirstExitOrDefault(this Actor actor, string productionType = null)
{ {
var all = actor.TraitsImplementing<Exit>() var all = actor.TraitsImplementing<Exit>()
.Where(Exts.IsTraitEnabled); .Where(Exts.IsTraitEnabled)
.OrderBy(e => e.Info.Priority);
if (string.IsNullOrEmpty(productionType)) if (string.IsNullOrEmpty(productionType))
return all.FirstOrDefault(e => e.Info.ProductionTypes.Count == 0); return all.FirstOrDefault(e => e.Info.ProductionTypes.Count == 0);
@@ -71,8 +75,18 @@ namespace OpenRA.Mods.Common.Traits
if (!allOfType.Any()) if (!allOfType.Any())
return null; return null;
var shuffled = allOfType.Shuffle(world.SharedRandom); foreach (var g in allOfType.GroupBy(e => e.Info.Priority))
return p != null ? shuffled.FirstOrDefault(p) : shuffled.First(); {
var shuffled = g.Shuffle(world.SharedRandom);
if (p == null)
return shuffled.First();
var valid = shuffled.FirstOrDefault(p);
if (valid != null)
return valid;
}
return null;
} }
} }
} }

View File

@@ -70,7 +70,13 @@ namespace OpenRA.Mods.Common.Traits
void INotifyCreated.Created(Actor self) void INotifyCreated.Created(Actor self)
{ {
self.World.Add(new RallyPointIndicator(self, this, self.Info.TraitInfos<ExitInfo>().ToArray())); // Display only the first level of priority
var priorityExits = self.Info.TraitInfos<ExitInfo>()
.GroupBy(e => e.Priority)
.FirstOrDefault();
var exits = priorityExits != null ? priorityExits.ToArray() : new ExitInfo[0];
self.World.Add(new RallyPointIndicator(self, this, exits));
} }
public void OnOwnerChanged(Actor self, Player oldOwner, Player newOwner) public void OnOwnerChanged(Actor self, Player oldOwner, Player newOwner)