Example #1
0
        // Update is called once per frame
        private void Update()
        {
            // Calculate the tile the target is standing on
            var p = new Int2(Mathf.RoundToInt((target.position.x - tileSize * 0.5f) / tileSize),
                             Mathf.RoundToInt((target.position.z - tileSize * 0.5f) / tileSize));

            // Clamp range
            range = range < 1 ? 1 : range;

            // Remove tiles which are out of range
            var changed = true;

            while (changed)
            {
                changed = false;
                foreach (var pair in tiles)
                {
                    if (Mathf.Abs(pair.Key.x - p.x) > range || Mathf.Abs(pair.Key.y - p.y) > range)
                    {
                        pair.Value.Destroy();
                        tiles.Remove(pair.Key);
                        changed = true;
                        break;
                    }
                }
            }

            // Add tiles which have come in range
            // and start calculating them
            for (var x = p.x - range; x <= p.x + range; x++)
            {
                for (var z = p.y - range; z <= p.y + range; z++)
                {
                    if (!tiles.ContainsKey(new Int2(x, z)))
                    {
                        var tile      = new ProceduralTile(this, x, z);
                        var generator = tile.Generate();
                        // Tick it one step forward
                        generator.MoveNext();
                        // Calculate the rest later
                        tileGenerationQueue.Enqueue(generator);
                        tiles.Add(new Int2(x, z), tile);
                    }
                }
            }

            // The ones directly adjacent to the current one
            // should always be completely calculated
            // make sure they are
            for (var x = p.x - disableAsyncLoadWithinRange; x <= p.x + disableAsyncLoadWithinRange; x++)
            {
                for (var z = p.y - disableAsyncLoadWithinRange; z <= p.y + disableAsyncLoadWithinRange; z++)
                {
                    tiles[new Int2(x, z)].ForceFinish();
                }
            }
        }
    // Update is called once per frame
    void Update()
    {
        // Calculate the tile the target is standing on
        Int2 p = new Int2(Mathf.RoundToInt((target.position.x - tileSize * 0.5f) / tileSize), Mathf.RoundToInt((target.position.z - tileSize * 0.5f) / tileSize));

        // Clamp range
        range = range < 1 ? 1 : range;

        // Remove tiles which are out of range
        bool changed = true;

        while (changed)
        {
            changed = false;
            foreach (KeyValuePair <Int2, ProceduralTile> pair in tiles)
            {
                if (Mathf.Abs(pair.Key.x - p.x) > range || Mathf.Abs(pair.Key.y - p.y) > range)
                {
                    pair.Value.Destroy();
                    tiles.Remove(pair.Key);
                    changed = true;
                    break;
                }
            }
        }

        // Add tiles which have come in range
        // and start calculating them
        for (int x = p.x - range; x <= p.x + range; x++)
        {
            for (int z = p.y - range; z <= p.y + range; z++)
            {
                if (!tiles.ContainsKey(new Int2(x, z)))
                {
                    ProceduralTile tile = new ProceduralTile(this, x, z);
                    StartCoroutine(tile.Generate());
                    tiles.Add(new Int2(x, z), tile);
                }
            }
        }

        // The ones directly adjacent to the current one
        // should always be completely calculated
        // make sure they are
        for (int x = p.x - 1; x <= p.x + 1; x++)
        {
            for (int z = p.y - 1; z <= p.y + 1; z++)
            {
                tiles[new Int2(x, z)].ForceFinish();
            }
        }
    }
        // Update is called once per frame
        void Update () {

            // Calculate the tile the target is standing on
            Int2 p = new Int2 ( Mathf.RoundToInt ((target.position.x - tileSize*0.5f) / tileSize), Mathf.RoundToInt ((target.position.z - tileSize*0.5f) / tileSize) );

            // Clamp range
            range = range < 1 ? 1 : range;

            // Remove tiles which are out of range
            bool changed = true;
            while ( changed ) {
                changed = false;
                foreach (KeyValuePair<Int2,ProceduralTile> pair in tiles ) {
                    if ( Mathf.Abs (pair.Key.x-p.x) > range || Mathf.Abs (pair.Key.y-p.y) > range ) {
                        pair.Value.Destroy ();
                        tiles.Remove ( pair.Key );
                        changed = true;
                        break;
                    }
                }
            }

            // Enqueue tiles which have come in range
            // and start calculating them
            for ( int x = p.x-range; x <= p.x+range; x++ ) {
                for ( int z = p.y-range; z <= p.y+range; z++ ) {
                    if ( !tiles.ContainsKey ( new Int2(x,z) ) ) {
                        ProceduralTile tile = new ProceduralTile ( this, x, z );
                        var generator = tile.Generate ();
                        // Tick it one step forward
                        generator.MoveNext ();
                        // Calculate the rest later
                        tileGenerationQueue.Enqueue (generator);
                        tiles.Add ( new Int2(x,z), tile );
                    }
                }
            }

            // The ones directly adjacent to the current one
            // should always be completely calculated
            // make sure they are
            for ( int x = p.x-1; x <= p.x+1; x++ ) {
                for ( int z = p.y-1; z <= p.y+1; z++ ) {
                    tiles[new Int2(x,z)].ForceFinish();
                }
            }

        }