Ejemplo n.º 1
0
        public WorldChunk GetChunk(Point chunkPosition)
        {
            if (_chunks.TryGetValue(chunkPosition, out var chunk))
            {
                return(chunk);
            }

            chunk = new WorldChunk(chunkPosition);
            _chunks.Add(chunkPosition, chunk);
            return(chunk);
        }
Ejemplo n.º 2
0
        public void UpdateChunk(WorldChunk worldChunk)
        {
            if (!worldChunk.IsDirty)
            {
                return;
            }

            if (_vertexBufferCache.TryGetValue(worldChunk.Position, out var worldChunkVertexBuffer))
            {
                worldChunkVertexBuffer.Dispose();
                _vertexBufferCache.Remove(worldChunk.Position);
            }

            worldChunkVertexBuffer = BuildVertexBuffer(worldChunk);
            _vertexBufferCache.Add(worldChunk.Position, worldChunkVertexBuffer);
        }
Ejemplo n.º 3
0
        private VertexBuffer BuildVertexBuffer(WorldChunk worldChunk)
        {
            var landVertices = new List <VertexPositionColorTexture>();

            const int TileSize     = 44;
            const int TileSizeHalf = TileSize / 2;

            void DrawSquare(int landId, int x, int y, float tileZ, float tileZEast, float tileZWest, float tileZSouth)
            {
                /*
                 * var tile = tileZ == 0
                 *  ? _textureAtlas.GetLandTile(landId)
                 *  : _textureAtlas.GetLandTextureTile(landId);
                 */

                /*
                 * var tile = tileZ != 0
                 *  ? _textureAtlas.GetLandTile(landId)
                 *  : _textureAtlas.GetLandTextureTile(landId);
                 */

                /* uncomment when atlas is in
                 * var p0 = new Vector3(x + TileSizeHalf, y - tileZ * 4, tile.Uvws.V1.W);
                 * var p1 = new Vector3(x + TileSize, y + TileSizeHalf - tileZEast * 4, tile.Uvws.V2.W);
                 * var p2 = new Vector3(x, y + TileSizeHalf - tileZWest * 4, tile.Uvws.V3.W);
                 * var p3 = new Vector3(x + TileSizeHalf, y + TileSize - tileZSouth * 4, tile.Uvws.V4.W);
                 */

                var p0 = new Vector3(x + TileSizeHalf, y - tileZ * 4, 0);
                var p1 = new Vector3(x + TileSize, y + TileSizeHalf - tileZEast * 4, 0);
                var p2 = new Vector3(x, y + TileSizeHalf - tileZWest * 4, 0);
                var p3 = new Vector3(x + TileSizeHalf, y + TileSize - tileZSouth * 4, 0);

                /* uncomment when atlas is in
                 * var uv0 = new Vector2(tile.Uvws.V1.U, tile.Uvws.V1.V);
                 * var uv1 = new Vector2(tile.Uvws.V2.U, tile.Uvws.V2.V);
                 * var uv2 = new Vector2(tile.Uvws.V3.U, tile.Uvws.V3.V);
                 * var uv3 = new Vector2(tile.Uvws.V4.U, tile.Uvws.V4.V);
                 */
                var uv0 = Vector2.Zero;
                var uv1 = Vector2.Zero;
                var uv2 = Vector2.Zero;
                var uv3 = Vector2.Zero;

                var n1 = Vector3.Cross(p2 - p0, p1 - p0);

                landVertices.Add(new VertexPositionColorTexture(p0, Color.Red, uv0));
                landVertices.Add(new VertexPositionColorTexture(p1, Color.Red, uv1));
                landVertices.Add(new VertexPositionColorTexture(p2, Color.Red, uv2));

                var n2 = Vector3.Cross(p2 - p3, p1 - p3);

                landVertices.Add(new VertexPositionColorTexture(p1, Color.Black, uv1));
                landVertices.Add(new VertexPositionColorTexture(p3, Color.Green, uv3));
                landVertices.Add(new VertexPositionColorTexture(p2, Color.Black, uv2));
            }

            for (var x = 0; x < ChunkData.ChunkSize; ++x)
            {
                for (var y = 0; y < ChunkData.ChunkSize; ++y)
                {
                    var staticTile = worldChunk.GetStaticTile(x, y);

                    var tileZ      = worldChunk.GetZ(x, y);
                    var tileZEast  = worldChunk.GetZ(x + 1, y);
                    var tileZWest  = worldChunk.GetZ(x, y + 1);
                    var tileZSouth = worldChunk.GetZ(x + 1, y + 1);

                    DrawSquare(staticTile.TileId, (x - y) * TileSizeHalf, (x + y) * TileSizeHalf, tileZ, tileZEast, tileZWest, tileZSouth);
                }
            }

            var vertexBuffer = new VertexBuffer(
                _graphicsDevice,
                typeof(VertexPositionColorTexture),
                landVertices.Count,
                BufferUsage.WriteOnly
                );

            vertexBuffer.SetData(landVertices.ToArray());
            return(vertexBuffer);
        }