I'm trying to make a Chunk class for my Tile class creating Chunks of 25 Tiles each, But for some reason it doesn't work correctly Tile.cs extends Sprite.cs
class Tile : Sprite
{
public TileType TileType { get; set; }
public Tile(Texture2D texture, Vector2 position, SpriteBatch spriteBatch) : base(texture, position, spriteBatch)
{
}
}
Sprite.cs
class Sprite
{
public Vector2 Position { get; set; }
public Texture2D Texture { get; set; }
public SpriteBatch SpriteBatch { get; set; }
public Sprite(Texture2D texture, Vector2 position, SpriteBatch spriteBatch)
{
this.Position = position;
this.Texture = texture;
this.SpriteBatch = spriteBatch;
}
public virtual void Draw()
{
SpriteBatch.Draw(Texture, Position, Color.White);
}
}
My Chunk.cs
class Chunk : Sprite
{
public Tile[,] Tiles { get; set; }
public const int CHUNK_SIZE = 25;
public Chunk(Texture2D texture, Vector2 position, SpriteBatch spriteBatch) : base(texture, position, spriteBatch)
{
Tiles = new Tile[CHUNK_SIZE, CHUNK_SIZE];
GenerateChunks();
}
public void GenerateChunks()
{
for (int x = 0; x < CHUNK_SIZE; x++)
{
for (int y = 0; y < CHUNK_SIZE; y++)
{
Vector2 tilePos = new Vector2(Texture.Width * x, Texture.Height * y);
Tiles[x, y] = new Tile(Texture, Position, SpriteBatch);
}
}
}
public override void Draw()
{
foreach (var tile in Tiles)
tile.Draw();
}
}
And my World.cs
class World
{
float NoiseValues { get; set; }
public Chunk[,] Chunks { get; set; }
public SpriteBatch SpriteBatch { get; set; }
public Texture2D Texture { get; set; }
public int Height { get; set; }
public int Width { get; set; }
public const int CHUNK_TILE_SIZE = 400;
private float NoiseScale = 0.1f; //Smaller the scale the better caves
private Random Random;
public World(SpriteBatch spriteBatch, Texture2D texture, int x, int y)
{
this.Height = y;
this.Width = x;
this.SpriteBatch = spriteBatch;
this.Texture = texture;
Chunks = new Chunk[x, y];
Random = new Random();
Simplex.Noise.Seed = Random.Next();
GenerateBasicWorld();
}
public void GenerateBasicWorld()
{
for (int x = 0; x < this.Width; x++)
{
for (int y = 0; y < this.Height; y++)
{
NoiseValues = Simplex.Noise.CalcPixel2D(x, y, NoiseScale); //Just a simplex noise for further generation doesn't affect current gen
Vector2 chunkPos = new Vector2(16 * x, 16 * y);
Chunks[x, y] = new Chunk(Texture, chunkPos, SpriteBatch);
System.Diagnostics.Debug.WriteLine(NoiseValues);
}
}
}
public void Draw()
{
foreach (var chunk in Chunks)
chunk.Draw();
}
}
My initialization goes like this:
World world = new World(spriteBatch, tilesTexture[0], 5, 5);
What I'm currently getting is 5x5 tile set drawn.
I'm expecting to get 5 chunks on X axis and 5 chunks on Y axis, each chunk has to be made of 25 tiles, am I missing something out? Thanks.