Ejemplo n.º 1
0
    public void RenderHexGrid()
    {
        HexagonMap hc = new HexagonMap();

        AxisCoordinate[] ac = new AxisCoordinate[] {
            new AxisCoordinate(0, 0),
            new AxisCoordinate(1, 0),
            new AxisCoordinate(2, 0),
            new AxisCoordinate(3, 0),

            new AxisCoordinate(0, 1),
            new AxisCoordinate(1, 1),
            new AxisCoordinate(2, 1),
            new AxisCoordinate(3, 1),

            new AxisCoordinate(-1, 2),
            new AxisCoordinate(0, 2),
            new AxisCoordinate(1, 2),
            new AxisCoordinate(2, 2),

            new AxisCoordinate(-1, 3),
            new AxisCoordinate(0, 3),
            new AxisCoordinate(1, 3),
            new AxisCoordinate(2, 3),
        };

        var ac1 = new List <AxisCoordinate>();

        foreach (AxisCoordinate a in ac)
        {
            DrawHexagon(a, 10);
        }
    }
Ejemplo n.º 2
0
    public void DrawHexagon(AxisCoordinate axisCorrdinate, float size)
    {
        Color color = Color.white;

        // using new formula
        Vector2 gridPosition = (new Vector2(
                                    size * (Mathf.Sqrt(3) * (axisCorrdinate.Q + axisCorrdinate.R * 0.5f)),
                                    size * (axisCorrdinate.R * 1.5f)));

        Ray     ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        float   rayDistance;
        Vector3 mousePlanePosition;
        Color   crossair = Color.white;


        if (groundPlane.Raycast(ray, out rayDistance))
        {
            mousePlanePosition = ray.GetPoint(rayDistance);
            DrawLine(
                new Vector3(mousePlanePosition.x + size, 0, mousePlanePosition.z),
                new Vector3(mousePlanePosition.x - size, 0, mousePlanePosition.z), Color.green);
            DrawLine(
                new Vector3(mousePlanePosition.x, 0, mousePlanePosition.z + size),
                new Vector3(mousePlanePosition.x, 0, mousePlanePosition.z - size), Color.green);

            float angleSideRight = 2 * Mathf.PI / 6 * (1 + 0.5f);
            float angleSideLeft  = 2 * Mathf.PI / 6 * (5 + 0.5f);
            float diameter       = size * (float)Math.Cos(angleSideLeft) - size * (float)Math.Cos(angleSideRight);

            var angle = 2.0f * Mathf.PI / 6.0f * (0.5f);
            var x     = gridPosition.x + size * Mathf.Cos(angle);
            var y     = gridPosition.y + size * Mathf.Sin(angle);
            //var selectedHex = HexagonCoordinates.ConvertPointCoordToAxialCoord(mousePlanePosition.x + 5, mousePlanePosition.z + 10f, diameter);
            var selectedHex = HexagonMap.ConvertPointCoordToAxialCoord(mousePlanePosition.x + diameter, mousePlanePosition.z + size, diameter, size);
            if (axisCorrdinate.Equals(selectedHex))
            {
                crossair = Color.red;
            }
        }

        Vector3[] hexPoints = new Vector3[6];
        for (int k = 0; k < 6; k++)
        {
            var angle = 2.0f * Mathf.PI / 6.0f * (k + 0.5f);

            var x = gridPosition.x + size * Mathf.Cos(angle);
            var y = gridPosition.y + size * Mathf.Sin(angle);

            if (k == 0)
            {
                hexPoints[0] = new Vector3(x, 0, y);
            }
            else
            {
                hexPoints[k] = new Vector3(x, 0, y);
                DrawLine(hexPoints[k - 1], hexPoints[k], color);
            }
        }
        DrawLine(hexPoints[5], hexPoints[0], color);

        DrawLine(
            new Vector3(gridPosition.x + 5, 0, gridPosition.y),
            new Vector3(gridPosition.x - 5, 0, gridPosition.y), crossair);
        DrawLine(
            new Vector3(gridPosition.x, 0, gridPosition.y + 5),
            new Vector3(gridPosition.x, 0, gridPosition.y - 5), crossair);
    }