Esempio n. 1
0
        // Calculate the distance between two nodes, using Cube Coordinates (the distance between a node and one of its neighbours is 1) - https://www.redblobgames.com/grids/hexagons/#distances
        public static int Distance(Node sourceNode, Node targetNode)
        {
            // Get the nodes' coordinates
            Node.CubeCoordinates sourceCoordinates = sourceNode.cubeCoordinates;
            Node.CubeCoordinates targetCoordinates = targetNode.cubeCoordinates;

            // Calculate the distance
            return(Mathf.RoundToInt((Mathf.Abs(sourceCoordinates.x - targetCoordinates.x) + Mathf.Abs(sourceCoordinates.y - targetCoordinates.y) + Mathf.Abs(sourceCoordinates.z - targetCoordinates.z)) / 2));
        }
Esempio n. 2
0
        public static float nodesLineDifference   = 1.8f;   // Distance needed to increase the Line coordinate of a tile by 1 (will be checked against the Z axis)
        #endregion

        #region Nodes Initialization
        // Function calculating the coordinates of every node. Should be called after loading the map.
        public void RefreshNodes()
        {
            nodesList.Clear();
            nodesCubeCoordinates.Clear();

            // Create the variables
            GameObject initialNode = null;
            int        i           = 1;

            // Go through each Node
            foreach (Node node in FindObjectsOfType <Node>())
            {
                // If no initial Node has been determined, use this one
                if (!initialNode)
                {
                    // Set the node as initial
                    initialNode = node.gameObject;

                    // Set the coordinates at 0
                    node.coordinates.column = 0;
                    node.coordinates.row    = 0;
                }

                // Calculate the node's coordinates
                Node.CubeCoordinates cubeCoordinates = node.CalculateNodeCoordinates(initialNode);

                // Rename the node
                node.gameObject.name = "Node " + i;
                i++;

                // Register the node
                nodesList.Add(node);
                nodesCubeCoordinates[cubeCoordinates] = node;
            }

            // Build the neighbour's graph
            BuildNeighboursGraph();
        }