Exemple #1
0
    /// <summary>
    /// Gets hexagon neighbours. starting from the top left corner, in  inverse trigo direction.
    /// </summary>
    /// <returns>Array of 6 neighbours. Some can be null!</returns>
    /// <param name="coordinate">Coordinate of the hexagon</param>
    /// <param name="hexas">array of hexagon used to get the neighbours from</param>
    public static Hexagon[] GetNeighbours(Vector2i coordinate, HexTerrainData hexaData)
    {
        Hexagon[] neighbours = new Hexagon[6];

        if (hexaData.Contains(coordinate.x - 1, coordinate.y + 1))
        {
            neighbours[0] = hexaData[coordinate.y + 1, coordinate.x - 1];
        }
        if (hexaData.Contains(coordinate.x, coordinate.y + 1))
        {
            neighbours[1] = hexaData[coordinate.y + 1, coordinate.x];
        }
        if (hexaData.Contains(coordinate.x + 1, coordinate.y))
        {
            neighbours[2] = hexaData[coordinate.y, coordinate.x + 1];
        }
        if (hexaData.Contains(coordinate.x + 1, coordinate.y - 1))
        {
            neighbours[3] = hexaData[coordinate.y - 1, coordinate.x + 1];
        }
        if (hexaData.Contains(coordinate.x, coordinate.y - 1))
        {
            neighbours[4] = hexaData[coordinate.y - 1, coordinate.x];
        }
        if (hexaData.Contains(coordinate.x - 1, coordinate.y))
        {
            neighbours[5] = hexaData[coordinate.y, coordinate.x - 1];
        }

        return(neighbours);
    }
    void OnGUI()
    {
        // Create HexTerrainData
        EditorGUILayout.LabelField("Terrain data");
        EditorGUI.indentLevel++;
        _terrainData = EditorGUILayout.ObjectField("object:", _terrainData, typeof(HexTerrainData), false) as HexTerrainData;
        if (_terrainData != null)
        {
            _terrainName = _terrainData.name;
            _size        = _terrainData.Size;
        }
        EditorGUILayout.Separator();
        GUI.enabled  = _terrainData == null;
        _terrainName = EditorGUILayout.TextField("name: ", _terrainName);
        _size        = EditorGUILayout.IntField("Size: ", _size);
        if (GUILayout.Button("Create Terrain data"))
        {
            _terrainData = MakeHexTerrainData(_terrainName, _size);
        }
        GUI.enabled = true;
        EditorGUI.indentLevel--;

        EditorGUILayout.Space();

        // Create HexagonTypeData
        EditorGUILayout.LabelField("Type data");
        EditorGUI.indentLevel++;
        _typeData = EditorGUILayout.ObjectField("object:", _typeData, typeof(HexagonTypeData), false) as HexagonTypeData;
        if (_typeData != null)
        {
            _typeName = _typeData.name;
        }
        EditorGUILayout.Separator();
        GUI.enabled = _typeData == null;
        _typeName   = EditorGUILayout.TextField("name: ", _typeName);
        if (GUILayout.Button("Create Type data"))
        {
            _typeData = MakeHexType(_typeName);
        }
        GUI.enabled = true;
        EditorGUI.indentLevel--;

        EditorGUILayout.Space();

        // Create Map
        _gameObjectName = EditorGUILayout.TextField("new gameObject name: ", _gameObjectName);
        GUI.enabled     = _typeData != null && _terrainData != null;
        if (GUILayout.Button("Instantiate gameObject"))
        {
            if (MakeHexTerrain(_gameObjectName, _terrainData, _typeData))
            {
                this.Close();
            }
        }
        GUI.enabled = true;
    }
    static bool MakeHexTerrain(string name, HexTerrainData terrainData, HexagonTypeData typeData)
    {
        GameObject gameObject = new GameObject(name);
        HexTerrain hexTerrain = gameObject.AddComponent <HexTerrain>();

        hexTerrain.HexData = terrainData;
        hexTerrain.Types   = typeData;
        hexTerrain.BuildChunks();
        return(true);
    }
    static HexTerrainData MakeHexTerrainData(string name, int size)
    {
        HexTerrainData terrainData = ScriptableObject.CreateInstance <HexTerrainData>();

        terrainData.Initialize(size);
        AssetDatabase.CreateAsset(terrainData, AssetDatabase.GenerateUniqueAssetPath("Assets/" + name + ".asset"));
        EditorUtility.FocusProjectWindow();
        Selection.activeObject = terrainData;
        AssetDatabase.SaveAssets();
        return(terrainData);
    }