From acba1dc8e4662a7802d62b6fa6b65671f962a42a Mon Sep 17 00:00:00 2001 From: bob Date: Mon, 16 Jul 2007 01:01:45 +0000 Subject: [PATCH] generalising selection, issuing of orders git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1283 993157c7-ee19-0410-b2c4-bb4e9862e678 --- OpenRa.Game/Actor.cs | 2 +- OpenRa.Game/ISelectable.cs | 12 ++++++++++++ OpenRa.Game/MainWindow.cs | 13 +++++++++---- OpenRa.Game/Mcv.cs | 16 ++++++++++++++-- OpenRa.Game/MoveOrder.cs | 16 ++++++++++++++-- OpenRa.Game/OpenRa.Game.csproj | 1 + OpenRa.Game/Refinery.cs | 2 +- OpenRa.Game/Tree.cs | 2 +- OpenRa.Game/World.cs | 6 +++--- 9 files changed, 56 insertions(+), 14 deletions(-) create mode 100644 OpenRa.Game/ISelectable.cs diff --git a/OpenRa.Game/Actor.cs b/OpenRa.Game/Actor.cs index cfce604caf..95aa0736a3 100644 --- a/OpenRa.Game/Actor.cs +++ b/OpenRa.Game/Actor.cs @@ -10,7 +10,7 @@ namespace OpenRa.Game { abstract class Actor { - public float2 location; + public float2 renderLocation; public int palette; public abstract Sprite[] CurrentImages { get; } public abstract void Tick( double t ); diff --git a/OpenRa.Game/ISelectable.cs b/OpenRa.Game/ISelectable.cs new file mode 100644 index 0000000000..eb0926fcd5 --- /dev/null +++ b/OpenRa.Game/ISelectable.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace OpenRa.Game +{ + interface ISelectable + { + //Sprite CurrentCursor( int x, int y ); + MoveOrder Order( int x, int y ); + } +} diff --git a/OpenRa.Game/MainWindow.cs b/OpenRa.Game/MainWindow.cs index 77a339d5f3..3aac970a29 100644 --- a/OpenRa.Game/MainWindow.cs +++ b/OpenRa.Game/MainWindow.cs @@ -23,7 +23,7 @@ namespace OpenRa.Game Sidebar sidebar; Viewport viewport; - Mcv myUnit; + ISelectable myUnit; static Size GetResolution(Settings settings) { @@ -62,7 +62,9 @@ namespace OpenRa.Game world.Add(new Mcv(24 * new float2(5, 5), 3)); world.Add(new Mcv(24 * new float2(7, 5), 2)); - world.Add(myUnit = new Mcv(24 * new float2(9, 5), 1)); + Mcv mcv = new Mcv( 24 * new float2( 9, 5 ), 1 ); + myUnit = mcv; + world.Add( mcv ); world.Add(new Refinery(24 * new float2(5, 7), 1)); @@ -87,8 +89,11 @@ namespace OpenRa.Game if (e.Button == MouseButtons.Left) { - MoveOrder order = new MoveOrder(lastPos + viewport.Location); - myUnit.Accept(order); + //MoveOrder order = new MoveOrder(lastPos + viewport.Location); + //myUnit.Accept(order); + int x = (int)( ( e.X + viewport.Location.X ) / 24 ); + int y = (int)( ( e.Y + viewport.Location.Y ) / 24 ); + myUnit.Order( x, y ).Apply(); } } diff --git a/OpenRa.Game/Mcv.cs b/OpenRa.Game/Mcv.cs index 00ff4d4d7d..a825ef1ebe 100644 --- a/OpenRa.Game/Mcv.cs +++ b/OpenRa.Game/Mcv.cs @@ -7,15 +7,17 @@ using BluntDirectX.Direct3D; namespace OpenRa.Game { - class Mcv : Actor + class Mcv : Actor, ISelectable { static Range? mcvRange = null; MoveOrder currentOrder = null; int facing = 0; + float2 location; public Mcv( float2 location, int palette ) { this.location = location; + this.renderLocation = this.location - new float2( 12, 12 ); // HACK: display the mcv centered in it's cell this.palette = palette; if (mcvRange == null) @@ -75,8 +77,11 @@ namespace OpenRa.Game if( currentOrder == null ) return; - if( float2.WithinEpsilon( location, currentOrder.Destination, 1.0f ) ) + if( float2.WithinEpsilon( renderLocation, currentOrder.Destination, 1.0f ) ) + { + currentOrder = null; return; + } Range r = new Range( new float2( -Speed * (float)t, -Speed * (float)t ), @@ -92,6 +97,13 @@ namespace OpenRa.Game facing = ( facing + 31 ) % 32; else facing = ( facing + 1 ) % 32; + + renderLocation = location - new float2( 12, 12 ); // HACK: center mcv in it's cell + } + + public MoveOrder Order( int x, int y ) + { + return new MoveOrder( this, x, y ); } } } diff --git a/OpenRa.Game/MoveOrder.cs b/OpenRa.Game/MoveOrder.cs index 1a18e92abb..432880dd4c 100644 --- a/OpenRa.Game/MoveOrder.cs +++ b/OpenRa.Game/MoveOrder.cs @@ -6,11 +6,23 @@ namespace OpenRa.Game { class MoveOrder { + public readonly Mcv Unit; public readonly float2 Destination; - public MoveOrder(float2 destination) + public MoveOrder( Mcv unit, int x, int y ) + : this( unit, new float2( x * 24, y * 24 ) ) { - this.Destination = destination - new float2(24,24); //HACK account for MCV size + } + + public MoveOrder(Mcv unit, float2 destination) + { + this.Unit = unit; + this.Destination = destination; + } + + public void Apply() + { + Unit.Accept( this ); } } } diff --git a/OpenRa.Game/OpenRa.Game.csproj b/OpenRa.Game/OpenRa.Game.csproj index 6e38aa90fe..6afaf20740 100644 --- a/OpenRa.Game/OpenRa.Game.csproj +++ b/OpenRa.Game/OpenRa.Game.csproj @@ -43,6 +43,7 @@ + diff --git a/OpenRa.Game/Refinery.cs b/OpenRa.Game/Refinery.cs index c1a0a56062..9fab4f597d 100644 --- a/OpenRa.Game/Refinery.cs +++ b/OpenRa.Game/Refinery.cs @@ -16,7 +16,7 @@ namespace OpenRa.Game if (refineryRange == null) refineryRange = UnitSheetBuilder.AddUnit("proc"); - this.location = location; + this.renderLocation = location; this.palette = palette; } diff --git a/OpenRa.Game/Tree.cs b/OpenRa.Game/Tree.cs index cfd2ff9c99..003e69eb91 100644 --- a/OpenRa.Game/Tree.cs +++ b/OpenRa.Game/Tree.cs @@ -10,7 +10,7 @@ namespace OpenRa.Game { public Tree(TreeReference r, TreeCache renderer, Map map) { - location = 24 * (new float2(r.Location) - new float2(map.XOffset, map.YOffset)); + renderLocation = 24 * (new float2(r.Location) - new float2(map.XOffset, map.YOffset)); currentImages = new Sprite[] { renderer.GetImage(r.Image) }; } diff --git a/OpenRa.Game/World.cs b/OpenRa.Game/World.cs index cf9eba9e04..9f8d9b7a53 100644 --- a/OpenRa.Game/World.cs +++ b/OpenRa.Game/World.cs @@ -41,14 +41,14 @@ namespace OpenRa.Game Sprite[] images = a.CurrentImages; - if( a.location.X > range.End.X || a.location.X < range.Start.X - images[ 0 ].bounds.Width ) + if( a.renderLocation.X > range.End.X || a.renderLocation.X < range.Start.X - images[ 0 ].bounds.Width ) continue; - if (a.location.Y > range.End.Y || a.location.Y < range.Start.Y - images[0].bounds.Height) + if (a.renderLocation.Y > range.End.Y || a.renderLocation.Y < range.Start.Y - images[0].bounds.Height) continue; foreach( Sprite image in images ) - spriteRenderer.DrawSprite(image, a.location, a.palette); + spriteRenderer.DrawSprite(image, a.renderLocation, a.palette); } spriteRenderer.Flush();