Ejemplo n.º 1
0
    // Adds a tree on top of stack if certain criteria are met
    void AddTree(CellStack cellStack)
    {
        if (cellStack.Count() > terrain.waterLevel && (cellStack.Peek() == CellType.Grass || cellStack.Peek() == CellType.Dirt))
        {
            Vector2Int offset = cellStack.indexWithinChunk;

            Vector3 treePos = transform.position + new Vector3(offset.x * HexMetrics.innerRadius * 2 + offset.y % 2 * HexMetrics.innerRadius,
                                                               transform.localPosition.y + HexMetrics.height * cellStack.Count(),
                                                               offset.y * HexMetrics.outerRadius * 1.5f);

            // Randomly rotate the tree so it looks less uniform
            float   rotation = UnityEngine.Random.Range(0, 359);
            Vector3 treeRot  = new Vector3(0, rotation, 0);

            TerrainObject tree = GameObject.Instantiate(treePrefab);
            tree.Init(cellStack.coordinates, offset);
            tree.transform.SetParent(transform);
            tree.transform.position            = treePos;
            tree.transform.rotation            = Quaternion.Euler(treeRot);
            terrainObjects[offset.x, offset.y] = tree;
        }
    }