/// <summary>
        /// Tries to spawn a child chunk.
        /// </summary>
        /// <returns>The spawn chunk.</returns>
        /// <param name="x">The x coordinate.</param>
        /// <param name="y">The y coordinate.</param>
        /// <param name="z">The z coordinate.</param>
        public Chunk TrySpawnChunk(int x, int y, int z)
        {
            // create the key
            string key = GenerateKey(x, y, z);

            // exit if the key already exists
            if (ChunkDict.ContainsKey(key))
            {
                return(null);
            }

            // spawn the chunk, and parent it
            Chunk newChunk = Instantiate(_chunkPrefab, transform);

            // generate and set the position
            Vector3 localPos = LocalisedPositionFromGroupCoord(x, y, z);

            newChunk.transform.localPosition = localPos;

            // set basic values
            // name
            newChunk.name = "Chunk_" + key;
            // parent
            newChunk.Parent = this;
            // group coord
            newChunk.GroupCoord = new Vector3Int(x, y, z);
            newChunk.SetChunkVars(ChunkSize);

            ChunkDict.Add(key, newChunk);

            return(newChunk);
        }
Exemple #2
0
    void Start()
    {
        seed = Random.Range(1000, 10000);

        loaded    = new ChunkDict();
        generated = new ChunkDict();

        //GenerateChunksAround(0, 0);
        LoadChunksAround(0, 0);
        //GenerateChunk(0, 0);
        //LoadChunk(0, 0);
        //LoadChunk(0, 1);
        //LoadChunk(1, 1);
        //LoadChunk(1, 0);
        //LoadChunk(-1, 0);
    }
        /// <summary>
        /// Gets the <see cref="UnityTools.ChunkGroup`1"/> with the specified key.
        ///
        /// Primary access for Chunks via the ChunkGroup.
        /// </summary>
        /// <param name="key">Key.</param>
        public Chunk this[string key]
        {
            get
            {
                if (!ChunkDict.ContainsKey(key))
                {
                    string[] xyz = key.Split(',');
                    //                    Debug.Log();
                    throw new NoChunkException(
                              string.Format("No Chunk present at key [{0}]", key),
                              new Vector3Int(
                                  int.Parse(xyz[0]),
                                  int.Parse(xyz[1]),
                                  int.Parse(xyz[2])
                                  )
                              );
                }

                return(ChunkDict[key]);
            }
        }