私の主な問題は、生成されたタイルの位置を取得する方法や、マウスがどこにあるかを知る方法がわからないことです。マウスなどを検出するために衝突を使用する必要がありますか?コードを最適化し、位置などを簡単に取得できるようにするためにできることはありますか
テクスチャのロードなど、コードからいくつかのことを取り除いて、問題の一部ではないため、テクスチャを短くしました。
私のタイル生成コード
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);
}
}
}
ブロックプロパティコード
public enum BlockType
{
Dirt,
Grass,
Selection
}
public class Block
{
public BlockType Type { get; set; }
public Vector2 Position { get; set; }
public Texture2D texture { get; set; }
}