예제 #1
0
        public void Init()
        {
            // clear
            Clear();

            // allocate
            Grid = new MatrixWrapper <ANode>();
            Grid.Allocate(GridSize.x, GridSize.y, (x, y) =>
            {
                var go   = new GameObject($"Node: {x} {y}", typeof(ANode));
                var node = go.GetComponent <ANode>();

                // set position
                go.transform.SetParent(transform);
                go.transform.localPosition = new Vector3(x, y, 0);

                return(node);
            });

            // make connection
            foreach (var valuePair in Grid)
            {
                if (Grid.TryGetValue(valuePair.Key.x - 1, valuePair.Key.y, out var left))
                {
                    valuePair.Value.Left = left;
                }
                if (Grid.TryGetValue(valuePair.Key.x + 1, valuePair.Key.y, out var right))
                {
                    valuePair.Value.Right = right;
                }
                if (Grid.TryGetValue(valuePair.Key.x, valuePair.Key.y + 1, out var top))
                {
                    valuePair.Value.Top = top;
                }
                if (Grid.TryGetValue(valuePair.Key.x, valuePair.Key.y - 1, out var bottom))
                {
                    valuePair.Value.Bottom = bottom;
                }
            }
        }
예제 #2
0
        private void OnValidate()
        {
            // format search coords
            if (From.x < 0)
            {
                From.x = 0;
            }
            if (From.x >= GridSize.x)
            {
                From.x = GridSize.x - 1;
            }
            if (From.y < 0)
            {
                From.y = 0;
            }
            if (From.y >= GridSize.x)
            {
                From.y = GridSize.y - 1;
            }
            if (To.x < 0)
            {
                To.x = 0;
            }
            if (To.x >= GridSize.x)
            {
                To.x = GridSize.x - 1;
            }
            if (To.y < 0)
            {
                To.y = 0;
            }
            if (To.y >= GridSize.x)
            {
                To.y = GridSize.y - 1;
            }

            // reconnect nodes
            if ((Grid?.Count ?? 0) == 0)
            {
                // organize node list
                var nodeList = new List <Tuple <Vector2Int, ANode> >();

                foreach (var child in gameObject.GetChildren())
                {
                    var node   = child.GetComponent <ANode>();
                    var coords = Regex.Matches(child.name, @" \d+").OfType <Match>().Select(n => int.Parse(n.Value))
                                 .ToList();
                    nodeList.Add(new Tuple <Vector2Int, ANode>(new Vector2Int(coords[0], coords[1]), node));
                }

                // initialize grid
                var w = nodeList.Max(n => n.Item1.x) + 1;
                var h = nodeList.Max(n => n.Item1.y) + 1;

                Grid = new MatrixWrapper <ANode>();
                Grid.Allocate(w, h);

                // fill grid
                foreach (var(pos, node) in nodeList)
                {
                    Grid[pos] = node;
                }
            }
        }