From fe78e227471598fbe7671f2be4df40e9971e5354 Mon Sep 17 00:00:00 2001 From: Matthew Bowra-Dean Date: Thu, 15 Oct 2009 03:16:44 +1300 Subject: [PATCH] Fixed the building placement to have the cursor in the centre. Replaced magic numbers with Game.CellSize Using the building footprint, the placement overlay is offset from the cursor so the cursor is in the approximate middle of the overlay (not precisely due to overlay snapping to tiles). The tile size (24) was used as a magic number in a lot of places, they have been replaced with Game.CellSize. --- OpenRa.Game/Actor.cs | 2 +- OpenRa.Game/Controller.cs | 6 +++--- OpenRa.Game/Game.cs | 4 +++- OpenRa.Game/GameRules/Footprint.cs | 7 ++++++- OpenRa.Game/Graphics/OverlayRenderer.cs | 2 +- OpenRa.Game/Graphics/TerrainRenderer.cs | 4 ++-- OpenRa.Game/Graphics/Viewport.cs | 2 +- OpenRa.Game/Sidebar.cs | 10 +++++++++- OpenRa.Game/Traits/Building.cs | 2 +- OpenRa.Game/Traits/Mobile.cs | 2 +- OpenRa.Game/Traits/RenderUnit.cs | 2 +- OpenRa.Game/Traits/Tree.cs | 2 +- OpenRa.Game/Traits/Turreted.cs | 2 +- OpenRa.Game/UiOverlay.cs | 12 ++++++------ 14 files changed, 37 insertions(+), 22 deletions(-) diff --git a/OpenRa.Game/Actor.cs b/OpenRa.Game/Actor.cs index b26b575d78..978aff61d8 100755 --- a/OpenRa.Game/Actor.cs +++ b/OpenRa.Game/Actor.cs @@ -23,7 +23,7 @@ namespace OpenRa.Game { unitInfo = Rules.UnitInfo[ name ]; Location = location; - CenterLocation = new float2( 12, 12 ) + 24 * (float2)Location; + CenterLocation = new float2( 12, 12 ) + Game.CellSize * (float2)Location; Owner = owner; switch( name ) diff --git a/OpenRa.Game/Controller.cs b/OpenRa.Game/Controller.cs index 4dbb45d19e..bf16bce2cf 100644 --- a/OpenRa.Game/Controller.cs +++ b/OpenRa.Game/Controller.cs @@ -42,9 +42,9 @@ namespace OpenRa.Game if (!(orderGenerator is PlaceBuilding)) { if (dragStart != xy) - orderGenerator = new UnitOrderGenerator( game.FindUnits( 24 * dragStart, 24 * xy ) ); /* band-box select */ + orderGenerator = new UnitOrderGenerator( game.FindUnits( Game.CellSize * dragStart, Game.CellSize * xy ) ); /* band-box select */ else - orderGenerator = new UnitOrderGenerator( game.FindUnits( 24 * xy, 24 * xy ) ); /* click select */ + orderGenerator = new UnitOrderGenerator( game.FindUnits( Game.CellSize * xy, Game.CellSize * xy ) ); /* click select */ } dragStart = dragEnd; @@ -67,7 +67,7 @@ namespace OpenRa.Game get { if (dragStart == dragEnd) return null; - return Pair.New(24 * dragStart, 24 * dragEnd); + return Pair.New(Game.CellSize * dragStart, Game.CellSize * dragEnd); } } } diff --git a/OpenRa.Game/Game.cs b/OpenRa.Game/Game.cs index 9219b2fe47..58bacad185 100644 --- a/OpenRa.Game/Game.cs +++ b/OpenRa.Game/Game.cs @@ -10,7 +10,9 @@ using IjwFramework.Collections; namespace OpenRa.Game { class Game - { + { + public static readonly int CellSize = 24; + public readonly World world; public readonly Map map; readonly TreeCache treeCache; diff --git a/OpenRa.Game/GameRules/Footprint.cs b/OpenRa.Game/GameRules/Footprint.cs index 77986fcfc3..bd8bf2d0ca 100644 --- a/OpenRa.Game/GameRules/Footprint.cs +++ b/OpenRa.Game/GameRules/Footprint.cs @@ -41,13 +41,18 @@ namespace OpenRa.Game.GameRules var footprint = Rules.Footprint.GetFootprint(name); var j = 0; + int maxWidth = 0; + foreach (var row in footprint) + if (row.Length > maxWidth) + maxWidth = row.Length; + foreach (var row in footprint) { var i = 0; foreach (var c in row) { if (c != '_') - yield return position + new int2(i, j); + yield return position + new int2(i, j) - new int2(maxWidth / 2, footprint.Length / 2); ++i; } ++j; diff --git a/OpenRa.Game/Graphics/OverlayRenderer.cs b/OpenRa.Game/Graphics/OverlayRenderer.cs index a697ffbf64..a662bbf000 100755 --- a/OpenRa.Game/Graphics/OverlayRenderer.cs +++ b/OpenRa.Game/Graphics/OverlayRenderer.cs @@ -74,7 +74,7 @@ namespace OpenRa.Game.Graphics spriteIndex = 11; else if( overlayIsGems[ map.MapTiles[ x, y ].overlay ] ) spriteIndex = 2; - spriteRenderer.DrawSprite( sprites[ spriteIndex ], 24 * (float2)( location - map.Offset ), 0 ); + spriteRenderer.DrawSprite( sprites[ spriteIndex ], Game.CellSize * (float2)( location - map.Offset ), 0 ); } } } diff --git a/OpenRa.Game/Graphics/TerrainRenderer.cs b/OpenRa.Game/Graphics/TerrainRenderer.cs index c713375ce5..4d9de120dc 100644 --- a/OpenRa.Game/Graphics/TerrainRenderer.cs +++ b/OpenRa.Game/Graphics/TerrainRenderer.cs @@ -30,7 +30,7 @@ namespace OpenRa.Game.Graphics tileSet = new TileSet( map.TileSuffix ); - Size tileSize = new Size( 24, 24 ); + Size tileSize = new Size( Game.CellSize, Game.CellSize ); var tileMapping = new Cache( x => SheetBuilder.Add(tileSet.GetBytes(x), tileSize)); @@ -44,7 +44,7 @@ namespace OpenRa.Game.Graphics for (int i = 0; i < map.Width; i++) { Sprite tile = tileMapping[map.MapTiles[i + map.XOffset, j + map.YOffset]]; - Util.FastCreateQuad(vertices, indices, 24 * new float2(i, j), tile, 0, nv, ni); + Util.FastCreateQuad(vertices, indices, Game.CellSize * new float2(i, j), tile, 0, nv, ni); nv += 4; ni += 6; } diff --git a/OpenRa.Game/Graphics/Viewport.cs b/OpenRa.Game/Graphics/Viewport.cs index deb9fb0bf3..f68b979ce5 100644 --- a/OpenRa.Game/Graphics/Viewport.cs +++ b/OpenRa.Game/Graphics/Viewport.cs @@ -25,7 +25,7 @@ namespace OpenRa.Game.Graphics public Viewport(float2 size, float2 mapSize, Renderer renderer) { this.size = size; - this.mapSize = 24 * mapSize - size + new float2(128, 0); + this.mapSize = Game.CellSize * mapSize - size + new float2(128, 0); this.renderer = renderer; } diff --git a/OpenRa.Game/Sidebar.cs b/OpenRa.Game/Sidebar.cs index 0045f131f0..f26ae57215 100644 --- a/OpenRa.Game/Sidebar.cs +++ b/OpenRa.Game/Sidebar.cs @@ -214,7 +214,15 @@ namespace OpenRa.Game game.world.AddFrameEndTask(_ => { Log.Write("Player \"{0}\" builds {1}", building.Owner.PlayerName, building.Name); - game.world.Add(new Actor(building.Name, xy, building.Owner)); + + //Adjust placement for cursor to be in middle + var footprint = Rules.Footprint.GetFootprint(building.Name); + int maxWidth = 0; + foreach (var row in footprint) + if (row.Length > maxWidth) + maxWidth = row.Length; + + game.world.Add(new Actor(building.Name, xy - new int2(maxWidth / 2, footprint.Length / 2), building.Owner)); game.controller.orderGenerator = null; game.worldRenderer.uiOverlay.KillOverlay(); diff --git a/OpenRa.Game/Traits/Building.cs b/OpenRa.Game/Traits/Building.cs index 3144c90fcd..5792d87872 100644 --- a/OpenRa.Game/Traits/Building.cs +++ b/OpenRa.Game/Traits/Building.cs @@ -17,7 +17,7 @@ namespace OpenRa.Game.Traits if (first && self.Owner == game.LocalPlayer) { self.Owner.TechTree.Build(self.unitInfo.Name, true); - self.CenterLocation = 24 * (float2)self.Location + 0.5f * self.SelectedSize; + self.CenterLocation = Game.CellSize * (float2)self.Location + 0.5f * self.SelectedSize; } first = false; } diff --git a/OpenRa.Game/Traits/Mobile.cs b/OpenRa.Game/Traits/Mobile.cs index 5a3c7a35b0..283a06dce4 100644 --- a/OpenRa.Game/Traits/Mobile.cs +++ b/OpenRa.Game/Traits/Mobile.cs @@ -59,7 +59,7 @@ namespace OpenRa.Game.Traits void UpdateCenterLocation() { float fraction = (moveFraction > 0) ? (float)moveFraction / moveFractionTotal : 0f; - self.CenterLocation = new float2(12, 12) + 24 * float2.Lerp(fromCell, toCell, fraction); + self.CenterLocation = new float2(12, 12) + Game.CellSize * float2.Lerp(fromCell, toCell, fraction); } public void Tick(Actor self, Game game, int dt) diff --git a/OpenRa.Game/Traits/RenderUnit.cs b/OpenRa.Game/Traits/RenderUnit.cs index 20853e2569..91edff000a 100644 --- a/OpenRa.Game/Traits/RenderUnit.cs +++ b/OpenRa.Game/Traits/RenderUnit.cs @@ -25,7 +25,7 @@ namespace OpenRa.Game.Traits { var mobile = self.traits.Get(); float fraction = (mobile.moveFraction > 0) ? (float)mobile.moveFraction / mobile.moveFractionTotal : 0f; - var centerLocation = new float2(12, 12) + 24 * float2.Lerp(mobile.fromCell, mobile.toCell, fraction); + var centerLocation = new float2(12, 12) + Game.CellSize * float2.Lerp(mobile.fromCell, mobile.toCell, fraction); yield return Centered(anim.Image, centerLocation); } } diff --git a/OpenRa.Game/Traits/Tree.cs b/OpenRa.Game/Traits/Tree.cs index 103e40123d..bb6aa717bc 100644 --- a/OpenRa.Game/Traits/Tree.cs +++ b/OpenRa.Game/Traits/Tree.cs @@ -18,7 +18,7 @@ namespace OpenRa.Game.Traits public IEnumerable> Render(Actor self) { - yield return Pair.New(Image, 24 * (float2)self.Location); + yield return Pair.New(Image, Game.CellSize * (float2)self.Location); } } } diff --git a/OpenRa.Game/Traits/Turreted.cs b/OpenRa.Game/Traits/Turreted.cs index 33c1d346d6..906dfc1258 100644 --- a/OpenRa.Game/Traits/Turreted.cs +++ b/OpenRa.Game/Traits/Turreted.cs @@ -7,7 +7,7 @@ namespace OpenRa.Game.Traits { class Turreted : ITick // temporary. { - public int turretFacing = 24; + public int turretFacing = Game.CellSize; public Turreted(Actor self) { diff --git a/OpenRa.Game/UiOverlay.cs b/OpenRa.Game/UiOverlay.cs index 8cfe685eae..2fabe6ad65 100644 --- a/OpenRa.Game/UiOverlay.cs +++ b/OpenRa.Game/UiOverlay.cs @@ -23,13 +23,13 @@ namespace OpenRa.Game static Sprite SynthesizeTile(byte paletteIndex) { - byte[] data = new byte[24 * 24]; + byte[] data = new byte[Game.CellSize * Game.CellSize]; - for (int i = 0; i < 24; i++) - for (int j = 0; j < 24; j++) - data[i * 24 + j] = ((i + j) % 4 < 2) ? (byte)0 : paletteIndex; + for (int i = 0; i < Game.CellSize; i++) + for (int j = 0; j < Game.CellSize; j++) + data[i * Game.CellSize + j] = ((i + j) % 4 < 2) ? (byte)0 : paletteIndex; - return SheetBuilder.Add( data, new Size(24,24) ); + return SheetBuilder.Add( data, new Size(Game.CellSize,Game.CellSize) ); } public void Draw() @@ -38,7 +38,7 @@ namespace OpenRa.Game return; foreach (var t in Footprint.Tiles(name,position)) - spriteRenderer.DrawSprite(game.IsCellBuildable(t) ? buildOk : buildBlocked, 24 * t, 0); + spriteRenderer.DrawSprite(game.IsCellBuildable(t) ? buildOk : buildBlocked, Game.CellSize * t, 0); spriteRenderer.Flush(); }