Exemple #1
0
        public void Load(XmlElement e)
        {
            this.Name           = e.GetAttributeValueAsString("Name", this.Name);
            this.DefaultState   = e.GetAttributeValueAsString("DefaultState", DefaultDestinationState);
            this.ExpandMethods  = e.GetAttributeValueAsBoolean("ExpandMethods", false);
            this.ExtraBehaviors = null;

            List <BehaviorData> list             = new List <BehaviorData>();
            XmlElement          behaviorsElement = e["Behaviors"];

            if (behaviorsElement != null)
            {
                foreach (var behaviorElement in behaviorsElement)
                {
                    BehaviorData behavior = CreateBehaviorFrom(behaviorElement);
                    if (behavior != null)
                    {
                        behavior.Load(behaviorElement);
                        list.Add(behavior);
                    }
                }
            }

            List <BehaviorTreeStateData> states = new List <BehaviorTreeStateData>();

            foreach (var b in list)
            {
                if (b.BehaviorType == Skill.Framework.AI.BehaviorType.Composite && ((CompositeData)b).CompositeType == Skill.Framework.AI.CompositeType.State)
                {
                    states.Add(b as BehaviorTreeStateData);
                }
            }

            // load hierarchy
            XmlElement hierarchy = e["Hierarchy"];

            if (hierarchy != null)
            {
                foreach (var behaviorChildrenElement in hierarchy)
                {
                    int          behaviorId = behaviorChildrenElement.GetAttributeValueAsInt("Id", -2);
                    BehaviorData behavior   = FindById(list, behaviorId);
                    if (behavior != null)
                    {
                        foreach (var containerElement in behaviorChildrenElement)
                        {
                            int          childId = containerElement.GetAttributeValueAsInt("ChildId", -2);
                            BehaviorData child   = FindById(list, childId);
                            if (child != null)
                            {
                                XmlElement parametersElement = containerElement[ParameterDataCollection.ElementName];
                                if (parametersElement != null)
                                {
                                    ParameterDataCollection parameters = new ParameterDataCollection();
                                    parameters.Load(parametersElement);
                                    behavior.Add(child, parameters);
                                }
                                else
                                {
                                    behavior.Add(child);
                                }
                            }
                        }
                    }
                }
            }

            foreach (var b in list)
            {
                b.FixParameters();
            }

            if (states.Count > 0)
            {
                this.States = states.ToArray();
            }
            else
            {
                // try to load as previouse version format
                int          rootId = e.GetAttributeValueAsInt("RootId", 0);
                BehaviorData root   = FindById(list, rootId);
                if (root != null && root.BehaviorType == Skill.Framework.AI.BehaviorType.Composite && ((CompositeData)root).CompositeType == Skill.Framework.AI.CompositeType.Priority)
                {
                    PrioritySelectorData  ps    = root as PrioritySelectorData;
                    BehaviorTreeStateData state = new BehaviorTreeStateData();

                    state.Comment     = ps.Comment;
                    state.Concurrency = ps.Concurrency;
                    state.Id          = ps.Id;
                    state.Name        = ps.Name;
                    state.Priority    = ps.Priority;
                    state.Weight      = ps.Weight;

                    foreach (var child in ps)
                    {
                        state.Add(child);
                    }

                    this.States = new BehaviorTreeStateData[] { state };

                    DefaultState = state.Name;
                }
                else
                {
                    this.States = new BehaviorTreeStateData[] { new BehaviorTreeStateData()
                                                                {
                                                                    Name = DefaultDestinationState
                                                                } };
                }
            }

            List <BehaviorData> extra = new List <BehaviorData>();

            foreach (var b in list)
            {
                if (!IsInHierarchy(b))
                {
                    extra.Add(b);
                }
            }
            if (extra.Count > 0)
            {
                ExtraBehaviors = extra.ToArray();
            }
        }
Exemple #2
0
        void AddMenuItem_Click(object sender, System.EventArgs e)
        {
            if (_TreeView.SelectedItem == null)
            {
                return;
            }
            if (!(_TreeView.SelectedItem is TreeViewFolder))
            {
                return;
            }


            Skill.Editor.UI.MenuItem item     = (Skill.Editor.UI.MenuItem)sender;
            BehaviorData             behavior = null;

            if (string.IsNullOrEmpty(item.Tag))
            {
                Skill.Framework.AI.BehaviorType type = (Framework.AI.BehaviorType)item.UserData;
                switch (type)
                {
                case Skill.Framework.AI.BehaviorType.Action:
                    behavior = new ActionData()
                    {
                        Name = _Editor.GetUniqueName("NewAction")
                    };
                    break;

                case Skill.Framework.AI.BehaviorType.Condition:
                    behavior = new ConditionData()
                    {
                        Name = _Editor.GetUniqueName("NewCondition")
                    };
                    break;

                default:
                    behavior = null;
                    break;
                }
            }
            else if (item.Tag == "Decorator")
            {
                Skill.Framework.AI.DecoratorType type = (Framework.AI.DecoratorType)item.UserData;
                switch (type)
                {
                case Skill.Framework.AI.DecoratorType.Default:
                    behavior = new DecoratorData()
                    {
                        Name = _Editor.GetUniqueName("NewDecorator")
                    };
                    break;

                case Skill.Framework.AI.DecoratorType.AccessLimit:
                    behavior = new AccessLimitDecoratorData()
                    {
                        Name = _Editor.GetUniqueName("NewAccessDecorator")
                    };
                    break;

                default:
                    behavior = null;
                    break;
                }
            }
            else if (item.Tag == "Composite")
            {
                Skill.Framework.AI.CompositeType type = (Framework.AI.CompositeType)item.UserData;
                switch (type)
                {
                case Skill.Framework.AI.CompositeType.Sequence:
                    behavior = new SequenceSelectorData()
                    {
                        Name = _Editor.GetUniqueName("NewSequence")
                    };
                    break;

                case Skill.Framework.AI.CompositeType.Concurrent:
                    behavior = new ConcurrentSelectorData()
                    {
                        Name = _Editor.GetUniqueName("NewConcurrent")
                    };
                    break;

                case Skill.Framework.AI.CompositeType.Random:
                    behavior = new RandomSelectorData()
                    {
                        Name = _Editor.GetUniqueName("NewRandom")
                    };
                    break;

                case Skill.Framework.AI.CompositeType.Priority:
                    behavior = new PrioritySelectorData()
                    {
                        Name = _Editor.GetUniqueName("NewPriority")
                    };
                    break;

                case Skill.Framework.AI.CompositeType.Loop:
                    behavior = new LoopSelectorData()
                    {
                        Name = _Editor.GetUniqueName("NewLoop")
                    };
                    break;

                default:
                    behavior = null;
                    break;
                }
            }

            if (behavior != null)
            {
                TreeViewFolder tvf = (TreeViewFolder)_TreeView.SelectedItem;
                tvf.Foldout.IsOpen = true;
                tvf.AddBehavior(behavior);
                _Editor.AddToList(behavior);
                _Editor.RefreshTree();
                SelectItem(tvf, behavior);
            }
        }
Exemple #3
0
        /// <summary>
        /// Detect Behavior data from Given XmlElement  and load it
        /// </summary>
        /// <param name="behavior">XmlElement contains Behavior data</param>
        /// <returns>Loaded Behavior</returns>
        public static BehaviorData CreateBehaviorFrom(XmlElement behavior)
        {
            BehaviorData result = null;

            Skill.Framework.AI.BehaviorType behaviorType = Skill.Framework.AI.BehaviorType.Action;
            bool isCorrect = false;

            try
            {
                behaviorType = behavior.GetAttributeValueAsEnum <Skill.Framework.AI.BehaviorType>("BehaviorType", Skill.Framework.AI.BehaviorType.Condition);
                isCorrect    = true;
            }
            catch (Exception)
            {
                isCorrect = false;
            }
            if (isCorrect)
            {
                switch (behaviorType)
                {
                case Skill.Framework.AI.BehaviorType.Action:
                    result = new ActionData();
                    break;

                case Skill.Framework.AI.BehaviorType.Condition:
                    result = new ConditionData();
                    break;

                case Skill.Framework.AI.BehaviorType.Decorator:
                    Skill.Framework.AI.DecoratorType decoratorType = behavior.GetAttributeValueAsEnum <Skill.Framework.AI.DecoratorType>("DecoratorType", Skill.Framework.AI.DecoratorType.Default);
                    switch (decoratorType)
                    {
                    case Skill.Framework.AI.DecoratorType.Default:
                        result = new DecoratorData();
                        break;

                    case Skill.Framework.AI.DecoratorType.AccessLimit:
                        result = new AccessLimitDecoratorData();
                        break;

                    default:
                        break;
                    }

                    break;

                case Skill.Framework.AI.BehaviorType.Composite:
                    Skill.Framework.AI.CompositeType selectorType = behavior.GetAttributeValueAsEnum <Skill.Framework.AI.CompositeType>("CompositeType", Skill.Framework.AI.CompositeType.Sequence);
                    switch (selectorType)
                    {
                    case Skill.Framework.AI.CompositeType.Sequence:
                        result = new SequenceSelectorData();
                        break;

                    case Skill.Framework.AI.CompositeType.Concurrent:
                        result = new ConcurrentSelectorData();
                        break;

                    case Skill.Framework.AI.CompositeType.Random:
                        result = new RandomSelectorData();
                        break;

                    case Skill.Framework.AI.CompositeType.Priority:
                        result = new PrioritySelectorData();
                        break;

                    case Skill.Framework.AI.CompositeType.Loop:
                        result = new LoopSelectorData();
                        break;

                    case Skill.Framework.AI.CompositeType.State:
                        result = new BehaviorTreeStateData();
                        break;
                    }
                    break;

                case Skill.Framework.AI.BehaviorType.ChangeState:
                    result = new ChangeStateData();
                    break;
                }
            }
            return(result);
        }
 public PrioritySelectorItem(PrioritySelectorData data)
     : base(data)
 {
 }