More solid grid to pixel conversion

This commit is contained in:
Simon Holmberg
2012-09-27 22:05:03 +02:00
parent 63012d301d
commit fecb2edcf8
5 changed files with 34 additions and 20 deletions
@@ -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);
}
}
}
@@ -41,5 +41,10 @@ namespace DepthsBelow.Component
Rotation = 0.0f;
Origin = Vector2.Zero;
}
public Point ToGrid()
{
return Grid.ScreenToGrid(Position);
}
}
}
+11 -6
View File
@@ -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)
);
}
}
}
+5 -6
View File
@@ -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);
}
}
}
+4 -4
View File
@@ -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;
}
}