duplication, in my game?

This commit is contained in:
Chris Forbes
2010-03-17 14:57:21 +13:00
parent 5d4d5b0f4f
commit 128fe48592
2 changed files with 45 additions and 68 deletions

View File

@@ -18,6 +18,7 @@
*/
#endregion
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using OpenRA.GameRules;
@@ -77,43 +78,49 @@ namespace OpenRA
// Linebuild for walls.
// Assumes a 1x1 footprint; weird things will happen for other footprints
if (Rules.Info[ name ].Traits.Contains<LineBuildInfo>())
{
int range = Rules.Info[ name ].Traits.Get<LineBuildInfo>().Range;
// Start at place location, search outwards
// TODO: First make it work, then make it nice
int[] dirs = {0,0,0,0};
for (int d = 0; d < 4; d++)
{
for (int i = 1; i < range; i++)
{
if (dirs[d] != 0)
continue;
int2 cell = world.OffsetCell(topLeft,i,d);
if (world.IsCellBuildable(cell, bi.WaterBound ? UnitMovementType.Float : UnitMovementType.Wheel,null))
continue; // Cell is empty; continue search
// Cell contains an actor. Is it the type we want?
if (Game.world.Queries.WithTrait<LineBuild>().Any(a => (a.Actor.Info.Name == name && a.Actor.Location.X == cell.X && a.Actor.Location.Y == cell.Y)))
dirs[d] = i; // Cell contains actor of correct type
else
dirs[d] = -1; // Cell is blocked by another actor type
}
// Place intermediate-line sections
if (dirs[d] > 0)
{
for (int i = 1; i < dirs[d]; i++)
{
int2 cell = world.OffsetCell(topLeft,i,d);
spriteRenderer.DrawSprite( world.IsCloseEnoughToBase(world.LocalPlayer, name, bi, cell) ? buildOk : buildBlocked, Game.CellSize * cell, "terrain" );
}
}
}
}
foreach( var t in LineBuildUtils.GetLineBuildCells(world, topLeft, name, bi ) )
spriteRenderer.DrawSprite(world.IsCloseEnoughToBase(world.LocalPlayer, name, bi, t)
? buildOk : buildBlocked, Game.CellSize * t, "terrain");
spriteRenderer.Flush();
}
}
static class LineBuildUtils
{
public static IEnumerable<int2> GetLineBuildCells(World world, int2 location, string name, BuildingInfo bi)
{
int range = Rules.Info[name].Traits.Get<LineBuildInfo>().Range;
var topLeft = location; // 1x1 assumption!
// Start at place location, search outwards
// TODO: First make it work, then make it nice
var vecs = new[] { new int2(1, 0), new int2(0, 1), new int2(-1, 0), new int2(0, -1) };
int[] dirs = { 0, 0, 0, 0 };
for (int d = 0; d < 4; d++)
{
for (int i = 1; i < range; i++)
{
if (dirs[d] != 0)
continue;
int2 cell = world.OffsetCell(topLeft, i, d);
if (world.IsCellBuildable(cell, bi.WaterBound ? UnitMovementType.Float : UnitMovementType.Wheel, null))
continue; // Cell is empty; continue search
// Cell contains an actor. Is it the type we want?
if (Game.world.Queries.WithTrait<LineBuild>().Any(a => (a.Actor.Info.Name == name && a.Actor.Location.X == cell.X && a.Actor.Location.Y == cell.Y)))
dirs[d] = i; // Cell contains actor of correct type
else
dirs[d] = -1; // Cell is blocked by another actor type
}
// Place intermediate-line sections
if (dirs[d] > 0)
for (int i = 1; i < dirs[d]; i++)
yield return topLeft + i * vecs[d];
}
}
}
}