Esempio n. 1
0
        public PathfinderTests() {
            fixture = new Fixture();
            widthMap = 3;
            heightMap = 3;
            tiles = new TileNode[widthMap * heightMap];

            for (int x = 0; x < widthMap; x++) {
                for (int y = 0; y < heightMap; y++) {
                    TileNode tile = new TileNode(x, y, 0, true);
                    if (x > 0)
                        tile.AddNeighbor(tiles[x - 1 + y * widthMap]);
                    if (y > 0)
                        tile.AddNeighbor(tiles[x + (y - 1) * widthMap]);
                    tiles[x + y * widthMap] = tile;
                }
            }
        }
Esempio n. 2
0
    private void GenerateGraphFromGameObject()
    {
        tileDict  = new Dictionary <Vector2Int, TileNode>();
        spawnDict = new Dictionary <Vector2Int, int>();

        // Add nodes from gameObject children
        foreach (Transform child in transform)
        {
            BattleGridMarker marker = child.gameObject.GetComponent <BattleGridMarker>();
            Vector3          v      = child.position;

            // If marker, add as special
            if (marker)
            {
                switch (marker.type)
                {
                case MarkerType.Spawn:
                    spawnDict.Add(Vector3ToKey(v), marker.val);
                    break;

                default:
                    Debug.Log("Should not reach here");
                    break;
                }

                child.gameObject.SetActive(false);
                continue;
            }

            // Else, add as tile
            TileNode node = new TileNode(v);
            tileDict.Add(node.GetKey(), node);

            // Update boundaries
            xMin = Mathf.Min(xMin, v.x - 0.45f);
            xMax = Mathf.Max(xMax, v.x + 0.45f);
            zMin = Mathf.Min(zMin, v.z - 0.45f);
            zMax = Mathf.Max(zMax, v.z + 0.45f);
        }

        // Add edges for all adjacent nodes
        foreach (KeyValuePair <Vector2Int, TileNode> entry in tileDict)
        {
            Vector2Int k = entry.Key;
            TileNode   n = entry.Value;

            for (int i = k.x - 1; i <= k.x + 1; i++)
            {
                for (int j = k.y - 1; j <= k.y + 1; j++)
                {
                    if (i == k.x && j == k.y)
                    {
                        continue;
                    }

                    Vector2Int t = new Vector2Int(i, j);

                    if (!tileDict.ContainsKey(t))
                    {
                        continue;
                    }

                    float w = (n.GetPos() - tileDict[t].GetPos()).magnitude;
                    w = Mathf.RoundToInt(w * 10) / 10f;
                    n.AddNeighbor(tileDict[t], w);
                }
            }
        }

        // Debug printing

        /*
         * foreach (KeyValuePair<Vector2Int, TileNode> entry in tileDict) {
         * Debug.Log(entry.Value);
         * }
         * foreach (KeyValuePair<Vector2Int, int> entry in spawnDict) {
         * Debug.Log(entry.Value);
         * }
         */
    }