Refactor footprint cell lookups and move them to Building

Removing FootprintUtils happens in the next commit for better
reviewability.
This commit is contained in:
reaperrr
2017-06-28 14:00:50 +02:00
committed by abcdefg30
parent 801796b184
commit 46dc827d46
7 changed files with 112 additions and 85 deletions

View File

@@ -17,62 +17,36 @@ namespace OpenRA.Mods.Common.Traits
{
public static class FootprintUtils
{
public static IEnumerable<CPos> Tiles(Ruleset rules, string name, BuildingInfo buildingInfo, CPos topLeft, bool includePassable = false)
{
var dim = buildingInfo.Dimensions;
var footprint = buildingInfo.Footprint.Where(x => !char.IsWhiteSpace(x));
return TilesWhere(name, dim, footprint.ToArray(), a => includePassable || a != '_').Select(t => t + topLeft);
}
public static IEnumerable<CPos> Tiles(Actor a)
{
return Tiles(a.World.Map.Rules, a.Info.Name, a.Info.TraitInfo<BuildingInfo>(), a.Location);
var info = a.Info.TraitInfo<BuildingInfo>();
return info.Tiles(a.Location);
}
public static IEnumerable<CPos> FrozenUnderFogTiles(Actor a)
{
return Tiles(a.World.Map.Rules, a.Info.Name, a.Info.TraitInfo<BuildingInfo>(), a.Location, true);
var info = a.Info.TraitInfo<BuildingInfo>();
return info.FrozenUnderFogTiles(a);
}
public static IEnumerable<CPos> UnpathableTiles(string name, BuildingInfo buildingInfo, CPos position)
{
var footprint = buildingInfo.Footprint.Where(x => !char.IsWhiteSpace(x)).ToArray();
foreach (var tile in TilesWhere(name, buildingInfo.Dimensions, footprint, a => a == 'x'))
yield return tile + position;
return buildingInfo.UnpathableTiles(position);
}
public static IEnumerable<CPos> PathableTiles(string name, BuildingInfo buildingInfo, CPos position)
{
var footprint = buildingInfo.Footprint.Where(x => !char.IsWhiteSpace(x)).ToArray();
foreach (var tile in TilesWhere(name, buildingInfo.Dimensions, footprint, a => a == '_'))
yield return tile + position;
}
static IEnumerable<CVec> TilesWhere(string name, CVec dim, char[] footprint, Func<char, bool> cond)
{
if (footprint.Length != dim.X * dim.Y)
throw new InvalidOperationException("Invalid footprint for " + name);
var index = 0;
for (var y = 0; y < dim.Y; y++)
for (var x = 0; x < dim.X; x++)
if (cond(footprint[index++]))
yield return new CVec(x, y);
return buildingInfo.PathableTiles(position);
}
public static CVec AdjustForBuildingSize(BuildingInfo buildingInfo)
{
var dim = buildingInfo.Dimensions;
return new CVec(dim.X / 2, dim.Y > 1 ? (dim.Y + 1) / 2 : 0);
return buildingInfo.AdjustForBuildingSize();
}
public static WVec CenterOffset(World w, BuildingInfo buildingInfo)
{
var dim = buildingInfo.Dimensions;
var localOffset = buildingInfo.LocalCenterOffset;
var off = (w.Map.CenterOfCell(new CPos(dim.X, dim.Y)) - w.Map.CenterOfCell(new CPos(1, 1))) / 2;
return (off - new WVec(0, 0, off.Z)) + localOffset;
return buildingInfo.CenterOffset(w);
}
}
}