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

@@ -21,7 +21,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using OpenRA.GameRules; using OpenRA.GameRules;
using OpenRA.Traits; using OpenRA.Traits;
using System.Linq;
namespace OpenRA.Orders namespace OpenRA.Orders
{ {
@@ -63,37 +62,8 @@ namespace OpenRA.Orders
// Linebuild for walls. // Linebuild for walls.
// Assumes a 1x1 footprint; weird things will happen for other footprints // Assumes a 1x1 footprint; weird things will happen for other footprints
if (Rules.Info[ Building ].Traits.Contains<LineBuildInfo>()) if (Rules.Info[ Building ].Traits.Contains<LineBuildInfo>())
{ foreach( var t in LineBuildUtils.GetLineBuildCells( world, topLeft, Building, BuildingInfo ))
int range = Rules.Info[ Building ].Traits.Get<LineBuildInfo>().Range; yield return new Order("PlaceBuilding", Producer.Owner.PlayerActor, t, Building);
// 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, BuildingInfo.WaterBound ? UnitMovementType.Float : UnitMovementType.Wheel,null))
continue; // Cell is empty; continue search
// Cell contains an actor. Is it the type we want?
if (world.Queries.WithTrait<LineBuild>().Any(a => (a.Actor.Info.Name == Building && 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 new Order("PlaceBuilding", Producer.Owner.PlayerActor, world.OffsetCell(topLeft,i,d), Building);
}
}
} }
} }

View File

@@ -18,6 +18,7 @@
*/ */
#endregion #endregion
using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using OpenRA.GameRules; using OpenRA.GameRules;
@@ -77,12 +78,25 @@ namespace OpenRA
// Linebuild for walls. // Linebuild for walls.
// Assumes a 1x1 footprint; weird things will happen for other footprints // Assumes a 1x1 footprint; weird things will happen for other footprints
if (Rules.Info[ name ].Traits.Contains<LineBuildInfo>()) if (Rules.Info[ name ].Traits.Contains<LineBuildInfo>())
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
{ {
int range = Rules.Info[ name ].Traits.Get<LineBuildInfo>().Range; 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 // Start at place location, search outwards
// TODO: First make it work, then make it nice // TODO: First make it work, then make it nice
int[] dirs = {0,0,0,0}; 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 d = 0; d < 4; d++)
{ {
for (int i = 1; i < range; i++) for (int i = 1; i < range; i++)
@@ -90,9 +104,9 @@ namespace OpenRA
if (dirs[d] != 0) if (dirs[d] != 0)
continue; continue;
int2 cell = world.OffsetCell(topLeft,i,d); int2 cell = world.OffsetCell(topLeft, i, d);
if (world.IsCellBuildable(cell, bi.WaterBound ? UnitMovementType.Float : UnitMovementType.Wheel,null)) if (world.IsCellBuildable(cell, bi.WaterBound ? UnitMovementType.Float : UnitMovementType.Wheel, null))
continue; // Cell is empty; continue search continue; // Cell is empty; continue search
// Cell contains an actor. Is it the type we want? // Cell contains an actor. Is it the type we want?
@@ -104,16 +118,9 @@ namespace OpenRA
// Place intermediate-line sections // Place intermediate-line sections
if (dirs[d] > 0) if (dirs[d] > 0)
{
for (int i = 1; i < dirs[d]; i++) for (int i = 1; i < dirs[d]; i++)
{ yield return topLeft + i * vecs[d];
int2 cell = world.OffsetCell(topLeft,i,d);
spriteRenderer.DrawSprite( world.IsCloseEnoughToBase(world.LocalPlayer, name, bi, cell) ? buildOk : buildBlocked, Game.CellSize * cell, "terrain" );
} }
} }
} }
}
spriteRenderer.Flush();
}
}
} }