Exemplo n.º 1
0
        public static BehaviorNode RemoveChildByName(this BehaviorNode root, string name)
        {
            var child = root.FindChildByName(name, out var parent);

            // didn't find child or child was root
            if (parent == null)
            {
                var rootName = root.GetName();
                Main.HBSLog?.Log($"Could not find child to remove named: {name} in tree with root {rootName}");
                return(null);
            }

            var parentName = parent.GetName();

            Main.HBSLog?.Log($"Deleting child named: {name} from parent named: {parentName}");
            switch (parent)
            {
            case CompositeBehaviorNode composite:
                composite.Children.Remove(child);
                break;

            case DecoratorBehaviorNode decorator:
                decorator.ChildNode = null;
                break;
            }

            return(child);
        }
Exemplo n.º 2
0
        public static bool AddNode(this BehaviorNode root, BehaviorNode nodeToAdd, string parentName = null, string siblingName = null, bool insertBefore = false)
        {
            var          parentNode = root;
            BehaviorNode sibling    = null;

            if (siblingName != null)
            {
                sibling = root.FindChildByName(siblingName, out parentNode);

                if (sibling == null)
                {
                    return(false);
                }

                var foundParentName = parentNode.GetName();
                if (foundParentName != parentName)
                {
                    return(false);
                }
            }
            else if (parentName != null)
            {
                parentNode = root.FindChildByName(parentName, out _);
            }

            if (parentNode == null)
            {
                return(false);
            }

            switch (parentNode)
            {
            case CompositeBehaviorNode composite:
            {
                if (sibling != null)
                {
                    var index = composite.Children.IndexOf(sibling);
                    if (!insertBefore)
                    {
                        index++;
                    }

                    composite.Children.Insert(index, nodeToAdd);
                }
                else
                {
                    composite.AddChild(nodeToAdd);
                }

                return(true);
            }

            case DecoratorBehaviorNode decorator:
            {
                if (decorator.ChildNode == null)
                {
                    decorator.ChildNode = nodeToAdd;
                    return(true);
                }
                break;
            }
            }

            return(false);
        }