Exemple #1
0
 internal DecoratorTaskBuilder(DecoratorTaskDescWrapper task)
     : base(task)
 {
     this.task = task;
 }
        private void OnCreateTaskCommand(CreateTaskCommand command)
        {
            var parent       = Repository.States[command.Id];
            var parentTaskId = 0u;

            if (parent is BehaviorTreeState)
            {
                var tree = (BehaviorTreeState)parent;
                if (tree.RootTaskId > 0)
                {
                    parent.ApplyEvent(new TaskNotCreatedEvent(command.Id)
                    {
                        Reason = "Behavior tree already has root task"
                    });
                    return;
                }
            }
            else if (parent is TaskState)
            {
                var task = (TaskState)parent;
                if (task.Desc is LeafTaskDescWrapper)
                {
                    parent.ApplyEvent(new TaskNotCreatedEvent(command.Id)
                    {
                        Reason = "Leaf task cannot have child task"
                    });
                    return;
                }
                else if (task.Desc is DecoratorTaskDescWrapper)
                {
                    var desc = (DecoratorTaskDescWrapper)task.Desc;
                    if (desc.ChildTaskId != 0)
                    {
                        parent.ApplyEvent(new TaskNotCreatedEvent(command.Id)
                        {
                            Reason = "Decorator task can only has one child task"
                        });
                        return;
                    }
                }

                parentTaskId = task.Desc.Id;
            }
            else
            {
                throw new NotSupportedException(command.Id);
            }

            {
                var             tree = (BehaviorTreeState)Repository.States[BehaviorTreeState.GetId()];
                TaskDescWrapper taskDescWrapper;
                if (typeof(ILeafTaskDesc).IsAssignableFrom(command.TaskType))
                {
                    taskDescWrapper = new LeafTaskDescWrapper();
                }
                else if (typeof(IDecoratorTaskDesc).IsAssignableFrom(command.TaskType))
                {
                    taskDescWrapper = new DecoratorTaskDescWrapper();
                }
                else if (typeof(ICompositeTaskDesc).IsAssignableFrom(command.TaskType))
                {
                    taskDescWrapper = new CompositeTaskDescWrapper();
                }
                else
                {
                    throw new NotSupportedException(command.TaskType.ToString());
                }
                taskDescWrapper.Id         = tree.NextTaskId;
                taskDescWrapper.CustomDesc = (ITaskDesc)Activator.CreateInstance(command.TaskType);
                var taskState = new TaskState(Domain, TaskState.GetId(tree.NextTaskId));
                taskState.ParentTaskId = parentTaskId;
                taskState.Desc         = taskDescWrapper;

                parent.ApplyEvent(new TaskCreatedEvent(command.Id)
                {
                    NewTask = taskState
                });
            }
        }