diff --git a/OpenRa.DataStructures/Delegates.cs b/OpenRa.DataStructures/Delegates.cs index e8d30a94b5..bc73953c79 100644 --- a/OpenRa.DataStructures/Delegates.cs +++ b/OpenRa.DataStructures/Delegates.cs @@ -8,4 +8,6 @@ namespace OpenRa // they are generic. public delegate T Provider(); + public delegate T Provider( U u ); + public delegate T Provider( U u, V v ); } diff --git a/OpenRa.Game/Game.cs b/OpenRa.Game/Game.cs index 5090bf0f6e..5316d98678 100644 --- a/OpenRa.Game/Game.cs +++ b/OpenRa.Game/Game.cs @@ -14,6 +14,10 @@ namespace OpenRa.Game public readonly Viewport viewport; public readonly PathFinder pathFinder; public readonly Network network; + public readonly TechTree.TechTree techTree = new TechTree.TechTree(); + + // temporary, until we remove all the subclasses of Building + public Dictionary> buildingCreation = new Dictionary>(); public Game(string mapName, Renderer renderer, int2 clientSize) { @@ -32,6 +36,27 @@ namespace OpenRa.Game pathFinder = new PathFinder(map, terrain.tileSet); network = new Network(); + + buildingCreation.Add( "fact", + delegate( int2 location, int palette ) + { + return new ConstructionYard( location, palette ); + } ); + buildingCreation.Add( "proc", + delegate( int2 location, int palette ) + { + return new Refinery( location, palette ); + } ); + buildingCreation.Add( "powr", + delegate( int2 location, int palette ) + { + return new Building( "powr", location, palette ); + } ); + buildingCreation.Add( "apwr", + delegate( int2 location, int palette ) + { + return new Building( "apwr", location, palette ); + } ); } public void Tick() @@ -41,7 +66,7 @@ namespace OpenRa.Game public void Issue(IOrder order) { - order.Apply(); + order.Apply( this ); } } } diff --git a/OpenRa.Game/Harvester.cs b/OpenRa.Game/Harvester.cs index a7f36b439c..3afa52261d 100644 --- a/OpenRa.Game/Harvester.cs +++ b/OpenRa.Game/Harvester.cs @@ -51,7 +51,7 @@ namespace OpenRa.Game harvester = harv; } - public void Apply() + public void Apply( Game game ) { harvester.AcceptHarvestOrder(); } diff --git a/OpenRa.Game/ISelectable.cs b/OpenRa.Game/IOrderGenerator.cs similarity index 81% rename from OpenRa.Game/ISelectable.cs rename to OpenRa.Game/IOrderGenerator.cs index 75b73e36f2..6dee93001b 100644 --- a/OpenRa.Game/ISelectable.cs +++ b/OpenRa.Game/IOrderGenerator.cs @@ -4,7 +4,7 @@ using System.Text; namespace OpenRa.Game { - interface ISelectable + interface IOrderGenerator { //Sprite CurrentCursor( int x, int y ); IOrder Order( int2 xy ); diff --git a/OpenRa.Game/MainWindow.cs b/OpenRa.Game/MainWindow.cs index 6aca770271..e233a3ee3d 100644 --- a/OpenRa.Game/MainWindow.cs +++ b/OpenRa.Game/MainWindow.cs @@ -48,11 +48,11 @@ namespace OpenRa.Game game.world.Add( new Mcv( new int2( 5, 5 ), 3 ) ); game.world.Add( new Mcv( new int2( 7, 5 ), 2 ) ); Mcv mcv = new Mcv( new int2( 9, 5 ), 1 ); - game.world.myUnit = mcv; + game.world.orderGenerator = mcv; game.world.Add( mcv ); game.world.Add( new Refinery( new int2( 7, 5 ), 2 ) ); - sidebar = new Sidebar(Race.Soviet, renderer, game.viewport); + sidebar = new Sidebar(game.techTree, Race.Soviet, renderer, game.viewport); renderer.SetPalette( new HardwarePalette( renderer, game.map ) ); } @@ -79,12 +79,15 @@ namespace OpenRa.Game RectangleF rect = new RectangleF(sidebar.Location.ToPointF(), new SizeF(sidebar.Width, game.viewport.Height)); if (rect.Contains(point.ToPointF())) { - sidebar.Build(sidebar.FindSpriteAtPoint(point)); + sidebar.Build( sidebar.FindSpriteAtPoint( point ), game ); return; } float2 xy = (1 / 24.0f) * point; - IOrder order = game.world.myUnit.Order( new int2( (int)xy.X, (int)xy.Y ) ); - game.Issue( order ); + if( game.world.orderGenerator != null ) + { + IOrder order = game.world.orderGenerator.Order( new int2( (int)xy.X, (int)xy.Y ) ); + game.Issue( order ); + } } } diff --git a/OpenRa.Game/Mcv.cs b/OpenRa.Game/Mcv.cs index 02c995b37f..b900861875 100644 --- a/OpenRa.Game/Mcv.cs +++ b/OpenRa.Game/Mcv.cs @@ -28,8 +28,8 @@ namespace OpenRa.Game world.Add( new ConstructionYard( fromCell - new int2( 1, 1 ), palette ) ); world.Add( new Refinery( fromCell - new int2( 1, -2 ), palette ) ); - world.myUnit = new Harvester( fromCell - new int2( 0, -4 ), palette ); - world.Add( (Actor)world.myUnit ); + world.orderGenerator = new Harvester( fromCell - new int2( 0, -4 ), palette ); + world.Add( (Actor)world.orderGenerator ); } ); currentOrder = null; }; diff --git a/OpenRa.Game/MoveOrder.cs b/OpenRa.Game/MoveOrder.cs index 70b7755896..8611f756e0 100644 --- a/OpenRa.Game/MoveOrder.cs +++ b/OpenRa.Game/MoveOrder.cs @@ -6,7 +6,7 @@ namespace OpenRa.Game { interface IOrder { - void Apply(); + void Apply( Game game ); } class MoveOrder : IOrder @@ -20,7 +20,7 @@ namespace OpenRa.Game this.Destination = destination; } - public void Apply() + public void Apply( Game game ) { Unit.AcceptMoveOrder( Destination ); } @@ -35,7 +35,7 @@ namespace OpenRa.Game this.Mcv = mcv; } - public void Apply() + public void Apply( Game game ) { Mcv.AcceptDeployOrder(); } diff --git a/OpenRa.Game/OpenRa.Game.csproj b/OpenRa.Game/OpenRa.Game.csproj index c94fdabe9d..af135ab1d3 100644 --- a/OpenRa.Game/OpenRa.Game.csproj +++ b/OpenRa.Game/OpenRa.Game.csproj @@ -44,6 +44,7 @@ + @@ -52,7 +53,6 @@ - diff --git a/OpenRa.Game/Sidebar.cs b/OpenRa.Game/Sidebar.cs index 50689d69de..f66a7560e2 100644 --- a/OpenRa.Game/Sidebar.cs +++ b/OpenRa.Game/Sidebar.cs @@ -12,7 +12,7 @@ namespace OpenRa.Game { class Sidebar { - TechTree.TechTree techTree = new TechTree.TechTree(); + TechTree.TechTree techTree; SpriteRenderer spriteRenderer; Sprite blank; @@ -24,11 +24,12 @@ namespace OpenRa.Game public float Width { get { return spriteWidth * 2; } - } + } - public Sidebar(Race race, Renderer renderer, Viewport viewport) + public Sidebar( TechTree.TechTree techTree, Race race, Renderer renderer, Viewport viewport ) { + this.techTree = techTree; this.viewport = viewport; viewport.AddRegion( Region.Create(viewport, DockStyle.Right, 128, Paint)); techTree.CurrentRace = race; @@ -41,9 +42,9 @@ namespace OpenRa.Game blank = SheetBuilder.Add(new Size((int)spriteWidth, (int)spriteHeight), 16); } - public bool Build(string key) + public void Build(string key, Game game ) { - return techTree.Build(key); + game.world.orderGenerator = new PlaceBuilding( 1, key.ToLowerInvariant() ); } void LoadSprites(string filename) @@ -117,4 +118,48 @@ namespace OpenRa.Game return null; } } + + class PlaceBuilding : IOrderGenerator + { + int palette; + string buildingName; + + public PlaceBuilding( int palette, string buildingName ) + { + this.palette = palette; + this.buildingName = buildingName; + } + + public IOrder Order( int2 xy ) + { + // todo: check that space is free + return new PlaceBuildingOrder( this, xy ); + } + + class PlaceBuildingOrder : IOrder + { + PlaceBuilding building; + int2 xy; + + public PlaceBuildingOrder( PlaceBuilding building, int2 xy ) + { + this.building = building; + this.xy = xy; + } + + public void Apply( Game game ) + { + game.world.AddFrameEndTask( delegate + { + Provider newBuilding; + if( game.buildingCreation.TryGetValue( building.buildingName, out newBuilding ) ) + { + game.world.Add( newBuilding( xy, building.palette ) ); + game.techTree.Build( building.buildingName ); + } + game.world.orderGenerator = null; + } ); + } + } + } } diff --git a/OpenRa.Game/Unit.cs b/OpenRa.Game/Unit.cs index 96fcc5a6e1..d0e08335c2 100644 --- a/OpenRa.Game/Unit.cs +++ b/OpenRa.Game/Unit.cs @@ -4,7 +4,7 @@ using System.Text; namespace OpenRa.Game { - abstract class Unit : Actor, ISelectable + abstract class Unit : Actor, IOrderGenerator { protected Animation animation; diff --git a/OpenRa.Game/World.cs b/OpenRa.Game/World.cs index 35127926d1..6a311f0bd9 100644 --- a/OpenRa.Game/World.cs +++ b/OpenRa.Game/World.cs @@ -14,7 +14,7 @@ namespace OpenRa.Game List> frameEndActions = new List>(); SpriteRenderer spriteRenderer; Viewport viewport; - public ISelectable myUnit; + public IOrderGenerator orderGenerator; public World(Renderer renderer, Viewport viewport) { diff --git a/OpenRa.TechTree/TechTree.cs b/OpenRa.TechTree/TechTree.cs index a107df9f49..bc741d6b2f 100644 --- a/OpenRa.TechTree/TechTree.cs +++ b/OpenRa.TechTree/TechTree.cs @@ -65,8 +65,9 @@ namespace OpenRa.TechTree public bool Build(string key, bool force) { - if (string.IsNullOrEmpty(key)) return false; - Item b = objects[key]; + if( string.IsNullOrEmpty( key ) ) return false; + key = key.ToUpperInvariant(); + Item b = objects[ key ]; if (!force && !b.CanBuild) return false; built.Add(key); CheckAll(); @@ -80,6 +81,7 @@ namespace OpenRa.TechTree public bool Unbuild(string key) { + key = key.ToUpperInvariant(); Item b = objects[key]; if (!built.Contains(key)) return false; built.Remove(key); diff --git a/sequences.xml b/sequences.xml index 7a5777bf26..b5caf51f05 100644 --- a/sequences.xml +++ b/sequences.xml @@ -47,7 +47,23 @@ + + + + + + + + + + + + + + + +