Exemplo n.º 1
0
    public void GenerateMap()
    {
        string holderName = "Generator Map";

        if (transform.Find(holderName))
        {
            DestroyImmediate(transform.Find(holderName).gameObject);
        }

        Transform mapHolder = new GameObject(holderName).transform;

        mapHolder.parent = transform;

        //tileRotations = Random.Range(-90, 90);

        hexes = new HexScript[numColumns, numRows];
        hexToGameObjectMap = new Dictionary <HexScript, GameObject>();

        for (int column = 0; column < numColumns; column++)
        {
            for (int row = 0; row < numRows; row++)
            {
                HexScript h = new HexScript(this, column, row);
                hexes[column, row] = h;

                h.movementCost = 1;

                GameObject hex_go = Instantiate(hexPrefab, h.Position(), Quaternion.identity, this.transform);

                if (h == GetHexAt(3, 4) || h == GetHexAt(4, 3) || h == GetHexAt(5, 2) || h == GetHexAt(15, 16) || h == GetHexAt(14, 17) || h == GetHexAt(13, 18) || h == GetHexAt(12, 19))
                {
                    h.movementCost = -99;
                    hex_go.GetComponentInChildren <MeshRenderer>().material.color = Color.black;
                }
                if (h == GetHexAt(9, 10))
                {
                    h.movementCost = 2;
                    hex_go.GetComponentInChildren <MeshRenderer>().material.color = Color.black;
                }
                if (h == GetHexAt(6, 9) || h == GetHexAt(7, 9) || h == GetHexAt(8, 9) || h == GetHexAt(9, 9) || h == GetHexAt(10, 9))
                {
                    h.movementCost = 3;
                    hex_go.GetComponentInChildren <MeshRenderer>().material.color = Color.black;
                }
                else if (h == GetHexAt(2, 5))
                {
                    h.movementCost = 10;
                    hex_go.GetComponentInChildren <MeshRenderer>().material.color = Color.green;
                }

                hexToGameObjectMap.Add(h, hex_go);

                hex_go.GetComponent <HexComponentScript>().hex = new HexScript(this, column, row);
                hex_go.GetComponent <HexComponentScript>().map = this;

                //hex_go.GetComponentInChildren<TextMesh>().text = string.Format("{0},{1}\n\t{2}", column, row, h.movementCost);

                hex_go.transform.localScale = Vector3.one * (1 - outlinePercent);

                //Rotation of the tiles around the Y axis of their creation
                var rotationVector = hex_go.transform.rotation.eulerAngles;
                rotationVector.y = tileRotation;
                hex_go.transform.localRotation = Quaternion.Euler(rotationVector);

                //Gives the game object a suitable name for debugging
                hex_go.name = "Hex_" + column + "_" + row;

                //Used to create a cleaner hierachy, thus parenting this hex.
                hex_go.transform.SetParent(mapHolder);

                hex_go.isStatic = true;
                #region My Attempt
                //float xPos = column * xOffset;

                ////Are we on a odd row
                //if (row % 2 == 1)
                //{
                //    xPos += xOffset / 2f;
                //}

                //Vector3 tileposition = new Vector3(xPos, 0, row * zOffest);
                //GameObject hex_go = (GameObject)Instantiate(hexPrefab, tileposition, Quaternion.identity);

                //hex_go.transform.localScale = Vector3.one * (1 - outlinePercent);

                ////Rotation of the tiles around the Y axis of their creation
                //var rotationVector = hex_go.transform.rotation.eulerAngles;
                //rotationVector.y = tileRotation;
                //hex_go.transform.localRotation = Quaternion.Euler(rotationVector);

                ////Gives the game object a suitable name for debugging
                //hex_go.name = "Hex_" + column + "_" + row;

                ////Give the Hex awareness of it's place in the array
                //hex_go.GetComponent<HexScript>().x = column;
                //hex_go.GetComponent<HexScript>().y = row;
                //hex_go.GetComponent<HexScript>().tilesWidthMax = width;
                //hex_go.GetComponent<HexScript>().tilesHeightMax = height;

                ////Used to create a cleaner hierachy, thus parenting this hex.
                //hex_go.transform.SetParent(mapHolder);

                //hex_go.isStatic = true;
                #endregion
            }
        }

        GameObject mapGenerator        = GameObject.Find(holderName);
        var        pivotRotationVector = mapGenerator.transform.rotation.eulerAngles;
        pivotRotationVector.y = pivotRotation;
        mapGenerator.transform.localRotation = Quaternion.Euler(pivotRotationVector);
    }