Как нарисовать динамическую сетку с XNA - PullRequest
4 голосов
/ 06 ноября 2011

Я пытаюсь нарисовать сетку, используя платформу XNA, эта сетка должна иметь фиксированное измерение во время выполнения XNA, но должна быть предоставлена ​​пользователю возможность настроить ее перед запуском страницы игры (ясборка моего приложения с использованием шаблона silverlight / xna).

У кого-нибудь есть предложения по достижению этой цели?

Спасибо

Ответы [ 2 ]

1 голос
/ 27 ноября 2011
    ContentManager contentManager;
    GameTimer timer;
    SpriteBatch spriteBatch;
    LifeGrid life;


    int tileSize = 32;
    Vector2 position = Vector2.Zero;
    Texture2D gridTexture;
    int[,] map;

    public GamePage()
    {
        InitializeComponent();

        // Get the content manager from the application
        contentManager = (Application.Current as App).Content;

        // Create a timer for this page
        timer = new GameTimer();
        //timer.UpdateInterval = TimeSpan.FromTicks(333333);
        timer.UpdateInterval = TimeSpan.Zero;
        timer.Update += OnUpdate;
        timer.Draw += OnDraw;
        List<Position> p = new List<Position>();
        p.Add(new Position(1,1));
        p.Add(new Position(1,4));
        p.Add(new Position(1,5));
        p.Add(new Position(1,6));
        p.Add(new Position(1,7));
        this.life = new LifeGrid(10, 10, p);


        map = new int[,]{{1, 1, 0,},{0, 1, 1,},{1, 1, 0,},};

        // LayoutUpdated += new EventHandler(GamePage_LayoutUpdated);
    }
    /// <summary>
    /// Allows the page to draw itself.
    /// </summary>
    private void OnDraw(object sender, GameTimerEventArgs e)
    {
        // SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.CornflowerBlue);
       // SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.Black);
        // Draw the sprite
        spriteBatch.Begin();

        for (int i = 0; i <= map.GetUpperBound(0); i++)
        {
            for (int j = 0; j <= map.GetUpperBound(1); j++)
            {
                int textureId = map[i, j];
                if (textureId != 0)
                {
                    Vector2 texturePosition = new Vector2(i * tileSize, j * tileSize) + position;

                    //Here you would typically index to a Texture based on the textureId.
                    spriteBatch.Draw(gridTexture, texturePosition, null, Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0f);

                }
            }
        }


        spriteBatch.End();
    }
1 голос
/ 07 ноября 2011

Установите tileSize, а затем нарисуйте текстуру по размеру сетки, которую вы хотите.

Вот немного переработанного кода. Вот так я бы начал с создания карты тайлов, используя 2d массив.

int tileSize = 32;
Vector2 position = Vector2.Zero;
Texture2D gridTexture;

int[,] map = new int[,]
{
    {1, 1, 0,},
    {0, 1, 1,},
    {1, 1, 0,},
};

Затем добавьте что-то вроде этого в функцию рисования:

for (int i = 0; i <= map.GetUpperBound(0); i++)
{
    for (int j = 0; j <= map.GetUpperBound(1); j++)
    {
        int textureId = map[i, j];
        if (textureId != 0)
        {
            Vector2 texturePosition = new Vector2(i * tileSize, j * tileSize) + position;

            //Here you would typically index to a Texture based on the textureId.
            spriteBatch.Draw(gridTexture, texturePosition, null, Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0f);             
        }
    }
}
...