/// <summary> /// Creates a new world manager. /// </summary> /// <param name="world">The world to manage.</param> /// <param name="initialPosition">The initial position to begin generation around.</param> public WorldManager( World world, Vector3 initialPosition ) { _world = world; _chunks = new Dictionary<Vector3, Chunk>(); // create some initial chunks (should take less than a second on most systems) const int initialSquareSize = 2; Vector3 center = new Vector3(); int minX = (int)( initialPosition.X * ChunkData.InverseSizeXZ - initialSquareSize ), maxX = (int)( initialPosition.X * ChunkData.InverseSizeXZ + initialSquareSize ), minZ = (int)( initialPosition.Z * ChunkData.InverseSizeXZ - initialSquareSize ), maxZ = (int)( initialPosition.Z * ChunkData.InverseSizeXZ + initialSquareSize ); for ( int x = minX; x <= maxX; ++x ) { for ( int z = minZ; z <= maxZ; ++z ) { center.X = x * ChunkData.SizeXZ; center.Z = z * ChunkData.SizeXZ; LoadChunk( ref center ); } } // begin the threads ShouldBeRunning = true; _threadLoad = new Thread( new ThreadStart( MaintainChunks ) ); _threadLoad.Start(); }
/// <summary> /// Creates a new chunk. /// </summary> /// <param name="world">The world this chunk belongs to.</param> /// <param name="index">The index of the chunk.</param> /// <param name="generateTerrain">True to generate some initial terrain, false to leave each block empty.</param> private Chunk( World world, Vector3 index, bool generateTerrain ) { _world = world; _data = new ChunkData( index ); _terrain = new VoxelBuffer(); // create bounds var sizeOffs = ChunkData.SizeXZ * 0.5f - 0.5f; Vector3 boundsMin = new Vector3( index.X - sizeOffs, -0.5f, index.Z - sizeOffs ); Vector3 boundsMax = new Vector3( index.X + sizeOffs, 0.5f + ChunkData.SizeY, index.Z + sizeOffs ); _bounds = new BoundingBox( boundsMin, boundsMax ); _octree = new ChunkOctree( _bounds ); // check if we need to populate if ( generateTerrain ) { PopulateTerrain(); } }
/// <summary> /// Creates the world instance if it does not already exist. /// </summary> /// <returns></returns> public static World CreateInstance( int seed ) { if ( _instance == null ) { _instance = new World( seed ); } return _instance; }
/// <summary> /// Creates a new world renderer. /// </summary> /// <param name="world">The world to render.</param> public WorldRenderer( World world ) { _graphicsDevice = Game.Instance.GraphicsDevice; _graphicsDevice.SamplerStates[ 0 ] = SamplerState.PointClamp; _graphicsDevice.BlendState = BlendState.Opaque; _world = world; _blockRenderEffect = new BlockRenderEffect(); ClearColor = Color.Gray; }
/// <summary> /// Creates a new chunk. /// </summary> /// <param name="world">The world this chunk belongs to.</param> /// <param name="index">The index of the chunk.</param> public Chunk( World world, Vector3 index ) : this(world, index, true) { }