单击鼠标创建新Tiles

时间:2012-08-28 17:38:53

标签: c# xna

我想知道你是否可以帮我解决这个问题,因为我有点陷入困境,用碰撞方法在屏幕上画一个新的瓷砖。我只是试图在点击鼠标时在屏幕上绘制一块瓷砖。注意此图块需要位于我创建的网格中。

这是我的Level代码,当我创建一个看起来像这样的文本文件时,将瓷砖绘制到屏幕上

...GGGGG....
....dddd....
............

代码

public class Level
{
    private Tile[,] tiles;

    ContentManager content;

    public Vector2 startPosition;

    public Level(ContentManager newContent, Stream fileStream)
    {
        content = newContent;
        LoadTiles(fileStream);
    }

    private void LoadTiles(Stream fileStream)
    {
        int width;
        List<string> lines = new List<string>();
        using (StreamReader reader = new StreamReader(fileStream))
        {
            string line = reader.ReadLine();
            width = line.Length;
            while (line != null)
            {
                lines.Add(line);
                if (line.Length != width)
                    throw new Exception(String.Format("Line {0} isn't the right length, sort it out, good debug method here ;)", lines.Count));
                line = reader.ReadLine();
            }
        }

        tiles = new Tile[width, lines.Count];

        for (int y = 0; y < Height; ++y)
            for (int x = 0; x < Width; ++x)
            {
                char tileType = lines[y][x];
                tiles[x, y] = LoadTile(tileType, x, y);
            }
    }

    private Tile LoadTile(char tileType, int x, int y)
    {
        switch (tileType)
        {
            case '.':
                return new Tile(null, TileCollision.Passable);

            // Grass
            case 'g':
                return LoadTile("Grass", TileCollision.Impassable);
            // Dirt
            case 'd':
                return LoadTile("Dirt", TileCollision.Impassable);

            // Log + Leaves || Log = "L" Leaves "l"
            case 'L':
                return LoadTile("Log", TileCollision.Passable);
            case 'l':
                return LoadTile("Leaves", TileCollision.Passable);

            // Stone
            case 'S':
                return LoadTile("Ore_Stone", TileCollision.Impassable);
            // Coal
            case 'c':
                return LoadTile("Ore_Coal", TileCollision.Impassable);


            // Door
            case '!':
                return LoadTile("Door_Top", TileCollision.Door);
            case '1':
                return LoadTile("Door_Bottom", TileCollision.Door);
            case 'b':
                // Brick
                return LoadTile("Brick", TileCollision.Passable);


            // Player spawn 'S'
            case 's':
                return LoadStartTile(x, y);

            default:
                throw new NotSupportedException(String.Format("Unsupported tile type character '{0}' at position {1}, {2}.", tileType, x, y));
        }
    }

    private Tile LoadStartTile(int x, int y)
    {

        startPosition = new Vector2(x * 32, y * 32);

        return new Tile(null, TileCollision.Passable);
    }

    private Tile LoadTile(string name, TileCollision collision)
    {
        return new Tile(content.Load<Texture2D>("Blocks/" + name), collision);
    }

    public Rectangle GetBounds(int x, int y)
    {
        return new Rectangle(x * Tile.Width, y * Tile.Height, Tile.Width, Tile.Height);
    }

    public int Width
    {
        get { return tiles.GetLength(0); }
    }

    public int Height
    {
        get { return tiles.GetLength(1); }
    }

    public TileCollision GetCollision(int x, int y)
    {
        // Prevent escaping past the level ends.
        if (x < 0 || x >= Width)
            return TileCollision.Passable;
        // Allow jumping past the level top and falling through the bottom.
        if (y < 0 || y >= Height)
            return TileCollision.Passable;

        return tiles[x, y].Collision;
    }


    public void Draw(SpriteBatch spriteBatch)
    {
        DrawTiles(spriteBatch);
    }

    public void DrawTiles(SpriteBatch spriteBatch)
    {
        for (int y = 0; y < Height; ++y)
        {
            for (int x = 0; x < Width; ++x)
            {
                Texture2D texture = tiles[x, y].Texture;
                if (texture != null)
                {
                    Vector2 position = new Vector2(x, y) * Tile.Size;
                    spriteBatch.Draw(texture, position, Color.White);
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:3)

类似的东西:

MouseState mouseState = Mouse.GetState();

int x = (int)(mouseState.X / Tile.Size);
int y = (int)(mouseState.Y / Tile.Size);

tiles[x, y] = //whatever tile type you want here