I'm working on my first XNA 2D game (overhead view), and I'm pondering how to manage the difference in position on screen vs. the position of a sprite on a global map.
The solution I'm currently thinking about is having the map keep track of everything on it, the player, other sprites, etc. The sprites will know where they are on the map, and when .Draw()
is called on the map it will delegate drawing items to the items themselves, but pass a position to draw on the screen. For instance, something akin to the following:
class Map {
Vector2D ScreenCenter;
List<DrawableItem> Content;
void Draw(GameTime gameTime) {
foreach (var i in Content) {
i.Draw(spriteBatch, i.Position - ScreenCenter);
}
}
}
class DrawableItem {
Vector2D Position;
void Draw(SpriteBatch spriteBatch, Vector2D screenPosition) {
// Draw ourselves at screenPosition
}
}
Is there some better way of doing this? Or am I on the right sort of track here?