Added support for filtering exits by production type.
This commit is contained in:
@@ -9,6 +9,9 @@
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
@@ -23,6 +26,9 @@ namespace OpenRA.Mods.Common.Traits
|
||||
public readonly CVec ExitCell = CVec.Zero;
|
||||
public readonly int Facing = -1;
|
||||
|
||||
[Desc("Type tags on this exit.")]
|
||||
public readonly HashSet<string> ProductionTypes = new HashSet<string>();
|
||||
|
||||
[Desc("AttackMove to a RallyPoint or stay where you are spawned.")]
|
||||
public readonly bool MoveIntoWorld = true;
|
||||
|
||||
@@ -31,4 +37,38 @@ namespace OpenRA.Mods.Common.Traits
|
||||
}
|
||||
|
||||
public class Exit { }
|
||||
|
||||
public static class ExitExts
|
||||
{
|
||||
public static ExitInfo FirstExitOrDefault(this ActorInfo info, string productionType = null)
|
||||
{
|
||||
var all = info.TraitInfos<ExitInfo>();
|
||||
if (string.IsNullOrEmpty(productionType))
|
||||
return all.FirstOrDefault(e => e.ProductionTypes.Count == 0);
|
||||
return all.FirstOrDefault(e => e.ProductionTypes.Count == 0 || e.ProductionTypes.Contains(productionType));
|
||||
}
|
||||
|
||||
public static IEnumerable<ExitInfo> Exits(this ActorInfo info, string productionType = null)
|
||||
{
|
||||
var all = info.TraitInfos<ExitInfo>();
|
||||
if (string.IsNullOrEmpty(productionType))
|
||||
return all.Where(e => e.ProductionTypes.Count == 0);
|
||||
return all.Where(e => e.ProductionTypes.Count == 0 || e.ProductionTypes.Contains(productionType));
|
||||
}
|
||||
|
||||
public static ExitInfo RandomExitOrDefault(this ActorInfo info, World world, string productionType, Func<ExitInfo, bool> p = null)
|
||||
{
|
||||
var allOfType = Exits(info, productionType);
|
||||
if (!allOfType.Any())
|
||||
return null;
|
||||
|
||||
var shuffled = allOfType.Shuffle(world.SharedRandom);
|
||||
return p != null ? shuffled.FirstOrDefault(p) : shuffled.First();
|
||||
}
|
||||
|
||||
public static ExitInfo RandomExitOrDefault(this Actor self, string productionType, Func<ExitInfo, bool> p = null)
|
||||
{
|
||||
return RandomExitOrDefault(self.Info, self.World, productionType, p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user