任务管理

以任务流程为主线的仿真开发中,需要监听任务之间的关系,一个任务往往由多个实现条件组成,条件既可以是一个更小的任务也可以是一个判断点。

任务基类:

public abstract class TaskComponent
{
    protected string mName;
    public string Name { get { return mName; } }

    protected List<TaskComponent> mChildren;

    protected bool isdone = false;
    public bool IsDone
    {
        get
        {
            if (mChildren!=null&&mChildren.Count!=0)
            {
                foreach (TaskComponent tc in mChildren)
                {
                    if (tc.IsDone == false)
                    {
                        return false;
                    }
                }
                return true;
            }
            else
            {
                return isdone;
            }

        }
    }

    public List<TaskComponent> Children { get { return mChildren; } }

    public abstract void AddChild(TaskComponent tc);
    public abstract void RemoveChild(TaskComponent tc);
    public abstract TaskComponent GetChild(int index);
    public virtual void SetLocalDone(bool isDone)
    {
        if (mChildren!=null||mChildren.Count!=0)
        {
            this.isdone = isDone;
        }
    }


    public TaskComponent(string name)
    {
        mName = name;
        mChildren = new List<TaskComponent>();
    }
}

基础任务类:

public class TaskBase : TaskComponent
{
    public TaskBase(string name) : base(name) { }

    public override void AddChild(TaskComponent tc)
    {
        mChildren.Add(tc);
    }

    public override TaskComponent GetChild(int index)
    {
        return mChildren[index];
    }

    public override void RemoveChild(TaskComponent tc)
    {
        mChildren.Remove(tc);
    }
}

任务条件类:

public class TaskConditionBase : TaskComponent
{
    public TaskConditionBase(string name) : base(name) { }

    public override void AddChild(TaskComponent tc)
    {
        return;
    }

    public override TaskComponent GetChild(int index)
    {
        return null;
    }

    public override void RemoveChild(TaskComponent tc)
    {
        return;
    }
}

基础任务类与任务条件类的区别在于是否存在子条件类,创建任务管理类,任务管理类负责创建任务、任务条件以及任务与任务条件之间的约束关系。最终通过GetTaskState()函数可获取任意一个任务或者任务条件的状态,此方法适用于服务端对任务的监听,也便于将任务完成流程图形化。

public class TaskManager : MonoBehaviour
{
    TaskBase mieHuoTask = new TaskBase("灭火");
    TaskConditionBase m_condition1 = new TaskConditionBase("关闭通风");
    TaskConditionBase m_condition2 = new TaskConditionBase("打开灭火器");
    TaskConditionBase m_condition3 = new TaskConditionBase("持续足够的时间");

    TaskBase tongFengTask = new TaskBase("通风");
    TaskConditionBase t_condition1 = new TaskConditionBase("打开通风口1");
    TaskConditionBase t_condition2 = new TaskConditionBase("打开通风口2");
    TaskConditionBase t_condition3 = new TaskConditionBase("打开通风口3");

    TaskBase totalTask = new TaskBase("灭火任务");

    // Start is called before the first frame update
    void Start()
    {

        mieHuoTask.AddChild(m_condition1);
        mieHuoTask.AddChild(m_condition2);
        mieHuoTask.AddChild(m_condition3);


        tongFengTask.AddChild(t_condition1);
        tongFengTask.AddChild(t_condition2);
        tongFengTask.AddChild(t_condition3);

        
        totalTask.AddChild(mieHuoTask);
        totalTask.AddChild(tongFengTask);
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            t_condition1.SetLocalDone(true);
            t_condition2.SetLocalDone(true);
            t_condition3.SetLocalDone(true);
        }
        if (Input.GetKeyDown(KeyCode.D))
        {
            m_condition1.SetLocalDone(true);
            m_condition2.SetLocalDone(true);
            m_condition3.SetLocalDone(true);
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
            t_condition3.SetLocalDone(false);
        }
        if (Input.GetKeyDown(KeyCode.G))
        {
            Debug.Log("getstate灭火状态:" + GetTaskState(totalTask)+"   isdone:"+totalTask.IsDone);
        }

    }

    public bool GetTaskState(TaskComponent task)
    {
        List<TaskComponent> taskChildren = task.Children;
        if (taskChildren==null||taskChildren.Count==0)//已经到任务条件的层级
        {
            return task.IsDone;
        }
        else
        {
            foreach (TaskComponent child in taskChildren)
            {
                bool isCurrentDone=GetTaskState(child);
                if (isCurrentDone==false)
                {
                    Debug.Log("状态【"+child.Name+"】没有通过");
                    return false;
                }
            }
            Debug.Log("状态【" + task.Name + "】没有通过");
            return true;
        }
    }

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值