Reputation: 482
I want to draw multiple players on the client. It works fine without the foreach rotations, but when i add the foreach rotations it updates both players with eachothers rotation instead of the player who the rotation is meant to be for.
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);
}
}
Each player has an unique ID which I store as key inside BOTH dictionaries. Is there a way that i can combine these dictionaries to achieve the correct result?
Right now Dictonary positions contains (long,Vector2(x,y)) where long is the unique user ID and the rotations contains (long, float) where long is the same user ID as in positions and float is the rotation.
Edit: This works fine but doesn't update the rotation as it's set.
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);
}
Upvotes: 0
Views: 272
Reputation: 6466
Why don't you create a Player class that contains all of the information about a player.
class Player
{
public Vector2 Position { get; set; }
public float Rotation { get; set; }
}
and just keep a single dictionary of that using the loop, here I'll show you a very simple game class that draws 5 players with the rotations, hopefully it'll help :)
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);
}
}
Upvotes: 2
Reputation: 22001
Is it not as simple as:
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);
}
Upvotes: 0