예제 #1
0
파일: HexGrid.cs 프로젝트: orlogan/UnityCat
    //instantiates a hex cell with parent and location and coordinates
    void CreateCell(int x, int z, int i)
    {
        //position
        Vector3 position;

        position.x = (x + z * 0.5f - (z / 2)) * (HexMetrics.innerRadius * 2f);
        position.y = 0f;
        position.z = z * (HexMetrics.outerRadius * 1.5f);

        //gives position a cell with the needed parent and position
        HexCell cell = cells[i] = Instantiate <HexCell>(cellPrefab);

        cell.transform.SetParent(transform, false);
        cell.transform.localPosition = position;
        cell.coordinates             = HexCoordinate.FromOffsetCoordinates(x, z);
        cell.color = defaultColor;


        //gives coordinates
        Text label = Instantiate <Text>(cellLabelPrefab);

        label.rectTransform.SetParent(gridCanvas.transform, false);
        label.rectTransform.anchoredPosition = new Vector2(position.x, position.z);
        label.text = cell.coordinates.ToStringSeperateLines();
    }
예제 #2
0
    private void createCell(int x, int z, int i)
    {
        // set position
        Vector3 position;

        position.x = (x + z * 0.5f - z / 2) * (HexMetrics.InnerRadius * 2f);
        position.y = 0;
        position.z = z * (HexMetrics.OuterRadius * 1.5f);

        // create cell game object
        HexCell cell = cells[i] = Instantiate <HexCell>(cellPrefab);

        cell.transform.localPosition = position;
        cell.coordinate = HexCoordinate.FromOffsetCoordinates(x, z);
        cell.Index      = i;
        cell.ShaderData = cellShaderData;

        cell.Explorable =
            x > 0 && z > 0 && x < cellCountX - 1 && z < cellCountZ - 1;

        // register neighbors
        if (x > 0)
        {
            cell.SetNeighbor(HexDirection.W, cells[i - 1]);
        }
        if (z > 0)
        {
            if ((z & 1) == 0)
            {
                cell.SetNeighbor(HexDirection.SE, cells[i - cellCountX]);
                if (x > 0)
                {
                    cell.SetNeighbor(HexDirection.SW, cells[i - cellCountX - 1]);
                }
            }
            else
            {
                cell.SetNeighbor(HexDirection.SW, cells[i - cellCountX]);
                if (x < cellCountX - 1)
                {
                    cell.SetNeighbor(HexDirection.SE, cells[i - cellCountX + 1]);
                }
            }
        }

        // mark label of coordinate
        TextMeshProUGUI label = Instantiate <TextMeshProUGUI>(cellLabelPrefab);

        label.rectTransform.anchoredPosition = new Vector2(position.x, position.z);
        cell.uiRect = label.rectTransform;

        cell.Elevation = 0;
        addCellToChunk(x, z, cell);
    }
예제 #3
0
    private static TileObject GenerateTile(Tile tile)
    {
        int   x = tile.x;
        int   z = tile.y;
        float e = 0f;

        if (tile.elevation == Elevation.Flat)
        {
            e = 0.5f;
        }
        else if (tile.elevation == Elevation.Hill)
        {
            e = 0.6f;
        }
        else if (tile.elevation == Elevation.Mountain)
        {
            e = 1f;
        }
        if (tile.terrainType == "TERRAIN_OCEAN")
        {
            e = 0f;
        }
        if (tile.terrainType == "TERRAIN_COAST")
        {
            e = 0.1f;
        }

        Vector3 position = new Vector3
        {
            x = (x + z * 0.5f - z / 2) * InnerRadius * 2f,
            y = e,
            z = z * OuterRadius * 1.5f
        };

        float a = (float)x / Config.GameSave.width;
        float b = (float)z / Config.GameSave.height;
        float m = Mathf.PerlinNoise(a + 1, b + 1);

        TileObject worldTile = new GameObject("Tile").AddComponent <TileObject>();

        worldTile.transform.SetParent(instance.tilesRoot);
        worldTile.transform.localPosition = position;
        worldTile.coordinate = HexCoordinate.FromOffsetCoordinates(x, z);

        if (tile.terrainType == "TERRAIN_OCEAN")
        {
            worldTile.color = Color.blue;
        }
        if (tile.terrainType == "TERRAIN_SNOW")
        {
            worldTile.color = Color.white;
        }
        if (tile.terrainType == "TERRAIN_COAST")
        {
            worldTile.color = Color.cyan;
        }
        if (tile.terrainType == "TERRAIN_GRASS")
        {
            worldTile.color = Color.green;
        }
        if (tile.terrainType == "TERRAIN_PLAINS")
        {
            worldTile.color = Color.green;
        }
        if (tile.terrainType == "TERRAIN_DESERT")
        {
            worldTile.color = Color.yellow;
        }
        if (tile.terrainType == "TERRAIN_TUNDRA")
        {
            worldTile.color = Color.gray;
        }
        if (tile.elevation == Elevation.Mountain)
        {
            worldTile.color *= 0.5f;
        }

        return(worldTile);
    }