0

我想在客户端绘制多个玩家。如果没有 foreach 轮换,它可以正常工作,但是当我添加 foreach 轮换时,它会使用彼此轮换更新两个玩家,而不是轮换的玩家。

foreach (var kvp in positions)
{
    foreach (var kvr in rotations)
    {
        // draw player
        spriteBatch.Draw(texture, kvp.Value, null, Color.White, 
        kvr.Value, new Vector2(texture.Width / 2, texture.Height / 2), 1f, 
        SpriteEffects.None, 1f);
    }
}

每个玩家都有一个唯一的 ID,我将其作为密钥存储在两个字典中。有没有办法可以结合这些字典来获得正确的结果?

现在,字典位置包含 (long,Vector2(x,y)),其中 long 是唯一的用户 ID,旋转包含 (long, float),其中 long 是与位置相同的用户 ID,float 是旋转。

编辑:这工作正常,但不会在设置时更新旋转。

foreach (var kvp in positions)
{
    // draw player
    spriteBatch.Draw(texture, kvp.Value, null, Color.White, 1f, 
    new Vector2(texture.Width / 2, texture.Height / 2), 1f, 
    SpriteEffects.None, 1f);
}
4

2 回答 2

2

为什么不创建一个包含有关玩家的所有信息的 Player 类。

class Player
{
    public Vector2 Position { get; set; }
    public float Rotation { get; set; }
}

并且只需使用循环保留一个字典,在这里我将向您展示一个非常简单的游戏类,它可以吸引 5 个轮换玩家,希望它会有所帮助:)

public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

Dictionary<int, Player> players;
Texture2D texture;

public Game1()
{
    graphics = new GraphicsDeviceManager(this);
    Content.RootDirectory = "Content";
}

protected override void LoadContent()
{
    spriteBatch = new SpriteBatch(GraphicsDevice);
    texture = Content.Load<Texture2D>("player");

    Random rnd = new Random();

    players = new Dictionary<int, Player>()
        {
            // Add 5 new players, their location is random and so is their rotation.
            {0,  new Player() { Position = new Vector2(rnd.Next(1024),rnd.Next(768)), Rotation = rnd.Next(360), Size = new Vector2(texture.Width, texture.Height)}}, 
            {1,  new Player() { Position = new Vector2(rnd.Next(1024),rnd.Next(768)), Rotation = rnd.Next(360), Size = new Vector2(texture.Width, texture.Height)}}, 
            {2,  new Player() { Position = new Vector2(rnd.Next(1024),rnd.Next(768)), Rotation = rnd.Next(360), Size = new Vector2(texture.Width, texture.Height)}}, 
            {3,  new Player() { Position = new Vector2(rnd.Next(1024),rnd.Next(768)), Rotation = rnd.Next(360), Size = new Vector2(texture.Width, texture.Height)}}, 
            {4,  new Player() { Position = new Vector2(rnd.Next(1024),rnd.Next(768)), Rotation = rnd.Next(360), Size = new Vector2(texture.Width, texture.Height)}}, 
        };
}

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);

    spriteBatch.Begin();
    for (int i = 0; i < players.Count; i++)
    {
        Player targetPlayer = players[i];
        spriteBatch.Draw(texture, targetPlayer.PlayerRectangle, null, Color.White, targetPlayer.Rotation, targetPlayer.Centre, SpriteEffects.None, 0.0f);
    }
    spriteBatch.End();

    base.Draw(gameTime);
}
}

class Player
{
Rectangle playerRectangle;
Vector2 position;
Vector2 size;
Vector2 center;

public Vector2 Position { get { return position; } set { position = value; UpdateRectangleAndCentre(); } }
public Vector2 Size { get { return size; } set { size = value; UpdateRectangleAndCentre(); } }
public float Rotation { get; set; }
public Rectangle PlayerRectangle { get { return playerRectangle; } }
public Vector2 Centre { get { return center; } }

void UpdateRectangleAndCentre()
{
    playerRectangle = new Rectangle((int)Position.X, (int)Position.Y, (int)Size.X, (int)Size.Y);
    center = new Vector2(size.X / 2, size.Y / 2);
}
}
于 2012-06-01T14:22:29.013 回答
0

是不是这么简单:

 foreach (var kvp in positions)
        {
            kvr = rotations[kvp.Key];
            // draw player
            spriteBatch.Draw(texture, kvp.Value, null, Color.White, 
            kvr.Value, new Vector2(texture.Width / 2, texture.Height / 2), 1f, 
            SpriteEffects.None, 1f);
        }
于 2012-06-01T14:19:13.860 回答