New types for cell and pixel coordinate position/vectors.

This commit is contained in:
James Dunne
2012-06-20 23:22:27 -05:00
parent 0b98a8ce5e
commit 9c49143534
162 changed files with 1291 additions and 865 deletions

View File

@@ -28,7 +28,7 @@ namespace OpenRA.Mods.RA.Buildings
{
World world;
BibLayerInfo info;
Dictionary<int2, TileReference<byte, byte>> tiles;
Dictionary<CPos, TileReference<byte, byte>> tiles;
Sprite[][] bibSprites;
public BibLayer(Actor self, BibLayerInfo info)
@@ -45,7 +45,7 @@ namespace OpenRA.Mods.RA.Buildings
public void WorldLoaded(World w)
{
world = w;
tiles = new Dictionary<int2, TileReference<byte, byte>>();
tiles = new Dictionary<CPos, TileReference<byte, byte>>();
}
public void DoBib(Actor b, bool isAdd)
@@ -63,9 +63,9 @@ namespace OpenRA.Mods.RA.Buildings
for (int i = 0; i < 2 * size; i++)
{
var p = b.Location + new int2(i % size, i / size + bibOffset);
var p = b.Location + new CVec(i % size, i / size + bibOffset);
if (isAdd)
tiles[p] = new TileReference<byte, byte>((byte)((isAdd) ? bib + 1 : 0), (byte)i);
tiles[p] = new TileReference<byte, byte>((byte)(bib + 1), (byte)i);
else
tiles.Remove(p);
}
@@ -81,8 +81,7 @@ namespace OpenRA.Mods.RA.Buildings
if (!world.LocalShroud.IsExplored(kv.Key))
continue;
bibSprites[kv.Value.type - 1][kv.Value.index].DrawAt( wr,
Game.CellSize * kv.Key, "terrain");
bibSprites[kv.Value.type - 1][kv.Value.index].DrawAt(wr, kv.Key.ToPPos().ToFloat2(), "terrain");
}
}
}

View File

@@ -32,28 +32,28 @@ namespace OpenRA.Mods.RA.Buildings
public object Create(ActorInitializer init) { return new Building(init, this); }
public bool IsCloseEnoughToBase(World world, Player p, string buildingName, int2 topLeft)
public bool IsCloseEnoughToBase(World world, Player p, string buildingName, CPos topLeft)
{
if (p.PlayerActor.Trait<DeveloperMode>().BuildAnywhere)
return true;
var buildingMaxBounds = Dimensions;
var buildingMaxBounds = (CVec)Dimensions;
if( Rules.Info[ buildingName ].Traits.Contains<BibInfo>() )
buildingMaxBounds.Y += 1;
buildingMaxBounds += new CVec(0, 1);
var scanStart = world.ClampToWorld( topLeft - new int2( Adjacent, Adjacent ) );
var scanEnd = world.ClampToWorld( topLeft + buildingMaxBounds + new int2( Adjacent, Adjacent ) );
var scanStart = world.ClampToWorld( topLeft - new CVec( Adjacent, Adjacent ) );
var scanEnd = world.ClampToWorld(topLeft + buildingMaxBounds + new CVec(Adjacent, Adjacent));
var nearnessCandidates = new List<int2>();
var nearnessCandidates = new List<CPos>();
var bi = world.WorldActor.Trait<BuildingInfluence>();
for( int y = scanStart.Y ; y < scanEnd.Y ; y++ )
for( int x = scanStart.X ; x < scanEnd.X ; x++ )
{
var at = bi.GetBuildingAt( new int2( x, y ) );
var at = bi.GetBuildingAt( new CPos( x, y ) );
if( at != null && at.Owner.Stances[ p ] == Stance.Ally && at.HasTrait<GivesBuildableArea>() )
nearnessCandidates.Add( new int2( x, y ) );
nearnessCandidates.Add( new CPos( x, y ) );
}
var buildingTiles = FootprintUtils.Tiles( buildingName, this, topLeft ).ToList();
@@ -68,27 +68,26 @@ namespace OpenRA.Mods.RA.Buildings
{
readonly Actor self;
public readonly BuildingInfo Info;
[Sync]
readonly int2 topLeft;
[Sync] readonly CPos topLeft;
PowerManager PlayerPower;
int2 pxPosition;
PPos pxPosition;
public int2 TopLeft { get { return topLeft; } }
public int2 PxPosition { get { return pxPosition; } }
public CPos TopLeft { get { return topLeft; } }
public PPos PxPosition { get { return pxPosition; } }
public IEnumerable<string> ProvidesPrerequisites { get { yield return self.Info.Name; } }
public Building(ActorInitializer init, BuildingInfo info)
{
this.self = init.self;
this.topLeft = init.Get<LocationInit,int2>();
this.topLeft = init.Get<LocationInit, CPos>();
this.Info = info;
this.PlayerPower = init.self.Owner.PlayerActor.Trait<PowerManager>();
occupiedCells = FootprintUtils.UnpathableTiles( self.Info.Name, Info, TopLeft )
.Select(c => Pair.New(c, SubCell.FullCell)).ToArray();
pxPosition = ( 2 * topLeft + Info.Dimensions ) * Game.CellSize / 2;
pxPosition = (PPos) (( 2 * topLeft.ToInt2() + Info.Dimensions ) * Game.CellSize / 2);
}
public int GetPowerUsage()
@@ -107,8 +106,8 @@ namespace OpenRA.Mods.RA.Buildings
PlayerPower.UpdateActor(self, GetPowerUsage());
}
Pair<int2,SubCell>[] occupiedCells;
public IEnumerable<Pair<int2, SubCell>> OccupiedCells() { return occupiedCells; }
Pair<CPos, SubCell>[] occupiedCells;
public IEnumerable<Pair<CPos, SubCell>> OccupiedCells() { return occupiedCells; }
public void OnCapture(Actor self, Actor captor, Player oldOwner, Player newOwner)
{

View File

@@ -43,7 +43,7 @@ namespace OpenRA.Mods.RA.Buildings
influence[ u.X, u.Y ] = isAdd ? a : null;
}
public Actor GetBuildingAt(int2 cell)
public Actor GetBuildingAt(CPos cell)
{
if (!map.IsInMap(cell)) return null;
return influence[cell.X, cell.Y];

View File

@@ -16,34 +16,34 @@ namespace OpenRA.Mods.RA.Buildings
{
public static class FootprintUtils
{
public static IEnumerable<int2> Tiles( string name, BuildingInfo buildingInfo, int2 topLeft )
public static IEnumerable<CPos> Tiles(string name, BuildingInfo buildingInfo, CPos topLeft)
{
var dim = buildingInfo.Dimensions;
var dim = (CVec)buildingInfo.Dimensions;
var footprint = buildingInfo.Footprint.Where(x => !char.IsWhiteSpace(x));
if (Rules.Info[ name ].Traits.Contains<BibInfo>())
{
dim.Y += 1;
dim += new CVec(0, 1);
footprint = footprint.Concat(new char[dim.X]);
}
return TilesWhere( name, dim, footprint.ToArray(), a => a != '_' ).Select( t => t + topLeft );
}
public static IEnumerable<int2> Tiles(Actor a)
public static IEnumerable<CPos> Tiles(Actor a)
{
return Tiles( a.Info.Name, a.Info.Traits.Get<BuildingInfo>(), a.Location );
}
public static IEnumerable<int2> UnpathableTiles( string name, BuildingInfo buildingInfo, int2 position )
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' ) )
foreach( var tile in TilesWhere( name, (CVec)buildingInfo.Dimensions, footprint, a => a == 'x' ) )
yield return tile + position;
}
static IEnumerable<int2> TilesWhere( string name, int2 dim, char[] footprint, Func<char, bool> cond )
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 );
@@ -52,13 +52,13 @@ namespace OpenRA.Mods.RA.Buildings
for( int y = 0 ; y < dim.Y ; y++ )
for( int x = 0 ; x < dim.X ; x++ )
if( cond( footprint[ index++ ] ) )
yield return new int2( x, y );
yield return new CVec(x, y);
}
public static int2 AdjustForBuildingSize( BuildingInfo buildingInfo )
public static CVec AdjustForBuildingSize( BuildingInfo buildingInfo )
{
var dim = buildingInfo.Dimensions;
return new int2( dim.X / 2, dim.Y > 1 ? ( dim.Y + 1 ) / 2 : 0 );
return new CVec(dim.X / 2, dim.Y > 1 ? (dim.Y + 1) / 2 : 0);
}
}
}

View File

@@ -29,7 +29,7 @@ namespace OpenRA.Mods.RA.Buildings
public void Killed(Actor self, AttackInfo e)
{
self.World.WorldActor.Trait<ScreenShaker>().AddEffect(Info.Intensity, self.CenterLocation, 1);
self.World.WorldActor.Trait<ScreenShaker>().AddEffect(Info.Intensity, self.CenterLocation.ToFloat2(), 1);
}
}
}

View File

@@ -16,12 +16,12 @@ namespace OpenRA.Mods.RA.Buildings
{
public static class BuildingUtils
{
public static bool IsCellBuildable(this World world, int2 a, BuildingInfo bi)
public static bool IsCellBuildable(this World world, CPos a, BuildingInfo bi)
{
return world.IsCellBuildable(a, bi, null);
}
public static bool IsCellBuildable(this World world, int2 a, BuildingInfo bi, Actor toIgnore)
public static bool IsCellBuildable(this World world, CPos a, BuildingInfo bi, Actor toIgnore)
{
if (world.WorldActor.Trait<BuildingInfluence>().GetBuildingAt(a) != null) return false;
if (world.ActorMap.GetUnitsAt(a).Any(b => b != toIgnore)) return false;
@@ -29,7 +29,7 @@ namespace OpenRA.Mods.RA.Buildings
return world.Map.IsInMap(a) && bi.TerrainTypes.Contains(world.GetTerrainType(a));
}
public static bool CanPlaceBuilding(this World world, string name, BuildingInfo building, int2 topLeft, Actor toIgnore)
public static bool CanPlaceBuilding(this World world, string name, BuildingInfo building, CPos topLeft, Actor toIgnore)
{
var res = world.WorldActor.Trait<ResourceLayer>();
return FootprintUtils.Tiles(name, building, topLeft).All(
@@ -37,7 +37,7 @@ namespace OpenRA.Mods.RA.Buildings
world.IsCellBuildable(t, building, toIgnore));
}
public static IEnumerable<int2> GetLineBuildCells(World world, int2 location, string name, BuildingInfo bi)
public static IEnumerable<CPos> GetLineBuildCells(World world, CPos location, string name, BuildingInfo bi)
{
int range = Rules.Info[name].Traits.Get<LineBuildInfo>().Range;
var topLeft = location; // 1x1 assumption!
@@ -47,7 +47,7 @@ namespace OpenRA.Mods.RA.Buildings
// 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) };
var vecs = new[] { new CVec(1, 0), new CVec(0, 1), new CVec(-1, 0), new CVec(0, -1) };
int[] dirs = { 0, 0, 0, 0 };
for (int d = 0; d < 4; d++)
{
@@ -56,7 +56,7 @@ namespace OpenRA.Mods.RA.Buildings
if (dirs[d] != 0)
continue;
int2 cell = topLeft + i * vecs[d];
CPos cell = topLeft + i * vecs[d];
if (world.IsCellBuildable(cell, bi))
continue; // Cell is empty; continue search