Ejemplo n.º 1
0
    // Method that generates an object on the terrain based on the inputs
    void GenerateObject(Vector3 position, Quaternion rotation, Vector3 scale, int biome, int objIndex, GridTile gridTile, Chunk chunk)
    {
        // create a TerrainObject
        TerrainObject curterrainObject;

        // Get the gameobjectList of this terrain object
        List <GameObject> curGameObjectList = worldObjects[biome][objIndex];

        // Determine the count
        int count = curGameObjectList.Count;

        // If the gameobject has more vertices than the maximum allowed reload the mesh and set it to full
        if (curGameObjectList[count - 1].GetComponent <TerrainObject>().verticesNow > curGameObjectList[count - 1].GetComponent <TerrainObject>().vertexMax)
        {
            curGameObjectList[count - 1].GetComponent <TerrainObject>().Reload();
        }

        // if the count is lower than 2 or the current gameobject is full add a new gameobject to the list
        if (curGameObjectList.Count < 2 || curGameObjectList[count - 1].GetComponent <TerrainObject>().isFull)
        {
            // Add another instance of the prefab (on index 0) to the list
            GameObject obj = Instantiate(curGameObjectList[0]);
            curGameObjectList.Add(obj);
            chunk.AddTerrainObject(obj.GetComponent <TerrainObject>());
            tempCount = 0;
            // recalculate the count
            count = curGameObjectList.Count;

            // Set the parent of the gameobject to this gameobject
            curGameObjectList[count - 1].transform.parent = transform;

            // Get the terrainobject component
            curterrainObject = curGameObjectList[count - 1].GetComponent <TerrainObject>();

            if (curterrainObject.GetComponent <AnimationParameters>() != null)
            {
                treeCounter++;
                TreeAnimationController.instance.treeObjects.Add(curterrainObject);
                TreeAnimationController.instance.lowestVertPerObject.Add(new List <float>());
            }

            // Add the biome and object index to this
            curterrainObject.biome    = biome;
            curterrainObject.objectNR = objIndex;
        }
        tempCount++;
        // Get the terrainobject component of the last gameobject in the list
        curterrainObject = curGameObjectList[count - 1].GetComponent <TerrainObject>();

        if (curterrainObject.GetComponent <AnimationParameters>() != null)
        {
            List <List <float> > lowestVertPerObject = TreeAnimationController.instance.lowestVertPerObject;
            TreeAnimationController.instance.lowestVertPerObject[treeCounter].Add(position.y);
        }

        // Get the submeshes of the object that needs to be placed
        Mesh[] subMeshes = biomeMeshes[biome].mesh[objIndex];

        // Loop trough the submeshes
        for (int i = 0; i < subMeshes.Length; i++)
        {
            // Add the submesh, moved to the right spot, scale and rotation, to the newcomponents list
            MoveMesh(curterrainObject, subMeshes[i], i, position, Quaternion.Euler(new Vector3(-90, 0, 0) + rotation.eulerAngles), scale);

            // Determine the vertex count of the total mesh
            curterrainObject.verticesNow += subMeshes[i].vertexCount;
        }


        // Set the occupant on the gridtile to this generated object
        GridTileOccupant gridOccupant = new GridTileOccupant(curterrainObject.gameObject, position, Quaternion.Euler(new Vector3(-90, 0, 0) + rotation.eulerAngles), scale, GridTileOccupant.OccupantType.TerrainGenerated);

        gridTile.AddOccupant(gridOccupant);
    }