Beispiel #1
0
    public void generateSectionsInView(int centredSection)
    {
        float halfWidth = (sectionWidth / 2);

        //Currently set to 3 sections in view
        //Centred section and the map section either side of it will be generated
        int[] sectionIndices = new int[] {
            centredSection != 0 ? centredSection - 1 : totalSections - 1,
            centredSection,
            centredSection != totalSections - 1 ? centredSection + 1 : 0
        };

        int initialX = -sectionWidth;

        for (int i = 0; i < sectionIndices.Length; i++)
        {
            int[,] tileMapping;
            int index = sectionIndices[i];
            //if section not in save data, create new section
            if (!planetTileMappings.ContainsKey(index))
            {
                generateSection(index);
            }
            tileMapping = planetTileMappings[index];

            //building section
            //Generate in instantiated clone of the section prefab
            GameObject instantiatedSection = (GameObject)Instantiate(sectionPrefab, new Vector3(initialX + (sectionWidth * i), 0, 0), Quaternion.identity, tileMapContainer.transform);
            sectionBuilder.buildSection(tileMapping, instantiatedSection, tiles);
            instantiatedSection.name = "Section-" + index;

            //set the boundaries, for the player to cross to load next and delete furthest sections, to the edges of the centred section
            if (index == centredSection)
            {
                xBoundaries[0] = instantiatedSection.transform.position.x - halfWidth;
                xBoundaries[1] = instantiatedSection.transform.position.x + halfWidth;
            }

            //store for later use (loading new sections, deleting old)
            sections.Add(new Section(index, instantiatedSection));
        }
    }