diff --git a/DepthsBelow/DepthsBelow/Component/GridTransform.cs b/DepthsBelow/DepthsBelow/Component/GridTransform.cs index 06f27a0..afd06f7 100644 --- a/DepthsBelow/DepthsBelow/Component/GridTransform.cs +++ b/DepthsBelow/DepthsBelow/Component/GridTransform.cs @@ -8,8 +8,8 @@ namespace DepthsBelow.Component { public class GridTransform : Component { - public Vector2 Position; - public float X + public Point Position; + public int X { get { @@ -20,7 +20,7 @@ namespace DepthsBelow.Component Position.X = value; } } - public float Y + public int Y { get { @@ -34,7 +34,12 @@ namespace DepthsBelow.Component public GridTransform(Entity parent) : base(parent) { - Position = Vector2.Zero; + Position = Point.Zero; + } + + public Vector2 ToScreen() + { + return Grid.GridToScreen(Position); } } } diff --git a/DepthsBelow/DepthsBelow/Component/PixelTransform.cs b/DepthsBelow/DepthsBelow/Component/PixelTransform.cs index 6a018b0..f1c6e75 100644 --- a/DepthsBelow/DepthsBelow/Component/PixelTransform.cs +++ b/DepthsBelow/DepthsBelow/Component/PixelTransform.cs @@ -41,5 +41,10 @@ namespace DepthsBelow.Component Rotation = 0.0f; Origin = Vector2.Zero; } + + public Point ToGrid() + { + return Grid.ScreenToGrid(Position); + } } } diff --git a/DepthsBelow/DepthsBelow/Grid.cs b/DepthsBelow/DepthsBelow/Grid.cs index 48ebde3..19d08d5 100644 --- a/DepthsBelow/DepthsBelow/Grid.cs +++ b/DepthsBelow/DepthsBelow/Grid.cs @@ -1,4 +1,5 @@ -using Microsoft.Xna.Framework; +using System; +using Microsoft.Xna.Framework; namespace DepthsBelow { @@ -6,13 +7,17 @@ namespace DepthsBelow { public const int TileSize = 32; - public static Vector2 GridToScreen(Vector2 gridPos) + public static Vector2 GridToScreen(Point gridPos) { - return gridPos * TileSize; + return new Vector2(gridPos.X * TileSize, gridPos.Y * TileSize); } - public static Vector2 ScreenToGrid(Vector2 screenPos) - { - return screenPos / TileSize; + + public static Point ScreenToGrid(Vector2 screenPos) + { + return new Point( + (int)Math.Floor(screenPos.X / TileSize), + (int)Math.Floor(screenPos.Y / TileSize) + ); } } } diff --git a/DepthsBelow/DepthsBelow/MouseInput.cs b/DepthsBelow/DepthsBelow/MouseInput.cs index 4dce4b4..4c29695 100644 --- a/DepthsBelow/DepthsBelow/MouseInput.cs +++ b/DepthsBelow/DepthsBelow/MouseInput.cs @@ -16,7 +16,6 @@ namespace DepthsBelow MouseState lastMouseState; Rectangle selectionRectangle; Texture2D selectionTexture; - Rectangle gridRectangle; Texture2D gridTexture; @@ -25,6 +24,7 @@ namespace DepthsBelow this.core = core; selectionRectangle = Rectangle.Empty; + gridRectangle = Rectangle.Empty; } public void LoadContent() @@ -41,6 +41,7 @@ namespace DepthsBelow MouseState ms = Mouse.GetState(); KeyboardState ks = Keyboard.GetState(); + // Selection rectangle if (ms.LeftButton == ButtonState.Pressed) { if (selectionRectangle == Rectangle.Empty) @@ -53,7 +54,6 @@ namespace DepthsBelow selectionRectangle.Height = ms.Y - selectionRectangle.Y + (int)core.camera.Position.Y; } } - if (ms.LeftButton == ButtonState.Released && selectionRectangle != Rectangle.Empty) { if (!ks.IsKeyDown(Keys.LeftControl)) @@ -67,9 +67,8 @@ namespace DepthsBelow selectionRectangle = Rectangle.Empty; } - Vector2 currentGrid = Grid.ScreenToGrid(new Vector2(ms.X + core.camera.Position.X - Grid.TileSize / 2, ms.Y + core.camera.Position.Y - Grid.TileSize / 2)); - currentGrid.X = Convert.ToInt32(currentGrid.X); - currentGrid.Y = Convert.ToInt32(currentGrid.Y); + // Grid highlighting + Point currentGrid = Grid.ScreenToGrid(new Vector2(ms.X + core.camera.Position.X, ms.Y + core.camera.Position.Y)); Vector2 currentScreen = Grid.GridToScreen(currentGrid); gridRectangle = new Rectangle((int)currentScreen.X, (int)currentScreen.Y, Grid.TileSize, Grid.TileSize); @@ -78,8 +77,8 @@ namespace DepthsBelow public void Draw(SpriteBatch spriteBatch) { - spriteBatch.Draw(gridTexture, gridRectangle, Color.Yellow * 0.3f); spriteBatch.Draw(selectionTexture, selectionRectangle, Color.Red * 0.5f); + spriteBatch.Draw(gridTexture, gridRectangle, Color.Yellow * 0.3f); } } } diff --git a/DepthsBelow/DepthsBelow/Soldier.cs b/DepthsBelow/DepthsBelow/Soldier.cs index 76a014c..285df1b 100644 --- a/DepthsBelow/DepthsBelow/Soldier.cs +++ b/DepthsBelow/DepthsBelow/Soldier.cs @@ -71,13 +71,13 @@ namespace DepthsBelow lastKeyboardState = ks; int speed = 2; - if (pixelTransform.X < gridTransform.X * 32) + if (pixelTransform.X < gridTransform.ToScreen().X) pixelTransform.X += speed; - else if (pixelTransform.X > gridTransform.X * 32) + else if (pixelTransform.X > gridTransform.ToScreen().X) pixelTransform.X -= speed; - else if (pixelTransform.Y < gridTransform.Y * 32) + else if (pixelTransform.Y < gridTransform.ToScreen().Y) pixelTransform.Y += speed; - else if (pixelTransform.Y > gridTransform.Y * 32) + else if (pixelTransform.Y > gridTransform.ToScreen().Y) pixelTransform.Y -= speed; } }