Question

My main problem is I don't understand how to get the position of the generated tiles or how to tell where the mouse is. Should I use collision to detect mouse or something else? Is there something I can do to optimize my code and make it easier to get things like the position

I took some things out of my code like loading of the textures just to make it shorter for you guys since that isn't part of the problem.

My Tile Generation Code

    public Block[] tiles = new Block[3];
    public int width, height;
    public int[,] index;
    public Rectangle tileRect;

public void Load(ContentManager content)
    {
        tiles[0] = new Block { Type = BlockType.Grass, Position = Vector2.Zero, texture = grass};
        tiles[1] = new Block { Type = BlockType.Dirt, Position = Vector2.Zero, texture = dirt};

        width = 50;
        height = 50;

        index = new int[width, height];

        Random rand = new Random();
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                index[x,y] = rand.Next(0,2);
            }
        }
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                    spriteBatch.Draw(tiles[index[x,y]].texture, tileRect = new Rectangle(x * 64, y * 64, 64, 64), 
                        Color.White);          
            }
        }  
    }

Block Properties Code

public enum BlockType
{
    Dirt,
    Grass,
    Selection
}

public class Block
{
    public BlockType Type { get; set; }
    public Vector2 Position { get; set; }
    public Texture2D texture { get; set; }
}
Was it helpful?

Solution

This code should do the trick. You can add this method in your tile generation class. (Untested)

public bool IsMouseInsideTile(int x, int y)
{
    MouseState MS = Mouse.GetState();
    return (MS.X >= x * 64 && MS.X <= (x + 1) * 64 &&
        MS.Y >= y * 64 && MS.Y <= (y + 1) * 64);
}

You can edit this function however you like to fit it to your needs.

Edit: I'll explain this code a little.

  • Mouse.GetState() gets the current position of the mouse, as a Vector2

  • A tile [a ,b] is at the position [a * 64, b * 64], as your draw method says

  • The tile's max x and y coordinates is at [(a + 1) * 64, (b + 1) * 64], since the texture is 64 by 64 pixels of dimensions.

  • I check the if the mouse is inside every tile. You could add a MouseHover event if you'd like.

More edits: Edited my code, based on your comment.

Even more edits: Here is the code for the Draw method:

public void Draw(SpriteBatch spriteBatch)
    {
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                    spriteBatch.Draw(tiles[index[x,y]].texture, tileRect = new Rectangle(x * 64, y * 64, 64, 64), 
                        Color.White);
                    if(IsMouseInsideTile(x, y))
                        spriteBatch.Draw(selected.texture, tileRect = new Rectangle(x * 64, y * 64, 64, 64), 
                                        Color.White);
            }
        }  
    }

OTHER TIPS

In the XNA Update function, you can get the mouse position using Mouse.GetState() which will give you the X and Y coordinate properties of the mouse. Just divide those by your tile size and round down (floor) to get the closest tile coordinate index.

Added Code

public static Vector2 GetGridCoordinates(MouseState mouseState, int gridSize){
    return new Vector2(
        (int)Math.Floor(mouseState.X / gridSize),
        (int)Math.Floor(mouseState.Y / gridSize)
    );
}

You could probably even make this an extension function of the MouseState class so all you'd have to do is something like:

Vector2 gridCoords = Mouse.GetState().GetGridCoordinates(MyGridSize);

But I'm probably overthinking it...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top