Ejemplo n.º 1
0
    public void TouchCell(Vector3 point, Color color)       //点中了某一个cell
    {
        Debug.Log(point);

        //将点中的cell对应的x, z index显示在inspector中

        //int z = (int)(point.z / (HexMetrices.OuterRadius * 1.5f));

        float x = point.x / (HexMetrices.InnerRadius * 2f);

        float y = -x;

        float offSet = point.z / (HexMetrices.OuterRadius * 3f);

        x -= offSet;
        y -= offSet;


        int iX = Mathf.RoundToInt(x);
        int iY = Mathf.RoundToInt(y);
        int iZ = Mathf.RoundToInt(-x - y);

        if (iX + iY + iZ != 0)
        {
            //出现这个情况,是由于,在上面取整的时候,出现了同时缩小了两个,而通过取整后放大了第三个。

            //解决方案,找到被放到的那个,然后利用x + y + z = 0的规则,通过另外两个算出正确的第三个。
            float dX = Mathf.Abs(x - iX);
            float dY = Mathf.Abs(y - iY);
            float dZ = Mathf.Abs(-x - y - iZ);

            if (dX > dY && dX > dZ)
            {
                iX = -iY - iZ;
            }
            else if (dZ > dY)
            {
                if (dZ < dX)
                {
                    Debug.Log(123);
                }

                iZ = -iX - iY;
            }
        }

#if UNITY_EDITOR
        Debug.LogFormat("Point x = {0}, z = {1}", iX, iZ);
#endif
        hexCoord = HexCoordinate.GetOffSetCoordinate(iX, iZ);

        //获取点中的cell的index
        int index = hexCoord.X + hexCoord.Z * width + hexCoord.Z / 2;
        if (index < 0 || index > cellArray.Length)
        {
            Debug.LogErrorFormat("Index({0}) is illegal", index);
            return;
        }

        cellArray[index].HexCellColor = color;

        hexMesh.Triangulate(cellArray);
    }