1
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ How do you set the position of the Chunk? \$\endgroup\$
    – Jay
    Commented Feb 9, 2019 at 5:53
  • \$\begingroup\$ @Jay Vector2 chunkPos = new Vector2(TileSize * TileAmountInChunk * x, TileSize * TileAmountInChunk * y); \$\endgroup\$
    – legendree
    Commented Feb 9, 2019 at 7:26
  • \$\begingroup\$ Have you checked if the chunks draw all over eachother? I have the same concern as Jay, I see you pass position as parameter in the chunk- however it is not used. Unless the code is different than you posted? \$\endgroup\$
    – Felsir
    Commented Feb 9, 2019 at 20:33
  • \$\begingroup\$ @Felsir I’ll recheck it as soon as I get home \$\endgroup\$
    – legendree
    Commented Feb 9, 2019 at 21:31

1 Answer 1

1
\$\begingroup\$

Ok I solved it by adding in the size of each chunk not overwriting, @Jay & @Felsir were right about me, redrawing the tiles on each other, thats the final result I'm having in my Chunk.cs

        public void GenerateChunks()
        {
            for (int x = 0; x < CHUNK_SIZE; x++)
            {
                for (int y = 0; y < CHUNK_SIZE; y++)
                {
                    Vector2 newPosition = new Vector2(Texture.Width * x, Texture.Height * y);
                    newPosition += Position;
                    Tiles[x, y] = new Tile(Texture, newPosition, SpriteBatch);

                }
            }
        }
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .