示例#1
0
        private void Add(BehaviorTreeStateData state)
        {
            StateItem item = new StateItem(this, state);

            _Editor.AddToList(state);
            SetStyle(item);
            _StateList.Items.Add(item);
        }
示例#2
0
 private bool IsStateExists(BehaviorTreeStateData state)
 {
     foreach (StateItem item in _StateList.Items)
     {
         if (item.State == state)
         {
             return(true);
         }
     }
     return(false);
 }
示例#3
0
 private void SetAsDefaultState()
 {
     if (_StateList.SelectedItem != null)
     {
         BehaviorTreeStateData state = ((StateItem)_StateList.SelectedItem).State;
         _Editor.BehaviorTree.DefaultState = state.Name;
         RefreshItemStyles();
     }
     else
     {
         Debug.LogError("there is no selected state to set as default");
     }
 }
示例#4
0
        private void AddNewState()
        {
            BehaviorTreeStateData state = new BehaviorTreeStateData();

            state.Name = _Editor.GetUniqueName("NewState");
            BehaviorData[] preStates = _Editor.BehaviorTree.States;
            BehaviorData[] newStates = new BehaviorData[preStates.Length + 1];
            preStates.CopyTo(newStates, 0);
            newStates[newStates.Length - 1] = state;
            _Editor.BehaviorTree.States     = newStates;
            Add(state);

            SetButtonsEnable();
        }
示例#5
0
        public void Rebuild()
        {
            if (_Editor.BehaviorTree.States == null || _Editor.BehaviorTree.States.Length < 1)
            {
                _Editor.BehaviorTree.States    = new BehaviorData[1];
                _Editor.BehaviorTree.States[0] = new BehaviorTreeStateData()
                {
                };
                _Editor.BehaviorTree.States[0].Name = "Default";
                _Editor.BehaviorTree.DefaultState   = _Editor.BehaviorTree.States[0].Name;
            }

            if (string.IsNullOrEmpty(_Editor.BehaviorTree.DefaultState))
            {
                _Editor.BehaviorTree.DefaultState = _Editor.BehaviorTree.States[0].Name;
            }

            bool foundDefaultState = false;

            foreach (BehaviorTreeStateData state in _Editor.BehaviorTree.States)
            {
                if (state.Name == _Editor.BehaviorTree.DefaultState)
                {
                    foundDefaultState = true;
                    break;
                }
            }

            if (!foundDefaultState)
            {
                _Editor.BehaviorTree.DefaultState = _Editor.BehaviorTree.States[0].Name;
            }

            _StateList.Items.Clear();
            foreach (var item in _Editor.BehaviorTree.States)
            {
                BehaviorTreeStateData state = (BehaviorTreeStateData)item;
                Add(state);
            }

            //SelectDefaultState();
        }
示例#6
0
        private void RemoveSelectedState()
        {
            if (_StateList.SelectedItem != null)
            {
                if (_StateList.Items.Count > 1)
                {
                    BehaviorTreeStateData state     = ((StateItem)_StateList.SelectedItem).State;
                    BehaviorData[]        preStates = _Editor.BehaviorTree.States;
                    BehaviorData[]        newStates = new BehaviorData[preStates.Length - 1];

                    int preIndex = 0;
                    int newIndex = 0;
                    while (newIndex < newStates.Length && preIndex < preStates.Length)
                    {
                        if (preStates[preIndex] == state)
                        {
                            preIndex++;
                            continue;
                        }
                        newStates[newIndex] = preStates[preIndex];
                        newIndex++;
                        preIndex++;
                    }
                    _Editor.BehaviorTree.States = newStates;
                    _StateList.Items.Remove(_StateList.SelectedItem);
                    SelectDefaultState();
                    RefreshItemStyles();
                    SetButtonsEnable();
                }
                else
                {
                    Debug.LogWarning("can not delete last state");
                }
            }
            else
            {
                Debug.LogError("there is no selected state to remove");
            }
        }
示例#7
0
        public void SelectDefaultState()
        {
            bool found = false;

            foreach (StateItem item in _StateList.Items)
            {
                if (item.State.Name == _Editor.BehaviorTree.DefaultState)
                {
                    _StateList.SelectedItem = item;
                    found = true;
                    break;
                }
            }
            if (!found)
            {
                if (_StateList.Items.Count > 0)
                {
                    BehaviorTreeStateData state = ((StateItem)_StateList.Items[0]).State;
                    _Editor.BehaviorTree.DefaultState = state.Name;
                    _StateList.SelectedIndex          = 0;
                    SetStyle(_StateList.SelectedItem as StateItem);
                }
            }
        }
示例#8
0
 public BehaviorTreeStateItem(BehaviorTreeStateData data)
     : base(data)
 {
 }
示例#9
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();
            }
        }
示例#10
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);
        }
示例#11
0
 public StateItem(StateEditor owner, BehaviorTreeStateData state)
 {
     this._Owner = owner;
     this.State  = state;
     this.Text   = State.Name;
 }