//[CustomPropertyDrawer(typeof(HexCoordinates))]
        //public class HexCoordinatesDrawer : PropertyDrawer
        //{
        //}

        //public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        //{
        //    HexCoordinates coordinates = new HexCoordinates(property.FindPropertyRelative("x").intValue, property.FindPropertyRelative("z").intValue);
        //    position = EditorGUI.PrefixLabel(position, label);
        //    GUI.Label(position, coordinates.ToString());
        //}

        /// <summary>
        /// 指定单元格到当前单元格坐标距离
        /// </summary>
        /// <param name="cell"></param>
        public int DistanceTo(HexagonCoordinates other)
        {
            // 三个坐标维度上的距离
            int x = this.x < other.x ? other.x - this.x : this.x - other.x;
            int y = this.Y < other.Y ? other.Y - this.Y : this.Y - other.Y;
            int z = this.z < other.z ? other.z - this.z : this.z - other.z;

            return((x + y + x) / 2);
        }
Esempio n. 2
0
        /// <summary>
        /// 获取点击位置的单元格
        /// </summary>
        /// <param name="position"></param>
        /// <returns></returns>
        public HexagonCell GetCell(Vector3 position)
        {
            position = transform.InverseTransformPoint(position);
            HexagonCoordinates coordinates = HexagonCoordinates.FromPosition(position, this);
            // Debug.Log("touched at " + coordinates);
            int index = coordinates.X + coordinates.Z * this.cellCountWidth + coordinates.Z / 2;

            // Debug.Log(this.cells.Length);
            Debug.Log("touched at " + coordinates + "index:" + index);
            return(this.cells [index]);
            // return null;
        }
Esempio n. 3
0
        /// <summary>
        /// 创建网格
        /// </summary>
        /// <param name="widthIndex">网格宽方向上的位置</param>
        /// <param name="heightIndex">网格高方向上的位置</param>
        /// <param name="index">网格的索引</param>
        /// <param name="outerRadius">网格的外径</param>
        public void CreateCell(int widthIndex, int heightIndex, int index, float outerRadius)
        {
            Vector3 position;             // 单元格的中心位置

            position.y = 0f;
            // 根据网格方向不同,设置不同的网格排列方式
            switch (this.cellOrientation)
            {
            case CellOrientation.Horizontal:
                position.x = (widthIndex + heightIndex * 0.5f - heightIndex / 2) * (this.CellInnerRadius * 2f);
                position.z = heightIndex * (this.cellOuterRadius * 1.5f);
                break;

            case CellOrientation.Vertical:
                // TODO 这部分没有实现
                // position.x = heightIndex * (this.cellOuterRadius * 1.5f);
                // position.z = (widthIndex + heightIndex * 0.5f - heightIndex / 2) * (this.CellInnerRadius * 2f);
                position.x = (widthIndex + heightIndex * 0.5f - heightIndex / 2) * (this.CellInnerRadius * 2f);
                position.z = heightIndex * (this.cellOuterRadius * 1.5f);
                break;

            default:
                position.x = widthIndex * outerRadius;
                position.z = heightIndex * outerRadius;
                break;
            }

            // 创建网格
            this.cells [index] = Instantiate <HexagonCell> (this.prefabCell);
            // 设置单元格的位置
            // this.cells[index].transform.SetParent(this.transform, false);
            this.cells [index].transform.localPosition = position;
            this.cells [index].Coordinates             = HexagonCoordinates.FromOffsetCoordinates(widthIndex, heightIndex);
            this.cells [index].Initialize(this);
            this.cells [index].Color = this.defaultColor;
            // 设置标签
            Text label = Instantiate <Text> (cellLabelPrefab);

            this.cells [index].uiRect = label.rectTransform;
            // label.rectTransform.SetParent(gridCanvas.transform, false);
            label.rectTransform.anchoredPosition = new Vector2(position.x, position.z);
            // label.text = x.ToString() + "\n" + z.ToString();

            // this.cells[index].uiRect = label.rectTransform;
            // 设置相邻的单元格
            if (widthIndex > 0)
            {
                this.cells [index].SetNeighbor(HexagonDirection.W, this.cells [index - 1]);
            }
            if (heightIndex > 0)
            {
                if ((heightIndex & 1) == 0)
                {
                    this.cells [index].SetNeighbor(HexagonDirection.SE, this.cells [index - this.cellCountWidth]);
                    if (widthIndex > 0)
                    {
                        this.cells [index].SetNeighbor(HexagonDirection.SW, this.cells [index - this.cellCountWidth - 1]);
                    }
                }
                else
                {
                    this.cells [index].SetNeighbor(HexagonDirection.SW, this.cells [index - this.cellCountWidth]);
                    if (widthIndex < this.cellCountWidth - 1)
                    {
                        this.cells [index].SetNeighbor(HexagonDirection.SE, this.cells [index - this.cellCountWidth + 1]);
                    }
                }
            }
            //
            this.AddCellToChunk(widthIndex, heightIndex, this.cells [index]);
        }