Exemple #1
0
        //~ConditionBase()
        //{
        //}

        public override bool IsValid(Agent pAgent, BehaviorTask pTask)
        {
            if (!(pTask.GetNode() is ConditionBase))
            {
                return false;
            }

            return base.IsValid(pAgent, pTask);
        }
	private void runBehaviors( BehaviorTask behaviorToRun ){
		runningBehaviors.Add (behaviorToRun);
		BehaviorReturnResult returnedResult = behaviorToRun.behave ();
		runningBehaviors.Remove (behaviorToRun);

		if (returnedResult == BehaviorReturnResult.Failure) {
			result = BehaviorReturnResult.Failure;
		} else if (runningBehaviors.Count <= 0) {
			result = BehaviorReturnResult.Success;
		} else
			result = BehaviorReturnResult.Running;
	}
        void OnGUI ()
        {
            GUI.BeginGroup (new Rect (panX, panY, 100000, 100000));

            if (behaviorTree != null)
            {
                Rect nodeRect = new Rect(50, 30, 150, 50);
                GUI.Label(nodeRect, string.Format("{0} - {1} nodes", behaviorTree.GetType().Name, 99));
                foreach (BehaviorTask task in behaviorTree.Nodes())
                {
                    string nodeText = string.Format("{0}\n{1}", task.GetType().Name, GetStatus(task));
                    nodeRect.y += 55;
                    if (GUI.Toggle(nodeRect, (selected == task), nodeText, GUI.skin.button))
                    {
                        selected = task;
                    }
                    else if (selected == task)
                    {
                        selected = null;
                    }

                }
             }
    //        EditorGUILayout.EndVertical();

            GUI.EndGroup ();    
             
            if (GUI.RepeatButton (new Rect (15, 5, 20, 20), "^")) {
                panY -= 1;
                Repaint ();
            }
                
            if (GUI.RepeatButton (new Rect (5, 25, 20, 20), "<")) {
                panX -= 1;
                Repaint ();
            }
                
            if (GUI.RepeatButton (new Rect (25, 25, 20, 20), ">")) {
                panX += 1;
                Repaint ();
            }
                
            if (GUI.RepeatButton (new Rect (15, 45, 20, 20), "v")) {
                panY += 1;
                Repaint ();
            }
        }
        public static IEnumerable<BehaviorTask> DepthFirstPreOrder(BehaviorTask node)
        {
            if (node != null)
            {
                yield return node;

                Composite composite = node as Composite;
                if (composite != null)
                {
                    foreach (BehaviorTask child in composite.Children)
                    {
                        foreach (BehaviorTask task in DepthFirstPreOrder(child))
                        {
                            yield return task;
                        }
                    }
                }
            }

            yield break;
        }
        public void Remove(BehaviorTask task)
		{
            task.Context= null;
            Children.Remove(task);
        }
        public void Add(BehaviorTask task)
		{
            task.Context = this.Context;
			Children.Add(task);
		}
Exemple #7
0
        protected override BehaviourTreeStatus internalUpdate(GameTime gameTime)
        {
            if (subject.Skills.Length > 0)
            {
                var attentionBlockRadius = LivingEntityType.lookup[subject.LivingEntityType].AttentionBlockRadius;
                var circle                = new Circle((int)subject.Position.X, (int)subject.Position.Y, attentionBlockRadius * WorldGrid.BlockSize.X);
                var hittedEntities        = CollisionUtils.GetLivingHittedObjects(circle, subject.InteriorID, subject, (int)FractionRelationType.HOSTILE);
                var subjectVectorPosition = subject.Position.ToVector();
                var enemyInSight          = false;

                if (hittedEntities.Count > 0)
                {
                    taskRater = new TaskRater();

                    foreach (var hittedEntity in hittedEntities)
                    {
                        if (CollisionUtils.IsSightBlocked(subject, hittedEntity))
                        {
                            continue;
                        }

                        enemyInSight = true;

                        var hittedEntityVector      = hittedEntity.Position.ToVector();
                        var getCloser               = false;
                        var distance                = GeometryUtils.GetEuclideanDistance(subject.Position, hittedEntity.Position);
                        var possibleBackOffDistance = -1.0f;

                        var aggro = subject.GetAggroTowardsEntity(hittedEntity);

                        foreach (var skill in subject.Skills)
                        {
                            if (skill.IsReady() == false)
                            {
                                continue;
                            }

                            if (skill is FireballSkill)
                            {
                                if (distance < Fireball.MaxDistance && CollisionUtils.IsSightBlocked(subject, hittedEntity, 15) == false)
                                {
                                    skill.Use(hittedEntityVector);

                                    if (Fireball.MaxDistance - distance < possibleBackOffDistance || possibleBackOffDistance == -1)
                                    {
                                        possibleBackOffDistance = Fireball.MaxDistance - distance;
                                    }
                                }
                                else
                                {
                                    getCloser = true;
                                }
                            }

                            if (skill is SlashSkill)
                            {
                                if (distance < SlashSkill.Range)
                                {
                                    skill.Use(hittedEntityVector);
                                }
                                else
                                {
                                    getCloser = true;
                                }
                            }
                        }

                        if (getCloser)
                        {
                            taskRater.AddTask(FollowTask.ID + hittedEntity.ID, () => new FollowTask((MovingEntity)subject, hittedEntity.ID, WorldGrid.BlockSize.X), 100 - (distance / WorldGrid.BlockSize.X) + -aggro);
                            taskRater.AddTask(BlinkTask.ID + hittedEntity.ID, () => new BlinkTask(subject, hittedEntity.Position), 101 - (distance / WorldGrid.BlockSize.X) + -aggro);
                        }
                        else if (possibleBackOffDistance != -1)
                        {
                            possibleBackOffDistance = possibleBackOffDistance - WorldGrid.BlockSize.X;

                            if (possibleBackOffDistance > 0)
                            {
                                var direction = Vector2.Subtract(
                                    new Vector2(
                                        subject.Position.X + random.Next(-WorldGrid.BlockSize.X * 3, WorldGrid.BlockSize.X * 3 + 1),
                                        subject.Position.Y + random.Next(-WorldGrid.BlockSize.Y * 3, WorldGrid.BlockSize.Y * 3 + 1)),
                                    hittedEntity.Position.ToVector());
                                direction.Normalize();

                                taskRater.AddTask(BlinkTask.ID + hittedEntity.ID, () => new BlinkTask(subject, new WorldPosition(subject.Position.X + direction.X * possibleBackOffDistance, subject.Position.Y + direction.Y * possibleBackOffDistance, subject.InteriorID)), 1 - (distance / WorldGrid.BlockSize.X) + -aggro);
                                taskRater.AddTask(FleeTask.ID + hittedEntity.ID, () => new FleeTask((MovingEntity)subject, hittedEntity, distance + possibleBackOffDistance), -(distance / WorldGrid.BlockSize.X) + -aggro);
                            }
                        }

                        if ((float)subject.CurrentLife / (float)subject.MaximumLife < 0.4f)
                        {
                            taskRater.AddTask(FleeTask.ID + hittedEntity.ID, () => new FleeTask((MovingEntity)subject, hittedEntity, 20 * WorldGrid.BlockSize.X), 1000 - (distance / WorldGrid.BlockSize.X) + -aggro);
                        }
                    }

                    if (taskRater.HasTask() == false)
                    {
                        activeTask   = null;
                        activeTaskId = null;
                    }

                    while (taskRater.HasTask())
                    {
                        var highestTask = taskRater.GetHighestRanked();

                        if (highestTask.TaskIdentifier != activeTaskId)
                        {
                            activeTask   = highestTask.TaskCreator();
                            activeTaskId = highestTask.TaskIdentifier;
                        }

                        if (activeTask.Status == BehaviourTreeStatus.Running)
                        {
                            activeTask.Update(gameTime);

                            if (activeTask.Status == BehaviourTreeStatus.Failure)
                            {
                                continue;
                            }

                            if (activeTask.Status == BehaviourTreeStatus.Success)
                            {
                                activeTask   = null;
                                activeTaskId = null;
                            }
                        }

                        break;
                    }

                    if (enemyInSight == true)
                    {
                        return(BehaviourTreeStatus.Running);
                    }
                }
            }

            return(BehaviourTreeStatus.Failure);
        }