Exemplo n.º 1
0
        // Use this for external initialization
        void Start()
        {
            int x = Mathf.RoundToInt(transform.position.x + 20.01f);
            int y = Mathf.RoundToInt(transform.position.y);
            int z = Mathf.RoundToInt(transform.position.z + 20.01f);

            cityManager = NavigationCityManager.Instance;

            Transform child;

            if (recursionLevel == 0)
            {
                if (!cityManager.CheckSlot(x, y, z))
                {
                    int meshNum = myProfile.groundBlocks.Length;
                    int matNum  = myProfile.groundMaterials.Length;
                    myMesh     = myProfile.groundBlocks[Random.Range(0, meshNum)];
                    myMaterial = myProfile.groundMaterials[Random.Range(0, matNum)];
                    cityManager.SetSlot(x, y, z, true);
                }
                else
                {
                    Destroy(gameObject);
                }
            }

            myMeshFilter.mesh   = myMesh;
            myRenderer.material = myMaterial;

            if (recursionLevel < maxLevel)
            {
                if (recursionLevel == maxLevel - 1)
                {
                    if (!cityManager.CheckSlot(x, y + 1, z))
                    {
                        child = Instantiate(basePrefab, transform.position + Vector3.up * 1.05f, Quaternion.identity, this.transform);
                        int meshNum = myProfile.roofBlocks.Length;
                        int matNum  = myProfile.roofMaterials.Length;
                        child.GetComponent <NavTowerBlock>().Initialize(recursionLevel + 1, myProfile.roofMaterials[Random.Range(0, matNum)], myProfile.roofBlocks[Random.Range(0, meshNum)]);

                        cityManager.SetSlot(x, y + 1, z, true);
                    }
                }
                else
                {
                    if (!cityManager.CheckSlot(x, y + 1, z))
                    {
                        child = Instantiate(basePrefab, transform.position + Vector3.up * 1.05f, Quaternion.identity, this.transform);
                        int meshNum = myProfile.mainBlocks.Length;
                        int matNum  = myProfile.mainMaterials.Length;
                        child.GetComponent <NavTowerBlock>().Initialize(recursionLevel + 1, myProfile.mainMaterials[Random.Range(0, matNum)], myProfile.mainBlocks[Random.Range(0, meshNum)]);

                        cityManager.SetSlot(x, y + 1, z, true);
                    }
                }
            }
        }
        // Use this for internal initialization
        void Awake()
        {
            if (_instance == null)
            {
                _instance = this;
            }

            else
            {
                Destroy(gameObject);
                Debug.LogError("Multiple NavigationCityManager instances in Scene. Destroying clone!");
            };
        }
Exemplo n.º 3
0
        // Use this for external initialization
        void Start()
        {
            int x = Mathf.RoundToInt(transform.position.x + 20.0f);
            int y = Mathf.RoundToInt(transform.position.y);

            if (mySlope == -1)
            {
                y = Mathf.RoundToInt(transform.position.y - 1f);
            }

            int z = Mathf.RoundToInt(transform.position.z + 20.0f);

            navCityManager  = NavigationCityManager.Instance;
            tilePrefab      = navCityManager.tilePrefab;
            slopeUpPrefab   = navCityManager.slopeUpPrefab;
            slopeDownPrefab = navCityManager.slopeDownPrefab;

            Transform child;

            if (recursionCount == 0)         //initial tile "crossroads"
            {
                for (int nextRotation = 0; nextRotation < 360; nextRotation += 90)
                {
                    SetIncrements(nextRotation, 0);

                    Vector3    incVector = Vector3.right * inc_x + Vector3.up * inc_y + Vector3.forward * inc_z;
                    Quaternion nextQuat  = Quaternion.Euler(0f, nextRotation, 0f);

                    if (!navCityManager.CheckSlot(x + inc_x, y + inc_y, z + inc_z))
                    {
                        navCityManager.SetSlot(x + inc_x, y + inc_y, z + inc_z, true);
                        child        = Instantiate(tilePrefab, transform.position + incVector, nextQuat);
                        child.parent = this.transform;
                        child.GetComponent <RouteTile>().Initialize(recursionCount + 1, 0, nextRotation);
                    }
                    else
                    {
                        GameObject.Destroy(gameObject);
                    }
                }
            }
            else if (recursionCount < maxLevel)
            {
                int corrector = (maxLevel - recursionCount) / 15; //biasing randomization in the beginning to avoid extinction
                int random    = Random.Range(0, 100);
                if (random < (80 + corrector))                    //move forward (most likely):
                {
                    int nextRotation = myRotation;

                    random = Random.Range(0, 100);
                    if ((random > 80) && (mySlope == 0)) //going up
                    {
                        SetIncrements(nextRotation, mySlope);

                        Vector3    incVector = Vector3.right * inc_x + Vector3.up * inc_y + Vector3.forward * inc_z;
                        Quaternion nextQuat  = Quaternion.Euler(0f, nextRotation, 0f);

                        if (!navCityManager.CheckSlot(x + inc_x, y + inc_y, z + inc_z))
                        {
                            navCityManager.SetSlot(x + inc_x, y + inc_y, z + inc_z, true);
                            child        = Instantiate(slopeUpPrefab, transform.position + incVector, nextQuat);
                            child.parent = this.transform;
                            child.GetComponent <RouteTile>().Initialize(recursionCount + 1, 1, nextRotation);
                        }
                        else
                        {
                            GameObject.Destroy(gameObject);
                        }
                    }
                    else if ((random < 20) && (mySlope == 0)) //going down
                    {
                        SetIncrements(nextRotation, mySlope);

                        Vector3    incVector = Vector3.right * inc_x + Vector3.up * inc_y + Vector3.forward * inc_z;
                        Quaternion nextQuat  = Quaternion.Euler(0f, nextRotation, 0f);

                        if (!navCityManager.CheckSlot(x + inc_x, y + inc_y, z + inc_z))
                        {
                            navCityManager.SetSlot(x + inc_x, y + inc_y, z + inc_z, true);
                            child        = Instantiate(slopeDownPrefab, transform.position + incVector, nextQuat);
                            child.parent = this.transform;
                            child.GetComponent <RouteTile>().Initialize(recursionCount + 1, -1, nextRotation);
                        }
                        else
                        {
                            GameObject.Destroy(gameObject);
                        }
                    }
                    else //flat path
                    {
                        SetIncrements(nextRotation, mySlope);

                        Vector3    incVector = Vector3.right * inc_x + Vector3.up * inc_y + Vector3.forward * inc_z;
                        Quaternion nextQuat  = Quaternion.Euler(0f, nextRotation, 0f);

                        if (!navCityManager.CheckSlot(x + inc_x, y + inc_y, z + inc_z))
                        {
                            navCityManager.SetSlot(x + inc_x, y + inc_y, z + inc_z, true);
                            child        = Instantiate(tilePrefab, transform.position + incVector, nextQuat);
                            child.parent = this.transform;
                            child.GetComponent <RouteTile>().Initialize(recursionCount + 1, 0, nextRotation);
                        }
                        else
                        {
                            GameObject.Destroy(gameObject);
                        }
                    }
                }
                //end move forward
                //if tile is not sloped, we can make a left or right split every once in a while. let's keep it as simple as possible:
                random = Random.Range(0, 100);
                if (((random + corrector) > 70) && (mySlope == 0))
                {
                    int nextRotation = myRotation;
                    random = Random.Range(0, 100);
                    if (random > 50) //left turn
                    {
                        nextRotation = (int)Mathf.Repeat(nextRotation - 90, 360);
                    }
                    else  //right turn
                    {
                        nextRotation = (int)Mathf.Repeat(nextRotation + 90, 360);
                    }
                    SetIncrements(nextRotation, 0);

                    Vector3    incVector = Vector3.right * inc_x + Vector3.up * inc_y + Vector3.forward * inc_z;
                    Quaternion nextQuat  = Quaternion.Euler(0f, nextRotation, 0f);

                    if (!navCityManager.CheckSlot(x + inc_x, y + inc_y, z + inc_z))
                    {
                        navCityManager.SetSlot(x + inc_x, y + inc_y, z + inc_z, true);
                        child        = Instantiate(tilePrefab, transform.position + incVector, nextQuat);
                        child.parent = this.transform;
                        child.GetComponent <RouteTile>().Initialize(recursionCount + 1, 0, nextRotation);
                    }
                    else
                    {
                        GameObject.Destroy(gameObject);
                    }
                }
            }
        }