I'm trying to learn Monogame / XNA by creating a little Terraria-like game. I currently want to create a world, which is completely filled with dirt. My problem is that only one block from an block array gets drawn. Here is my code:
public class Survival2DGame : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private World world;
private Block dirtBlock;
public Survival2DGame()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
dirtBlock = new Block(Content.Load<Texture2D>("Dirt"), Vector2.Zero);
world = new World(100, 100);
WorldGeneration.GenerateWorld(world, dirtBlock);
Debug.WriteLine(world.blocks[50, 50].Texture);
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
_spriteBatch.Begin();
world.Draw(_spriteBatch);
_spriteBatch.End();
base.Draw(gameTime);
}
}
static class WorldGeneration
{
public static void GenerateWorld(World world, Block dirtBlock)
{
for (int x = 0; x < world.Length; x++)
{
for (int y = 0; y < world.Height; y++)
{
world.blocks[x, y] = dirtBlock;
world.blocks[x, y].Position = new Vector2(x * 16, y * 16);
}
}
}
}
class World
{
public int Length { get; set; }
public int Height { get; set; }
public Block[,] blocks;
public World(int length, int height)
{
Length = length;
Height = height;
blocks = new Block[length, height];
}
public void Draw(SpriteBatch spriteBatch)
{
foreach (Block block in blocks)
{
block.Draw(spriteBatch);
}
}
}
class Block
{
public Texture2D Texture { get; set; }
public Vector2 Position { get; set; }
public Block(Texture2D texture, Vector2 position)
{
Texture = texture;
Position = position;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, Position, Color.White);
}
}
When i run the game its completely empty.
I think it has something to do with me only creating one dirt block and not a new one everytime i add it to the array. I so far only have experience with unity and there i would handle this with Scriptable Objects.